:root {
  --bg: #ffffff;
  --soft: #f5f5f4;
  --line: #e7e5e4;
  --ink: #1c1917;
  --mute: #78716c;
  --faint: #a8a29e;
  --accent: #1c1917;
  --r: 14px;
  --tabs-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 15px/1.45 "IBM Plex Sans Thai", -apple-system, "Segoe UI", sans-serif;
  padding-bottom: calc(var(--tabs-h) + var(--safe-b));
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.top {
  position: sticky; top: 0; z-index: 5; background: rgba(255,255,255,.92);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  padding: calc(10px + var(--safe-t)) 16px 8px; border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; gap: 6px; min-height: 40px; }
.top h1 { font-size: 20px; font-weight: 600; margin: 0; flex: 1; letter-spacing: -.01em; }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; color: var(--ink); }
.icon-btn:active, .pill:active { background: var(--soft); }
.pill { height: 32px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); font-size: 13px; font-weight: 500; white-space: nowrap; }
.pill.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.wrap { max-width: 1200px; margin: 0 auto; }
.pad { padding: 16px; }

/* ---------- week strip ---------- */
.strip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-top: 8px; }
.day {
  display: flex; flex-direction: column; align-items: center; padding: 6px 0 7px; border-radius: 12px; gap: 1px;
}
.day small { font-size: 11px; color: var(--mute); }
.day b { font-size: 16px; font-weight: 500; width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; }
.day.today b { color: #fff; background: #ef4444; }
.day.sel { background: var(--soft); }
.day.sel b { font-weight: 600; }
.day .dots { display: flex; gap: 2px; height: 5px; }
.day .dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--c); }

