:root {
    color-scheme: light;
    --bg: #f3f6fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #eef2f7;
    --text: #0f172a;
    --muted: #526071;
    --line: #d8dee8;
    --line-strong: #c2cad6;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-contrast: #ffffff;
    --danger: #b42318;
    --danger-bg: #fff0ee;
    --success: #067647;
    --success-bg: #e9fbf1;
    --warning: #9a5200;
    --warning-bg: #fff5d6;
    --orange: #a84012;
    --orange-bg: #fff0e6;
    --shadow: 0 14px 40px rgba(15, 23, 42, .09);
    --focus-ring: rgba(37, 99, 235, .22);
    --report-bg: #ffffff;
    --report-text: #101828;
    --report-muted: #475467;
    --report-line: #d0d5dd;
    --report-head: #f2f4f7;
    --radius: 18px;
    --radius-sm: 12px;
    --nav-h: 68px;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #070b12;
    --surface: #111827;
    --surface-2: #172033;
    --surface-3: #22304a;
    --text: #f8fafc;
    --muted: #c6d0df;
    --line: #334155;
    --line-strong: #475569;
    --primary: #93c5fd;
    --primary-hover: #bfdbfe;
    --primary-contrast: #0b1220;
    --danger: #fda29b;
    --danger-bg: #3a171b;
    --success: #6ce9a6;
    --success-bg: #123326;
    --warning: #fec84b;
    --warning-bg: #3a2e0d;
    --orange: #fdb022;
    --orange-bg: #3b2612;
    --shadow: 0 18px 54px rgba(0, 0, 0, .42);
    --focus-ring: rgba(147, 197, 253, .28);
    --report-bg: #111827;
    --report-text: #f8fafc;
    --report-muted: #cbd5e1;
    --report-line: #475569;
    --report-head: #1e293b;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", sans-serif;
    transition: background-color .18s ease, color .18s ease;
}
button, input, select, textarea { font: inherit; }
button, .clickable { touch-action: manipulation; }
a { color: inherit; text-decoration: none; }

