/* ================= 色彩與主題 ================= */
:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #8a97a8;
  --line: #e3e8ee;
  --line-2: #eef2f6;
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.10);
  --series: #2a78d6;
  --series-2: #7c6cf0;
  --series-3: #8fbcf2;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.04);

  /* 狀態：良好 → 留意 → 警戒 → 不佳 */
  --good: #22c55e;  --good-text: #15803d;  --good-soft: rgba(34, 197, 94, 0.14);
  --fair: #facc15;  --fair-text: #a16207;  --fair-soft: rgba(250, 204, 21, 0.20);
  --warn: #f97316;  --warn-text: #c2410c;  --warn-soft: rgba(249, 115, 22, 0.14);
  --bad: #ef4444;   --bad-text: #b91c1c;   --bad-soft: rgba(239, 68, 68, 0.13);
  --info: #94a3b8;  --info-text: #475569;  --info-soft: rgba(148, 163, 184, 0.16);

  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0e13;
  --surface: #121821;
  --surface-2: #0f141b;
  --ink: #e6edf3;
  --ink-2: #a3b1bf;
  --ink-3: #6b7a8a;
  --line: #1f2934;
  --line-2: #18202a;
  --accent: #8b93ff;
  --accent-soft: rgba(139, 147, 255, 0.14);
  --series: #4b95ea;
  --series-2: #a196ff;
  --series-3: #2f6cb8;
  --shadow: none;

  --good-text: #4ade80;  --good-soft: rgba(34, 197, 94, 0.16);
  --fair-text: #fde047;  --fair-soft: rgba(250, 204, 21, 0.14);
  --warn-text: #fb923c;  --warn-soft: rgba(249, 115, 22, 0.16);
  --bad-text: #f87171;   --bad-soft: rgba(239, 68, 68, 0.16);
  --info-text: #a3b1bf;  --info-soft: rgba(148, 163, 184, 0.14);
}

.s-good { --s: var(--good); --s-text: var(--good-text); --s-soft: var(--good-soft); }
.s-fair { --s: var(--fair); --s-text: var(--fair-text); --s-soft: var(--fair-soft); }
.s-warn { --s: var(--warn); --s-text: var(--warn-text); --s-soft: var(--warn-soft); }
.s-bad  { --s: var(--bad);  --s-text: var(--bad-text);  --s-soft: var(--bad-soft); }
.s-info { --s: var(--info); --s-text: var(--info-text); --s-soft: var(--info-soft); }

/* ================= 基本 ================= */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.3; }
button, input, select, textarea { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-3); }
.hidden, [hidden] { display: none !important; }

/* ================= 登入 ================= */
.gate { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.gate-card { width: 100%; max-width: 420px; padding: 26px 24px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); }
.gate-card .brand { padding: 0 0 16px; }
.gate-card h1 { margin-bottom: 6px; font-size: 21px; }
.gate-msg { margin: 0 0 12px; color: var(--ink-2); font-size: 14px; }
.gate-err { margin: 0 0 12px; padding: 8px 12px; border-radius: 10px; background: var(--bad-soft); color: var(--bad-text); font-size: 14px; }
.gate-form { display: grid; gap: 12px; }
.sync { padding: 0 6px; color: var(--ink-3); font-size: 12px; }
.sync[data-state="busy"] { color: var(--accent); }
.sync[data-state="warn"] { color: var(--warn-text); font-weight: 600; }

/* ================= 版面 ================= */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  height: 100vh;
  padding: 22px 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 18px; font-size: 18px; font-weight: 750; letter-spacing: 0.01em; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 15px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--ink-2);
  font-weight: 550;
  text-decoration: none;
}
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { flex: none; width: 20px; height: 20px; }
.side-actions { display: flex; flex-direction: column; gap: 6px; margin-top: auto; }

.topbar { display: none; }
.main { width: 100%; max-width: 1180px; padding: 28px 32px 72px; }

.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 22px; }
.page-head h1 { font-size: 26px; font-weight: 750; }
.page-head .sub { margin-top: 4px; color: var(--ink-2); font-size: 14px; }

.section { margin-top: 30px; }
.section-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 6px 12px; margin-bottom: 12px; }
.section-head h2 { font-size: 18px; font-weight: 700; }
.section-head .sub { color: var(--ink-3); font-size: 13px; }

.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.gauto { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }

.card {
  min-width: 0;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card h3 { font-size: 15px; font-weight: 650; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 10px; }

/* ================= 狀態標籤 ================= */
.chip {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 4px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--s-soft);
  color: var(--s-text);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}
