/* 健保同儕資料儀表板 —— Linear／Attio 風格：近乎單色、細框線（不靠陰影）、緊湊字級、只在小地方用顏色。
 * 設計變數（顏色、圓角、間距、字級、框線）全部在 :root。 */

/* 圖示字型：自架子集（python -m tools.build_icon_font 會更新檔名） */
@font-face {
  font-family: "Material Symbols Rounded"; font-style: normal; font-weight: 400; font-display: block;
  src: url("fonts/material-symbols-rounded.647394ff67.woff2") format("woff2");
}

:root {
  color-scheme: light;
  /* 顏色 */
  --app: #f7f7f8;          /* 側欄、頁面底色 */
  --panel: #ffffff;        /* 卡片、主面板 */
  --line: #e9e9eb;         /* 1px 細框線 */
  --line-2: #f1f1f2;       /* 表格分隔線 */
  --line-strong: #dcdcdf;
  --hover: #f4f4f5;
  --row-hover: #fafafa;
  --active: #ececee;       /* 側欄選中 */
  --track: #f1f1f2;        /* 分段控制的軌道 */
  --fg: #111111;
  --fg-2: #4b4b50;
  --muted: #6b6b6b;
  --faint: #a1a1a6;
  --accent: #2563eb;
  --accent-ring: rgba(37, 99, 235, .16);
  --up: #15803d;
  --down: #c2410c;
  /* 圓角、陰影 */
  --r-sm: 6px;
  --r: 8px;
  --radius: 10px;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, .04);
  --shadow-pop: 0 8px 24px rgba(0, 0, 0, .08), 0 0 0 1px rgba(0, 0, 0, .02);
  /* 字級（基準 13px） */
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs: 13px;
  --fs-md: 14px;
  --fs-kpi: 22px;
  /* 間距（4px 格） */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 20px;
  --ctl-h: 30px;           /* 按鈕、選單高度 */
  --font: "Inter", "Noto Sans TC", system-ui, -apple-system, "Segoe UI", sans-serif;
  --sidebar-w: 236px;
  --ai-w: 400px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--app); color: var(--fg); font-family: var(--font); font-size: var(--fs); line-height: 1.45; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