.app-shell {
    width: min(1120px, calc(100% - 28px));
    margin: 0 auto;
    padding: 18px 0 96px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 18px;
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-icon {
    width: 42px; height: 42px; border-radius: 14px;
    display: grid; place-items: center;
    background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
    font-size: 22px;
}
.brand h1 { margin: 0; font-size: clamp(22px, 4vw, 30px); letter-spacing: -.025em; }
.brand small { display:block; color:var(--muted); margin-top:2px; font-size:12px; }
.top-actions { display:flex; gap:8px; align-items:center; }

.theme-button, .icon-button, .secondary-button, .danger-button, .primary-button, .ghost-button {
    border: 1px solid var(--line);
    border-radius: 13px;
    min-height: 44px;
    padding: 10px 14px;
    cursor: pointer;
    transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
.theme-button:active, .icon-button:active, .secondary-button:active, .danger-button:active, .primary-button:active, .ghost-button:active { transform: scale(.98); }
.theme-button, .secondary-button, .ghost-button, .icon-button { background: var(--surface); color: var(--text); }
.primary-button { background: var(--primary); color: var(--primary-contrast); border-color: var(--primary); font-weight: 800; }
.danger-button { background: var(--danger-bg); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 25%, var(--line)); font-weight: 700; }
.ghost-button { background: transparent; }
.theme-button { display:flex; align-items:center; gap:7px; font-weight:700; white-space:nowrap; }

.desktop-nav {
    display:flex; gap:6px; margin-bottom:18px; padding:6px;
    background:var(--surface); border:1px solid var(--line); border-radius:16px;
}
.desktop-nav a { padding:10px 14px; border-radius:11px; color:var(--muted); font-weight:700; font-size:14px; }
.desktop-nav a.active { background:var(--surface-3); color:var(--text); }

.mobile-nav {
    display:none; position:fixed; z-index:90; left:0; right:0; bottom:0;
    padding:6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: blur(16px); border-top:1px solid var(--line);
}
.mobile-nav-inner { display:grid; grid-template-columns:repeat(4,1fr); gap:4px; max-width:620px; margin:0 auto; }
.mobile-nav a { min-height:54px; border-radius:12px; display:grid; place-items:center; align-content:center; gap:1px; color:var(--muted); font-size:11px; font-weight:700; }
.mobile-nav a .nav-icon { font-size:20px; line-height:1; }
.mobile-nav a.active { color:var(--primary); background:color-mix(in srgb, var(--primary) 10%, transparent); }

.card { background:var(--surface); border:1px solid var(--line); border-radius:var(--radius); box-shadow:var(--shadow); }
.card-body { padding:18px; }
.section-title { display:flex; justify-content:space-between; gap:12px; align-items:center; margin-bottom:14px; }
.section-title h2 { margin:0; font-size:18px; }
.section-title p { margin:3px 0 0; color:var(--muted); font-size:13px; }

.form-grid { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:12px; }
.field label { display:block; margin-bottom:7px; color:var(--muted); font-size:12px; font-weight:800; }
.input, input[type="number"], input[type="text"], input[type="password"], input[type="date"], select, textarea {
    width:100%; min-height:50px; border:1px solid var(--line); border-radius:13px; background:var(--surface-2); color:var(--text); padding:11px 13px; outline:none; caret-color:var(--text);
}
.input:focus, input:focus, select:focus, textarea:focus { border-color:var(--primary); box-shadow:0 0 0 3px var(--focus-ring); }

.date-display { display:flex; align-items:center; justify-content:space-between; text-align:left; }
.time-selects { display:grid; grid-template-columns:1fr auto 1fr; align-items:center; gap:6px; }
.time-sep { color:var(--muted); font-weight:900; }

.measure-grid { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:10px; margin-top:14px; }
.measure-box { min-width:0; }
.measure-box label { display:flex; justify-content:center; gap:5px; margin-bottom:7px; font-size:12px; font-weight:850; }
.measure-box label span { color:var(--muted); font-weight:600; }
.measure-box input { min-height:76px; text-align:center; font-size:30px; font-weight:900; letter-spacing:-.03em; padding:10px 4px; }

.period-wrap { margin-top:14px; }
.period-head { display:flex; justify-content:space-between; gap:10px; align-items:end; margin-bottom:8px; }
.period-head label { font-size:12px; font-weight:850; color:var(--muted); }
.auto-badge { font-size:11px; color:var(--primary); background:color-mix(in srgb,var(--primary) 10%,transparent); padding:5px 8px; border-radius:999px; font-weight:800; }
.period-options { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:8px; }
.period-button { border:1px solid var(--line); border-radius:13px; background:var(--surface-2); color:var(--text); min-height:62px; padding:8px; cursor:pointer; font-weight:800; }
.period-button small { display:block; margin-top:2px; color:var(--muted); font-size:10px; font-weight:600; }
.period-button.active { border-color:var(--primary); background:color-mix(in srgb,var(--primary) 11%,var(--surface)); color:var(--primary); }

.form-actions { display:flex; gap:9px; margin-top:16px; }
.form-actions .primary-button { flex:1; min-height:54px; font-size:16px; }
.edit-banner { display:none; margin-bottom:12px; padding:11px 13px; border-radius:12px; background:var(--warning-bg); color:var(--warning); font-weight:700; font-size:13px; }
.editing .edit-banner { display:block; }

.summary-grid { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:10px; }
.summary-card { padding:14px; background:var(--surface); border:1px solid var(--line); border-radius:15px; }
.summary-label { color:var(--muted); font-size:11px; font-weight:800; }
.summary-value { margin-top:3px; font-size:22px; font-weight:900; letter-spacing:-.03em; }
.summary-sub { color:var(--muted); font-size:11px; margin-top:2px; }

.bp-status { display:inline-flex; align-items:center; gap:6px; border-radius:999px; padding:6px 9px; font-size:11px; font-weight:850; line-height:1.2; }
.bp-normal { background:var(--success-bg); color:var(--success); }
.bp-elevated { background:var(--warning-bg); color:var(--warning); }
.bp-stage1 { background:var(--orange-bg); color:var(--orange); }
.bp-stage2, .bp-severe { background:var(--danger-bg); color:var(--danger); }

.history-group { margin-bottom:16px; }
.history-date-head { display:flex; justify-content:space-between; align-items:center; gap:10px; margin:0 2px 8px; }
.history-date-head strong { font-size:15px; }
.history-date-head span { color:var(--muted); font-size:12px; }
.reading-list { display:grid; gap:9px; }
.reading-card { background:var(--surface); border:1px solid var(--line); border-radius:16px; padding:14px; }
.reading-top { display:flex; justify-content:space-between; align-items:flex-start; gap:10px; }
.reading-period { font-weight:850; }
.reading-time { color:var(--muted); font-size:12px; margin-top:2px; }
.reading-values { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:8px; margin-top:11px; }
.reading-value { text-align:center; padding:10px 4px; border-radius:12px; background:var(--surface-2); border:1px solid var(--line); }
.reading-value strong { display:block; font-size:26px; line-height:1; letter-spacing:-.03em; }
.reading-value span { display:block; margin-top:5px; color:var(--muted); font-size:10px; font-weight:800; }
.reading-actions { display:flex; justify-content:flex-end; gap:7px; margin-top:10px; }
.reading-actions button, .reading-actions a { min-height:40px; padding:8px 12px; border-radius:11px; font-size:12px; font-weight:800; }

.table-wrap { overflow:auto; border:1px solid var(--line); border-radius:16px; }
table { width:100%; border-collapse:collapse; min-width:790px; background:var(--surface); }
th, td { padding:12px 13px; border-bottom:1px solid var(--line); text-align:left; font-size:13px; }
th { color:var(--muted); font-size:11px; letter-spacing:.02em; background:var(--surface-2); }
td.num { font-weight:900; font-size:16px; }
tr:last-child td { border-bottom:0; }

.toolbar { display:flex; flex-wrap:wrap; gap:8px; align-items:end; margin-bottom:14px; }
.toolbar .field { min-width:150px; flex:1; }
.range-tabs { display:flex; gap:6px; flex-wrap:wrap; }
.range-button { border:1px solid var(--line); border-radius:999px; min-height:38px; padding:7px 12px; cursor:pointer; background:var(--surface); color:var(--text); font-size:12px; font-weight:800; }
.range-button.active { background:var(--primary); color:var(--primary-contrast); border-color:var(--primary); }

.report-paper { background:var(--report-bg); color:var(--report-text); border:1px solid var(--report-line); border-radius:16px; padding:28px; box-shadow:var(--shadow); }
.report-header { display:flex; justify-content:space-between; gap:20px; border-bottom:2px solid var(--report-text); padding-bottom:14px; }
.report-header h2 { margin:0; font-size:22px; }
.report-meta { margin-top:4px; color:var(--report-muted); font-size:12px; }
.report-name { text-align:right; font-size:13px; }
.report-summary { display:grid; grid-template-columns:repeat(4,1fr); gap:9px; margin:16px 0; }
.report-summary > div { border:1px solid var(--report-line); border-radius:10px; padding:10px; background:color-mix(in srgb, var(--report-bg) 94%, var(--report-head)); }
.report-summary span { display:block; color:var(--report-muted); font-size:10px; }
.report-summary strong { display:block; margin-top:2px; font-size:18px; }
.report-table-scroll { width:100%; overflow-x:auto; overscroll-behavior-inline:contain; border:1px solid var(--report-line); border-radius:12px; }
.report-table { width:100%; min-width:720px; color:var(--report-text); background:var(--report-bg); }
.report-table th { background:var(--report-head); color:var(--report-muted); }
.report-table th, .report-table td { padding:10px 9px; font-size:12px; border-color:var(--report-line); white-space:nowrap; }
.report-table td.num { font-size:13px; }
.report-note { margin-top:14px; color:var(--report-muted); font-size:10px; line-height:1.6; }
.print-actions { display:flex; justify-content:flex-end; gap:8px; margin-bottom:12px; }

.settings-list { display:grid; gap:12px; }
.setting-row { display:flex; align-items:center; justify-content:space-between; gap:14px; padding:14px; border:1px solid var(--line); border-radius:15px; background:var(--surface); }
.setting-row h3 { margin:0; font-size:14px; }
.setting-row p { margin:3px 0 0; color:var(--muted); font-size:12px; }
.theme-segment { display:flex; gap:6px; background:var(--surface-2); padding:5px; border-radius:13px; }
.theme-option { border:0; background:transparent; color:var(--muted); border-radius:9px; min-height:38px; padding:7px 10px; cursor:pointer; font-weight:800; font-size:12px; }
.theme-option.active { background:var(--surface); color:var(--text); box-shadow:0 1px 4px rgba(0,0,0,.10); }

.empty-state { text-align:center; padding:34px 18px; color:var(--muted); }
.empty-state .emoji { font-size:34px; margin-bottom:6px; }
.hidden { display:none !important; }

.dialog-backdrop { position:fixed; inset:0; z-index:120; display:none; align-items:flex-end; justify-content:center; background:rgba(0,0,0,.42); padding:12px; }
.dialog-backdrop.open { display:flex; }
.calendar-modal { width:min(430px,100%); max-height:calc(100dvh - 24px); overflow:auto; background:var(--surface); border:1px solid var(--line); border-radius:22px; padding:15px; box-shadow:0 30px 80px rgba(0,0,0,.25); }
.calendar-head { display:grid; grid-template-columns:46px 1fr 46px; gap:7px; align-items:center; }
.calendar-head button { min-height:44px; border:1px solid var(--line); border-radius:12px; background:var(--surface-2); color:var(--text); cursor:pointer; }
.calendar-title { text-align:center; font-weight:900; }
.week-grid, .day-grid { display:grid; grid-template-columns:repeat(7,1fr); gap:4px; }
.week-grid { margin-top:12px; color:var(--muted); font-size:10px; font-weight:850; text-align:center; }
.day-grid { margin-top:5px; }
.day-grid button { min-height:42px; border:0; border-radius:10px; background:transparent; color:var(--text); cursor:pointer; }
.day-grid button:hover { background:var(--surface-2); }
.day-grid button.today { outline:1px solid var(--primary); color:var(--primary); font-weight:900; }
.day-grid button.selected { background:var(--primary); color:var(--primary-contrast); font-weight:900; }
.day-grid .blank { min-height:42px; }
.calendar-foot { display:flex; justify-content:space-between; gap:8px; margin-top:10px; }

.login-shell { min-height:100dvh; display:grid; place-items:center; padding:18px; }
.login-card { width:min(430px,100%); background:var(--surface); border:1px solid var(--line); border-radius:24px; box-shadow:var(--shadow); padding:24px; }
.login-top { display:flex; justify-content:flex-end; margin-bottom:4px; }
.login-logo { width:64px; height:64px; display:grid; place-items:center; margin:0 auto 12px; border-radius:20px; background:var(--surface-2); border:1px solid var(--line); font-size:30px; }
.login-card h1 { text-align:center; margin:0; font-size:25px; }
.login-card .lead { text-align:center; color:var(--muted); margin:7px 0 20px; font-size:13px; }
.login-error { margin-bottom:12px; padding:10px 12px; border-radius:12px; background:var(--danger-bg); color:var(--danger); font-size:12px; font-weight:700; }
.password-wrap { position:relative; }
.password-wrap input { padding-right:50px; }
.password-toggle { position:absolute; right:4px; top:4px; width:42px; height:42px; border:0; background:transparent; color:var(--muted); cursor:pointer; }
.login-card .primary-button { width:100%; margin-top:12px; min-height:52px; }
.remember-note { text-align:center; color:var(--muted); font-size:11px; margin-top:12px; }


/* ===== High-contrast theme polish ===== */
::selection { background: color-mix(in srgb, var(--primary) 35%, transparent); color: var(--text); }
::placeholder { color: var(--muted); opacity: .78; }
select option { background: var(--surface); color: var(--text); }

.theme-button:hover, .secondary-button:hover, .icon-button:hover, .ghost-button:hover {
    background: var(--surface-3);
    border-color: var(--line-strong);
}
.primary-button:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.danger-button:hover { border-color: var(--danger); }

button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
}

