:root {
  --bg: #f3f6f7;
  --surface: #ffffff;
  --surface-2: #eef3f4;
  --border: #d9e2e4;
  --text: #17262b;
  --muted: #5f7379;
  --primary: #0f8b8d;
  --primary-ink: #ffffff;
  --primary-soft: #dcf1f1;
  --accent: #d9622b;
  --accent-soft: #fbe7dc;
  --good: #1f8a4c;
  --good-soft: #dff3e6;
  --bad: #c43d3d;
  --bad-soft: #f9e1e1;
  --hint: #9a6b00;
  --hint-soft: #fbf0d3;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 6px 20px rgb(15 40 45 / 0.07);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1719;
    --surface: #172225;
    --surface-2: #1e2b2f;
    --border: #2c3b3f;
    --text: #e3eced;
    --muted: #93a7ab;
    --primary: #2fb7b3;
    --primary-ink: #04201f;
    --primary-soft: #143a3a;
    --accent: #f08a55;
    --accent-soft: #3b2418;
    --good: #4cc47e;
    --good-soft: #15321f;
    --bad: #f07272;
    --bad-soft: #3a1c1c;
    --hint: #e7bb4c;
    --hint-soft: #352a10;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 6px 20px rgb(0 0 0 / 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.label { font-size: 12px; font-weight: 600; letter-spacing: .04em; color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 10px; padding: 9px 16px;
  font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: filter .15s, background .15s, opacity .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:not(:disabled):hover { filter: brightness(1.07); }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.accent { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.big { width: 100%; padding: 13px 18px; font-size: 16px; }
.btn.small { padding: 5px 12px; font-size: 13px; }
.icon-btn { border: 0; background: none; font-size: 20px; cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }

/* ---------- menu ---------- */
#menu { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.menu-card {
  width: 100%; max-width: 460px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 30px; color: var(--primary); letter-spacing: .04em; }
.brand-icon { font-size: 30px; }
.brand.small { gap: 6px; font-weight: 700; color: var(--primary); }
.brand.small .brand-icon { font-size: 20px; }
.tagline { color: var(--muted); margin: 6px 0 22px; }
.field { margin-bottom: 18px; display: grid; gap: 6px; }
.help { color: var(--muted); font-size: 12.5px; min-height: 1em; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; border: 0; background: none; padding: 7px 6px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.seg button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 700; color: var(--primary); }
.seg button:disabled { opacity: .4; cursor: not-allowed; }
select { padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); }
.best { text-align: center; margin: 14px 0 0; font-weight: 600; }
.status-line { text-align: center; color: var(--muted); font-size: 12.5px; margin: 6px 0 0; }

