:root {
  --ink: #0f1b2d; --ink-2: #3b475a; --muted: #6b7586; --line: #e3e0da; --line-2: #eeebe6;
  --bg: #f6f4f0; --card: #ffffff; --dark: #0c1218; --dark-2: #141c24;
  --red: #e5484d; --red-soft: #fdecec; --amber: #c27a00; --amber-soft: #fff4dd;
  --green: #148a55; --green-soft: #e3f5ec; --blue: #2f6fdb; --blue-soft: #e8f0fd; --purple: #7d4d96;
  --radius: 16px; --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(15,27,45,.05), 0 8px 24px -12px rgba(15,27,45,.18);
  --font: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--font); color: var(--ink); background: var(--bg); line-height: 1.6; font-size: 16px; -webkit-font-smoothing: antialiased; padding-bottom: env(safe-area-inset-bottom); }
a { color: var(--blue); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { line-height: 1.3; margin: 0 0 .4em; letter-spacing: -0.01em; }
h1 { font-size: clamp(1.6rem, 4vw, 2.3rem); }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.08rem; }
p { margin: 0 0 .8em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.mono { font-family: var(--mono); }
:focus-visible { outline: 3px solid rgba(47,111,219,.45); outline-offset: 2px; border-radius: 6px; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 40; display: flex; align-items: center; gap: 24px; padding: 10px clamp(16px, 4vw, 40px); background: rgba(246,244,240,.9); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 1.15rem; }
.brand svg { width: 32px; height: 32px; }
.brand small { font-family: var(--mono); font-size: .7rem; margin-left: 4px; color: var(--red); vertical-align: super; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a, .bottomnav a { text-decoration: none; color: var(--ink-2); padding: 8px 14px; border-radius: 999px; font-weight: 500; font-size: .95rem; }
.nav a:hover { background: var(--line-2); }
.nav a.active { background: var(--ink); color: #fff; }
.user { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.user .who { color: var(--muted); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bottomnav { display: none; }
@media (max-width: 760px) {
  .nav { display: none; }
  .user .who { display: none; }
  .bottomnav { display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 40; background: #fff; border-top: 1px solid var(--line); padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); justify-content: space-around; }
  .bottomnav a { display: flex; flex-direction: column; align-items: center; font-size: .72rem; padding: 4px 8px; border-radius: 10px; gap: 2px; }
  .bottomnav a svg { width: 22px; height: 22px; }
  .bottomnav a.active { color: var(--red); }
  main { padding-bottom: 90px !important; }
}

main { max-width: 1180px; margin: 0 auto; padding: clamp(16px, 3vw, 32px) clamp(14px, 4vw, 40px) 60px; outline: none; }

/* ---------- basics ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: clamp(16px, 2.4vw, 24px); box-shadow: var(--shadow); }
.card + .card, .stack > * + * { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line); background: #fff; padding: 10px 18px; border-radius: 999px; font-weight: 600; cursor: pointer; text-decoration: none; color: var(--ink); transition: transform .08s, background .15s; min-height: 44px; }
.btn:hover { background: var(--line-2); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.primary:hover { background: #1c2c44; }
.btn.red { background: var(--red); border-color: var(--red); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 6px 12px; min-height: 34px; font-size: .88rem; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn svg { width: 18px; height: 18px; }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 6px; }
label.field > em { display: block; font-style: normal; color: var(--muted); font-size: .8rem; margin-top: 4px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 11px 14px; background: #fff; min-height: 46px; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(47,111,219,.15); }
.check { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; cursor: pointer; }
.check input { width: 20px; height: 20px; margin-top: 3px; accent-color: var(--ink); flex: none; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-size: .88rem; cursor: pointer; display: inline-flex; gap: 6px; align-items: center; text-decoration: none; color: var(--ink); }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.chip.beh { background: var(--amber-soft); border-color: #f1d9a6; color: #7a4d00; }
.seg { display: inline-flex; background: var(--line-2); border-radius: 999px; padding: 4px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 8px 16px; border-radius: 999px; cursor: pointer; font-weight: 600; color: var(--ink-2); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }

/* ---------- status badges (สี + ไอคอน + ข้อความ เสมอ) ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px; border-radius: 999px; font-weight: 600; font-size: .85rem; white-space: nowrap; }
.badge svg { width: 15px; height: 15px; }
.badge.normal, .badge.low, .badge.good, .badge.confirmed { background: var(--green-soft); color: var(--green); }
.badge.borderline, .badge.moderate, .badge.warn, .badge.pending { background: var(--amber-soft); color: var(--amber); }
.badge.abnormal, .badge.high, .badge.bad, .badge.critical, .badge.very_high { background: var(--red-soft); color: #b42328; }
.badge.unreadable, .badge.revised { background: var(--blue-soft); color: var(--blue); }

/* ---------- auth ---------- */
.auth-wrap { display: grid; grid-template-columns: 1.1fr 1fr; gap: 32px; align-items: center; min-height: calc(100vh - 160px); }
.auth-hero h1 { font-size: clamp(1.9rem, 4.5vw, 3rem); }
.auth-hero h1 em { font-style: normal; color: var(--red); }
.auth-hero ul { padding: 0; list-style: none; margin: 24px 0 0; }
.auth-hero li { display: flex; gap: 12px; margin-bottom: 14px; align-items: flex-start; }
.auth-hero li b { display: block; }
.auth-hero .ico { flex: none; width: 40px; height: 40px; border-radius: 12px; background: #fff; border: 1px solid var(--line); display: grid; place-items: center; }
.auth-hero .ico svg { width: 22px; height: 22px; color: var(--red); }
@media (max-width: 860px) { .auth-wrap { grid-template-columns: 1fr; } .auth-hero ul { display: none; } }

/* ---------- upload ---------- */
.upload-grid { display: grid; grid-template-columns: 1.25fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .upload-grid { grid-template-columns: 1fr; } }
.drop { border: 2px dashed #cfc9bf; border-radius: var(--radius); padding: 36px 20px; text-align: center; background: #fbfaf8; cursor: pointer; transition: border-color .15s, background .15s; position: relative; }
.drop.over { border-color: var(--red); background: var(--red-soft); }
.drop svg.big { width: 56px; height: 56px; color: var(--red); }
.drop .preview { max-width: 100%; max-height: 360px; border-radius: 10px; display: block; margin: 0 auto; box-shadow: var(--shadow); }
.drop .pdf { display: inline-flex; gap: 10px; align-items: center; padding: 14px 20px; background: #fff; border: 1px solid var(--line); border-radius: 12px; }
.tips li { margin-bottom: 6px; }
.progress-steps { list-style: none; padding: 0; margin: 16px 0 0; }
.progress-steps li { display: flex; gap: 12px; align-items: center; padding: 10px 0; color: var(--muted); transition: color .3s; }
.progress-steps li .dot { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; display: grid; place-items: center; }
.progress-steps li.on { color: var(--ink); font-weight: 600; }
.progress-steps li.on .dot { border-color: var(--red); border-top-color: transparent; animation: spin .8s linear infinite; }
.progress-steps li.done { color: var(--green); }
.progress-steps li.done .dot { border-color: var(--green); background: var(--green); }
.progress-steps li.done .dot::after { content: '✓'; color: #fff; font-size: .75rem; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- result ---------- */
.alert { border-radius: var(--radius); padding: 16px 20px; display: flex; gap: 14px; align-items: flex-start; margin-bottom: 16px; }
.alert svg { width: 26px; height: 26px; flex: none; }
.alert.emergency { background: #b42328; color: #fff; animation: pulseBg 2s infinite; }
.alert.emergency a { color: #fff; font-weight: 700; }
.alert.urgent { background: var(--red-soft); color: #8d1b1f; border: 1px solid #f5c2c3; }
.alert.info { background: var(--blue-soft); color: #1d4a9a; }
.alert.warn { background: var(--amber-soft); color: #7a4d00; }
@keyframes pulseBg { 50% { background: #8f1a1e; } }
.summary-card { display: grid; grid-template-columns: 1fr auto; gap: 20px; }
.summary-card .thumb { width: 180px; height: 130px; border-radius: 12px; object-fit: cover; border: 1px solid var(--line); cursor: zoom-in; background: #f2f0ec; display: grid; place-items: center; overflow: hidden; }
.summary-card .thumb img { width: 100%; height: 100%; object-fit: cover; }
@media (max-width: 640px) { .summary-card { grid-template-columns: 1fr; } .summary-card .thumb { width: 100%; height: 160px; } }
.big-status { display: flex; align-items: center; gap: 14px; margin-bottom: 10px; }
.big-status .ring { width: 58px; height: 58px; border-radius: 50%; display: grid; place-items: center; flex: none; }
.big-status .ring svg { width: 30px; height: 30px; }
.ring.normal { background: var(--green-soft); color: var(--green); }
.ring.borderline { background: var(--amber-soft); color: var(--amber); }
.ring.abnormal { background: var(--red-soft); color: var(--red); }
.ring.unreadable { background: var(--blue-soft); color: var(--blue); }
.big-status h1 { font-size: 1.6rem; margin: 0; }
.riskbar { display: flex; gap: 4px; margin: 8px 0 4px; max-width: 360px; }
.riskbar i { flex: 1; height: 8px; border-radius: 4px; background: var(--line-2); }
.riskbar.l1 i:nth-child(-n+1) { background: var(--green); }
.riskbar.l2 i:nth-child(-n+2) { background: #d9a21b; }
.riskbar.l3 i:nth-child(-n+3) { background: var(--red); }
.riskbar.l4 i:nth-child(-n+4) { background: #8d1b1f; }

.metrics { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.metric { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; background: #fff; }
.metric .k { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.metric .v { font-family: var(--mono); font-size: 1.5rem; font-weight: 500; line-height: 1.2; margin: 2px 0; }
.metric .v small { font-size: .75rem; color: var(--muted); margin-left: 3px; }
.metric .r { font-size: .75rem; color: var(--muted); }
.metric.flag-high, .metric.flag-low { border-color: #f1c9a0; background: #fffaf2; }
.metric .flag { font-size: .72rem; font-weight: 700; color: var(--amber); }

.finding { padding: 14px 0; border-top: 1px solid var(--line-2); }
.finding:first-child { border-top: 0; padding-top: 0; }
.finding .row { justify-content: space-between; }
.conf { font-size: .75rem; color: var(--muted); }

.risk { border: 1px solid var(--line); border-radius: 14px; padding: 16px; background: #fff; display: flex; flex-direction: column; gap: 8px; }
.risk .lk { display: flex; gap: 4px; }
.risk .lk i { width: 22px; height: 6px; border-radius: 3px; background: var(--line-2); }
.risk.low .lk i:nth-child(1), .risk.moderate .lk i:nth-child(-n+2), .risk.high .lk i { background: currentColor; }
.risk.low .lk { color: var(--green); } .risk.moderate .lk { color: #d9a21b; } .risk.high .lk { color: var(--red); }
.risk h3 { margin: 0; }
.risk .en { font-size: .8rem; color: var(--muted); }

/* ---------- 3D story ---------- */
.story { background: var(--dark); color: #e8edf2; border-radius: 20px; overflow: hidden; border: 1px solid #1d2832; box-shadow: var(--shadow); }
.story-tabs { display: flex; gap: 6px; padding: 12px; overflow-x: auto; scrollbar-width: none; border-bottom: 1px solid #1d2832; }
.story-tabs button { flex: none; border: 1px solid #26323e; background: #121a22; color: #b9c4cf; border-radius: 999px; padding: 7px 14px; cursor: pointer; font-size: .88rem; display: flex; gap: 8px; align-items: center; }
.story-tabs button i { width: 8px; height: 8px; border-radius: 50%; }
.story-tabs button.on { background: #e8edf2; color: var(--dark); border-color: #e8edf2; }
.story-body { display: grid; grid-template-columns: 1.45fr 1fr; }
@media (max-width: 860px) { .story-body { grid-template-columns: 1fr; } }
.stage { position: relative; height: clamp(340px, 52vh, 520px); background: radial-gradient(ellipse at 50% 40%, #1c2833 0%, #0c1218 70%); touch-action: none; }
.stage canvas { cursor: grab; }
.stage .hint { position: absolute; left: 12px; bottom: 10px; font-size: .72rem; color: #6f7f8e; pointer-events: none; }
.stage .xray { position: absolute; right: 10px; top: 10px; }
.hl-labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.hl-label { position: absolute; left: 0; top: 0; background: rgba(12,18,24,.82); border: 1px solid rgba(255,255,255,.18); color: #fff; padding: 3px 9px; border-radius: 8px; font-size: .75rem; white-space: nowrap; transition: opacity .2s; }
.hl-label::after { content: ''; position: absolute; left: 50%; bottom: -6px; width: 1px; height: 6px; background: rgba(255,255,255,.4); }
.ecg-wrap { position: relative; border-top: 1px solid #1d2832; }
.ecg-wrap canvas { width: 100%; height: 96px; display: block; }
.ecg-wrap .tag { position: absolute; left: 10px; top: 6px; font-family: var(--mono); font-size: .68rem; color: #4dffb0; opacity: .75; }
.narr { padding: 20px 22px; display: flex; flex-direction: column; gap: 12px; border-left: 1px solid #1d2832; }
@media (max-width: 860px) { .narr { border-left: 0; border-top: 1px solid #1d2832; } }
.narr .stepno { font-family: var(--mono); font-size: .75rem; color: #7f8e9c; }
.narr h3 { color: #fff; font-size: 1.2rem; margin: 0; }
.narr p { color: #c3ccd5; margin: 0; min-height: 7.5em; }
.narr .ctrl { display: flex; gap: 8px; align-items: center; margin-top: auto; }
.narr .btn { background: #18222c; color: #e8edf2; border-color: #26323e; }
.narr .btn:hover { background: #22303c; }
.narr .btn.primary { background: #e8edf2; color: var(--dark); }
.dots { display: flex; gap: 6px; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: #2a3642; cursor: pointer; }
.dots i.on { background: #e8edf2; width: 22px; border-radius: 4px; }
.narr .beh { border-top: 1px solid #1d2832; padding-top: 12px; }
.narr .beh .chip { background: #241c0c; border-color: #4a3712; color: #f1c76b; }
.webgl-fallback { padding: 40px; text-align: center; color: #9aa7b3; }

/* ---------- charts ---------- */
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.chart { position: relative; }
.chart h4 { font-size: .9rem; margin-bottom: 2px; }
.chart svg { width: 100%; height: 170px; display: block; overflow: visible; }
.chart .tip { position: absolute; pointer-events: none; background: var(--ink); color: #fff; font-size: .78rem; padding: 6px 10px; border-radius: 8px; transform: translate(-50%, -115%); white-space: nowrap; opacity: 0; transition: opacity .1s; }
.chart .grid line { stroke: #ece9e4; }
.chart .axis text { fill: var(--muted); font-size: 10px; font-family: var(--mono); }
.chart .band { fill: var(--green-soft); }

/* ---------- history ---------- */
.rec { display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: center; padding: 14px; border: 1px solid var(--line); border-radius: 14px; background: #fff; text-decoration: none; color: inherit; }
.rec + .rec { margin-top: 10px; }
.rec:hover { border-color: #cfc9bf; }
.rec .when { font-family: var(--mono); font-size: .8rem; color: var(--muted); }
.rec input { width: 20px; height: 20px; accent-color: var(--ink); }
.rec .vals { font-family: var(--mono); font-size: .82rem; color: var(--ink-2); }
.cmp-bar { position: sticky; bottom: 16px; z-index: 30; display: flex; justify-content: center; pointer-events: none; }
.cmp-bar .inner { pointer-events: auto; background: var(--ink); color: #fff; border-radius: 999px; padding: 8px 8px 8px 20px; display: flex; gap: 14px; align-items: center; box-shadow: 0 10px 30px rgba(0,0,0,.25); }
@media (max-width: 760px) { .cmp-bar { bottom: 86px; } }

.cmp-table { width: 100%; border-collapse: collapse; }
.cmp-table th, .cmp-table td { padding: 10px 8px; border-bottom: 1px solid var(--line-2); text-align: left; }
.cmp-table td.num { font-family: var(--mono); }
.delta.up { color: #b42328; } .delta.down { color: var(--blue); } .delta.zero { color: var(--muted); }

/* ---------- behavior ---------- */
.gauge { display: flex; align-items: center; gap: 20px; }
.gauge > svg { width: 130px; height: 80px; flex: none; }
.factor { display: grid; grid-template-columns: auto 1fr; gap: 12px; padding: 14px 0; border-top: 1px solid var(--line-2); }
.factor:first-child { border-top: 0; }
.factor .ico { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; }
.factor .ico svg { width: 18px; height: 18px; }
.factor.good .ico { background: var(--green-soft); color: var(--green); }
.factor.warn .ico { background: var(--amber-soft); color: var(--amber); }
.factor.bad .ico { background: var(--red-soft); color: var(--red); }
.factor .link { font-size: .88rem; color: var(--ink-2); margin-top: 4px; }
.factor.relevant { background: linear-gradient(90deg, #fff7e6, transparent); border-radius: 10px; padding-left: 8px; }
.factor .tag { font-size: .7rem; background: var(--ink); color: #fff; border-radius: 6px; padding: 1px 6px; margin-left: 6px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 16px; }
fieldset { border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px 4px; margin: 0 0 16px; }
legend { font-weight: 700; padding: 0 6px; }
input[type=range] { width: 100%; accent-color: var(--ink); }

/* ---------- learn ---------- */
.learn-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; margin-top: 16px; }
.learn-card { text-align: left; border: 1px solid var(--line); background: #fff; border-radius: 14px; padding: 14px 16px; cursor: pointer; }
.learn-card i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; }
.learn-card.on { border-color: var(--ink); box-shadow: 0 0 0 2px var(--ink); }

/* ---------- misc ---------- */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; visibility: hidden; transition: all .25s; background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 12px; z-index: 100; transition: transform .25s; max-width: calc(100vw - 32px); }
.toast.show { transform: translate(-50%, 0); opacity: 1; visibility: visible; }
.toast.err { background: #b42328; }
@media (max-width: 760px) { .toast { bottom: 90px; } }
.loading-page { display: grid; place-items: center; min-height: 50vh; }
.pulse-line { width: 160px; height: 40px; background: linear-gradient(90deg, transparent, var(--red), transparent); mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 40'%3E%3Cpath d='M0 20h50l8-14 10 28 8-14h84' stroke='black' stroke-width='3' fill='none'/%3E%3C/svg%3E"); background-size: 200% 100%; animation: sweep 1.2s linear infinite; }
@keyframes sweep { from { background-position: 100% 0; } to { background-position: -100% 0; } }
.disclaimer { font-size: .82rem; color: var(--muted); border-top: 1px solid var(--line); padding-top: 14px; margin-top: 24px; }
.lightbox { position: fixed; inset: 0; background: rgba(5,8,12,.92); z-index: 200; display: grid; place-items: center; padding: 20px; cursor: zoom-out; }
.lightbox img, .lightbox iframe { max-width: 100%; max-height: 100%; border-radius: 8px; background: #fff; }
.lightbox iframe { width: min(1000px, 100%); height: 90vh; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 28px 0 12px; }
.section-title h2 { margin: 0; }
ol.clean, ul.clean { margin: 0; padding-left: 1.2em; }
ol.clean li, ul.clean li { margin-bottom: 6px; }
.doc-note { background: var(--green-soft); border: 1px solid #bfe5d0; border-radius: 14px; padding: 16px; }
.doc-note.revised { background: var(--blue-soft); border-color: #c8d9f6; }

@media print {
  .topbar, .bottomnav, .story, .no-print, .toast, .cmp-bar { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; break-inside: avoid; }
  main { max-width: none; padding: 0; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
