:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #f0f3f1;
  --text: #1c2522;
  --muted: #5f6b67;
  --line: #dfe5e2;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e3f3f1;
  --ok: #15803d;
  --ok-soft: #e6f4ea;
  --warn: #b45309;
  --warn-soft: #fdf3e1;
  --danger: #b91c1c;
  --danger-soft: #fdecec;
  --accent: #f5c84c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 40, 34, .06), 0 2px 8px rgba(16, 40, 34, .05);
  --side: 260px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111715; --surface: #18201e; --surface-2: #202a27; --text: #e7eeeb; --muted: #9fb0aa; --line: #2c3834;
    --primary: #2fb3a5; --primary-ink: #06221f; --primary-soft: #183a36;
    --ok: #4ade80; --ok-soft: #173325; --warn: #fbbf24; --warn-soft: #3a2f14; --danger: #f87171; --danger-soft: #3b1d1d;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111715; --surface: #18201e; --surface-2: #202a27; --text: #e7eeeb; --muted: #9fb0aa; --line: #2c3834;
  --primary: #2fb3a5; --primary-ink: #06221f; --primary-soft: #183a36;
  --ok: #4ade80; --ok-soft: #173325; --warn: #fbbf24; --warn-soft: #3a2f14; --danger: #f87171; --danger-soft: #3b1d1d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.6 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif;
}
body.no-scroll { overflow: hidden; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { line-height: 1.3; margin: 0 0 .5rem; }
h2 { font-size: 1.3rem; } h3 { font-size: 1.08rem; } h4 { font-size: 1rem; margin-top: 1rem; }
p { margin: 0 0 .6rem; }
.muted { color: var(--muted); }
.sm { font-size: .9rem; }
.req { color: var(--danger); }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--side); background: var(--surface);
  border-right: 1px solid var(--line); padding: 1rem .75rem; overflow-y: auto; z-index: 40;
  display: flex; flex-direction: column;
}
.brand { display: flex; gap: .7rem; align-items: center; padding: .25rem .5rem 1rem; }
.brand-mark { width: 42px; height: 42px; border-radius: 12px; background: var(--primary); color: var(--primary-ink); display: grid; place-items: center; font-weight: 700; font-size: 1.2rem; flex: none; }
.brand strong { display: block; } .brand small { color: var(--muted); font-size: .8rem; }
.sidebar nav a {
  display: flex; align-items: center; gap: .7rem; padding: .65rem .75rem; border-radius: 10px; color: var(--text); font-weight: 500;
}
.sidebar nav a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav-ico { width: 1.4rem; text-align: center; }
.badge { margin-left: auto; background: var(--danger); color: #fff; border-radius: 99px; font-size: .75rem; padding: 0 .45rem; min-width: 1.3rem; text-align: center; }
.side-note { margin-top: auto; font-size: .8rem; color: var(--muted); padding: 1rem .5rem 0; }
.main { margin-left: var(--side); min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: .75rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  padding: .8rem 1.5rem; border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 1.15rem; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { display: none; }
.view { padding: 1.25rem 1.5rem 3rem; max-width: 1200px; }
.bottom-nav { display: none; }
.scrim { display: none; }

@media (max-width: 899px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  body.drawer-open .sidebar { transform: none; }
  body.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 35; }
  .main { margin-left: 0; padding-bottom: 72px; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: .6rem 1rem; }
  .top-add { display: none; }
  .view { padding: 1rem 1rem 2rem; }
  .bottom-nav {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; inset: auto 0 0 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a, .bottom-nav button {
    display: flex; flex-direction: column; align-items: center; gap: 2px; padding: .45rem 0 .5rem; font: inherit;
    font-size: .75rem; color: var(--muted); background: none; border: 0; position: relative; min-height: 56px; justify-content: center;
  }
  .bottom-nav span:first-child { font-size: 1.25rem; line-height: 1; }
  .bottom-nav a.active { color: var(--primary); font-weight: 600; }
  .bottom-nav a:hover { text-decoration: none; }
  .bottom-nav .badge { position: absolute; top: 4px; left: 56%; margin: 0; }
}

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); margin-bottom: 1rem; display: block; color: inherit; }
a.card:hover { text-decoration: none; border-color: var(--primary); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .75rem; margin-bottom: .5rem; flex-wrap: wrap; }
.card-head h3 { margin: 0; }
.grid-2 { display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.cards { display: grid; gap: .9rem; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); }
.cards .card { margin: 0; display: flex; flex-direction: column; gap: .3rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem; min-height: 44px; padding: .5rem 1rem;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { text-decoration: none; border-color: var(--primary); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-sm { min-height: 36px; padding: .25rem .7rem; font-size: .9rem; }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn-ghost-danger { background: transparent; color: var(--danger); border-color: transparent; }
.btn-ghost-danger:hover { background: var(--danger-soft); border-color: transparent; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.btn-row.quick { margin-bottom: 1rem; }
.icon-btn { width: 40px; height: 40px; display: inline-grid; place-items: center; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; font-size: 1.05rem; flex: none; }

.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-bottom: .75rem; }
.toolbar input[type=search] { flex: 1 1 220px; }
.toolbar .spacer { flex: 1; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: .55rem .75rem; min-height: 44px; width: 100%; max-width: 100%;
}
.toolbar input, .toolbar select { width: auto; }
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 45%, transparent); border-color: var(--primary); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem 1rem; }
.field { display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.field.full, .form-grid > .full { grid-column: 1 / -1; }
.field label { font-weight: 600; font-size: .95rem; }
.hint { color: var(--muted); font-size: .82rem; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } }
.checks { display: flex; flex-wrap: wrap; gap: .45rem; }
.check { display: inline-flex; align-items: center; gap: .45rem; padding: .4rem .7rem; border: 1px solid var(--line); border-radius: 99px; cursor: pointer; background: var(--surface); min-height: 40px; font-weight: 400 !important; }
.check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); margin: 0; }
.check:has(input:checked) { background: var(--primary-soft); border-color: var(--primary); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; width: fit-content; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: block; padding: .55rem 1.1rem; min-height: 44px; }
.seg input:checked + span { background: var(--primary); color: var(--primary-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: -3px; }
fieldset { border: 0; border-top: 1px solid var(--line); margin: 0 0 1rem; padding: 1rem 0 0; }
fieldset:first-of-type { border-top: 0; padding-top: 0; }
legend { font-weight: 700; font-size: 1.05rem; color: var(--primary); padding: 0; margin-bottom: .6rem; }
.form-actions { display: flex; gap: .6rem; justify-content: flex-end; flex-wrap: wrap; margin-top: .5rem; }
.sticky-actions { position: sticky; bottom: 0; background: var(--surface); padding: .75rem 0; border-top: 1px solid var(--line); margin: 0; }
@media (max-width: 899px) { .sticky-actions { bottom: 64px; } }
.fu-row { display: grid; grid-template-columns: 1fr 170px auto; gap: .5rem; margin-bottom: .5rem; align-items: center; }
@media (max-width: 640px) { .fu-row { grid-template-columns: 1fr auto; } .fu-row input[type=text], .fu-row input:first-child { grid-column: 1 / -1; } }
.file-drop { display: flex; align-items: center; justify-content: center; border: 2px dashed var(--line); border-radius: var(--radius); padding: 1.1rem; cursor: pointer; font-weight: 600; color: var(--primary); min-height: 64px; }
.file-drop input { display: none; }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: .5rem; margin-top: .6rem; }
.photos img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 10px; display: block; background: var(--surface-2); }
.ph { position: relative; }
.ph .icon-btn { position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; font-size: .85rem; }

