:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dddcd7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #77756f;
  --grid: #e6e5e0;
  --accent: #2a78d6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0ca30c;
  --warning: #fab219;
  --info: #2a78d6;
  --critical: #d03b3b;
  --good-ink: #006300;
  --critical-ink: #b02a2a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #383835;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a998f;
    --grid: #2c2c2a;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --info: #3987e5;
    --good-ink: #4cc94c;
    --critical-ink: #ef7a7a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #383835;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #9a998f;
  --grid: #2c2c2a;
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-2: #d95926;
  --info: #3987e5;
  --good-ink: #4cc94c;
  --critical-ink: #ef7a7a;
  --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  font-variant-numeric: tabular-nums;
}
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 15px; }
h2.sub { margin-top: 16px; }

body::before {
  content: ""; position: fixed; inset: 0 0 auto; z-index: 11;
  height: env(safe-area-inset-top, 0px); background: var(--surface);
}
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding-block: 10px;
  padding-left: max(20px, env(safe-area-inset-left, 0px));
  padding-right: max(20px, env(safe-area-inset-right, 0px));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.actions { margin-left: auto; display: flex; gap: 8px; }
.save-state { color: var(--muted); font-size: 13px; }
.save-state.dirty { color: var(--text-2); }

.btn {
  font: inherit; font-size: 14px;
  padding: 6px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 2px 10px; font-size: 13px; }
