/* Hour Meter - mobile first. Colors as tokens; dark mode is its own set of values. */
:root {
  color-scheme: light;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --text: #191919;
  --text-2: #5c6470;
  --border: #dde2e8;
  --primary: #1757ac;
  --primary-ink: #ffffff;
  --primary-soft: #e6effb;
  --ok: #006c10;
  --ok-soft: #e3f4e5;
  --warn: #8a5a00;
  --warn-soft: #fff3d6;
  --bad: #b42324;
  --bad-soft: #fde6e6;
  --focus: #257de0;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --tabbar-h: 62px;
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0b0b0b;
    --surface: #191919;
    --surface-2: #232323;
    --text: #efefef;
    --text-2: #a3a3a3;
    --border: #333333;
    --primary: #4a96ee;
    --primary-ink: #0b0b0b;
    --primary-soft: #16263b;
    --ok: #53c556;
    --ok-soft: #14301a;
    --warn: #fbb315;
    --warn-soft: #3a2c0b;
    --bad: #f07576;
    --bad-soft: #3d1717;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
}
body.has-tabbar { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
body.modal-open { overflow: hidden; }
[hidden] { display: none !important; }
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.skip-link { position: absolute; left: -9999px; top: 0; }
.skip-link:focus { left: 8px; top: 8px; z-index: 100; background: var(--surface); padding: 8px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }
.strong { font-weight: 600; }
.grow { flex: 1; min-width: 0; }
.text-bad { color: var(--bad); }
.text-warn { color: var(--warn); }
.print-only { display: none; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner { max-width: 1200px; margin: 0 auto; display: flex; align-items: center; gap: 12px; padding: 8px 16px; min-height: 52px; }
.brand { font-weight: 700; color: var(--primary); white-space: nowrap; }
.nav { display: none; gap: 4px; }
.nav-link { padding: 8px 12px; border-radius: 8px; text-decoration: none; color: var(--text-2); font-weight: 500; }
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--primary-soft); color: var(--primary); }
.who { margin-left: auto; display: flex; align-items: center; gap: 8px; min-width: 0; }
.who .btn { white-space: nowrap; flex: none; }
@media (max-width: 420px) { .brand { display: none; } .who-name { max-width: 58vw; } }
.who-name { color: var(--text-2); font-size: .85rem; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 46vw; }
.who-name:hover { text-decoration: underline; }
.main { max-width: 1200px; margin: 0 auto; padding: 16px; outline: none; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; min-height: var(--tabbar-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--text-2); font-size: .78rem;
}
.tab.active { color: var(--primary); font-weight: 600; }
.icon { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

@media (min-width: 900px) {
  .nav { display: flex; }
  .tabbar { display: none !important; }
  body.has-tabbar { padding-bottom: 0; }
  .who-name { max-width: 260px; }
  .main { padding: 24px; }
}

/* ---------- page ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 8px 16px; margin-bottom: 12px; }
.page-title { font-size: 1.35rem; margin: 0; line-height: 1.3; }
.page-head p { margin: 2px 0 0; }
.head-actions { display: flex; gap: 8px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-title { font-size: 1.05rem; margin: 0 0 4px; }
.card-head, .section-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin: 8px 0; }
.group-title { font-size: 1rem; margin: 20px 0 8px; display: flex; align-items: center; gap: 6px; }
.badge { display: inline-block; background: var(--primary); color: var(--primary-ink); border-radius: 6px; padding: 1px 8px; font-size: .85rem; font-weight: 700; }
.stack { display: grid; gap: 10px; }
.stack-sm { display: grid; gap: 4px; margin: 8px 0 12px; }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }

/* ---------- forms & buttons ---------- */
.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field-label { font-size: .9rem; font-weight: 500; }
.hint { color: var(--text-2); font-size: .8rem; }
.input {
  width: 100%; min-height: 44px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); font-size: 16px;
}
textarea.input { resize: vertical; }
.input:disabled { background: var(--surface-2); color: var(--text-2); }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 8px 0; cursor: pointer; }
.check input { width: 20px; height: 20px; margin-top: 2px; flex: none; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 8px 16px;
  border-radius: 8px; border: 1px solid transparent; background: var(--surface-2); color: var(--text);
  font-weight: 600; text-decoration: none; cursor: pointer; text-align: center; -webkit-tap-highlight-color: transparent;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-danger { background: var(--bad); color: #fff; }
.btn-danger-ghost { background: transparent; border-color: var(--bad); color: var(--bad); }
.btn-sm { min-height: 36px; padding: 4px 12px; font-size: .88rem; }
.btn-lg { min-height: 52px; font-size: 1.05rem; }
.btn-block { width: 100%; }
.btn.is-busy { cursor: progress; }
.link-back { background: none; border: 0; color: var(--primary); padding: 8px 0; cursor: pointer; font-weight: 500; }
.form-error, .modal-error { color: var(--bad); margin: 4px 0 8px; min-height: 0; }
.form-error:empty, .modal-error:empty { display: none; }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; margin-bottom: 8px; }
.filters .field:first-child { grid-column: 1 / -1; }
.filters-5 .field:first-child { grid-column: auto; }
.filters-5 .field:nth-child(5) { grid-column: 1 / -1; }
@media (min-width: 700px) {
  .filters { grid-template-columns: repeat(3, minmax(0, 220px)); }
  .filters .field:first-child { grid-column: auto; }
  .filters-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .filters-5 .field:nth-child(5) { grid-column: auto; }
}

/* ---------- notices / chips / states ---------- */
.notice { padding: 10px 12px; border-radius: 8px; margin: 8px 0; border-left: 4px solid; }
.notice-warn { background: var(--warn-soft); border-color: var(--warn); color: var(--text); }
.notice-ok { background: var(--ok-soft); border-color: var(--ok); }
.notice-card { border-left: 4px solid var(--warn); }
.chips { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.chip { display: inline-block; font-size: .75rem; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; margin: 2px 2px 0 0; }
.chip-info { background: var(--surface-2); color: var(--text-2); }
.chip-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.chip-warn::before { content: "⚠ "; }
.chip-bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.chip-bad::before { content: "✕ "; }
.status { display: inline-block; font-size: .8rem; font-weight: 600; padding: 1px 8px; border-radius: 6px; margin-right: 4px; }
.status-ok { background: var(--ok-soft); color: var(--ok); }
.status-ok::before { content: "✓ "; }
.status-todo { background: var(--warn-soft); color: var(--warn); }
.status-bad { background: var(--bad-soft); color: var(--bad); }
.state { padding: 24px 16px; text-align: center; color: var(--text-2); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.state-loading { flex-direction: row; justify-content: center; }
.state-error { color: var(--bad); background: var(--bad-soft); border-radius: var(--radius); }
.state-error p { margin: 0; }
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

/* ---------- auth ---------- */
.page-auth { display: flex; justify-content: center; padding-top: 4vh; }
.auth-card { width: 100%; max-width: 400px; }
.auth-title { font-size: 1.4rem; margin: 0 0 4px; color: var(--primary); }

/* ---------- record ---------- */
.meter-grid { display: grid; gap: 10px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .meter-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1000px) { .meter-grid { grid-template-columns: repeat(3, 1fr); } }
.meter-card {
  display: grid; gap: 2px; text-align: left; padding: 14px; background: var(--surface); border: 1px solid var(--border);
  border-left: 5px solid var(--warn); border-radius: var(--radius); cursor: pointer; box-shadow: var(--shadow);
}
.meter-card.is-done { border-left-color: var(--ok); }
.meter-card:hover { border-color: var(--primary); }
.meter-name { font-weight: 700; font-size: 1.02rem; }
.meter-code, .meter-last { color: var(--text-2); font-size: .85rem; }
.meter-status { font-size: .85rem; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.meter-status.ok { color: var(--ok); }
.meter-status.todo { color: var(--warn); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.meter-header { margin: 4px 0 12px; }
.meter-title { margin: 0; font-size: 1.2rem; }
.capture-preview { background: var(--surface); border: 2px dashed var(--border); border-radius: var(--radius); min-height: 200px; display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 12px 0; padding: 8px; text-align: center; }
.capture-preview img { max-width: 100%; max-height: 60vh; border-radius: 8px; }
.capture-hint p { margin: 4px 0; }
.list { list-style: none; padding: 0; margin: 8px 0 0; }
.list-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); }

.confirm-layout { display: grid; gap: 16px; }
@media (min-width: 900px) { .confirm-layout { grid-template-columns: 1.2fr 1fr; align-items: start; } }
.confirm-photo img { width: 100%; max-height: 42vh; object-fit: contain; border-radius: var(--radius); display: block; background: #111; cursor: zoom-in; }
.confirm-photo img.zoomed { max-height: none; cursor: zoom-out; }
@media (min-width: 900px) { .confirm-photo { position: sticky; top: 72px; } .confirm-photo img { max-height: 80vh; } }
.confirm-photo .thumb-lg { width: 100%; height: auto; min-height: 220px; }
.ai-box { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 10px 12px; background: var(--primary-soft); border-radius: 8px; margin-bottom: 12px; }
.ai-box > span:first-child { flex-basis: 100%; }
.ai-value { font-size: 1.6rem; font-variant-numeric: tabular-nums; letter-spacing: .04em; }
.prev-box { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; margin-bottom: 12px; }
.prev-box strong { font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.reading-input { border: 0; padding: 0; margin: 0 0 8px; }
.reading-input legend { font-weight: 600; margin-bottom: 6px; padding: 0; }
.reading-row { display: flex; align-items: flex-end; gap: 6px; }
.input-reading, .input-decimal { font-size: 2rem; font-weight: 700; min-height: 60px; text-align: center; font-variant-numeric: tabular-nums; letter-spacing: .08em; }
.input-reading { flex: 1; min-width: 0; }
.input-decimal { width: 64px; flex: none; border-color: var(--bad); color: var(--bad); }
.input-decimal.ai-filled { background: var(--warn-soft); box-shadow: 0 0 0 3px var(--warn-soft); }
.dec-check { margin: 6px 0 2px; padding: 6px 10px; border-left: 4px solid var(--warn); background: var(--warn-soft); border-radius: 6px; font-size: .9rem; font-weight: 600; color: var(--text); }
.reading-dot { font-size: 2rem; font-weight: 700; line-height: 60px; }
.delta-preview { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.delta-preview p { margin: 4px 0 0; }
.kv { display: flex; justify-content: space-between; gap: 8px; }
.confirm-form .btn { margin-top: 8px; }
.ack-list { padding-left: 20px; }
.ack-list li { margin: 4px 0; }
.done-card { max-width: 560px; margin: 0 auto; text-align: center; }
.done-card .meter-header { text-align: center; }
.done-card .btn { margin-top: 8px; }
.done-icon { width: 56px; height: 56px; margin: 0 auto 8px; border-radius: 50%; background: var(--ok-soft); color: var(--ok); font-size: 2rem; line-height: 56px; font-weight: 700; }
.kv-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; margin: 12px 0; }
.kv-item { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; display: grid; }
.kv-item strong { font-variant-numeric: tabular-nums; }

/* ---------- report ---------- */
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 16px; }
@media (min-width: 900px) { .cards { grid-template-columns: repeat(4, 1fr); } }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 2px; box-shadow: var(--shadow); }
.stat-label { color: var(--text-2); font-size: .82rem; }
.stat-value { font-size: 1.35rem; line-height: 1.25; }
.stat-sub { font-size: .8rem; color: var(--text-2); }
.stat-warn { border-left: 4px solid var(--warn); }
.stat-ok { border-left: 4px solid var(--ok); }
.chart-box { position: relative; width: 100%; }
.chart-bar { height: 300px; }
.chart-line { height: 300px; }
@media (min-width: 900px) { .chart-line { height: 340px; } }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg { background: transparent; border: 0; padding: 6px 12px; min-height: 36px; cursor: pointer; font-size: .88rem; color: var(--text-2); }
.seg.active { background: var(--primary); color: var(--primary-ink); font-weight: 600; }
.series-picker { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.pick { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; font-size: .85rem; cursor: pointer; }
.pick input { margin: 0; }
.swatch { width: 10px; height: 10px; border-radius: 50%; }

/* ---------- tables (stack into cards on phones) ---------- */
.table-wrap { width: 100%; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { font-size: .8rem; color: var(--text-2); font-weight: 600; white-space: nowrap; background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-missing td { background: var(--warn-soft); }
.row-void td { opacity: .6; }
.cell-actions { white-space: nowrap; }
.cell-actions .btn { margin: 2px; }
.cell-note { max-width: 220px; overflow-wrap: anywhere; }
.cell-json { font-family: ui-monospace, Consolas, monospace; font-size: .78rem; max-width: 280px; overflow-wrap: anywhere; }
@media (max-width: 760px) {
  .table-stack thead { display: none; }
  .table-stack, .table-stack tbody, .table-stack tr, .table-stack td { display: block; width: 100%; }
  .table-stack tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; padding: 6px 12px; }
  .table-stack td { border: 0; padding: 4px 0; display: grid; grid-template-columns: minmax(84px, 38%) 1fr; column-gap: 12px; align-items: start; text-align: right; }
  .table-stack td > * { grid-column: 2; justify-self: end; text-align: right; }
  .table-stack td::before { content: attr(data-label); grid-column: 1; grid-row: 1 / span 6; color: var(--text-2); font-size: .8rem; text-align: left; }
  .table-stack td:not([data-label]) { display: block; }
  .table-stack td .chips { justify-content: flex-end; }
  .table-stack td:not([data-label])::before { content: none; }
  .table-stack td.num { text-align: right; }
  .table-stack .row-missing td { background: transparent; }
  .table-stack tr.row-missing { border-left: 4px solid var(--warn); }
  .cell-note, .cell-json { max-width: none; }
}
.pager { display: flex; justify-content: center; align-items: center; gap: 12px; margin: 12px 0; }

/* ---------- photos ---------- */
.thumb { width: 64px; height: 64px; padding: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); overflow: hidden; cursor: zoom-in; display: inline-flex; align-items: center; justify-content: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-loading { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--primary); animation: spin .8s linear infinite; }
.photo-missing { font-size: .72rem; color: var(--text-2); text-align: center; padding: 4px; }
.photo-full { min-height: 160px; display: flex; align-items: center; justify-content: center; }
.photo-full img { max-width: 100%; max-height: 75vh; border-radius: 8px; }
.photo-full .photo-missing { font-size: .95rem; padding: 32px; }

/* ---------- admin ---------- */
.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--border); margin: 12px 0 16px; }
.tab-btn { background: none; border: 0; border-bottom: 3px solid transparent; padding: 10px 14px; cursor: pointer; white-space: nowrap; color: var(--text-2); font-weight: 500; min-height: 44px; }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.admin-body select.input { min-height: 36px; padding: 4px 8px; width: auto; }

/* ---------- modal & toast ---------- */
.modal-backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--surface); width: 100%; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 18px 16px calc(16px + env(safe-area-inset-bottom)); }
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; padding: 24px; }
  .modal { max-width: 560px; border-radius: 16px; }
}
.modal-title { font-size: 1.15rem; margin: 0 0 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 12px; }
.modal-actions .btn { flex: 1 1 140px; }
@media (min-width: 640px) { .modal-actions .btn { flex: 0 0 auto; } }
.toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom)); z-index: 60; display: grid; gap: 8px; width: min(92vw, 420px); }
.toast { padding: 12px 14px; border-radius: 10px; background: #1f2937; color: #fff; box-shadow: 0 4px 16px rgba(0, 0, 0, .2); transition: opacity .4s; }
.toast-success { background: #0b5d18; }
.toast-error { background: #a3191a; }
.toast.out { opacity: 0; }
@media (min-width: 900px) { .toasts { bottom: 24px; } }

/* ---------- print (report -> PDF) ---------- */
@media print {
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --surface-2: #f3f3f3; --text: #000; --text-2: #444; --border: #bbb; --shadow: none; }
  body { background: #fff; padding: 0 !important; font-size: 11pt; }
  .topbar, .tabbar, .toasts, .no-print, .skip-link, .head-actions, .series-picker, .thumb { display: none !important; }
  .print-only { display: block; }
  .main { max-width: none; padding: 0; }
  .card, .stat { break-inside: avoid; box-shadow: none; }
  .cards { grid-template-columns: repeat(4, 1fr); }
  .chart-bar, .chart-line { height: 260px; }
  .table-stack thead { display: table-header-group !important; }
  .table-stack, .table-stack tbody { display: table !important; width: 100%; }
  .table-stack tbody { display: table-row-group !important; }
  .table-stack tr { display: table-row !important; border: 0; padding: 0; }
  .table-stack td { display: table-cell !important; text-align: left; }
  .table-stack td::before { content: none !important; }
  .table { font-size: 9pt; }
  .cell-photo { display: none !important; }
  .table th:last-child { display: none; }
  @page { size: A4 landscape; margin: 12mm; }
}
