:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6d68;
  --axis-text: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.04);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --on-accent: #ffffff;
  --good-text: #006300;
  --bad-text: #b02e2e;
  --warn-bg: #fff6e0;
  --warn-ink: #6b4a00;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --other: #b5b3ab;
  --missing: rgba(11, 11, 11, 0.05);
  --seq-lo: #e4effc;
  --seq-hi: #0d366b;
  --radius: 10px;
  font-family: system-ui, -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #a5a39b;
    --axis-text: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.06); --accent: #3987e5; --accent-ink: #86b6ef; --on-accent: #ffffff;
    --good-text: #0ca30c; --bad-text: #e66767; --warn-bg: #33290f; --warn-ink: #f4d27e;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --other: #6b6a64; --missing: rgba(255, 255, 255, 0.05); --seq-lo: #1c2a3b; --seq-hi: #86b6ef;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #a5a39b;
  --axis-text: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.06); --accent: #3987e5; --accent-ink: #86b6ef; --on-accent: #ffffff;
  --good-text: #0ca30c; --bad-text: #e66767; --warn-bg: #33290f; --warn-ink: #f4d27e;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --other: #6b6a64; --missing: rgba(255, 255, 255, 0.05); --seq-lo: #1c2a3b; --seq-hi: #86b6ef;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--page); color: var(--ink); }
body { font-size: 14px; line-height: 1.55; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; min-height: 34px; white-space: nowrap;
}
.btn:hover { background: var(--wash); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--on-accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(0.94); }
.btn:disabled { opacity: 0.45; cursor: default; }

/* ---------- 載入畫面 ---------- */
.landing { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.landing-box { width: 100%; max-width: 560px; }
.landing h1 { font-size: 26px; font-weight: 700; margin-bottom: 6px; }
.landing .lead { color: var(--ink-2); margin-bottom: 20px; }
.drop {
  display: block; background: var(--surface); border: 1.5px dashed var(--axis); border-radius: var(--radius);
  padding: 36px 20px; text-align: center; cursor: pointer;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--wash); }
.drop:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.drop strong { display: block; font-size: 16px; margin-bottom: 4px; }
.drop span { color: var(--muted); }
.drop input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.remember { display: flex; gap: 8px; align-items: flex-start; margin: 14px 0 0; color: var(--ink-2); }
.remember input { margin-top: 4px; }
.privacy { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--grid); color: var(--muted); font-size: 13px; }
.load-msg { margin-top: 14px; }
.load-msg.error { color: var(--bad-text); font-weight: 600; }

/* ---------- 頁首與導覽 ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; font-weight: 700; }
.topbar .range { color: var(--muted); font-size: 13px; }
.topbar .spacer { flex: 1; }
.topbar .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tabs {
  display: flex; gap: 4px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 12px;
  color: var(--ink-2); white-space: nowrap;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-current="page"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- 篩選列 ---------- */
