:root {
  color-scheme: light;
  --bg: #f6f1e8;
  --surface: #fffdf8;
  --raised: #f3ebe0;
  --border: #e4d8c4;
  --border-strong: #c9b89a;
  --text: #1c1710;
  --text-2: #4a4033;
  --muted: #7a6d5b;
  --signal: #e3a008;
  --signal-ink: #1c0a00;
  --success: #0f7a4a;
  --success-soft: #e8f6ee;
  --danger: #c2410c;
  --danger-soft: #fdece6;
  --info: #1d6b8a;
  --info-soft: #e7f3f8;
  --shadow-hard: 3px 3px 0 #1c1710;
  --shadow-hover: 1px 1px 0 #1c1710;
  --radius: 10px;
  --font: "Iowan Old Style", "Palatino Linotype", Palatino, "Noto Serif TC", "Songti TC", serif;
  --mono: "Source Serif 4", "Iowan Old Style", Palatino, ui-monospace, monospace;
  --max: 1180px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #161310;
  --surface: #211c17;
  --raised: #2b241c;
  --border: #3d3428;
  --border-strong: #5a4c3a;
  --text: #f3ece2;
  --text-2: #d4c8b6;
  --muted: #a39480;
  --success: #34d399;
  --success-soft: #143024;
  --danger: #fb7185;
  --danger-soft: #3a1c1c;
  --info: #7dd3fc;
  --info-soft: #173044;
  --shadow-hard: 3px 3px 0 #f3ece2;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; }
body {
  font-family: var(--font);
  background:
    radial-gradient(900px 400px at 10% -10%, rgba(227, 160, 8, 0.12), transparent 60%),
    var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-bottom: 64px;
}

a { color: inherit; }

.top-nav {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.nav-inner {
  max-width: var(--max);
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-badge {
  background: var(--signal);
  color: var(--signal-ink);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 2px 8px;
  border: 1.5px solid var(--text);
  box-shadow: 2px 2px 0 var(--text);
}
.brand-title { font-size: 1.12rem; font-weight: 700; }
.brand-sub { font-size: 0.78rem; color: var(--muted); }

.nav-tabs, .nav-actions, .filter-tabs, .toolbar {
  display: flex; gap: 6px; flex-wrap: wrap; align-items: center;
}
.tab, .theme-btn, .ghost, .btn {
  border: 1.5px solid var(--text);
  background: var(--surface);
  color: var(--text);
  padding: 5px 10px;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 650;
  cursor: pointer;
  box-shadow: var(--shadow-hard);
}
.tab.active, .btn.primary {
  background: var(--signal);
  color: var(--signal-ink);
}
.tab:hover, .theme-btn:hover, .btn:hover, .ghost:hover {
  transform: translate(1px, 1px);
  box-shadow: var(--shadow-hover);
}
.theme-btn, .ghost { font-size: 0.8rem; }
.btn:disabled, .tab:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
  box-shadow: var(--shadow-hard);
}

main { max-width: var(--max); margin: 24px auto 0; padding: 0 20px; }
.view { display: none; }
.view.active { display: block; }

.hero {
  background: var(--surface);
  border: 2px solid var(--text);
  box-shadow: var(--shadow-hard);
  border-radius: 14px;
  padding: 18px 22px;
  margin-bottom: 18px;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.hero h2 { font-size: 1.15rem; margin-bottom: 4px; }
.hero p { color: var(--text-2); font-size: 0.9rem; max-width: 70ch; }

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.metric {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  position: relative;
  overflow: hidden;
}
.metric::before {
  content: "";
  position: absolute; inset: 0 0 auto;
  height: 3px;
  background: var(--signal);
}
.metric .k { font-size: 0.75rem; color: var(--muted); letter-spacing: 0.04em; }
.metric .v { font-family: var(--mono); font-size: 1.35rem; font-weight: 700; margin-top: 4px; }
.metric .d { font-size: 0.78rem; color: var(--text-2); margin-top: 2px; }
.metric.good::before { background: var(--success); }
.metric.bad::before { background: var(--danger); }
.metric.info::before { background: var(--info); }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px 8px;
  margin-bottom: 18px;
}
.panel h3 {
  font-size: 1rem;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

select, input[type="text"], input[type="number"] {
  font: inherit;
  font-size: 0.86rem;
  padding: 5px 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--raised);
  color: var(--text);
  max-width: 100%;
}
input[type="number"] { width: 108px; font-family: var(--mono); }
label.chk { font-size: 0.82rem; color: var(--text-2); display: inline-flex; gap: 6px; align-items: center; }

.table-wrap { overflow-x: auto; margin: 0 -6px 12px; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
  min-width: 720px;
}
.table-fit {
  overflow-x: visible;
  margin: 0 0 12px;
}
.table-fit table {
  width: 100%;
  min-width: 0;
  table-layout: fixed;
  font-size: 0.76rem;
}
.table-fit th,
.table-fit td {
  padding: 6px 7px;
  writing-mode: horizontal-tb;
  text-orientation: mixed;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
}
.table-fit th.period,
.table-fit td.period {
  white-space: nowrap;
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  min-width: 4.8em;
  font-weight: 650;
}
.table-fit thead th.period,
.table-fit tr.total td.period {
  background: var(--raised);
}
.table-fit tbody tr {
  cursor: pointer;
}
.table-fit tbody tr.pick td {
  background: var(--info-soft);
}
.col-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin: 4px 0 12px;
}
.col-picker .chk { font-size: 0.78rem; }
.explain {
  font-size: 0.82rem;
  background: var(--info-soft);
  border: 1px solid var(--color-info-border, var(--border));
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0 0 12px;
}
.explain h4 { font-size: 0.88rem; margin-bottom: 6px; }
.explain li { margin: 4px 0; }
th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th { color: var(--muted); font-weight: 650; background: var(--raised); }
td.num, th.num { text-align: right; font-family: var(--mono); white-space: nowrap; }
tr.total td { font-weight: 700; background: var(--raised); }
tr.zero { opacity: 0.45; }
.pos { color: var(--success); }
.neg { color: var(--danger); }
.badge {
  display: inline-flex;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.badge.yes { background: var(--success-soft); color: var(--success); }
.badge.no { background: var(--raised); color: var(--muted); }
.badge.co { background: var(--info-soft); color: var(--info); }
.badge.pe { background: #f6e7c8; color: #8a5a00; }
.badge.un { background: var(--raised); color: var(--muted); }
.badge.signal { background: var(--color-signal-soft, #fff3d6); color: #8a5a00; }
.freshness-bar {
  max-width: var(--max);
  margin: 0 auto;
  padding: 6px 20px 0;
  font-size: 0.78rem;
  color: var(--muted);
}
.freshness-bar strong { color: var(--text); }
.key-cell {
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card h4 { font-size: 1rem; margin-bottom: 6px; display: flex; justify-content: space-between; gap: 8px; }
.row2 { display: flex; justify-content: space-between; font-size: 0.82rem; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.row2:last-child { border-bottom: 0; }
.hint {
  font-size: 0.8rem;
  color: var(--text-2);
  background: var(--raised);
  padding: 8px 10px;
  border-radius: 8px;
  margin: 8px 0 14px;
}
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.toast {
  position: fixed; bottom: 16px; right: 16px;
  background: var(--text);
  color: var(--surface);
  padding: 10px 14px;
  font-size: 0.84rem;
  box-shadow: var(--shadow-hard);
  display: none;
  z-index: 40;
}
.toast.show { display: block; }
footer { text-align: center; color: var(--muted); font-size: 0.78rem; margin-top: 28px; }

@media (max-width: 800px) {
  .split { grid-template-columns: 1fr; }
  .brand-title { font-size: 1rem; }
  table { min-width: 640px; }
  .table-fit table { min-width: 0; font-size: 0.7rem; }
  .table-fit th,
  .table-fit td { padding: 5px 4px; }
}

/* private gate */
html[data-gated="1"] body > :not(#gate) { visibility: hidden; }
#gate {
  position: fixed; inset: 0; z-index: 9999;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg, #f6f1ea) 92%, #000 8%);
}
.gate-card {
  background: var(--surface, #fff);
  border: 1px solid var(--border, #ddd);
  border-radius: 12px;
  padding: 22px 24px;
  width: min(360px, 92vw);
  box-shadow: 0 10px 40px rgba(0,0,0,.08);
}
.gate-card h2 { font-size: 1.05rem; margin: 0 0 8px; }
.gate-card p { font-size: 0.85rem; color: var(--text-2, #666); margin: 0 0 12px; }
.gate-card form { display: flex; gap: 8px; }
.gate-card input {
  flex: 1; font: inherit; padding: 8px 10px;
  border: 1px solid var(--border-strong, #ccc); border-radius: 8px;
}
.gate-card button {
  font: inherit; padding: 8px 14px; border-radius: 8px;
  border: 0; background: #2c5f4a; color: #fff; cursor: pointer;
}
.gate-err { color: var(--danger, #b00020) !important; margin-top: 10px !important; }
