/* =====================================================================
 * EPS 互動教學 — EP Lab 深色高對比介面
 * Mobile-first：直向＝波形＋底部抽屜；橫向／桌機＝70% 波形｜30% 傳導圖與控制
 * ===================================================================== */
:root {
  --bg: #04080b;
  --panel: #0b131a;
  --panel2: #101b24;
  --line: #1f2f3c;
  --txt: #d6e2ea;
  --muted: #7d93a3;
  --acc: #3dff8b;
  --hra: #ffd84a;
  --his: #ff9a3c;
  --cs: #5fb2ff;
  --rva: #ff5c5c;
  --cyan: #7df9ff;
  --sheet-peek: 52px;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--txt);
  font: 14px/1.5 system-ui, -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding: var(--sat) var(--sar) 0 var(--sal);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
button, input { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }

/* ---------- 標題列 ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px;
  background: linear-gradient(180deg, #0c1720, #08111a);
  border-bottom: 1px solid var(--line);
  flex: none;
}
.logo { width: 34px; height: 34px; flex: none; }
.ttl { flex: 1; min-width: 0; }
.ttl h1 { font-size: 16px; letter-spacing: .02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ttl p { margin: 0; font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- 通用按鈕（觸控 ≥ 44px） ---------- */
.btn, .segb, .tog, .mtab, .stab {
  min-height: 44px; min-width: 44px;
  border: 1px solid #2a3e4d;
  background: var(--panel2);
  border-radius: 10px;
  padding: 0 12px;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover, .segb:hover, .tog:hover { border-color: #43627a; }
.btn:active, .segb:active, .tog:active { transform: translateY(1px); }
.btn:focus-visible, .segb:focus-visible, .tog:focus-visible, .mtab:focus-visible, .stab:focus-visible, input:focus-visible {
  outline: 2px solid var(--cyan); outline-offset: 2px;
}
.btn.small { min-height: 44px; font-size: 12.5px; padding: 0 10px; }
.btn.primary { background: #123227; border-color: #2c7a55; color: #c9ffe0; font-weight: 600; }
.btn.primary:hover { background: #17402f; }
.btn.tgl.on, .tog.on { background: #0f3640; border-color: #2fa7b8; color: var(--cyan); }
.tog:disabled { opacity: .38; cursor: not-allowed; }
.btn.sq { padding: 0; width: 44px; font-size: 18px; }

.seg { display: inline-flex; border-radius: 10px; overflow: hidden; border: 1px solid #2a3e4d; flex: none; }
.seg .segb { border: 0; border-radius: 0; border-right: 1px solid #2a3e4d; background: var(--panel2); }
.seg .segb:last-child { border-right: 0; }
.seg .segb.on { background: #173a2b; color: #b9ffd7; font-weight: 600; box-shadow: inset 0 -3px 0 var(--acc); }

/* ---------- 模組分頁 ---------- */
#modTabs {
  display: flex; gap: 6px; padding: 6px 8px;
  overflow-x: auto; scrollbar-width: none;
  background: #070d12; border-bottom: 1px solid var(--line);
  flex: none;
}
#modTabs::-webkit-scrollbar { display: none; }
.mtab { flex: 1 0 auto; display: inline-flex; align-items: center; gap: 6px; justify-content: center; background: transparent; }
.mtab .mn {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: #1a2a36; font-size: 12px; font-weight: 700;
}
.mtab.on { background: #11261d; border-color: #2c7a55; color: #c9ffe0; font-weight: 600; }
.mtab.on .mn { background: var(--acc); color: #032; }

/* ---------- 工具列（窄螢幕橫向滑動） ---------- */
#toolbar {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: thin;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: none;
  -webkit-overflow-scrolling: touch;
}
#toolbar > * { flex: none; }
.btn.tb { padding: 0 11px; }
#btnPlay { font-size: 17px; width: 48px; }
.tsep { width: 1px; height: 28px; background: var(--line); margin: 0 2px; }

/* ---------- 主版面（直向：控制在上、波形佔滿、底部抽屜） ---------- */
#layout {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "controls" "trace";
  padding-bottom: calc(var(--sheet-peek) + var(--sab));
  overflow-y: auto;
}
#tracePane { grid-area: trace; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
#traceWrap { position: relative; flex: 1; min-height: 280px; background: var(--bg); }
#ekg {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block;
  touch-action: none;               /* 防止拖曳卡尺觸發下拉重整／返回手勢 */
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
#panRow {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 10px; min-height: 44px;
  background: var(--panel); border-top: 1px solid var(--line);
  flex: none;
}
#panRow .pl { font-size: 12px; color: var(--muted); }
#timeRead { font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); min-width: 92px; text-align: right; }

input[type=range] { flex: 1; min-width: 80px; height: 44px; accent-color: var(--acc); background: transparent; }

.chip {
  flex: none;
  min-height: 34px;
  padding: 7px 10px 6px 12px;
  background: #08141b;
  border-bottom: 1px solid var(--line);
  border-left: 3px solid #3d6a85;
  font-size: 12.5px; line-height: 1.4;
  color: #cfe3ef;
  transition: color .2s;
}
.chip.hidden { color: transparent; }
.chip.tone-warn { border-left-color: #d9a520; color: #ffe39a; }
.chip.tone-bad { border-left-color: #ff5c5c; color: #ffb3b3; }
.chip.tone-ok { border-left-color: #3dff8b; color: #b8ffd5; }

/* ---------- 控制面板 ---------- */
#controls {
  grid-area: controls;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 6px 8px 4px;
  min-width: 0;
}
.ctl-title { font-size: 12px; color: var(--muted); margin: 0 2px 4px; }
.ctl-row {
  display: flex; align-items: center; gap: 6px;
  overflow-x: auto; flex-wrap: nowrap;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.ctl-row::-webkit-scrollbar { display: none; }
.ctl-row > * { flex: none; }
.sl { display: flex; align-items: center; gap: 6px; flex: 1 0 280px; }
.sl input { flex: 1; }
.sl-l { font-size: 12px; color: var(--muted); }
.sl output { font: 600 13px ui-monospace, Menlo, monospace; color: var(--hra); min-width: 58px; text-align: right; }
.hint { font-size: 12px; color: var(--muted); padding: 0 2px 4px; }
.quiz label { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; }
.quiz input {
  width: 76px; min-height: 44px; border-radius: 8px; border: 1px solid #2a3e4d;
  background: #071017; padding: 0 8px; text-align: center;
}
.quizout { font-size: 12.5px; padding: 0 2px 4px; }
.quizout.ok { color: #9bffc6; }
.quizout.warn { color: #ffd98a; }

/* ---------- 側欄 / 底部抽屜 ---------- */
#side {
  position: fixed; z-index: 30;
  left: 0; right: 0; bottom: 0;
  height: min(72dvh, 640px);
  padding-bottom: var(--sab);
  background: var(--panel);
  border-top: 1px solid #2a3e4d;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, .55);
  transform: translateY(calc(100% - var(--sheet-peek) - var(--sab)));
  transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  display: flex; flex-direction: column;
}
#side.open { transform: translateY(0); }
#sheetHead { flex: none; padding: 0 8px 0 calc(8px + var(--sal)); touch-action: none; }
.grab {
  display: block; width: 100%; height: 14px; min-height: 14px; border: 0; background: transparent; padding: 0; cursor: pointer;
}
.grab span { display: block; width: 44px; height: 4px; border-radius: 2px; background: #3a5162; margin: 5px auto 0; }
.stabs { display: flex; gap: 6px; }
.stab { flex: 1; min-height: 38px; height: 38px; font-size: 13px; padding: 0 6px; background: transparent; border-color: transparent; }
.stab.on { background: #11261d; border-color: #2c7a55; color: #c9ffe0; }
#sheetBody { flex: 1; overflow: auto; padding: 8px 12px 16px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.sheet-sec { display: none; }
.sheet-sec.active { display: block; }
.sec-h { font-size: 14px; margin: 4px 0 6px; color: #e9f3f9; }
.sec-h small { font-size: 11.5px; color: var(--muted); font-weight: 400; margin-left: 6px; }

/* ---------- SVG 心臟傳導圖 ---------- */
#heartSvg { width: 100%; max-width: 420px; height: auto; display: block; margin: 0 auto; }
.hs .reg { fill: #131f29; stroke: #2a4050; stroke-width: 1.5; transition: fill .07s; }
.hs .reg.on-a { fill: #4b3f12; stroke: #a08a2a; }
.hs .reg.on-r { fill: #0e3d47; stroke: #2fa7b8; }
.hs .reg.on-s { fill: #3a2346; stroke: #b670d8; }
.hs .ring { stroke: #3a4f5f; stroke-width: 2; stroke-dasharray: 5 4; fill: none; }
.hs .lbl { fill: #9fb5c4; font: 600 10px system-ui, sans-serif; }
.hs .lbl.big { fill: #4f6676; font-size: 16px; font-weight: 800; }
.hs .lbl.tiny { font-size: 8.5px; font-weight: 500; }
.hs .path { fill: none; stroke: #566c7b; stroke-width: 4; stroke-linecap: round; transition: stroke .07s; }
.hs .path.thin { stroke-width: 2.6; }
.hs .path.on-a { stroke: #ffe066; filter: drop-shadow(0 0 4px #ffe066); }
.hs .path.on-r { stroke: #4df0ff; filter: drop-shadow(0 0 4px #4df0ff); }
.hs .path.on-s { stroke: #e3a8ff; }
.hs .node { fill: #2c3d49; stroke: #6a8394; stroke-width: 1.5; transition: fill .07s; }
.hs .node.on-a { fill: #ffe066; stroke: #fff3b0; filter: drop-shadow(0 0 5px #ffe066); }
.hs .node.on-r { fill: #4df0ff; stroke: #d0fbff; filter: drop-shadow(0 0 5px #4df0ff); }
.hs .node.on-x { fill: #ff5c5c; stroke: #ffb3b3; }
.hs .os { fill: #0a1218; stroke: #3d5566; stroke-width: 1.2; }
.hs .cath { fill: none; stroke-width: 2; opacity: .55; }
.hs .cath.cs { stroke: var(--cs); stroke-width: 3; opacity: .7; }
.hs .cath.hra { stroke: var(--hra); }
.hs .cath.hisc { stroke: var(--his); }
.hs .cath.rva { stroke: var(--rva); }
.hs .cs-e rect { fill: #a9d4ff; }
.hs .cs-t { fill: #7fbfff; }
.hs .tip { stroke: #fff; stroke-width: 1; }
.hs .tip.hra { fill: var(--hra); }
.hs .tip.hisc { fill: var(--his); }
.hs .tip.rva { fill: var(--rva); }
.hs .tip.on-s { fill: #fff; filter: drop-shadow(0 0 6px #fff); }
.hs .star { fill: #5a4a1a; stroke: #ffcf4a; stroke-width: 1; }
.hs .on-a .star, .hs g.on-a .star { fill: #ffe066; filter: drop-shadow(0 0 5px #ffe066); }
.hs .focus-el, .hs .ap-el, .hs .abl { display: none; }
.hs.has-focus .focus-el { display: inline; }
.hs.has-ap .ap-el { display: inline; }
.hs .path.ap { stroke: #a06a2c; stroke-width: 5; }
.hs.abl-ap .path.ap { stroke: #3a3a3a; filter: none; }
.hs.abl-sp .path[data-a="sp"] { stroke: #3a3a3a; stroke-dasharray: 3 3; filter: none; }
.hs.abl-sp .abl-sp-el, .hs.abl-ap .abl-ap-el { display: inline; }
.hs .abl path { stroke: #ff5c5c; stroke-width: 3; stroke-linecap: round; }
.hs .abl circle { fill: rgba(255, 92, 92, .12); stroke: #ff5c5c; stroke-width: 1; stroke-dasharray: 2 2; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; justify-content: center; font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.lg { display: inline-block; width: 14px; height: 4px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.lg.a { background: #ffe066; } .lg.r { background: #4df0ff; } .lg.s { background: #e3a8ff; } .lg.x { background: #ff5c5c; }

/* ---------- 判讀重點 ---------- */
#notes h2 { font-size: 15px; margin: 2px 0 8px; color: #eaf6ff; }
#notes h3 { font-size: 13.5px; margin: 12px 0 4px; color: var(--hra); }
#notes ul, #notes ol { margin: 0; padding-left: 20px; }
#notes li { margin: 3px 0; }
#notes b { color: #fff; }
.kv { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.kv th, .kv td { border: 1px solid var(--line); padding: 4px 6px; text-align: left; vertical-align: top; }
.kv th { color: var(--his); white-space: nowrap; }
.result { margin-top: 12px; padding: 8px 10px; border-radius: 10px; background: #0a1a14; border: 1px solid #1f5a3f; font-size: 13px; }
.result:empty { display: none; }
.result .rt { font-weight: 700; color: #b9ffd7; margin-bottom: 2px; }
b.bad, .bad { color: #ff8f8f !important; }

/* ---------- 曲線與紀錄 ---------- */
#curveWrap.dim { opacity: .55; }
#curve { width: 100%; height: 200px; display: block; border-radius: 10px; border: 1px solid var(--line); }
.log { list-style: none; margin: 0; padding: 0; font-size: 12.5px; }
.log li { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.log .lm { display: inline-block; font-size: 10px; padding: 0 5px; margin-right: 6px; border-radius: 4px; background: #1a2a36; color: var(--muted); }
.muted { color: var(--muted); }

/* ---------- 卡尺懸浮氣泡 / Toast ---------- */
#calTip {
  position: fixed; left: 0; top: 0; z-index: 50;
  padding: 6px 12px; border-radius: 10px;
  background: rgba(6, 28, 34, .95); border: 1px solid var(--cyan);
  color: var(--cyan); font: 700 15px ui-monospace, Menlo, monospace;
  pointer-events: none; opacity: 0; transition: opacity .15s;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .5);
}
#calTip.show { opacity: 1; }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--sheet-peek) + var(--sab) + 14px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0;
  background: #10202a; border: 1px solid #2c5568; color: #dff3ff;
  padding: 8px 14px; border-radius: 12px; font-size: 13px;
  transition: opacity .2s, transform .2s; pointer-events: none; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.nojs { padding: 20px; }

/* ---------- 全螢幕判讀模式：隱藏標題列 ---------- */
body.immersive #topbar, body.immersive #modTabs { display: none; }

/* =====================================================================
 * 寬螢幕／手機橫向：70% 波形｜30% 控制＋傳導圖
 * ===================================================================== */
@media (min-width: 900px), (orientation: landscape) and (max-height: 600px) {
  #layout {
    grid-template-columns: minmax(0, 7fr) minmax(260px, 3fr);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "trace controls" "trace side";
    padding-bottom: var(--sab);
    overflow: hidden;
  }
  #controls { border-left: 1px solid var(--line); border-bottom: 1px solid var(--line); max-height: 52vh; overflow: auto; }
  .ctl-row { flex-wrap: wrap; overflow: visible; }
  .sl { flex: 1 1 100%; min-width: 0; }
  .sl input { min-width: 40px; }
  .ctl-row .seg { display: flex; width: 100%; }
  .ctl-row .seg .segb { flex: 1 1 0; min-width: 0; padding: 4px 6px; white-space: normal; line-height: 1.2; font-size: 12.5px; }
  #side {
    grid-area: side;
    position: static; transform: none; height: auto; min-height: 0;
    border-radius: 0; box-shadow: none; border-top: 0; border-left: 1px solid var(--line);
    padding-bottom: 0;
  }
  #sheetHead { display: none; }
  .sheet-sec { display: block; margin-bottom: 14px; }
  #tracePane { grid-row: 1 / span 2; }
  #toast { bottom: 20px; }
}

/* 手機橫向：更緊湊 */
@media (orientation: landscape) and (max-height: 600px) {
  #topbar { padding: 2px 10px; }
  .logo { width: 26px; height: 26px; }
  .ttl p { display: none; }
  #modTabs, #toolbar { padding-top: 3px; padding-bottom: 3px; }
  #traceWrap { min-height: 180px; }
  .ctl-title { display: none; }
  #controls { max-height: 58vh; }
}

/* 桌機大螢幕 */
@media (min-width: 1280px) {
  #layout { grid-template-columns: minmax(0, 7fr) minmax(340px, 3fr); }
  #traceWrap { min-height: 520px; }
}

/* 極窄螢幕 */
@media (max-width: 380px) {
  .ttl h1 { font-size: 14.5px; }
  .mtab { font-size: 13px; padding: 0 8px; }
}

@media (prefers-reduced-motion: reduce) {
  #side, #toast, #calTip, .chip { transition: none; }
}