.filters {
  position: sticky; top: 0; z-index: 20; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
  padding: 10px 24px; background: var(--page); border-bottom: 1px solid var(--border);
}
.field { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.field select, .ms summary {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 8px; min-height: 34px; color: var(--ink);
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { background: none; border: 0; padding: 5px 10px; min-height: 32px; color: var(--ink-2); white-space: nowrap; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button:hover { background: var(--wash); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.ms { position: relative; }
.ms summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.ms summary::-webkit-details-marker { display: none; }
.ms summary::after { content: "▾"; color: var(--muted); }
.ms[data-active="true"] summary { border-color: var(--accent); font-weight: 600; }
.ms-panel {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 30; min-width: 200px; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
}
.ms-panel label { display: flex; gap: 8px; align-items: center; padding: 5px 6px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.ms-panel label:hover { background: var(--wash); }
.ms-clear { width: 100%; background: none; border: 0; border-bottom: 1px solid var(--grid); padding: 5px 6px 8px; margin-bottom: 4px; text-align: left; color: var(--accent-ink); }
.link-btn { background: none; border: 0; color: var(--accent-ink); padding: 5px 4px; }
.link-btn:hover { text-decoration: underline; }

/* ---------- 內容 ---------- */
main { padding: 20px 24px 40px; max-width: 1480px; margin: 0 auto; }
.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 16px; }
.span-12 { grid-column: span 12; }
.span-8 { grid-column: span 8; }
.span-7 { grid-column: span 7; }
.span-6 { grid-column: span 6; }
.span-5 { grid-column: span 5; }
.span-4 { grid-column: span 4; }
.span-3 { grid-column: span 3; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card-head { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 10px; }
.card-head > div { flex: 1; min-width: 0; }
.card h2 { font-size: 15px; font-weight: 600; }
.card .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.card .toggle { background: none; border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; font-size: 12px; color: var(--ink-2); white-space: nowrap; }
.card .toggle:hover { background: var(--wash); }
.chart { width: 100%; height: 320px; }
.chart.tall { height: 420px; }
.chart.short { height: 240px; }

.kpi { display: flex; flex-direction: column; gap: 2px; }
.kpi .label { color: var(--ink-2); font-size: 13px; }
.kpi .value { font-size: 28px; font-weight: 650; line-height: 1.25; }
.kpi .value small { font-size: 14px; font-weight: 500; color: var(--ink-2); margin-left: 3px; }
.kpi .delta { font-size: 13px; color: var(--muted); }
.delta .up { color: var(--good-text); font-weight: 600; }
.delta .down { color: var(--bad-text); font-weight: 600; }
.kpi svg { display: block; width: 120px; height: 34px; margin-top: 8px; }

.insights { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.insights li { padding-left: 14px; position: relative; }
.insights li::before { content: ""; position: absolute; left: 0; top: 0.62em; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.notice { background: var(--warn-bg); color: var(--warn-ink); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }
.foot-note { color: var(--muted); font-size: 12.5px; margin-top: 8px; }
.section-title { font-size: 15px; font-weight: 600; margin: 8px 0 -4px; grid-column: span 12; }

/* ---------- 表格 ---------- */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: top; }
th { color: var(--ink-2); font-weight: 600; font-size: 13px; white-space: nowrap; background: var(--surface); }
td.num, th.num { text-align: right; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
td.wrap { min-width: 240px; }
tbody tr:hover { background: var(--wash); }
tr.clickable { cursor: pointer; }
th button { background: none; border: 0; padding: 0; font-weight: 600; color: inherit; white-space: nowrap; }
th button:hover { color: var(--accent-ink); }
.row-link { background: none; border: 0; padding: 0; text-align: left; color: var(--accent-ink); }
.row-link:hover { text-decoration: underline; }
.tag { display: inline-block; padding: 0 6px; border-radius: 4px; font-size: 12px; background: var(--wash); color: var(--ink-2); white-space: nowrap; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; color: var(--muted); flex-wrap: wrap; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar input[type="search"] {
  flex: 1; min-width: 180px; max-width: 420px; background: var(--page); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; min-height: 34px;
}

/* 缺月方格 */
.cal th, .cal td { text-align: center; padding: 6px 4px; }
.cal td { font-size: 13px; min-width: 40px; }
.cal .has { color: var(--ink-2); }
.cal .miss { background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; }
.cal .out { color: var(--axis-text); }

details.issue { border-top: 1px solid var(--grid); padding: 10px 0; }
details.issue:first-of-type { border-top: 0; }
details.issue summary { cursor: pointer; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
details.issue summary b { font-weight: 600; }
details.issue .count { color: var(--ink-2); }
details.issue .hint { color: var(--muted); font-size: 12.5px; }
details.issue .table-wrap { margin-top: 8px; max-height: 360px; overflow: auto; }

dialog {
  width: min(960px, calc(100vw - 32px)); max-height: calc(100vh - 48px); border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); color: var(--ink); padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog-head { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.dialog-head h2 { flex: 1; font-size: 16px; font-weight: 600; }

footer.site { padding: 0 24px 32px; color: var(--muted); font-size: 12.5px; text-align: center; }

@media (max-width: 1100px) {
  .span-3 { grid-column: span 6; }
  .span-4, .span-5, .span-6, .span-7, .span-8 { grid-column: span 12; }
}
@media (max-width: 640px) {
  .topbar, .tabs, .filters { padding-left: 16px; padding-right: 16px; }
  main { padding: 14px 16px 32px; }
  footer.site { padding: 0 16px 24px; }
  .grid { gap: 12px; }
  .card { padding: 12px; }
  .kpi .value { font-size: 22px; }
  .filters { position: static; }
  .chart { height: 280px; }
  .chart.tall { height: 400px; }
  .ms-panel { position: fixed; left: 16px; right: 16px; top: auto; max-height: 50vh; }
}
@media print {
  .filters, .tabs, .actions, .toggle { display: none !important; }
  .card { break-inside: avoid; }
}