.desktop-nav a:hover { background: var(--surface-2); color: var(--text); }
.mobile-nav a.active { border: 1px solid color-mix(in srgb, var(--primary) 35%, transparent); }

.card, .summary-card, .reading-card, .setting-row, .login-card, .calendar-modal {
    color: var(--text);
}

.table-wrap { background: var(--surface); }
tbody tr:nth-child(even) { background: color-mix(in srgb, var(--surface-2) 55%, var(--surface)); }
tbody tr:hover { background: var(--surface-3); }

html[data-theme="dark"] body { background: #070b12; }
html[data-theme="dark"] .brand-icon,
html[data-theme="dark"] .theme-button,
html[data-theme="dark"] .secondary-button,
html[data-theme="dark"] .icon-button,
html[data-theme="dark"] .card,
html[data-theme="dark"] .summary-card,
html[data-theme="dark"] .reading-card,
html[data-theme="dark"] .setting-row,
html[data-theme="dark"] .login-card,
html[data-theme="dark"] .calendar-modal {
    border-color: var(--line-strong);
}
html[data-theme="dark"] .desktop-nav { border-color: var(--line-strong); }
html[data-theme="dark"] .input,
html[data-theme="dark"] input[type="number"],
html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="date"],
html[data-theme="dark"] select,
html[data-theme="dark"] textarea,
html[data-theme="dark"] .period-button,
html[data-theme="dark"] .reading-value {
    border-color: var(--line-strong);
}