.chip i { font-style: normal; font-size: 11px; }
.dot { display: inline-block; flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--s); }
.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink-2);
  font-size: 12px;
  white-space: nowrap;
}

/* ================= 指標卡 ================= */
.metric { display: flex; flex-direction: column; gap: 10px; }
.metric-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.metric-name { color: var(--ink-2); font-size: 14px; font-weight: 600; }
.metric-value { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; }
.metric-value b { font-size: 28px; font-weight: 750; line-height: 1.1; letter-spacing: -0.01em; }
.metric-value .unit { color: var(--ink-3); font-size: 14px; }
.metric-meta { color: var(--ink-3); font-size: 12px; }
.metric-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.delta { color: var(--ink-2); font-size: 13px; font-weight: 600; }

.ai { display: flex; gap: 8px; color: var(--ink-2); font-size: 13px; line-height: 1.6; }
.ai::before {
  content: "AI";
  flex: none;
  height: 18px;
  margin-top: 1px;
  padding: 0 5px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 10px;
  font-weight: 800;
  line-height: 18px;
}

/* ================= 範圍條 ================= */
.rangebar { position: relative; padding-top: 14px; }
.rb-track { display: flex; height: 8px; }
.rb-track span { height: 100%; border-right: 2px solid var(--surface); background: color-mix(in srgb, var(--s) 62%, transparent); }
.rb-track span:last-child { border-right: 0; }
.rb-track span:first-child { border-radius: 4px 0 0 4px; }
.rb-track span:last-child { border-radius: 0 4px 4px 0; }
.rb-mark, .rb-prev { position: absolute; top: 3px; width: 0; height: 0; transform: translateX(-50%); }
.rb-mark { border-top: 9px solid var(--ink); border-right: 6px solid transparent; border-left: 6px solid transparent; }
.rb-prev { top: 5px; width: 9px; height: 9px; border: 2px solid var(--ink-3); border-radius: 50%; background: var(--surface); }
.rb-scale { display: flex; justify-content: space-between; margin-top: 4px; color: var(--ink-3); font-size: 11px; }

/* ================= 迷你趨勢 ================= */
.spark { display: block; flex: none; width: 140px; height: 40px; overflow: visible; }
.spark polyline { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.spark circle { fill: var(--s); stroke: var(--surface); stroke-width: 2; }

/* ================= 圖表 ================= */
.chart { position: relative; height: 230px; }
.chart.tall { height: 300px; }
.chart.short { height: 180px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; color: var(--ink-2); font-size: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

/* ================= 總覽 ================= */
.hero { padding: 22px; }
.hero h2 { font-size: 19px; font-weight: 700; line-height: 1.45; }
.hero p { margin: 10px 0 0; color: var(--ink-2); }
.pillar { display: flex; flex-direction: column; gap: 10px; border-top: 3px solid var(--s); }
.pillar h3 { font-size: 17px; }
.pillar p { margin: 0; color: var(--ink-2); font-size: 14px; }
.mini-list { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; }
.mini-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; }
.mini-list li span:first-child { color: var(--ink-2); }

.watch { display: flex; flex-direction: column; gap: 8px; padding: 14px 16px; border: 1px solid var(--line); border-left: 3px solid var(--s); border-radius: 12px; background: var(--surface); }

.reco h3 { margin-bottom: 8px; }
.reco ul { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: 14px; }
.reco li + li { margin-top: 6px; }

.plan { position: relative; display: grid; gap: 12px; }
.plan-step { display: grid; grid-template-columns: 150px minmax(0, 1fr); gap: 14px; }
.plan-step .when { color: var(--ink-3); font-size: 13px; font-weight: 600; }
.plan-step h3 { font-size: 15px; }
.plan-step p { margin: 2px 0 8px; color: var(--ink-2); font-size: 14px; }
.plan-step .tags { display: flex; flex-wrap: wrap; gap: 6px; }

.source-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; padding: 10px 0; border-bottom: 1px solid var(--line-2); font-size: 14px; }
.source-row:last-child { border-bottom: 0; }
.source-row .why { width: 100%; color: var(--ink-3); font-size: 12px; }

/* ================= 表格 ================= */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line-2); text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { color: var(--ink-3); font-size: 12px; font-weight: 600; }
tbody th { font-weight: 500; }
td { font-variant-numeric: tabular-nums; }
.rg { display: block; color: var(--ink-3); font-size: 11px; }
details > summary { color: var(--ink-2); font-size: 14px; font-weight: 600; cursor: pointer; }
details[open] > summary { margin-bottom: 12px; }