.btn.link { border: none; background: none; color: var(--muted); padding: 2px 6px; }
.btn.link:hover { color: var(--critical-ink); background: none; }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  width: 40px; height: 22px; border-radius: 11px; background: var(--border);
  position: relative; transition: background .15s;
}
.switch .thumb {
  position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track .thumb { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-label { font-size: 14px; font-weight: 500; }

.error {
  margin: 12px 20px 0; padding: 10px 14px; border-radius: 8px;
  border: 1px solid var(--critical); color: var(--critical-ink); background: var(--surface);
}

.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 20px;
  padding-block: 20px;
  padding-left: max(20px, env(safe-area-inset-left, 0px));
  padding-right: max(20px, env(safe-area-inset-right, 0px));
  max-width: 1440px; margin: 0 auto;
}
.btn:disabled { opacity: .6; cursor: progress; }

/* password sign-in: shown instead of everything else until the API accepts the session */
body[data-auth="login"] .topbar,
body[data-auth="login"] .view-tabs,
body[data-auth="login"] .error,
body[data-auth="login"] main { display: none; }
.login {
  min-height: 100vh; display: grid; place-items: center;
  padding-block: 24px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.login-card {
  width: min(360px, 100%); display: flex; flex-direction: column; gap: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 24px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 20px; }
.login-hint { margin: 0 0 6px; color: var(--text-2); font-size: 14px; }
.login-field { font-size: 14px; color: var(--text-2); }
.login-card input {
  font: inherit; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-card .btn { margin-top: 6px; padding: 8px 14px; }
.login-error { color: var(--critical-ink); font-size: 14px; }

/* phone and tablet: one view at a time, switched by the tabs under the top bar */
.view-tabs { display: none; }
.results { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.results.stale { opacity: .55; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card h2 { margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; box-shadow: var(--shadow);
}
.kpi .label { color: var(--text-2); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 600; margin-top: 2px; }
.kpi .sub { color: var(--muted); font-size: 12px; }

.reminders { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.reminders li { display: flex; gap: 10px; align-items: flex-start; }
.badge {
  flex: none; display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px;
  border: 1px solid currentColor; color: var(--text-2); white-space: nowrap; margin-top: 1px;
}
.badge .dot { width: 8px; height: 8px; border-radius: 50%; }
.badge.alert .dot { background: var(--critical); }
.badge.warn .dot { background: var(--warning); }
.badge.info .dot { background: var(--info); }
.badge.ok .dot { background: var(--good); }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 5px 6px; border-bottom: 1px solid var(--grid); text-align: right; white-space: nowrap; }
.table th:first-child, .table td:first-child { text-align: left; }
.table thead th { color: var(--text-2); font-weight: 500; font-size: 13px; }
.table tr.total td { font-weight: 600; border-top: 1px solid var(--border); border-bottom: none; }
.table .cell-sub { color: var(--muted); font-size: 12px; }
.table td.note { color: var(--muted); font-size: 12px; text-align: left; white-space: normal; }
.table .neg { color: var(--critical-ink); }
.scroll { overflow-x: auto; }
.note { color: var(--muted); font-size: 13px; margin-top: 8px; }
.steps { margin: 0; padding-left: 20px; color: var(--text-2); font-size: 14px; }
.steps li { margin: 4px 0; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); }
.legend .legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend svg { display: block; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .label-strong { fill: var(--text-2); font-size: 12px; }
.tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font-size: 13px; box-shadow: 0 4px 12px rgba(0,0,0,.12); min-width: 160px;
}
.tooltip .row { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.tooltip .key { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }
.tooltip .swatch { width: 10px; height: 2px; }
.tooltip .title { font-weight: 600; margin-bottom: 4px; }

.editor {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px; align-self: start; position: sticky; top: 68px;
  max-height: calc(100vh - 88px); overflow-y: auto; box-shadow: var(--shadow);
}
.editor-head { margin-bottom: 8px; }
.editor-head .hint { display: block; color: var(--muted); font-size: 12px; }
details.section { border-top: 1px solid var(--grid); }
details.section > summary {
  cursor: pointer; padding: 10px 2px; font-weight: 600; list-style: none;
  display: flex; justify-content: space-between; align-items: center;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::after { content: "＋"; color: var(--muted); font-weight: 400; }
details.section[open] > summary::after { content: "－"; }
.section-body { padding: 0 2px 12px; display: flex; flex-direction: column; gap: 10px; }

.field { display: grid; grid-template-columns: 130px 1fr; align-items: center; gap: 8px; font-size: 14px; }
.field > span { color: var(--text-2); }
.field input, .field select {
  font: inherit; font-size: 14px; width: 100%; min-width: 0;
  padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field .unit { position: relative; }
.field .unit input { padding-right: 34px; }
.field .unit::after {
  content: attr(data-unit); position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-size: 12px; pointer-events: none;
}
.field.check { grid-template-columns: 130px auto; }
.field.check input { width: 18px; height: 18px; }

.item {
  border: 1px solid var(--grid); border-radius: 8px; padding: 10px;
  display: flex; flex-direction: column; gap: 8px; background: var(--bg);
}
.item-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.list-title { font-size: 13px; font-weight: 600; color: var(--text-2); margin-top: 4px; }
.split-sum { font-size: 12px; color: var(--muted); text-align: right; }
.split-sum.bad { color: var(--critical-ink); }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .editor { position: static; max-height: none; }
  .view-tabs {
    display: flex; gap: 4px; padding: 4px; margin: 12px auto 0;
    width: min(320px, calc(100% - 32px));
    background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  }
  .view-tabs button {
    flex: 1; font: inherit; font-size: 14px; font-weight: 500; padding: 6px 0;
    border: none; border-radius: 7px; background: transparent; color: var(--text-2); cursor: pointer;
  }
  .view-tabs button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
  .view-tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
  body[data-view="results"] .editor { display: none; }
  body[data-view="editor"] .results { display: none; }
}
@media (max-width: 720px) {
  .topbar {
    gap: 10px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  .layout {
    padding-block: 16px; gap: 16px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  .two-col { grid-template-columns: 1fr; }
  .error { margin: 12px 16px 0; }
  .field { grid-template-columns: 1fr; gap: 4px; }
  .field.check { grid-template-columns: auto 1fr; }
  .kpi .value { font-size: 20px; }
}