.report-paper .empty-state { color: var(--report-muted); }
.report-table tbody tr:nth-child(even) { background: color-mix(in srgb, var(--report-head) 48%, var(--report-bg)); }
.report-table tbody tr:hover { background: var(--report-head); }

@media (max-width: 760px) {
    .app-shell { width:min(100% - 18px, 640px); padding-top:10px; }
    .desktop-nav { display:none; }
    .mobile-nav { display:block; }
    .topbar { margin-bottom:12px; }
    .brand small { display:none; }
    .brand-icon { width:38px; height:38px; border-radius:12px; }
    .theme-button .theme-label { display:none; }
    .theme-button { width:44px; padding:8px; justify-content:center; }
    .card-body { padding:14px; }
    .form-grid { grid-template-columns:1fr; }
    .measure-grid { gap:6px; }
    .measure-box input { min-height:67px; font-size:25px; }
    .measure-box label span { display:none; }
    .period-button { min-height:58px; font-size:12px; }
    .summary-grid { grid-template-columns:repeat(2,minmax(0,1fr)); }
    .table-desktop { display:none; }
    .toolbar { display:block; }
    .toolbar > * { margin-bottom:8px; }
    .report-paper { padding:16px; border-radius:14px; }
    .report-header { display:block; }
    .report-name { text-align:left; margin-top:8px; }
    .report-summary { grid-template-columns:repeat(2,1fr); }
    .report-table { min-width:680px; }
    .report-table th, .report-table td { padding:9px 8px; font-size:11px; }
    .report-table td.num { font-size:13px; }
    .setting-row { align-items:flex-start; flex-direction:column; }
    .theme-segment { width:100%; }
    .theme-option { flex:1; }
}