[hidden] { display: none !important; }
h1, h2, p { margin: 0; }
button { font: inherit; color: inherit; }
* { scrollbar-width: thin; scrollbar-color: #d6d6da transparent; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #d6d6da; border-radius: 99px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background-color: #bdbdc2; }
::-webkit-scrollbar-track { background: transparent; }

.ms {
  font-family: "Material Symbols Rounded"; font-weight: normal; font-style: normal; font-size: 16px; line-height: 1;
  letter-spacing: normal; text-transform: none; white-space: nowrap; display: inline-block; direction: ltr;
  font-feature-settings: "liga"; font-variation-settings: "FILL" 0, "wght" 400, "GRAD" 0, "opsz" 20; user-select: none; flex: none;
  width: 1em; overflow: hidden;
}
/* 字型還沒載入：不顯示圖示的英文字（例如 calendar_today） */
html:not(.icons-ready) .ms { color: transparent !important; }
kbd {
  font-family: var(--font); font-size: 10.5px; color: var(--muted); background: var(--panel);
  border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; line-height: 16px;
}
.grow { flex: 1; }
.muted { color: var(--muted); }
.sig { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: currentColor; vertical-align: 1px; }

/* ============================================================ shell */
.shell { display: flex; height: 100vh; }
.sidebar {
  width: var(--sidebar-w); flex: none; display: flex; flex-direction: column; gap: 6px;
  padding: 10px 6px 8px 8px; overflow-y: auto; overflow-x: hidden;
}
.panel {
  flex: 1; min-width: 0; margin: 8px 8px 8px 0; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; display: flex; flex-direction: column; overflow: hidden;
  box-shadow: var(--shadow-card);
}
main { flex: 1; overflow: auto; padding: 16px 20px 40px; container: main / inline-size; }
main:has(.flush-view:not([hidden])) { padding: 0; display: flex; flex-direction: column; }

/* ---------- sidebar ---------- */
.ws { display: flex; align-items: center; gap: 6px; height: 32px; padding: 0 0 0 8px; }
.ws-name { font-weight: 600; font-size: var(--fs-md); flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-mark { display: none; font-weight: 700; font-size: 12px; color: var(--fg-2); letter-spacing: .02em; }
.sb-toggle .ms { color: var(--muted); }

.quick { display: flex; gap: 4px; padding: 2px 0 6px; }
.quick-btn {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; height: var(--ctl-h); padding: 0 6px 0 8px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); color: var(--fg-2); font-weight: 500;
}
.quick-btn span:nth-child(2) { flex: 1; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quick-btn:hover { border-color: var(--line-strong); }
.icon-sm {
  width: 28px; height: 28px; display: grid; place-items: center; border-radius: var(--r-sm); cursor: pointer; flex: none;
  background: transparent; border: 1px solid transparent; color: var(--fg-2); padding: 0;
}
.icon-sm:hover { background: var(--hover); }
.quick .icon-sm { background: var(--panel); border-color: var(--line); width: var(--ctl-h); height: var(--ctl-h); }

.nav { display: flex; flex-direction: column; gap: 1px; }
.nav-item {
  display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 8px; border-radius: var(--r);
  color: var(--fg-2); font-weight: 500; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.nav-item .ms { color: var(--muted); }
.nav-item:hover { background: var(--hover); color: var(--fg); text-decoration: none; }
.nav-item.active { background: var(--active); color: var(--fg); }
.nav-item.active .ms { color: var(--fg); font-variation-settings: "FILL" 1, "wght" 400, "GRAD" 0, "opsz" 20; }
.nav-item.sub { padding-left: 14px; font-weight: 400; }
.nav-item .label, .nav-label { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.nav-item .count { margin-left: auto; color: var(--muted); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; background: var(--hover); border-radius: 5px; padding: 0 5px; line-height: 17px; }
.nav-item .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; margin: 0 4px; }
.nav-sec { display: flex; align-items: center; gap: 4px; padding: 14px 8px 4px; font-size: var(--fs-sm); color: var(--muted); font-weight: 500; white-space: nowrap; }
.nav-sec .ms { font-size: 15px; color: var(--faint); }
.nav-empty { padding: 4px 8px 4px 14px; color: var(--faint); font-size: var(--fs-sm); }

.sb-foot {
  margin-top: auto; padding: 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); font-size: var(--fs-sm); color: var(--fg-2);
}
.sb-foot .row { display: flex; align-items: center; gap: 7px; font-weight: 600; color: var(--fg); white-space: nowrap; }
.sb-foot .sub { color: var(--muted); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pulse { position: relative; width: 7px; height: 7px; border-radius: 50%; background: #22c55e; flex: none; }
.pulse::after { content: ""; position: absolute; inset: -3px; border-radius: 50%; background: inherit; opacity: .3; animation: ping 2s ease-out infinite; }
.pulse.warn { background: #f59e0b; }
.pulse.err { background: #ef4444; }
@keyframes ping { 0% { transform: scale(.6); opacity: .4; } 100% { transform: scale(1.8); opacity: 0; } }

/* 側欄邊緣：拖曳調整寬度 */
.sb-resizer { flex: none; width: 6px; margin-left: -2px; cursor: col-resize; position: relative; z-index: 5; touch-action: none; }
.sb-resizer::after { content: ""; position: absolute; top: 8px; bottom: 8px; left: 2px; width: 2px; border-radius: 2px; background: transparent; transition: background .12s; }
.sb-resizer:hover::after, .sb-resizer:focus-visible::after, body.resizing .sb-resizer::after { background: var(--line-strong); }
.sb-resizer:focus-visible { outline: none; }
body.resizing, body.resizing * { cursor: col-resize !important; user-select: none !important; }
body.resizing iframe { pointer-events: none; }

/* 側欄收合：只剩圖示列（桌機） */
@media (min-width: 861px) {
  body.sb-collapsed { --sidebar-w: 56px; }
  body.sb-collapsed .sidebar { padding: 10px 8px 8px; align-items: center; }
  body.sb-collapsed .ws { flex-direction: column; height: auto; gap: 6px; padding: 0; }
  body.sb-collapsed .ws-name, body.sb-collapsed .quick-btn, body.sb-collapsed .nav-sec, body.sb-collapsed #fav-list,
  body.sb-collapsed #region-list, body.sb-collapsed .nav-label { display: none; }
  body.sb-collapsed .ws-mark { display: block; }
  body.sb-collapsed .quick { padding: 2px 0 6px; }
  body.sb-collapsed .nav { width: 100%; align-items: center; }
  body.sb-collapsed .nav-item { width: 36px; height: 36px; padding: 0; justify-content: center; }
  body.sb-collapsed .nav-item .ms { font-size: 18px; }
  body.sb-collapsed .sb-foot { padding: 8px; display: grid; place-items: center; }
  body.sb-collapsed .sb-foot .sub { display: none; }
  body.sb-collapsed .sb-foot .row { font-size: 0; }
}
.rail-tip {
  position: fixed; z-index: 80; transform: translateY(-50%); pointer-events: none; white-space: nowrap;
  background: var(--fg); color: #fff; font-size: var(--fs-sm); padding: 4px 8px; border-radius: var(--r-sm);
}

/* ---------- top bar ---------- */
.bar { display: flex; align-items: center; gap: 8px; height: 48px; padding: 0 12px 0 16px; border-bottom: 1px solid var(--line); flex: none; }
.bar-icon { color: var(--muted); }
.bar h1 { font-size: var(--fs-md); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.star .ms { color: var(--faint); }
.star.on .ms { color: #eab308; font-variation-settings: "FILL" 1, "wght" 400, "GRAD" 0, "opsz" 20; }
.menu-btn.icon-sm { display: none; }

.chips-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 8px 16px; border-bottom: 1px solid var(--line); flex: none; }

/* ---------- chips / controls ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: var(--ctl-h); padding: 0 9px; border-radius: var(--r); white-space: nowrap;
  background: var(--panel); border: 1px solid var(--line); color: var(--fg-2); font-weight: 500; cursor: pointer;
  transition: border-color .12s, background .12s;
}
.chip .ms { font-size: 15px; color: var(--muted); }
.chip:hover { border-color: var(--line-strong); }
.chip.ghost { border-color: transparent; background: transparent; color: var(--muted); font-weight: 400; cursor: default; }
button.chip.ghost { cursor: pointer; }
button.chip.ghost:hover { background: var(--hover); color: var(--fg); }
.chip.sel.on { background: #f4f7ff; border-color: #c9d8fb; }
.chip.sel.on .dd-val, .chip.sel.on .ms { color: #1d4ed8; }
.chip.input { cursor: text; min-width: 220px; }
.chip.input input { border: 0; outline: 0; background: transparent; font: inherit; color: var(--fg); flex: 1; min-width: 0; height: 100%; }
.chip:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }

/* 分段控制：淺灰軌道＋白色選取膠囊 */
.seg { display: inline-flex; gap: 2px; padding: 2px; background: var(--track); border: 1px solid var(--line); border-radius: var(--r); flex: none; }
.seg button { border: 1px solid transparent; background: transparent; color: var(--muted); font-weight: 500; height: 24px; padding: 0 10px; border-radius: var(--r-sm); cursor: pointer; white-space: nowrap; }
.seg button:hover { color: var(--fg); }
.seg button.on { background: var(--panel); color: var(--fg); border-color: var(--line); box-shadow: 0 1px 1px rgba(0, 0, 0, .04); }
.seg.sm button { height: 22px; padding: 0 8px; font-size: var(--fs-sm); }

.btn {
  display: inline-flex; align-items: center; gap: 6px; height: var(--ctl-h); padding: 0 10px; border-radius: var(--r); cursor: pointer; white-space: nowrap;
  background: var(--panel); border: 1px solid var(--line); color: var(--fg); font-weight: 500;
}
.btn:hover { border-color: var(--line-strong); background: var(--row-hover); }
.btn .ms { font-size: 16px; color: var(--muted); }
.btn.dark { background: var(--fg); border-color: var(--fg); color: #fff; }
.btn.dark .ms { color: #fff; }
.btn.dark:hover { opacity: .9; }
.btn.ghost { background: var(--panel); color: var(--fg-2); border-color: var(--line); }
.btn.ghost:hover { background: var(--hover); color: var(--fg); }

/* ============================================================ 自製下拉選單（dropdown.js） */
.dd { position: relative; }
.dd-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 100%; width: 100%; min-width: 0; padding: 0; margin: 0;
  border: 0; background: transparent; color: var(--fg); font-weight: 500; cursor: pointer; text-align: left;
}
.dd-btn:focus-visible { outline: none; }
.dd:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.dd .dd-icon { font-size: 15px; color: var(--muted); }
.dd-val { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }
.dd-caret { font-size: 16px; color: var(--faint); transition: transform .15s; }
.dd.open .dd-caret { transform: rotate(180deg); }
.chip.dd { padding: 0; }
.chip.dd > .dd-btn { padding: 0 8px 0 9px; }
/* 自製圖表側欄的欄位樣式 */
.bd-dd:not(.chip) { height: var(--ctl-h); border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--panel); min-width: 0; }
.bd-dd:not(.chip) > .dd-btn { padding: 0 6px 0 8px; }

.dd-pop {
  position: fixed; z-index: 90; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r);
  box-shadow: var(--shadow-pop); display: flex; flex-direction: column; overflow: hidden; animation: dd-in .12s ease;
  max-width: min(420px, calc(100vw - 16px));
}
@keyframes dd-in { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }
.dd-list { list-style: none; margin: 0; padding: 4px; overflow-y: auto; outline: none; overscroll-behavior: contain; }
.dd-opt {
  display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 8px; border-radius: var(--r-sm); cursor: pointer;
  white-space: nowrap; color: var(--fg); font-variant-numeric: tabular-nums;
}
.dd-opt .dd-opt-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.dd-opt .dd-opt-icon { color: var(--muted); }
.dd-opt .dd-check { visibility: hidden; color: var(--accent); }
.dd-opt.sel { font-weight: 600; }
.dd-opt.sel .dd-check { visibility: visible; }
.dd-opt.active { background: var(--hover); }
.dd-opt[aria-disabled="true"] { color: var(--faint); cursor: default; }
/* 月份：民國年分頁＋12 個月格 */
.dd-pop-month { width: 300px; }
.dd-years { display: flex; gap: 2px; padding: 6px; overflow-x: auto; border-bottom: 1px solid var(--line-2); scrollbar-width: none; flex: none; }
.dd-years::-webkit-scrollbar { display: none; }
.dd-year {
  flex: none; height: 26px; padding: 0 9px; border-radius: var(--r-sm); border: 1px solid transparent; background: transparent;
  color: var(--muted); font-weight: 500; cursor: pointer; font-variant-numeric: tabular-nums;
}
.dd-year:hover { color: var(--fg); background: var(--hover); }
.dd-year.on { color: var(--fg); background: var(--panel); border-color: var(--line); box-shadow: 0 1px 1px rgba(0, 0, 0, .04); }
.dd-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 8px; outline: none; }
.dd-month {
  height: 34px; display: grid; place-items: center; border-radius: var(--r-sm); cursor: pointer; border: 1px solid transparent;
  font-variant-numeric: tabular-nums; color: var(--fg);
}
.dd-month:hover:not([aria-disabled="true"]) { background: var(--hover); }
.dd-month.active { border-color: var(--line-strong); background: var(--hover); }
.dd-month.sel { background: var(--fg); color: #fff; font-weight: 600; }
.dd-month[aria-disabled="true"] { color: #d0d0d4; cursor: default; }
.dd-grid:focus-visible .dd-month.active { box-shadow: 0 0 0 2px var(--accent-ring); border-color: var(--accent); }
/* 窄螢幕：底部面板 */
.dd-scrim { position: fixed; inset: 0; z-index: 89; background: rgba(17, 17, 17, .28); animation: dd-fade .15s ease; }
@keyframes dd-fade { from { opacity: 0; } }
.dd-pop.sheet {
  left: 0 !important; right: 0; bottom: 0; top: auto !important; max-width: none; width: auto; max-height: 72vh !important;
  border-radius: 14px 14px 0 0; border-bottom: 0; animation: dd-up .18s ease; padding-bottom: env(safe-area-inset-bottom);
}
@keyframes dd-up { from { transform: translateY(24px); opacity: 0; } }
.dd-pop.sheet .dd-list { max-height: none !important; flex: 1; }
.dd-pop.sheet .dd-opt { height: 44px; font-size: var(--fs-md); }
.dd-pop.sheet .dd-month { height: 44px; font-size: var(--fs-md); }
.dd-sheet-head { display: flex; align-items: center; gap: 8px; height: 46px; padding: 0 8px 0 16px; border-bottom: 1px solid var(--line-2); flex: none; font-size: var(--fs-md); }
.dd-sheet-head b { flex: 1; }
.dd-x { width: 36px; height: 36px; display: grid; place-items: center; border: 0; background: transparent; border-radius: var(--r); cursor: pointer; color: var(--muted); }

/* 卡片的「下載」選單按鈕 */
.dl { flex: none; height: 26px; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--panel); }
.dl > .dd-btn { padding: 0 5px; gap: 4px; color: var(--fg-2); font-size: var(--fs-sm); }
.dl:hover { border-color: var(--line-strong); }
.dl .dd-icon { font-size: 16px; }
.dl.wide { height: var(--ctl-h); border-radius: var(--r); }
.dl.wide > .dd-btn { padding: 0 10px 0 8px; font-size: var(--fs); color: var(--fg); }

/* ============================================================ 載入動畫：思考中的光球
 * 參考 21st.dev 的 Thinking Orbs（@larsen66，MIT）的視覺，以純 CSS 重新製作，未使用原始碼。 */
.torb { --s: 18px; position: relative; display: inline-block; width: var(--s); height: var(--s); flex: none; border-radius: 50%; isolation: isolate; }
.torb i {
  position: absolute; inset: 16%; border-radius: 50%; filter: blur(calc(var(--s) * .06)); opacity: .85;
  animation: torb-orbit 2.4s cubic-bezier(.45, .05, .55, .95) infinite; will-change: transform;
}
.torb i:nth-child(1) { background: radial-gradient(circle at 35% 35%, #93b4ff, #2563eb); }
.torb i:nth-child(2) { background: radial-gradient(circle at 35% 35%, #a5f3fc, #0891b2); animation-delay: -.8s; }
.torb i:nth-child(3) { background: radial-gradient(circle at 35% 35%, #e9d5ff, #7c3aed); animation-delay: -1.6s; }
.torb.sm { --s: 16px; }
.torb.lg { --s: 56px; }
@keyframes torb-orbit {
  0% { transform: rotate(0deg) translateX(14%) scale(.82); }
  50% { transform: rotate(180deg) translateX(20%) scale(1.04); }
  100% { transform: rotate(360deg) translateX(14%) scale(.82); }
}
html.page-hidden .torb i, html.page-hidden .pulse::after { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) { .torb i, .pulse::after, .dd-pop, .view { animation: none !important; } }

/* ============================================================ content */
.loading { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: var(--muted); padding: 96px 0; }
.view { animation: fade .2s ease; }
.flush-view:not([hidden]) { display: flex; flex-direction: column; flex: 1; min-height: 0; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 12px; }
.col-4 { grid-column: span 4; } .col-6 { grid-column: span 6; } .col-8 { grid-column: span 8; } .col-12 { grid-column: span 12; }
/* 依主區域寬度（不是視窗寬度）換欄：AI 面板開著、側欄拉寬時也不會把卡片擠爆 */
@container main (max-width: 960px) { .col-4, .col-8 { grid-column: span 12; } }
@container main (max-width: 860px) { .col-6 { grid-column: span 12; } }

.box { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 0 14px 12px; min-width: 0; box-shadow: var(--shadow-card); container: card / inline-size; }
.box.flush { padding: 0; overflow: hidden; }
.box-head { display: flex; align-items: center; gap: 7px; height: 44px; margin: 0 -14px 8px; padding: 0 12px 0 14px; border-bottom: 1px solid var(--line-2); min-width: 0; }
.box.flush .box-head { margin: 0; }
.box-head > .ms { color: var(--muted); }
.box-head h2 { font-size: var(--fs); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 2em; flex: 0 1 auto; }
.box-head .note { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 0 1 auto; }
.box-head .seg { flex: none; }
@container card (max-width: 520px) { .box-head .note { display: none; } }
@container card (max-width: 420px) { .ask-ai .lbl { display: none; } .ask-ai { padding: 0 5px; } }
.chart { width: 100%; height: 270px; min-width: 0; }
.chart.tall { height: 320px; }
.chart.xtall { height: 600px; }

/* ---------- KPI ---------- */
.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 12px;
  border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--line);
  gap: 1px; box-shadow: var(--shadow-card);
}
.stat { background: var(--panel); padding: 12px 14px 8px; min-width: 0; display: flex; flex-direction: column; }
.stat-top { display: flex; align-items: center; gap: 6px; color: var(--muted); font-weight: 500; font-size: var(--fs-sm); min-width: 0; }
.stat-top .ms { font-size: 15px; }
.stat-label { flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-top .pill { margin-left: auto; flex: none; }
.stat-value { font-size: var(--fs-kpi); font-weight: 600; letter-spacing: -.02em; margin-top: 6px; white-space: nowrap; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; }
.stat-value small { font-size: var(--fs-sm); font-weight: 500; color: var(--muted); margin-left: 4px; letter-spacing: 0; }
.stat-foot { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spark { height: 30px; margin-top: 6px; }
.spark svg { width: 100%; height: 100%; display: block; overflow: visible; }

/* ---------- pills ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 2px; height: 20px; padding: 0 6px; border-radius: var(--r-sm); font-size: var(--fs-sm); font-weight: 500;
  white-space: nowrap; border: 1px solid; font-variant-numeric: tabular-nums; line-height: 1;
}
.pill .ms { font-size: 13px; }
.pill.gray { color: var(--fg-2); background: #f6f6f7; border-color: var(--line); }
.pill.blue { color: #1d4ed8; background: #f4f7ff; border-color: #d6e2fd; }
.pill.up { color: var(--up); background: #f3fbf5; border-color: #cdeed8; }
.pill.down { color: var(--down); background: #fff8f2; border-color: #fbdcc4; }
.pill.flat { color: var(--muted); background: #fafafa; border-color: var(--line); }
.pill.lv-醫學中心 { color: #1d4ed8; background: #f4f7ff; border-color: #d6e2fd; }
.pill.lv-區域醫院 { color: #6d28d9; background: #f8f6ff; border-color: #e4dcfd; }
.pill.lv-地區醫院 { color: #0f766e; background: #f2fbf9; border-color: #c3ece4; }
.pill.lv-其他醫院 { color: #92400e; background: #fffbef; border-color: #f6e3b0; }

/* ---------- 院徽 ---------- */
.av {
  width: 22px; height: 22px; border-radius: 50%; display: inline-grid; place-items: center; flex: none; overflow: hidden;
  background: #fff; border: 1px solid var(--line);
}
.av img { width: 100%; height: 100%; object-fit: contain; padding: 2px; display: block; }
.av.gen { background: #f4f4f5; border-color: #e9e9eb; }
.av.gen .ms { font-size: 13px; color: #a1a1aa; font-variation-settings: "FILL" 1, "wght" 400, "GRAD" 0, "opsz" 20; }
.av.lg { width: 48px; height: 48px; border-radius: 12px; }
.av.lg img { padding: 4px; }
.av.lg.gen .ms { font-size: 26px; }

/* ---------- tables ---------- */
.tbl { width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { height: 36px; padding: 0 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line-2); }
.tbl th { font-weight: 500; color: var(--muted); font-size: var(--fs-sm); background: #fafafa; }
.tbl th .ms { font-size: 14px; vertical-align: -3px; margin-right: 4px; color: var(--faint); }
.tbl .l { text-align: left; }
.tbl tbody tr:hover td { background: var(--row-hover); }
.tbl tr.click { cursor: pointer; }
.tbl.mini td { height: 38px; }
.tbl.mini tr:last-child td { border-bottom: 0; }
.tbl .num { color: var(--fg); font-weight: 500; }
.tbl .dim { color: var(--muted); }
.tbl td.idx { color: var(--faint); width: 40px; text-align: center; font-size: var(--fs-sm); }
/* 固定欄寬的小表（成長／衰退最多）：名稱欄省略號，太窄就在卡片內水平捲動 */
.tbl-scroll { overflow-x: auto; overscroll-behavior-x: contain; }
.tbl.fit { table-layout: fixed; }
.tbl.fit td { overflow: hidden; text-overflow: ellipsis; padding: 0 10px; }
.tbl.fit td.idx { width: auto; padding: 0 4px; }
.tbl.fit td.name { padding-right: 6px; }

.grid-tbl th, .grid-tbl td { border-right: 1px solid var(--line-2); border-bottom: 1px solid var(--line-2); }
.grid-tbl th:last-child, .grid-tbl td:last-child { border-right: 0; }
.grid-tbl thead th { position: sticky; top: 0; z-index: 2; border-bottom: 1px solid var(--line); }
.grid-tbl th.sortable { cursor: pointer; user-select: none; }
.grid-tbl th.sortable:hover { color: var(--fg); background: var(--hover); }
.grid-tbl th.sorted { color: var(--fg); }
.grid-tbl th .arrow { font-size: 14px; color: var(--accent); margin: 0 0 0 2px; }
.grid-tbl tfoot td { position: sticky; bottom: 0; background: #fafafa; color: var(--muted); font-size: var(--fs-sm); border-top: 1px solid var(--line); border-bottom: 0; z-index: 2; }
.grid-tbl tfoot td b { color: var(--fg); font-weight: 600; }
.grid-tbl.defs th { width: 120px; color: var(--fg); background: #fafafa; }
.grid-tbl.defs td { white-space: normal; height: auto; padding: 9px 12px; color: var(--fg-2); }
.grid-tbl.defs tr:last-child th, .grid-tbl.defs tr:last-child td { border-bottom: 0; }

.toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 8px 16px; border-bottom: 1px solid var(--line); flex: none; }
.grid-wrap { flex: 1; overflow: auto; min-height: 0; }
.grid-wrap.auto { max-height: 560px; }

.cell-h { display: flex; align-items: center; gap: 8px; max-width: 300px; min-width: 0; }
.cell-h b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg); min-width: 0; }
.cell-h .code { color: var(--faint); font-size: var(--fs-sm); }
.tbl.fit .cell-h { max-width: 100%; }

/* ---------- mix ---------- */
.mix { display: flex; flex-direction: column; gap: 14px; padding-top: 4px; }
.mix .big { font-size: 24px; font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.mix .big small { font-size: var(--fs-sm); color: var(--muted); font-weight: 500; margin-left: 4px; }
.mix-bar { display: flex; height: 8px; border-radius: 99px; overflow: hidden; gap: 2px; }
.mix-bar span { height: 100%; }
.mix-row { display: grid; grid-template-columns: 10px minmax(0, 1fr) auto auto 48px; gap: 10px; align-items: center; height: 36px; border-top: 1px solid var(--line-2); }
.mix-row i { width: 8px; height: 8px; border-radius: 50%; }
.mix-row .v { font-weight: 500; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mix-row .p { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }

/* ---------- hospital hero ---------- */
.hero { display: flex; align-items: center; gap: 12px; padding: 4px 2px 14px; flex-wrap: wrap; }
.hero .info { flex: 1; min-width: 240px; }
.hero h2 { font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.hero .meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; }
.hero .changes { color: var(--muted); margin-top: 6px; display: flex; align-items: center; gap: 4px; }

/* ---------- about ---------- */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 0; margin: -8px -14px -12px; }
.kv dt, .kv dd { margin: 0; padding: 9px 14px; border-top: 1px solid var(--line-2); }
.kv dt { color: var(--muted); }
.kv dd { color: var(--fg-2); min-width: 0; overflow-wrap: anywhere; }
.kv dt:first-of-type, .kv dt:first-of-type + dd { border-top: 0; }
.prose p { margin: 4px 0; color: var(--fg-2); }
.prose ul { margin: 6px 0; padding-left: 18px; color: var(--fg-2); }
.prose li { margin: 3px 0; }

/* ============================================================ 總覽主視覺：筆記本點格＋一句話查詢 */
.notebook {
  position: relative; border-radius: 14px; border: 1px solid var(--line); background-color: #fff;
  background-image: radial-gradient(circle at 1px 1px, rgba(17, 17, 17, .14) 1px, transparent 1.2px);
  background-size: 17px 17px; background-position: 8px 8px; box-shadow: var(--shadow-card); overflow: hidden;
}
.notebook::before {   /* 點格往邊緣淡出 */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(120% 140% at 30% 0%, transparent 55%, rgba(255, 255, 255, .85) 100%);
}
.notebook > * { position: relative; }
.ask { padding: 22px 24px 18px; margin-bottom: 12px; }
.ask-eyebrow { display: flex; align-items: center; gap: 6px; color: var(--muted); font-weight: 500; font-size: 12.5px; }
.ask-eyebrow .ms { font-size: 15px; color: var(--muted); }
.ask-title { font-size: 22px; font-weight: 600; letter-spacing: -.02em; margin-top: 6px; color: var(--fg); max-width: 900px; font-variant-numeric: tabular-nums; }
.ask-title em { font-style: normal; color: var(--up); }
.ask-title em.neg { color: var(--down); }
.ask-input {
  display: flex; align-items: center; gap: 10px; margin-top: 16px; max-width: 720px; height: 46px; padding: 0 6px 0 16px;
  background: #fff; border: 1px solid var(--line); border-radius: 999px;
  box-shadow: 0 6px 20px -8px rgba(0, 0, 0, .14), 0 1px 2px rgba(0, 0, 0, .04);
  transition: box-shadow .2s, border-color .2s;
}
.ask-input:focus-within { border-color: var(--line-strong); box-shadow: 0 8px 24px -8px rgba(0, 0, 0, .18), 0 0 0 4px rgba(17, 17, 17, .05); }
.ask-input > .ms { color: var(--muted); font-size: 20px; }
.ask-input input { flex: 1; border: 0; outline: 0; background: transparent; font: inherit; font-size: var(--fs-md); color: var(--fg); min-width: 0; }
.ask-send {
  width: 34px; height: 34px; border-radius: 50%; border: 0; display: grid; place-items: center; cursor: pointer;
  background: var(--fg); color: #fff; transition: transform .15s, opacity .15s; flex: none;
}
.ask-send .ms { font-size: 18px; }
.ask-send:hover { transform: translateY(-1px); }
.ask-send:disabled { opacity: .3; cursor: default; transform: none; }
.ask-preview { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-height: 26px; margin-top: 10px; color: var(--fg-2); font-size: 12.5px; }
.ask-preview .tag { display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: 999px; background: #fff; border: 1px solid var(--line); color: var(--fg); font-weight: 500; }
.ask-preview .tag .ms { font-size: 14px; color: var(--muted); }
.ask-sugs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.ask-sugs button {
  height: 26px; padding: 0 10px; border-radius: 999px; cursor: pointer; font-size: 12.5px; color: var(--fg-2);
  background: #fff; border: 1px solid var(--line); transition: border-color .15s, color .15s;
}
.ask-sugs button:hover { border-color: var(--line-strong); color: var(--fg); }

/* ============================================================ 頁尾署名 */
.credit {
  display: flex; align-items: center; justify-content: center; gap: 8px; padding: 28px 0 4px; color: var(--muted); font-size: var(--fs-sm);
}
.credit img { height: 20px; width: auto; display: block; background: transparent; }
.credit a { color: var(--fg-2); font-weight: 500; }
.credit a:hover { color: var(--fg); text-decoration: underline; }
main:has(.flush-view:not([hidden])) .credit { padding: 10px 0; border-top: 1px solid var(--line); flex: none; }

/* ============================================================ command palette */
.cmdk { position: fixed; inset: 0; z-index: 50; background: rgba(17, 17, 17, .22); display: grid; place-items: start center; padding-top: 14vh; animation: fade .12s ease; }
.cmdk-panel { width: min(600px, calc(100vw - 32px)); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow-pop); }
.cmdk-input { display: flex; align-items: center; gap: 8px; padding: 0 12px; border-bottom: 1px solid var(--line); }
.cmdk-input .ms { color: var(--muted); }
.cmdk-input input { flex: 1; border: 0; outline: 0; font: inherit; font-size: var(--fs-md); height: 46px; color: var(--fg); background: transparent; min-width: 0; }
.cmdk-list { list-style: none; margin: 0; padding: 6px; max-height: 52vh; overflow: auto; }
.cmdk-list li { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 8px; border-radius: var(--r-sm); cursor: pointer; }
.cmdk-list li .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cmdk-list li .sub { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.cmdk-list li.sel { background: var(--hover); }
.cmdk-list .group { height: 26px; font-size: var(--fs-sm); color: var(--muted); cursor: default; }
.cmdk-list .none { justify-content: center; color: var(--muted); cursor: default; height: 64px; }
.cmdk-foot { display: flex; gap: 14px; justify-content: flex-end; padding: 7px 12px; border-top: 1px solid var(--line); font-size: var(--fs-sm); color: var(--muted); background: #fafafa; }
.cmdk-foot kbd { margin-right: 2px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 8px); background: var(--fg); color: #fff; max-width: calc(100vw - 32px);
  padding: 8px 12px; border-radius: var(--r); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 95;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast a { color: #93c5fd; font-weight: 600; margin-left: 6px; }

/* ---------- 期間（全域） ---------- */
.per-ctl { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.per-custom { display: inline-flex; align-items: center; gap: 6px; }
.per-custom .chip.dd { min-width: 150px; }
.per-hint { font-variant-numeric: tabular-nums; }
.seg button.auto-pick { color: var(--fg); text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 3px; }
#f-month { min-width: 148px; }
#f-metric { min-width: 150px; max-width: 220px; }

/* ---------- 問 AI 小按鈕 ---------- */
.ask-ai { display: inline-flex; align-items: center; gap: 4px; height: 26px; padding: 0 8px; border-radius: var(--r-sm); border: 1px solid var(--line);
  background: var(--panel); color: var(--fg-2); font-size: var(--fs-sm); font-weight: 500; cursor: pointer; white-space: nowrap; flex: none; }
.ask-ai .ms { font-size: 14px; color: #7c3aed; }
.ask-ai:hover { border-color: var(--line-strong); color: var(--fg); }
.ai-btn { width: auto; padding: 0 8px; gap: 4px; display: inline-flex; align-items: center; border-color: var(--line); background: var(--panel); height: var(--ctl-h); }
.ai-btn .ms { color: #7c3aed; }
.ai-btn-label { font-size: var(--fs-sm); font-weight: 600; }
.ai-btn.on { background: #f8f6ff; border-color: #e4dcfd; }

/* ---------- Albireus AI 側邊面板（可收合、可拖曳左緣調整寬度） ---------- */
.ai-drawer { position: relative; width: var(--ai-w); flex: none; margin: 8px 8px 8px 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  display: flex; flex-direction: column; overflow: hidden; box-shadow: var(--shadow-card); }
.ai-resizer { position: absolute; left: 0; top: 0; bottom: 0; width: 6px; cursor: col-resize; z-index: 6; touch-action: none; }
.ai-resizer::after { content: ""; position: absolute; top: 12px; bottom: 12px; left: 1px; width: 2px; border-radius: 2px; background: transparent; transition: background .12s; }
.ai-resizer:hover::after, .ai-resizer:focus-visible::after, body.resizing .ai-resizer::after { background: var(--line-strong); }
.ai-resizer:focus-visible { outline: none; }
.ai-head { display: flex; align-items: center; gap: 6px; height: 48px; padding: 0 8px; border-bottom: 1px solid var(--line); flex: none; white-space: nowrap; }
.ai-head b { font-weight: 600; }
.ai-head > .ai-mark { color: #7c3aed; font-variation-settings: "FILL" 1, "wght" 400, "GRAD" 0, "opsz" 20; }
.ai-collapse .ms { color: var(--muted); }
.ai-auth { color: var(--muted); font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.ai-notice { display: flex; gap: 6px; align-items: flex-start; margin: 0; padding: 8px 14px; font-size: var(--fs-sm); color: var(--fg-2); background: #fafafa; border-bottom: 1px solid var(--line); flex: none; }
.ai-notice .ms { font-size: 15px; color: var(--muted); margin-top: 1px; }
.ai-frame { flex: 1; min-height: 0; position: relative; }
.ai-frame iframe { position: relative; z-index: 1; width: 100%; height: 100%; border: 0; display: block; }
.ai-loading { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }
/* 收合：只剩一條窄欄（iframe 保留，不會重新載入） */
.ai-drawer.collapsed { width: 44px; }
.ai-drawer.collapsed .ai-head { flex-direction: column; height: auto; padding: 8px 0; border-bottom: 0; gap: 8px; }
.ai-drawer.collapsed .ai-head > :not(.ai-collapse):not(.ai-mark) { display: none; }
.ai-drawer.collapsed .ai-notice, .ai-drawer.collapsed .ai-frame { visibility: hidden; height: 0; flex: 0; border: 0; padding: 0; }

/* ---------- 自製圖表 ---------- */
body[data-view="builder"] .bar #f-metric, body[data-view="builder"] #f-window,
body[data-view="builder"] .chips-row .chip.ghost:has(#window-hint) { display: none; }
.bd { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 14px; align-items: start; }
@container main (max-width: 900px) { .bd { grid-template-columns: 1fr; } .bd-side { position: static !important; max-height: none !important; } }
.bd-side { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 4px 14px 14px; position: sticky; top: 0;
  max-height: calc(100vh - 150px); overflow-y: auto; box-shadow: var(--shadow-card); }
.bd-side section { padding: 12px 0; border-bottom: 1px solid var(--line-2); }
.bd-side section:last-child { border-bottom: 0; }
.bd-side h3 { display: flex; align-items: center; gap: 6px; margin: 0 0 10px; font-size: var(--fs); font-weight: 600; }
.bd-side h3 .ms { color: var(--muted); }
.bd-side .hint { color: var(--muted); font-size: var(--fs-sm); margin: 6px 0 0; }
.bd-types { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; }
.bd-type { display: flex; align-items: center; gap: 6px; height: var(--ctl-h); padding: 0 8px; border: 1px solid var(--line); border-radius: var(--r); background: var(--panel); color: var(--fg-2); cursor: pointer; font-weight: 500; white-space: nowrap; overflow: hidden; }
.bd-type .ms { color: var(--muted); }
.bd-type:hover { border-color: var(--line-strong); }
.bd-type.on { background: #f4f7ff; border-color: #c9d8fb; color: #1d4ed8; }
.bd-type.on .ms { color: #1d4ed8; }
.bd-type:disabled { opacity: .4; cursor: not-allowed; }
.bd-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.bd-chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 2px 2px 8px; border-radius: 999px; background: var(--hover); font-size: var(--fs-sm); max-width: 100%; }
.bd-chip .ms { font-size: 14px; color: var(--muted); }
.bd-chip button { border: 0; background: transparent; cursor: pointer; display: grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; color: var(--muted); padding: 0; }
.bd-chip button:hover { background: var(--line); color: var(--fg); }
.bd-search { width: 100%; min-width: 0; }
.bd-results { list-style: none; margin: 4px 0 8px; padding: 0; }
.bd-results button { width: 100%; display: grid; grid-template-columns: 18px minmax(0, 1fr); column-gap: 6px; text-align: left; padding: 5px 6px; border: 0; background: transparent; border-radius: var(--r-sm); cursor: pointer; }
.bd-results button:hover:not(:disabled) { background: var(--hover); }
.bd-results button:disabled { color: var(--faint); cursor: default; }
.bd-results .ms { grid-row: span 2; color: var(--muted); }
.bd-results .nm { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-results .sub, .bd-results .none { color: var(--muted); font-size: 11.5px; }
.bd-row { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; min-width: 0; }
.bd-row.wrap { flex-wrap: wrap; }
.bd-row > .bd-dd { flex: 1 1 0; min-width: 0; }
.bd-row .chip.sel { flex: 1 1 90px; }
.bd-field input:not([type=range]) { height: var(--ctl-h); border: 1px solid var(--line-strong); border-radius: var(--r); padding: 0 8px; font: inherit; color: var(--fg); background: var(--panel); min-width: 0; width: 100%; }
.bd-lbl { width: 34px; color: var(--muted); font-size: var(--fs-sm); flex: none; }
.bd-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; color: var(--fg-2); font-size: var(--fs-sm); font-weight: 500; min-width: 0; }
.bd-field .bd-dd { width: 100%; font-size: var(--fs); }
.bd-field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.bd-grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 8px; }
.bd-check { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--fg-2); }
.bd-ser { padding: 8px 0; border-top: 1px dashed var(--line); }
.bd-ser:first-child { border-top: 0; }
.bd-ser input { width: 100%; height: 28px; border: 1px solid var(--line-strong); border-radius: var(--r-sm); padding: 0 8px; font: inherit; margin-bottom: 6px; }
.sws { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.sw { width: 20px; height: 20px; border-radius: 5px; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line-strong); cursor: pointer; padding: 0; }
.sw.on { box-shadow: 0 0 0 2px var(--fg); }
.sw.custom { position: relative; display: grid; place-items: center; background: var(--panel); overflow: hidden; }
.sw.custom .ms { font-size: 13px; color: var(--muted); pointer-events: none; }
.sw.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.bd-note { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-start; margin: 10px 0 0; padding: 8px 10px; border-radius: var(--r); background: #f8f6ff; color: #5b21b6; font-size: var(--fs-sm); }
.bd-note .ms { font-size: 15px; }
.bd-note span:nth-child(2) { flex: 1; min-width: 180px; }
.bd-main { min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.bd-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.bd-dl { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.bd-stage { border: 1px solid var(--line); border-radius: var(--radius); background: repeating-conic-gradient(#fafafa 0 25%, #f4f4f5 0 50%) 0 0 / 16px 16px; padding: 14px; }
.bd-canvas { width: 100%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .08); }
.bd-canvas svg { width: 100%; height: 100%; display: block; }
.bd-empty { padding: 40px; color: var(--muted); text-align: center; }
.bd-lib { list-style: none; margin: 0; padding: 0; }
.bd-lib li { display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-top: 1px solid var(--line-2); }
.bd-lib li:first-child { border-top: 0; }
.bd-lib .ms { color: var(--muted); }
.bd-lib .nm { font-weight: 500; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-lib .sub { color: var(--muted); font-size: var(--fs-sm); }
.bd-lib .none { color: var(--muted); }

/* ============================================================ responsive（手機：抽屜側欄、全螢幕 AI 面板） */
@media (max-width: 1100px) { :root { --ai-w: 340px; } }
@media (max-width: 860px) {
  .shell { height: auto; min-height: 100vh; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; width: 264px; background: var(--app); transform: translateX(-100%); transition: transform .2s ease;
    border-right: 1px solid var(--line); box-shadow: 0 0 40px rgba(0, 0, 0, .15); padding-right: 8px; }
  .sidebar.open { transform: none; }
  .sb-toggle, .sb-resizer, .ai-resizer, .ai-collapse { display: none !important; }
  .scrim { position: fixed; inset: 0; background: rgba(17, 17, 17, .25); z-index: 30; display: none; }
  .scrim.show { display: block; }
  .menu-btn.icon-sm { display: grid; }
  .panel { margin: 0; border-radius: 0; border: 0; overflow: visible; box-shadow: none; }
  /* 頂端列：第一行標題＋按鈕，第二行兩個選單平均分 */
  .bar { position: sticky; top: 0; z-index: 20; background: var(--panel); flex-wrap: wrap; height: auto; padding: 8px 12px; row-gap: 8px; }
  .bar h1 { flex: 1 1 auto; }
  .bar .grow { display: none; }
  .bar .ai-btn { order: 1; }
  .bar::after { content: ""; order: 2; flex-basis: 100%; height: 0; }
  .bar .chip.sel { order: 3; flex: 1 1 0; min-width: 0; max-width: none; }
  main { overflow: visible; padding: 12px 12px 32px; }
  main:has(.flush-view:not([hidden])) { padding: 0; }
  .grid-wrap { max-height: 70vh; }
  /* 統計區間、期間：橫向捲動的一列，不換行擠在一起 */
  .chips-row { flex-wrap: nowrap; overflow-x: auto; padding: 8px 12px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .chips-row::-webkit-scrollbar { display: none; }
  .chips-row > .grow { display: none; }
  .chips-row > *, .per-ctl > * { flex: none; }
  .per-ctl { flex-wrap: nowrap; }
  .toolbar { padding: 8px 12px; }
  .ai-drawer { position: fixed; inset: 0; width: auto !important; margin: 0; border-radius: 0; border: 0; z-index: 60; }
  .ai-drawer.collapsed .ai-notice, .ai-drawer.collapsed .ai-frame { visibility: visible; height: auto; flex: 1; }
  .ai-drawer.collapsed .ai-notice { flex: none; padding: 8px 14px; border-bottom: 1px solid var(--line); }
  .ai-drawer.collapsed .ai-head { flex-direction: row; height: 48px; padding: 0 8px; border-bottom: 1px solid var(--line); }
  .ai-drawer.collapsed .ai-head > :not(.ai-collapse):not(.ai-mark) { display: revert; }
  .ai-btn-label { display: none; }
}
@media (max-width: 560px) {
  .chip.input { min-width: 0; flex: 1 1 100%; }
  /* KPI：兩欄，標籤在上、數值在下，標籤不會被擠成直排 */
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats .stat:last-child:nth-child(odd) { grid-column: span 2; }
  .stat { padding: 10px 12px 8px; }
  .stat-top { flex-wrap: wrap; row-gap: 4px; }
  .stat-top .pill { margin-left: 0; }
  .stat-value { font-size: 18px; }
  .chart.xtall { height: 680px; }
  .ask { padding: 16px; }
  .ask-title { font-size: 18px; }
  .ask-input { height: 44px; }
  .toolbar .dl.wide .dd-val { display: none; }
}
@media (max-width: 380px) {
  .stats { grid-template-columns: minmax(0, 1fr); }
  .stats .stat:last-child:nth-child(odd) { grid-column: auto; }
  .stat-top .pill { margin-left: auto; }
}
