:root {
  --bg: #0a1018;
  --bg-glow: #0f2030;
  --surface: #101a26;
  --surface-2: #15212f;
  --line: rgba(150, 180, 220, 0.12);
  --line-strong: rgba(150, 180, 220, 0.22);
  --text: #e8eef7;
  --muted: #8d9db3;
  --primary: #3ccfc6;
  --primary-soft: rgba(60, 207, 198, 0.16);
  --secondary: #5b8def;
  --accent: #ffb547;
  --up: #4fd69c;
  --down: #ff7a6b;
  --focus: #ffd27a;

  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 40px;
  --radius: 14px;
  --shadow-sm: 0 1px 0 rgba(255, 255, 255, 0.04) inset, 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 1px 0 rgba(255, 255, 255, 0.05) inset, 0 10px 30px rgba(0, 0, 0, 0.35);
  --ease: cubic-bezier(0.22, 0.8, 0.24, 1);
  --font: 'BA Sans', 'PingFang TC', 'Microsoft JhengHei', sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(60, 207, 198, 0.10), transparent 60%),
    radial-gradient(900px 500px at -10% 10%, rgba(91, 141, 239, 0.10), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  line-height: 1.55;
  font-feature-settings: 'tnum' 1;
}
h1, h2, p { margin: 0; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }

.skip {
  position: absolute; left: -999px; top: 8px; z-index: 50;
  background: var(--accent); color: #1b1305; padding: 8px 14px; border-radius: 8px; font-weight: 700;
}
.skip:focus { left: 8px; }

.shell { display: grid; grid-template-columns: minmax(0, 1fr); min-height: 100vh; }

/* ---- 篩選欄 ---- */
.rail {
  position: sticky; top: 0; z-index: 20;
  display: flex; flex-wrap: wrap; gap: 6px var(--space-md); align-items: center;
  padding: var(--space-sm) var(--space-md);
  padding-top: max(var(--space-sm), env(safe-area-inset-top));
  background: rgba(10, 16, 24, 0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.rail { min-width: 0; }
.rail > .filter:first-of-type { flex: 1 1 100%; min-width: 0; }
.age-grid { overflow-x: auto; scrollbar-width: none; padding-bottom: 1px; }
.age-grid::-webkit-scrollbar { display: none; }
.long { display: none; }
@media (min-width: 640px) { .long { display: inline; } .short { display: none; } }
@media (max-width: 639px) { .rail { gap: 6px 8px; } .rail > .filter:nth-of-type(2) .filter-title { display: none; } .seg-btn { padding: 0 9px; } .btn { padding: 0 11px; } }
.brand { display: none; }
.filter { display: flex; align-items: center; gap: var(--space-sm); flex: none; }
.filter-title { font-size: 12px; font-weight: 700; color: var(--muted); white-space: nowrap; letter-spacing: 0.04em; }
.filter-title .hint { display: none; }
.age-grid { display: flex; gap: 6px; min-width: 0; }
.age-btn {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  flex: none; min-width: 44px; height: 40px; padding: 2px 8px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px;
  cursor: pointer; transition: background 0.18s var(--ease), border-color 0.18s var(--ease), transform 0.18s var(--ease);
}
.age-btn:hover { border-color: var(--line-strong); background: #1a2838; }
.age-btn:active { transform: translateY(1px); }
.age-btn[aria-pressed='true'] { background: var(--primary-soft); border-color: var(--primary); }
.age-btn[aria-pressed='true'] .age-key { color: var(--primary); }
.age-key { font-size: 15px; font-weight: 700; line-height: 1.1; }
.age-count { font-size: 10px; color: var(--muted); line-height: 1.2; }

.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg-btn {
  border: 0; background: transparent; padding: 0 12px; height: 36px; border-radius: 8px; cursor: pointer;
  font-size: 14px; color: var(--muted); white-space: nowrap; transition: background 0.18s var(--ease), color 0.18s var(--ease);
}
.seg-btn:hover { color: var(--text); }
.seg-btn[aria-checked='true'] { background: var(--primary); color: #04201e; font-weight: 700; }

.rail-actions { display: flex; gap: var(--space-sm); align-items: center; flex: none; position: relative; margin-left: auto; }
.btn {
  height: 38px; padding: 0 14px; border-radius: 10px; cursor: pointer; white-space: nowrap; font-size: 14px; font-weight: 700;
  background: var(--accent); color: #241703; border: 1px solid transparent;
  transition: filter 0.18s var(--ease), transform 0.18s var(--ease), opacity 0.18s var(--ease);
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line-strong); font-weight: 400; }
.btn:disabled { opacity: 0.38; cursor: not-allowed; filter: none; transform: none; }
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 12px);
  background: var(--surface-2); border: 1px solid var(--line-strong); color: var(--text);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow-md);
  opacity: 0; pointer-events: none; transition: opacity 0.2s var(--ease), transform 0.2s var(--ease); z-index: 60; width: max-content; max-width: 90vw;
}
.toast[data-show='1'] { opacity: 1; transform: translate(-50%, 0); }
.rail-note { display: none; }

/* ---- 主區 ---- */
.main { padding: var(--space-lg) var(--space-md) var(--space-xl); min-width: 0; }
.top { display: grid; gap: var(--space-sm); margin-bottom: var(--space-lg); }
h1 { font-size: clamp(24px, 4.6vw, 40px); line-height: 1.15; font-weight: 700; letter-spacing: 0.01em; }
.lede { color: var(--muted); font-size: clamp(14px, 1.6vw, 16px); max-width: 62em; }
.scope { font-size: 13px; color: var(--primary); }