/* ================= 檢驗列 ================= */
.lab-row { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 8px 20px; padding: 14px 0; border-bottom: 1px solid var(--line-2); }
.lab-row:last-child { border-bottom: 0; }
.lab-row .ai { grid-column: 1 / -1; }
.lab-name { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-weight: 600; }
.lab-val { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; margin-top: 2px; }
.lab-val b { font-size: 20px; font-weight: 750; }
.lab-val .unit, .lab-ref { color: var(--ink-3); font-size: 12px; }
.text-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; }
.text-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); font-size: 13px; }

/* ================= 按鈕與表單 ================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { border-color: transparent; background: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad-text); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn svg { width: 18px; height: 18px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { color: var(--ink-2); font-size: 13px; font-weight: 600; }
input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 16px;
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }

.ex-card { padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); }
.ex-card + .ex-card { margin-top: 10px; }
.ex-head { display: grid; grid-template-columns: minmax(0, 1fr) 120px auto; gap: 8px; align-items: end; }
.set-list { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.set-row { display: grid; grid-template-columns: 34px minmax(0, 1fr) minmax(0, 1fr) 36px; gap: 6px; align-items: center; }
.set-row .idx { color: var(--ink-3); font-size: 13px; text-align: center; }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--ink-3);
  cursor: pointer;
}
.icon-btn:hover { background: var(--line-2); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

/* ================= 紀錄列表 ================= */
.log { display: flex; flex-direction: column; gap: 10px; }
.log-item { padding: 14px 16px; }
.log-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.log-head h3 { font-size: 15px; }
.log-meta { color: var(--ink-3); font-size: 13px; }
.log-body { margin-top: 8px; color: var(--ink-2); font-size: 13px; }
.log-body div + div { margin-top: 3px; }

.empty { padding: 36px 20px; color: var(--ink-2); text-align: center; }
.empty h3 { margin-bottom: 6px; color: var(--ink); font-size: 16px; }
.empty p { max-width: 520px; margin: 0 auto 14px; font-size: 14px; }

.fab {
  position: fixed;
  right: 20px;
  bottom: 88px;
  z-index: 30;
  display: none;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 20px;
  border: 0;
  border-radius: 26px;
  background: var(--accent);
  box-shadow: 0 6px 20px rgba(79, 70, 229, 0.35);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

/* ================= 對話框 ================= */
.sheet-backdrop { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; background: rgba(2, 6, 12, 0.5); }
.sheet {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 680px;
  max-height: calc(100vh - 40px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 17px; }
.sheet-body { padding: 18px; overflow-y: auto; }
.sheet-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); background: var(--surface-2); }
.preview { width: 100%; min-height: 320px; font: 12px/1.55 ui-monospace, Consolas, monospace; }
.toast { position: fixed; left: 50%; bottom: 100px; z-index: 60; padding: 10px 16px; border-radius: 10px; background: var(--ink); color: var(--bg); font-size: 14px; transform: translateX(-50%); }

.disclaimer { margin-top: 30px; color: var(--ink-3); font-size: 12px; }

/* ================= 響應式 ================= */
@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
  }
  .topbar .brand { padding: 0; font-size: 17px; }
  .topbar .btn-row { gap: 2px; }
  .sidebar {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    height: auto;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    border-right: 0;
  }
  .sidebar .brand, .side-actions { display: none; }
  .nav { flex-direction: row; justify-content: space-around; }
  .nav a { flex: 1; flex-direction: column; gap: 3px; padding: 6px 2px; font-size: 11px; text-align: center; }
  .main { padding: 18px 16px 110px; }
  .page-head h1 { font-size: 22px; }
  .g2, .g3 { grid-template-columns: minmax(0, 1fr); }
  .fab { display: inline-flex; }
  .desktop-only { display: none !important; }
  .plan-step { grid-template-columns: minmax(0, 1fr); gap: 2px; }
  .lab-row { grid-template-columns: minmax(0, 1fr); }
  .sheet-backdrop { align-items: stretch; padding: 0; }
  .sheet { max-width: none; max-height: none; height: 100%; border: 0; border-radius: 0; }
}

@media (max-width: 520px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .ex-head { grid-template-columns: minmax(0, 1fr) auto; }
  .ex-head .muscle { grid-column: 1 / -1; grid-row: 2; }
  .metric-value b { font-size: 24px; }
}