/* ---------- schedule grid ---------- */
.banner {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 12px; background: #fefce8; color: #854d0e;
  font-size: 14px; display: flex; gap: 8px; align-items: center;
}
.grid { display: grid; padding: 8px 12px 24px; }
.grid .hd { position: sticky; top: var(--hd-top, 0); font-size: 12px; color: var(--mute); text-align: center; padding: 6px 0; background: #fff; z-index: 2; }
.grid .hd.today { color: #ef4444; font-weight: 600; }
.time { font-size: 12px; color: var(--mute); padding: 10px 8px 0 0; text-align: right; font-variant-numeric: tabular-nums; }
.cell {
  min-height: 56px; border-top: 1px solid var(--line); padding: 5px 4px; display: flex; flex-direction: column; gap: 4px;
  position: relative; cursor: pointer;
}
.cell.week { border-left: 1px solid var(--line); min-height: 52px; }
.cell.past { background: repeating-linear-gradient(135deg, transparent 0 6px, #fafaf9 6px 12px); }
.cell.now { box-shadow: inset 3px 0 0 #ef4444; }
.cell .hint { font-size: 12px; color: var(--faint); padding: 6px 4px; }
.cell .add { position: absolute; right: 6px; top: 8px; width: 26px; height: 26px; border-radius: 8px; color: var(--faint); font-size: 18px; line-height: 1; }
.cell.week .add { display: none; }
.cell:hover .add { color: var(--ink); background: var(--soft); }
.chip {
  text-align: left; border-radius: 9px; padding: 6px 9px 6px 11px; background: color-mix(in srgb, var(--c) 13%, white);
  border-left: 3px solid var(--c); display: flex; flex-direction: column; min-width: 0; margin-right: 36px;
}
.cell.week .chip { margin-right: 0; padding: 4px 6px 4px 7px; }
.chip span { font-weight: 500; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip small { font-size: 11px; color: color-mix(in srgb, var(--c) 70%, black); }
.cell.week .chip span { font-size: 12px; }
.cell.week .chip small { display: none; }

/* ---------- lists & cards ---------- */
.search {
  width: 100%; height: 40px; border-radius: 12px; border: 0; background: var(--soft); padding: 0 14px; outline: none; margin-top: 8px;
}
.list { display: flex; flex-direction: column; }
.row {
  display: flex; gap: 12px; align-items: center; padding: 12px 4px; border-bottom: 1px solid var(--line); text-align: left; width: 100%;
}
.row:active { background: var(--soft); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--soft); display: grid; place-items: center;
  font-weight: 600; color: var(--mute); flex: none;
}
.row .main { flex: 1; min-width: 0; }
.row .name { font-weight: 600; font-size: 17px; line-height: 1.3; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row .sub { font-size: 13px; color: var(--mute); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .nums { text-align: right; font-size: 12px; color: var(--mute); flex: none; line-height: 1.35; }
.row .nums b { color: var(--ink); font-size: 15px; font-weight: 600; }
.row[disabled] { opacity: .45; pointer-events: none; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--soft); color: var(--mute); font-weight: 500; }
.tag.hot { background: #dcfce7; color: #166534; }
.tag.blue { background: #dbeafe; color: #1e40af; }
.code { font-size: 12px; color: var(--faint); font-weight: 400; }
.empty { text-align: center; color: var(--mute); padding: 48px 16px; }

/* ---------- sheet ---------- */
#scrim { position: fixed; inset: 0; background: rgba(28,25,23,.32); z-index: 20; animation: fade .18s; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: #fff; border-radius: 20px 20px 0 0;
  max-height: 92dvh; overflow: auto; padding: 8px 18px calc(20px + var(--safe-b)); animation: up .22s cubic-bezier(.2,.8,.2,1);
  overscroll-behavior: contain;
}
@media (min-width: 700px) {
  #sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 520px; border-radius: 20px; max-height: 86vh; animation: fade .18s; }
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.grab { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sh-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.sh-head h2 { font-size: 18px; margin: 0; flex: 1; font-weight: 600; }
.sh-head .sub { font-size: 13px; color: var(--mute); }
.seg { display: flex; background: var(--soft); border-radius: 11px; padding: 3px; gap: 3px; }
.seg button { flex: 1; height: 32px; border-radius: 9px; font-size: 14px; color: var(--mute); font-weight: 500; }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.card { border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin: 10px 0; }
.card h3 { margin: 0 0 2px; font-size: 20px; font-weight: 600; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.stat { background: var(--soft); border-radius: 12px; padding: 10px 12px; }
.stat small { color: var(--mute); font-size: 12px; display: block; }
.stat b { font-size: 22px; font-weight: 600; }
.stat span { font-size: 12px; color: var(--mute); margin-left: 4px; }
.note-box { background: #fafaf9; border-radius: 12px; padding: 10px 12px; font-size: 14px; white-space: pre-wrap; margin-top: 10px; }
.note-box:empty::before { content: 'ไม่มีโน้ต'; color: var(--faint); }
.contact { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.contact a { font-size: 13px; color: var(--ink); text-decoration: none; background: var(--soft); padding: 4px 10px; border-radius: 999px; }

label.f { display: block; font-size: 13px; color: var(--mute); margin: 12px 0 4px; }
.in, textarea.in, select.in {
  width: 100%; border: 1px solid var(--line); border-radius: 11px; padding: 9px 12px; background: #fff; outline: none; font-size: 16px;
}
.in:focus { border-color: var(--ink); }
textarea.in { min-height: 76px; resize: vertical; }
.ta-wrap { position: relative; }
.ta-wrap .mic { position: absolute; right: 8px; bottom: 8px; width: 34px; height: 34px; border-radius: 50%; background: var(--soft); display: grid; place-items: center; }
.ta-wrap .mic.rec { background: #ef4444; color: #fff; animation: pulse 1.2s infinite; }
.ta-wrap .mic svg { width: 18px; height: 18px; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(239,68,68,.2); } }

.btn {
  height: 48px; border-radius: 13px; width: 100%; font-weight: 600; font-size: 16px; background: var(--ink); color: #fff; margin-top: 14px;
}
.btn:active { opacity: .85; }
.btn.light { background: var(--soft); color: var(--ink); }
.btn.danger { background: #fff; color: #dc2626; border: 1px solid #fecaca; }
.btn.small { height: 40px; font-size: 14px; margin-top: 8px; }
.btns { display: flex; gap: 8px; }
.btns .btn { flex: 1; }
.link { color: var(--mute); font-size: 14px; padding: 6px 0; }

.statuses { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.st-btn {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); text-align: left; font-size: 14px;
}
.st-btn i { width: 12px; height: 12px; border-radius: 50%; background: var(--c); flex: none; }
.st-btn.on { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, white); font-weight: 600; }

.avail-day { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.avail-day > b { width: 30px; font-weight: 500; padding-top: 6px; }
.avail-day .ranges { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.range { display: flex; gap: 6px; align-items: center; }
.range select { flex: 1; padding: 6px 8px; }
.range .x { width: 32px; height: 32px; color: var(--faint); font-size: 18px; }
.mini { font-size: 13px; color: var(--mute); padding: 6px 0; text-align: left; }

.hist { font-size: 13px; color: var(--mute); }
.hist div { padding: 6px 0; border-bottom: 1px dashed var(--line); }

.slots { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.slot-btn { display: flex; justify-content: space-between; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; text-align: left; }
.slot-btn small { color: var(--mute); }

/* ---------- summary ---------- */
table.sum { width: 100%; border-collapse: collapse; font-size: 14px; }
table.sum th { font-weight: 500; color: var(--mute); font-size: 12px; text-align: right; padding: 8px 6px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none; white-space: nowrap; }
table.sum th:first-child, table.sum td:first-child { text-align: left; }
table.sum th.on { color: var(--ink); }
table.sum td { padding: 11px 6px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
table.sum td.z { color: var(--faint); }
table.sum tfoot td { font-weight: 600; border-bottom: 0; }
.bar { height: 4px; border-radius: 2px; background: #3b82f6; margin-top: 4px; opacity: .7; }

/* ---------- settings ---------- */
.group { margin: 18px 0 6px; font-size: 13px; color: var(--mute); font-weight: 500; }
.st-row { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.st-row input[type=color] { width: 34px; height: 34px; border: 0; padding: 0; background: none; border-radius: 50%; }
.st-row input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
.st-row input[type=color]::-webkit-color-swatch { border: 0; border-radius: 50%; }
.st-row .in { flex: 1; padding: 6px 10px; }
.st-row small { color: var(--faint); font-size: 12px; width: 64px; }

/* ---------- login ---------- */
.login { min-height: 90dvh; display: grid; place-items: center; padding: 24px; }
.login form { width: 100%; max-width: 340px; text-align: center; }
.login .logo { width: 64px; height: 64px; border-radius: 18px; margin-bottom: 12px; }
.login h1 { font-size: 22px; margin: 0 0 20px; font-weight: 600; }
.err { color: #dc2626; font-size: 14px; margin-top: 10px; min-height: 20px; }

/* ---------- tabs ---------- */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; background: rgba(255,255,255,.94);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); height: calc(var(--tabs-h) + var(--safe-b));
}
#tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--faint); font-size: 11px; font-weight: 500; }
#tabs button.on { color: var(--ink); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + var(--safe-b) + 14px); transform: translateX(-50%); z-index: 30;
  background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 999px; font-size: 14px; animation: fade .15s; max-width: 90vw;
}

/* ---------- availability editor ---------- */
.av-days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; background: var(--soft); padding: 4px; border-radius: 12px; }
.av-day { height: 38px; border-radius: 9px; font-weight: 500; color: var(--mute); position: relative; }
.av-day.has { color: var(--ink); }
.av-day.has::after { content: ''; position: absolute; left: 50%; bottom: 4px; width: 5px; height: 5px; margin-left: -2.5px; border-radius: 50%; background: #22c55e; }
.av-day.sel { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); font-weight: 600; }
.av-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin: 12px 2px 8px; }
.av-head span { font-size: 13px; color: #15803d; text-align: right; }
.av-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; touch-action: none; user-select: none; -webkit-user-select: none; }
.av-h {
  height: 50px; border-radius: 11px; border: 1px solid var(--line); background: #fff; display: flex; flex-direction: column;
  align-items: center; justify-content: center; font-weight: 600; font-size: 15px; font-variant-numeric: tabular-nums; line-height: 1.15;
}
.av-h small { font-size: 10px; font-weight: 400; color: var(--faint); }
.av-h.on { background: #22c55e; border-color: #22c55e; color: #fff; }
.av-h.on small { color: rgba(255,255,255,.85); }
.av-tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px; }
.av-hint { font-size: 13px; color: var(--mute); }
.av-sum { margin-top: 12px; background: var(--soft); border-radius: 12px; padding: 10px 12px; font-size: 13px; display: flex; flex-direction: column; gap: 3px; }
.av-sum b { display: inline-block; width: 26px; font-weight: 600; }

/* ---------- availability in list rows ---------- */
.wk { display: flex; gap: 3px; margin-top: 5px; }
.wk i {
  font-style: normal; font-size: 11px; min-width: 24px; height: 20px; padding: 0 3px; border-radius: 6px;
  display: grid; place-items: center; background: var(--soft); color: #d6d3d1; font-weight: 500;
}
.wk i.on { background: #dcfce7; color: #166534; font-weight: 600; }
.wk i.mark { box-shadow: 0 0 0 1.5px var(--ink); }
.wk-t { font-size: 13px; color: var(--mute); margin-top: 4px; display: flex; flex-wrap: wrap; gap: 2px 10px; font-variant-numeric: tabular-nums; }
.wk-t b { color: var(--ink); font-weight: 600; }
