/* VancoDose — 病房藥局標籤的冷調配色 */
:root {
  --paper: #eef3f5;
  --surface: #ffffff;
  --ink: #14283a;
  --ink-2: #4a5f70;
  --line: #cfdae0;
  --vial: #1d6e78;          /* 瓶蓋藍綠：主色 */
  --vial-soft: #d9ecee;
  --amber: #c27400;         /* 藥瓶琥珀：只用在目標帶 */
  --label-bg: #14283a;      /* 建議劑量卡 */
  --label-ink: #f4f8fa;
  --label-dim: #9fb6c6;
  --err: #b42318; --err-bg: #fdecea;
  --warn: #8a5300; --warn-bg: #fff3dc;
  --info: #2c5a83; --info-bg: #e7f0f8;
  --focus: #1d6e78;
  --radius-l: 14px; --radius-s: 8px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0d1922; --surface: #142430; --ink: #e2ebf0; --ink-2: #9db0bd; --line: #284050;
    --vial: #5fb7c1; --vial-soft: #173a42; --amber: #f0a23a;
    --label-bg: #07121a; --label-ink: #f1f6f8; --label-dim: #8ea7b8;
    --err: #ff8a80; --err-bg: #3a1716; --warn: #f5c26b; --warn-bg: #33270f; --info: #8cc0ea; --info-bg: #15283a; --focus: #5fb7c1;
  }
}
:root[data-theme="dark"] {
  --paper: #0d1922; --surface: #142430; --ink: #e2ebf0; --ink-2: #9db0bd; --line: #284050;
  --vial: #5fb7c1; --vial-soft: #173a42; --amber: #f0a23a;
  --label-bg: #07121a; --label-ink: #f1f6f8; --label-dim: #8ea7b8;
  --err: #ff8a80; --err-bg: #3a1716; --warn: #f5c26b; --warn-bg: #33270f; --info: #8cc0ea; --info-bg: #15283a; --focus: #5fb7c1;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 16px/1.55 var(--font); font-variant-numeric: tabular-nums;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- 頂列 ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .name { font-weight: 700; letter-spacing: .01em; font-size: 18px; }
.brand .tag { color: var(--ink-2); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topact { display: flex; align-items: center; gap: 8px; min-width: 0; }
.chip { border: 0; background: var(--vial); color: var(--surface); border-radius: 999px; padding: 5px 12px; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.chip.bad { background: var(--err); }
@media (max-width: 480px) { .brand .tag { display: none; } .topbar:has(#sumChip:not([hidden])) .brand .name { display: none; } }
.ghost { background: none; border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; font-size: 14px; color: var(--ink-2); cursor: pointer; white-space: nowrap; flex-shrink: 0; }
.update { background: var(--info-bg); color: var(--info); padding: 8px 16px; font-size: 14px; }
.update button { margin-left: 8px; border: 0; background: var(--info); color: var(--surface); border-radius: 6px; padding: 3px 10px; cursor: pointer; }

/* ---------- 版面 ---------- */
.layout { max-width: 1180px; margin: 0 auto; padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
@media (min-width: 960px) {
  .layout { max-width: 1320px; grid-template-columns: minmax(380px, 440px) minmax(0, 1fr); align-items: start; gap: 28px; padding: 24px; }
}
@media (min-width: 1180px) {
  .results { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .results > .span, .results > .alerts, .results > .empty { grid-column: 1 / -1; }
  .label { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 8px 28px; align-items: center; }
  .tool { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); column-gap: 28px;
    grid-template-areas: "h h" "s o" "m o" "i o" "n o"; align-content: start; }
  .tool > h3 { grid-area: h; } .tool > .srcs { grid-area: s; } .tool > .modes { grid-area: m; }
  .tool > .grid2 { grid-area: i; grid-template-columns: minmax(0, 1fr); } .tool > #tOut { grid-area: o; } .tool > .note { grid-area: n; }
  .tool #tOut { border-left: 1px solid var(--line); padding-left: 24px; }
  .label .lcurve { align-self: stretch; display: flex; align-items: center; border-left: 1px solid rgba(255,255,255,.12); padding-left: 24px; }
}

/* ---------- 輸入 ---------- */
.inputs { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.panel {
  margin: 0; border: 1px solid var(--line); border-radius: var(--radius-l);
  background: var(--surface); padding: 12px 14px 14px; min-width: 0;
}
.panel > legend, .panel > summary { font-weight: 700; font-size: 15px; padding: 0 4px; color: var(--ink); }
.panel > legend { float: left; width: 100%; padding: 0 0 8px; }
.panel > legend + * { clear: both; }
details.panel > summary { cursor: pointer; list-style: none; padding: 0; }
details.panel > summary::-webkit-details-marker { display: none; }
details.panel > summary::after { content: "＋"; float: right; color: var(--ink-2); }
details.panel[open] > summary::after { content: "－"; }
details.panel[open] > summary { margin-bottom: 10px; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 10px; flex-wrap: wrap; }
.grid2, .grid3 { display: grid; gap: 10px 12px; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.grid3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 400px) { .grid3 { grid-template-columns: 1fr 1fr; } .grid3 > :first-child { grid-column: 1 / -1; } }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.lbl { font-size: 13px; color: var(--ink-2); }
.inp {
  display: flex; align-items: center; border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--paper); min-height: 44px; overflow: hidden;
}
.inp:focus-within { border-color: var(--vial); box-shadow: 0 0 0 1px var(--vial); }
.inp input, .inp select {
  flex: 1; min-width: 0; border: 0; background: transparent; padding: 9px 10px; font-size: 17px; outline: none;
}
.inp select { appearance: none; -webkit-appearance: none; padding-right: 26px;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-2) 50%), linear-gradient(135deg, var(--ink-2) 50%, transparent 50%);
  background-position: calc(100% - 15px) 52%, calc(100% - 10px) 52%; background-size: 5px 5px; background-repeat: no-repeat; }
.inp em { font-style: normal; color: var(--ink-2); font-size: 13px; padding: 0 10px 0 2px; white-space: nowrap; }
.inp input[type="time"] { font-variant-numeric: tabular-nums; }
.unit {
  border: 0; border-left: 1px solid var(--line); background: var(--vial-soft); color: var(--vial);
  font-size: 13px; font-weight: 600; padding: 0 10px; align-self: stretch; cursor: pointer; white-space: nowrap;
}
.hint { color: var(--ink-2); font-size: 12.5px; line-height: 1.45; }
.hint:empty { display: none; }
.check { display: flex; align-items: center; gap: 8px; min-height: 30px; font-size: 15px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--vial); margin: 0; }

.seg { display: inline-flex; background: var(--paper); border: 1px solid var(--line); border-radius: 999px; padding: 3px; gap: 2px; }
.seg.wide { display: flex; margin-bottom: 10px; }
.seg.wide label { flex: 1; min-width: 0; }
.seg.wide span { white-space: normal; padding: 6px 8px; line-height: 1.3; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 6px 14px; border-radius: 999px; font-size: 15px; color: var(--ink-2); white-space: nowrap; }
.seg input:checked + span { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 0 0 1px var(--line); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }

.times { display: grid; gap: 12px; margin-top: 12px; }
.dtpair { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 8px; }
.dt { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.draw { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.draw > .field { max-width: 60%; }
.draw .elapsed { grid-column: 1 / -1; color: var(--vial); font-size: 13px; }
.draw .elapsed.bad { color: var(--err); }
.elapsed:empty { display: none; }
[hidden] { display: none !important; }

/* ---------- 結果 ---------- */
.results { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; align-content: start; }
.empty {
  border: 1px dashed var(--line); border-radius: var(--radius-l); padding: 22px 18px; color: var(--ink-2); background: var(--surface);
}
.empty h2 { margin: 0 0 6px; font-size: 17px; color: var(--ink); }
.empty ul { margin: 6px 0 0; padding-left: 20px; }

.alerts { display: grid; gap: 8px; }
.alert { display: grid; grid-template-columns: 22px 1fr; gap: 8px; padding: 10px 12px; border-radius: var(--radius-s); font-size: 14.5px; line-height: 1.5; }
.alert .ico { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; font-size: 13px; color: var(--surface); margin-top: 1px; }
.alert.error { background: var(--err-bg); color: var(--err); } .alert.error .ico { background: var(--err); }
.alert.warn { background: var(--warn-bg); color: var(--warn); } .alert.warn .ico { background: var(--warn); }
.alert.info { background: var(--info-bg); color: var(--info); } .alert.info .ico { background: var(--info); }
.alert b { display: block; font-weight: 700; }

/* 建議劑量：藥局標籤 */
.label {
  background: var(--label-bg); color: var(--label-ink); border-radius: 18px; padding: 18px 18px 12px; position: relative; overflow: hidden;
}
.label .head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; color: var(--label-dim); font-size: 14px; }
.label .dose { font-size: clamp(44px, 12vw, 60px); line-height: 1.05; font-weight: 750; letter-spacing: -.02em; margin: 8px 0 2px; }
.label .dose small { font-size: .42em; font-weight: 600; letter-spacing: 0; margin-left: 4px; color: var(--label-dim); }
.label .freq { font-size: 20px; font-weight: 600; }
.label .freq span { color: var(--label-dim); font-weight: 400; font-size: 15px; margin-left: 8px; }
.label .stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin: 14px 0 6px; border-top: 1px solid rgba(255,255,255,.14); padding-top: 12px; }
.label .stats div { min-width: 0; }
.label .stats dt { font-size: 12px; color: var(--label-dim); }
.label .stats dd { margin: 0; font-size: 19px; font-weight: 650; }
.label .stats dd small { display: block; font-size: 11.5px; color: var(--label-dim); font-weight: 400; line-height: 1.2; }
.label .curve { display: block; width: 100%; height: auto; margin-top: 6px; }
.label .loading { margin-top: 8px; font-size: 14px; color: var(--label-dim); border-top: 1px solid rgba(255,255,255,.14); padding-top: 10px; }
.label .loading strong { color: var(--label-ink); font-size: 16px; }
@media (max-width: 380px) { .label .stats { grid-template-columns: repeat(2, 1fr); } }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-l); padding: 14px 16px; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.card h3 small { font-weight: 400; color: var(--ink-2); margin-left: 6px; font-size: 13px; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px 14px; margin: 0; }
.kv div { min-width: 0; }
.kv dt { font-size: 12.5px; color: var(--ink-2); }
.kv dd { margin: 0; font-size: 17px; font-weight: 600; }
.kv dd small { font-size: 12.5px; font-weight: 400; color: var(--ink-2); margin-left: 2px; }
.model { margin: 0 0 10px; font-size: 14px; color: var(--ink-2); }
.model strong { color: var(--ink); }

table.cmp { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.cmp th { text-align: right; font-weight: 500; color: var(--ink-2); font-size: 12.5px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.cmp th:first-child, .cmp td:first-child { text-align: left; }
.cmp td { text-align: right; padding: 7px 6px; border-bottom: 1px solid var(--line); }
.cmpgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px 18px; }
.cmp caption { text-align: left; font-weight: 600; font-size: 14px; padding: 2px 0 4px; color: var(--ink); }
.cmp tr.rec td { background: var(--vial-soft); font-weight: 700; }
.cmp tr.rec td:first-child::after { content: "建議"; font-size: 11px; font-weight: 600; color: var(--vial); margin-left: 6px; }
.cmp .out { color: var(--ink-2); }
.card > .hint { margin: 10px 0 0; }
#recCard { scroll-margin-top: 76px; }

.tool .modes { margin-bottom: 10px; }
.tool .grid2 { margin-bottom: 8px; }
.tool .answer { font-size: 22px; font-weight: 700; margin: 6px 0 2px; }
.tool .answer small { font-size: 14px; font-weight: 400; color: var(--ink-2); margin-left: 4px; }
.tool svg { width: 100%; height: auto; display: block; margin-top: 6px; }
.tool .note { font-size: 12.5px; color: var(--ink-2); margin: 8px 0 0; padding-left: 18px; }
.srcs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.srcs .seg span { padding: 5px 11px; font-size: 14px; }
/* 窄螢幕（375 寬）四個選項要能換行，不超出畫面 */
.srcs .seg { display: flex; flex: 1 1 100%; min-width: 0; border-radius: 16px; }
.srcs .seg label { flex: 1; min-width: 0; display: flex; }
.srcs .seg span { flex: 1; display: flex; align-items: center; justify-content: center; white-space: normal; padding: 5px 6px; line-height: 1.3; border-radius: 13px; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.btn { border: 1px solid var(--vial); background: var(--vial); color: var(--surface); border-radius: 999px; padding: 9px 18px; font-weight: 600; cursor: pointer; font-size: 15px; }
.btn.secondary { background: transparent; color: var(--vial); }
.toast { font-size: 14px; color: var(--vial); align-self: center; }

/* ---------- 頁尾 ---------- */
.foot { max-width: 1320px; margin: 8px auto 0; padding: 8px 16px 32px; color: var(--ink-2); font-size: 13.5px; }
.foot .disclaimer { color: var(--ink); font-weight: 600; }
.foot details { border-top: 1px solid var(--line); padding: 8px 0; }
.foot summary { cursor: pointer; color: var(--ink); }
.foot ul { padding-left: 18px; margin: 6px 0; }
.foot .refs li { margin-bottom: 4px; }
.foot .meta { border-top: 1px solid var(--line); padding-top: 10px; }

@media (prefers-reduced-motion: no-preference) {
  .label.fresh { animation: settle .35s ease-out; }
  @keyframes settle { from { transform: translateY(4px); opacity: .6; } to { transform: none; opacity: 1; } }
}
@media print {
  .topbar, .inputs, .actions, .tool, .update { display: none !important; }
  .layout { display: block; }
  .label { color: #000; background: #fff; border: 2px solid #000; }
}
