:root {
  --bg: #efe7db;
  --bg-2: #f7f3ec;
  --ink: #1c1915;
  --muted: #6f675e;
  --line: #e3d9cb;
  --card: #fffdf9;
  --pine: #16352f;
  --pine-2: #21493f;
  --gold: #b8883d;
  --up: #0c7a43;
  --down: #c81e3a;
  --up-bg: #e7f6ee;
  --down-bg: #fdecef;
  --shadow: 0 12px 40px rgba(48, 32, 14, 0.08);
  --safe-t: env(safe-area-inset-top);
  --safe-b: env(safe-area-inset-bottom);
  --nav-h: 62px;
  --font: "Noto Sans TC", "PingFang HK", "Heiti TC", sans-serif;
  --display: "Fraunces", "Noto Serif TC", serif;
  --num: "IBM Plex Sans", "Noto Sans TC", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; max-width: 100%; overflow-x: clip; }
body {
  min-height: 100dvh;
  background:
    radial-gradient(1200px 400px at 10% -10%, rgba(184, 136, 61, 0.16), transparent 50%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
}
body.modal-open { overflow: hidden; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: calc(8px + var(--safe-t)) 16px 8px;
  background: var(--pine);
  color: #f6f1e8;
}
.brand {
  font-family: var(--display);
  font-size: 28px;
  font-weight: 560;
  letter-spacing: -0.03em;
  text-decoration: none;
  line-height: 1;
}
.top-tabs { display: flex; gap: 4px; }
.tab, .nav-btn, .chip, .btn, .icon-btn, .tv-btn {
  min-height: 44px;
}
.tab, .nav-btn {
  border: 0;
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 0 12px;
}
.tab.active, .nav-btn.active { background: rgba(255, 255, 255, 0.12); }
.auth-slot { margin-left: auto; display: flex; gap: 8px; align-items: center; min-width: 0; }
.who {
  max-width: 34vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: #eadfce;
}
.bottom-nav { display: none; }

main {
  width: min(1120px, calc(100% - 28px));
  margin: 0 auto;
  padding: 18px 0 40px;
}
.page-head { margin-bottom: 14px; }
.page-head-row, .actions { display: flex; gap: 8px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.eyebrow {
  margin: 0 0 2px;
  color: var(--gold);
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
h1, h2, h3 { font-family: var(--display); font-weight: 560; letter-spacing: -0.03em; margin: 0; }
h1 { font-size: 34px; line-height: 1.15; }
h2 { font-size: 24px; }
h3 { font-size: 18px; margin-top: 8px; }
.lede, .meta, .metric-sub, .brief { color: var(--muted); }
.lede { margin: 6px 0 0; max-width: 62ch; }

.btn, .icon-btn, .tv-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 0 16px;
  text-decoration: none;
  white-space: nowrap;
}
.btn-primary { background: var(--pine); color: #f7f3ec; }
.btn-ghost { background: transparent; border-color: currentColor; color: inherit; }
.topbar .btn-ghost { color: #f6f1e8; border-color: rgba(255, 255, 255, 0.35); }
.topbar .btn-primary { background: #f3eadc; color: var(--pine); }
.btn-danger { background: transparent; color: var(--down); border-color: #f0b7c1; }
.icon-btn { width: 44px; padding: 0; background: transparent; border: 0; font-size: 28px; }
.tv-btn {
  width: 64px;
  min-width: 64px;
  background: #fff;
  border: 1px solid var(--line);
  color: var(--pine);
  flex-direction: column;
  gap: 0;
  padding: 4px;
}
.tv-btn svg { width: 18px; height: 18px; }
.tv-btn span { font-size: 11px; font-weight: 700; letter-spacing: 0.04em; }

.board, .index-grid, .stat-grid, .form-grid {
  display: grid;
  gap: 10px;
}
.board, .stat-grid { grid-template-columns: 1fr 1fr; }
.board .metric:first-child, .gate, .panel, .empty, .error-box, .notice {
  grid-column: 1 / -1;
}
.metric, .stat, .index-card, .panel, .list, .gate, .empty, .error-box {
  background: var(--card);
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.metric, .stat, .index-card { padding: 12px 14px; min-width: 0; }
.metric-label { margin: 0; font-size: 13px; color: var(--muted); }
.metric-value, .pl, .index-value {
  margin: 2px 0;
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  font-weight: 650;
  letter-spacing: -0.03em;
}
.metric-value { font-size: 22px; overflow-wrap: anywhere; }
.index-value { font-size: 28px; }
.metric-sub, .pl-sub { margin: 0; font-size: 12px; font-family: var(--num); }
.notice, .empty, .error-box, .gate { padding: 14px 16px; margin: 10px 0; }
.notice { background: #fff8ec; border: 1px solid #efd7b0; border-radius: 14px; }
.empty { color: var(--muted); }
.error-box { border: 1px solid #f3c2cb; }
.gate { padding: 28px 20px; }
.gate h1 { margin-bottom: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 999px;
  padding: 0 14px;
}
.chip.active { background: var(--pine); color: #f6f1e8; border-color: var(--pine); }

.list { overflow: hidden; }
.row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--card);
}
.row:last-child { border-bottom: 0; }
.row-hit {
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 0;
  min-width: 0;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
}
.row-line { display: flex; gap: 8px; align-items: baseline; min-width: 0; }
.row-line.sub { color: var(--muted); font-size: 13px; justify-content: space-between; }
.sym { font-weight: 700; }
.name { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.badge {
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 12px;
  border: 1px solid var(--line);
}
.badge.long { color: var(--pine); background: #e7f3ef; }
.badge.short { color: #8a3b12; background: #f8ecdf; }
.pl-col { text-align: right; align-self: center; }
.pl { font-size: 16px; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.row-actions, .expand { grid-column: 1 / -1; }
.row-actions { display: flex; gap: 8px; }
.row-actions .btn { flex: 1; }
.expand { padding-bottom: 6px; }
.kv { display: grid; grid-template-columns: 7.5em 1fr; gap: 4px 8px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.thesis { margin: 8px 0 0; overflow-wrap: anywhere; }
.filters { display: grid; gap: 8px; margin-bottom: 10px; }
.filters input, .filters select, .field input, .field select, .choice label {
  width: 100%;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: white;
  padding: 0 12px;
  font-size: 16px;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field span { font-size: 13px; color: var(--muted); }
.field small { color: var(--muted); }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice label { display: flex; align-items: center; gap: 8px; }
.history { margin-top: 12px; }
.history summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; }
.stat-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  min-height: 44px;
  align-items: center;
}
.stack { display: flex; flex-direction: column; gap: 12px; }
.loading { color: var(--muted); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(22, 18, 14, 0.48);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal {
  width: min(560px, 100%);
  max-height: min(92dvh, 100%);
  overflow: auto;
  background: var(--bg-2);
  border-radius: 20px 20px 0 0;
  padding: 16px 16px calc(16px + var(--safe-b));
}
.modal-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.modal-form { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.form-error { color: var(--down); margin: 0; }
.google-btn-host { min-height: 44px; width: 100%; }
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-b) + 12px);
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: white;
  border-radius: 999px;
  padding: 10px 16px;
  max-width: min(92vw, 420px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 759px) {
  .top-tabs { display: none; }
  .bottom-nav {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: #fffdf9;
    border-top: 1px solid var(--line);
    padding-bottom: var(--safe-b);
    z-index: 20;
  }
  .nav-btn { color: var(--ink); border-radius: 0; }
  .nav-btn.active { background: #f3eee6; color: var(--pine); }
  body { padding-bottom: calc(var(--nav-h) + var(--safe-b)); }
  h1 { font-size: 30px; }
  .row-actions .btn { min-height: 44px; }
}

@media (min-width: 760px) {
  .modal-backdrop { align-items: center; padding: 24px; }
  .modal { border-radius: 20px; }
  .filters { grid-template-columns: 1.4fr 0.8fr 0.8fr; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-grid .field:has(input[name="notes"]),
  .form-grid .choice,
  .form-grid > p { grid-column: 1 / -1; }
  .index-grid { grid-template-columns: 1fr 1fr; }
  .row-actions .btn { flex: 0 0 auto; min-width: 112px; }
  #toast { bottom: 24px; }
}

@media (min-width: 1080px) {
  .board { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .board .metric:first-child { grid-column: auto; }
  .index-grid { grid-template-columns: 1fr 1fr 1fr; }
  .metric-value { font-size: 20px; }
}