/* ---------- game layout ---------- */
#game { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.stats { margin-left: auto; display: flex; gap: 14px; font-size: 14px; }
.stat b { font-variant-numeric: tabular-nums; }
.stat.low b { color: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.layout {
  flex: 1; min-height: 0; display: grid; grid-template-columns: 340px 1fr;
  gap: 16px; padding: 16px; max-width: 1180px; width: 100%; margin: 0 auto;
}
.side { display: flex; flex-direction: column; gap: 12px; min-height: 0; overflow-y: auto; }
.patient-card, .cc, .findings, .chat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.patient-card { display: flex; gap: 14px; padding: 14px; align-items: center; }
.avatar {
  width: 58px; height: 58px; flex: none; border-radius: 50%; background: var(--primary-soft);
  display: grid; place-items: center; font-size: 32px;
}
.p-name { font-weight: 700; font-size: 17px; }
.p-meta { color: var(--muted); font-size: 13.5px; }
.p-tags { display: flex; gap: 6px; margin-top: 4px; }
.tag { font-size: 12px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--hint); }
.tag.ai { color: var(--primary); background: var(--primary-soft); }
.cc { padding: 12px 14px; }
.cc p { margin: 2px 0 0; font-weight: 600; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.action {
  display: flex; flex-direction: column; align-items: flex-start; gap: 0;
  padding: 9px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; text-align: left; transition: border-color .15s, background .15s;
}
.action small { color: var(--muted); font-size: 11.5px; }
.action:not(:disabled):hover { border-color: var(--primary); background: var(--primary-soft); }
.action:disabled { opacity: .45; cursor: not-allowed; }
.action.done { border-style: dashed; }
.action.warn:not(:disabled):hover { border-color: var(--accent); background: var(--accent-soft); }

.findings { padding: 12px 14px; display: grid; gap: 10px; }
.findings .empty { margin: 0; color: var(--muted); font-size: 13px; }
.finding { animation: pop .25s ease-out; }
.finding p { margin: 2px 0 0; font-size: 14px; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { display: flex; gap: 8px; max-width: 85%; animation: pop .2s ease-out; }
.msg .bubble { padding: 9px 13px; border-radius: 14px; white-space: pre-wrap; word-break: break-word; }
.msg .who { width: 32px; height: 32px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--primary-soft); font-size: 18px; }
.msg.patient .bubble { background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.doctor { align-self: flex-end; flex-direction: row-reverse; }
.msg.doctor .bubble { background: var(--primary); color: var(--primary-ink); border-bottom-right-radius: 4px; }
.msg.doctor .who { background: var(--surface-2); }
.msg.system { align-self: center; max-width: 92%; }
.msg.system .bubble { background: none; color: var(--muted); font-size: 13px; padding: 2px 8px; text-align: center; }
.msg.card { align-self: stretch; max-width: none; }
.msg.card .bubble { width: 100%; border: 1px solid; border-radius: 12px; }
.msg.good .bubble { background: var(--good-soft); border-color: var(--good); }
.msg.bad .bubble { background: var(--bad-soft); border-color: var(--bad); }
.msg.hint .bubble { background: var(--hint-soft); border-color: var(--hint); }
.msg.info .bubble { background: var(--surface-2); border-color: var(--border); }
.bubble h3 { margin: 0 0 4px; font-size: 16px; }
.bubble .answer { font-size: 18px; font-weight: 800; }
.bubble .teach { margin: 6px 0 0; font-size: 14px; }
.bubble .pts { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--muted); margin-top: 6px; }
.typing span { display: inline-block; width: 6px; height: 6px; margin: 0 2px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer { border-top: 1px solid var(--border); padding: 10px 12px 12px; display: grid; gap: 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips button {
  flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 99px;
  padding: 3px 11px; font-size: 13px; cursor: pointer; color: var(--muted);
}
.chips button:hover { border-color: var(--primary); color: var(--primary); }
.row { display: flex; gap: 8px; }
#input {
  flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg);
}
#input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.attempts { font-size: 12.5px; color: var(--muted); min-height: 1em; }
.next-bar { padding: 12px; border-top: 1px solid var(--border); }

/* ---------- game over ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 20px; padding: 0; width: min(560px, calc(100% - 32px));
  max-height: calc(100% - 48px); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
.over-head { text-align: center; padding: 24px 20px 8px; }
.over-head h2 { margin: 0; }
.big-score { font-size: 44px; font-weight: 800; color: var(--primary); margin: 4px 0; font-variant-numeric: tabular-nums; }
.over-head p { margin: 0; }
.review { list-style: none; margin: 0; padding: 8px 20px; overflow-y: auto; display: grid; gap: 8px; }
.review li { border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; }
.review summary { cursor: pointer; display: flex; gap: 8px; align-items: baseline; }
.review summary b { flex: 1; }
.review .teach { font-size: 13.5px; color: var(--muted); margin: 6px 0 2px; }
.over-actions { display: flex; gap: 10px; padding: 14px 20px 20px; justify-content: flex-end; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  #game { height: auto; min-height: 100%; }
  .topbar { position: sticky; top: 0; z-index: 5; }
  .layout { display: flex; flex-direction: column; padding: 10px; gap: 10px; }
  .side { overflow: visible; }
  .chat { height: 78vh; }
  .actions { grid-template-columns: repeat(3, 1fr); }
  .action { padding: 7px 9px; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .topbar .brand { display: none; }
  .stats { gap: 10px; font-size: 13px; }
  .msg { max-width: 94%; }
}
@media (max-width: 420px) {
  .actions { grid-template-columns: 1fr 1fr; }
  .row .btn { padding: 9px 11px; }
}