.hero { display: grid; gap: var(--space-md); margin-bottom: var(--space-md); }
.panel {
  position: relative;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 40%), var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: var(--space-md);
  min-width: 0;
  transition: border-color 0.25s var(--ease), box-shadow 0.25s var(--ease);
}
.panel:hover { border-color: var(--line-strong); box-shadow: var(--shadow-md); }
.panel::before {
  content: ''; position: absolute; left: 18px; right: 18px; top: -1px; height: 1px; pointer-events: none;
  background: linear-gradient(90deg, transparent, rgba(60, 207, 198, 0.0) 10%, rgba(60, 207, 198, 0.45) 50%, rgba(60, 207, 198, 0.0) 90%, transparent);
  opacity: 0; transition: opacity 0.3s var(--ease);
}
.panel:hover::before { opacity: 1; }

.gauge-panel { display: grid; align-content: center; background: radial-gradient(420px 260px at 50% 70%, rgba(255, 181, 71, 0.10), transparent 70%), var(--surface); }
.chart { width: 100%; height: 280px; }
.chart.tall { height: 320px; }
.chart.gauge { height: 250px; }
.gauge-foot { text-align: center; font-size: 13px; color: var(--muted); margin-top: -6px; }

.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-sm); }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 14px 12px;
  display: grid; gap: 2px; align-content: start;
}
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi-value { font-size: clamp(22px, 3vw, 30px); font-weight: 700; line-height: 1.2; letter-spacing: 0.01em; }
.kpi-unit { font-size: 13px; color: var(--muted); font-weight: 400; margin-left: 4px; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.tone-up { color: var(--up); }
.tone-down { color: var(--down); }

.grid { display: grid; grid-template-columns: 1fr; gap: var(--space-md); }
.ph { display: grid; gap: 2px; margin-bottom: var(--space-sm); }
.ph h2 { font-size: 16px; font-weight: 700; }
.pi { font-size: 13px; color: var(--muted); min-height: 1.5em; }

.notes { margin-top: var(--space-xl); padding-top: var(--space-lg); border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; max-width: 70em; }
.notes h2 { font-size: 14px; color: var(--text); margin-bottom: var(--space-sm); }
.notes ul { margin: 0; padding-left: 1.2em; display: grid; gap: 4px; }

.load-error {
  position: fixed; inset: auto 16px 16px 16px; z-index: 70; padding: 14px 16px; border-radius: 12px;
  background: #3a1513; border: 1px solid #7a2a24; color: #ffd9d4; font-size: 14px;
}

/* 載入前的骨架 */
html:not([data-ready]) .chart { background: linear-gradient(90deg, var(--surface-2) 0%, #1a2a3b 50%, var(--surface-2) 100%); background-size: 200% 100%; border-radius: 10px; animation: shimmer 1.4s var(--ease) infinite; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* 入場 */
html[data-ready='1'] .panel, html[data-ready='1'] .kpi { animation: rise 0.6s var(--ease) both; }
.grid .panel:nth-child(2) { animation-delay: 60ms; }
.grid .panel:nth-child(3) { animation-delay: 120ms; }
.grid .panel:nth-child(4) { animation-delay: 180ms; }
.grid .panel:nth-child(n + 5) { animation-delay: 240ms; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

@media (min-width: 640px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (min-width: 900px) {
  .main { padding: var(--space-xl) var(--space-lg); }
  .hero { grid-template-columns: minmax(280px, 4fr) 8fr; }
  .grid { grid-template-columns: repeat(12, minmax(0, 1fr)); }
  .span-4 { grid-column: span 4; }
  .span-6 { grid-column: span 6; }
  .span-8 { grid-column: span 8; }
}
@media (min-width: 900px) and (max-width: 1199px) {
  .span-4 { grid-column: span 12; }
  .span-8 { grid-column: span 12; }
}
@media (min-width: 1200px) {
  .shell { grid-template-columns: 264px minmax(0, 1fr); }
  .rail > .filter:first-of-type { flex: none; }
  .rail {
    flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: var(--space-lg);
    height: 100vh; padding: var(--space-lg) var(--space-lg);
    border-bottom: 0; border-right: 1px solid var(--line); overflow: auto; background: rgba(10, 16, 24, 0.7);
  }
  .brand { display: flex; align-items: center; gap: 12px; }
  .brand-mark {
    width: 34px; height: 34px; border-radius: 10px; flex: none;
    background: conic-gradient(from 210deg, var(--primary), var(--secondary), var(--accent), var(--primary));
    box-shadow: 0 0 24px rgba(60, 207, 198, 0.35);
    -webkit-mask: radial-gradient(circle at 50% 50%, transparent 7px, #000 8px);
    mask: radial-gradient(circle at 50% 50%, transparent 7px, #000 8px);
  }
  .brand-name { font-weight: 700; font-size: 16px; }
  .brand-sub { font-size: 12px; color: var(--muted); }
  .filter { flex-direction: column; align-items: stretch; gap: 10px; }
  .filter-title { font-size: 13px; }
  .filter-title .hint { display: inline; font-weight: 400; color: var(--muted); opacity: 0.8; margin-left: 6px; }
  .age-grid { display: grid; grid-template-columns: repeat(4, 1fr); }
  .age-btn { min-width: 0; height: 52px; }
  .seg { display: grid; grid-template-columns: repeat(2, 1fr); gap: 3px; }
  .seg-btn { padding: 0 6px; }
  .rail-actions { flex-direction: column; align-items: stretch; margin-left: 0; }
  .rail-note { display: block; font-size: 12px; color: var(--muted); line-height: 1.6; }
  .hero { grid-template-columns: minmax(300px, 4fr) 8fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