@media (min-width: 761px) {
    .cards-mobile { display:none; }
}

@media (max-width: 350px) {
    .measure-grid { grid-template-columns:1fr; }
    .measure-box label { justify-content:flex-start; }
    .measure-box input { text-align:left; padding-left:14px; }
}

@page { size: A4 portrait; margin: 12mm; }
@media print {
    html, body { background:#fff !important; color:#111827 !important; color-scheme:light !important; }
    .no-print, .topbar, .desktop-nav, .mobile-nav, .print-actions, .toolbar { display:none !important; }
    .app-shell { width:100%; margin:0; padding:0; }
    .report-paper { background:#fff !important; color:#111827 !important; border:0 !important; box-shadow:none !important; border-radius:0; padding:0; }
    .report-header { border-bottom-color:#111827 !important; }
    .report-meta, .report-note { color:#475467 !important; }
    .report-summary > div { background:#fff !important; border-color:#d0d5dd !important; }
    .report-summary span { color:#667085 !important; }
    .report-table-scroll { overflow:visible !important; border:0 !important; border-radius:0 !important; }
    .report-table { min-width:0 !important; background:#fff !important; color:#111827 !important; page-break-inside:auto; }
    .report-table th { background:#f2f4f7 !important; color:#475467 !important; }
    .report-table th, .report-table td { padding:8px 7px !important; font-size:10px !important; border-color:#e4e7ec !important; white-space:normal !important; }
    .report-table td.num { font-size:12px !important; }
    .report-table tbody tr, .report-table tbody tr:nth-child(even), .report-table tbody tr:hover { background:#fff !important; }
    .report-table tr { page-break-inside:avoid; page-break-after:auto; }
    .report-table thead { display:table-header-group; }
    .report-summary > div { break-inside:avoid; }
}