.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: thin; }
.tabs a { padding: .6rem .9rem; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; font-weight: 500; }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }
.tabs a:hover { text-decoration: none; color: var(--text); }

.list { list-style: none; margin: 0; padding: 0; }
.item { display: flex; gap: .75rem; align-items: center; padding: .65rem 0; border-bottom: 1px solid var(--line); }
.item:last-child { border-bottom: 0; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: .1rem; color: inherit; }
a.item-main:hover { text-decoration: none; }
a.item-main:hover strong { color: var(--primary); }
.item small { font-size: .86rem; }
.date-chip { width: 50px; flex: none; text-align: center; background: var(--primary-soft); color: var(--primary); border-radius: 10px; padding: .25rem 0; line-height: 1.15; }
.date-chip b { display: block; font-size: 1.25rem; } .date-chip small { font-size: .75rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--warn); }
.dot-overdue { background: var(--danger); } .dot-done { background: var(--ok); }
.fu-done strong { text-decoration: line-through; color: var(--muted); }
.plan-late .date-chip { background: var(--warn-soft); color: var(--warn); }
.plan-cancelled { opacity: .6; }

.tags { display: flex; flex-wrap: wrap; gap: .35rem; margin: .2rem 0; }
.tag, .pill { display: inline-block; font-size: .78rem; padding: .1rem .55rem; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.tag-ok, .pill-done { background: var(--ok-soft); color: var(--ok); }
.tag-warn, .pill-planned { background: var(--warn-soft); color: var(--warn); }
.tag-danger, .pill-late { background: var(--danger-soft); color: var(--danger); }
.pill-onsite { background: var(--primary-soft); color: var(--primary); }
.chips { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .4rem; }
.chip { display: inline-block; padding: .35rem .75rem; border-radius: 99px; background: var(--surface-2); color: var(--text); font-size: .9rem; }
.chip:hover { background: var(--primary-soft); text-decoration: none; }

.alert { border-radius: var(--radius); padding: .75rem 1rem; margin-bottom: 1rem; display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; justify-content: space-between; }
.alert.sm { padding: .5rem .75rem; font-size: .9rem; }
.alert-warn { background: var(--warn-soft); color: var(--warn); }
.alert-info { background: var(--primary-soft); color: var(--text); }

.hello { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.hello h2 { font-size: 1.5rem; margin: 0; }
.hello p { margin: 0; }
.hello-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(46%, 180px), 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; display: flex; flex-direction: column; color: inherit; box-shadow: var(--shadow); }
a.stat:hover { text-decoration: none; border-color: var(--primary); }
.stat-num { font-size: 1.9rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-num small { font-size: 1rem; color: var(--muted); font-weight: 500; }
.stat-lbl { color: var(--muted); font-size: .88rem; }
.stat-danger .stat-num { color: var(--danger); }
.meter { height: 6px; background: var(--surface-2); border-radius: 99px; margin-top: .5rem; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.onboarding ol { margin: .3rem 0 .6rem; padding-left: 1.3rem; }
.empty { padding: 2rem 1rem; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); grid-column: 1 / -1; }
.empty .btn { margin-top: .75rem; }
.empty-sm { color: var(--muted); font-size: .92rem; padding: .5rem 0; margin: 0; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: .5rem 0; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
@media (max-width: 520px) { .kv { grid-template-columns: 1fr; } .kv dd { margin-bottom: .4rem; } }
.h-school { margin: 0; }
.rec-top { display: flex; justify-content: space-between; gap: .5rem; align-items: center; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.clamp3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.doc-card h3 { margin: .2rem 0; }
.doc-card.past { opacity: .7; }
.doc-card .btn-row { margin-top: auto; }

.timeline { list-style: none; padding: 0 0 0 1rem; margin: 0; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 0 0 1rem .75rem; }
.timeline li::before { content: ''; position: absolute; left: -1.43rem; top: .45rem; width: 12px; height: 12px; border-radius: 50%; background: var(--primary); border: 2px solid var(--surface); }
.timeline a { color: inherit; display: block; }
.timeline a:hover b { color: var(--primary); }

.qtrend { display: flex; gap: .5rem; align-items: flex-end; height: 140px; padding: .5rem 0; overflow-x: auto; }
.qbar { flex: 0 0 56px; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: .25rem; }
.qbar i { display: block; width: 28px; background: var(--primary); border-radius: 6px 6px 0 0; }
.qbar small { font-size: .7rem; color: var(--muted); white-space: nowrap; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .93rem; }
.table th, .table td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table.compact td { padding: .35rem .4rem; }
.row-mute td { color: var(--muted); }
@media (max-width: 640px) {
  .table-wrap > .table:not(.compact) thead { display: none; }
  .table-wrap > .table:not(.compact) tr { display: block; border-bottom: 1px solid var(--line); padding: .5rem 0; }
  .table-wrap > .table:not(.compact) td { display: flex; gap: .75rem; border: 0; padding: .15rem 0; }
  .table-wrap > .table:not(.compact) td[data-l]::before { content: attr(data-l); color: var(--muted); min-width: 6.5rem; flex: none; }
}
.bars { list-style: none; padding: 0; margin: 0; }
.bars li { display: grid; grid-template-columns: minmax(0, 1.4fr) 1fr 2rem; gap: .6rem; align-items: center; padding: .3rem 0; font-size: .92rem; }
.bar { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.bars b { text-align: right; font-variant-numeric: tabular-nums; }
.report-head { text-align: center; margin-bottom: 1rem; }
.report-head p { margin: 0; }
.confirm-msg { font-size: 1.02rem; }

/* ---------- modal & toast ---------- */
.modal-wrap { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 100; display: grid; place-items: center; padding: 1rem; }
.modal { background: var(--surface); border-radius: var(--radius); width: min(560px, 100%); max-height: calc(100vh - 2rem); display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { width: min(780px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .9rem 1.1rem; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 1rem 1.1rem 1.1rem; overflow-y: auto; }
@media (max-width: 640px) {
  .modal-wrap { padding: 0; place-items: end stretch; }
  .modal, .modal.wide { width: 100%; max-height: 92vh; border-radius: var(--radius) var(--radius) 0 0; padding-bottom: env(safe-area-inset-bottom); }
}
.toast { position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px); opacity: 0; transition: .25s; z-index: 200; background: #1c2522; color: #fff; padding: .7rem 1.1rem; border-radius: 10px; box-shadow: var(--shadow); max-width: calc(100% - 2rem); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-err { background: var(--danger); }

/* ---------- print / PDF ---------- */
.print-only { display: none; }
.signature { margin-top: 2.5rem; text-align: center; }
.signature p { margin: .2rem 0; }
@media print {
  @page { size: A4; margin: 15mm; }
  :root { --bg: #fff; --surface: #fff; --text: #000; --muted: #444; --line: #bbb; --surface-2: #eee; }
  body { font-size: 14pt; background: #fff; }
  .sidebar, .topbar, .bottom-nav, .no-print, .toast, .alert, .scrim, .tabs, .toolbar { display: none !important; }
  .main { margin: 0; padding: 0; }
  .view { padding: 0; max-width: none; }
  .print-only { display: block; }
  .card, .stat { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  .grid-2 { grid-template-columns: 1fr 1fr; }
  a { color: #000; }
  .table th { background: #eee !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .table-wrap > .table thead { display: table-header-group !important; }
  .table-wrap > .table tr { display: table-row !important; }
  .table-wrap > .table td { display: table-cell !important; }
  .table-wrap > .table td::before { content: none !important; }
  .bar i, .meter i, .qbar i { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .photos { grid-template-columns: repeat(3, 1fr); }
  .record-doc { border: 0; }
}
