:root {
  --navy: #0b1726;
  --navy-2: #13243a;
  --ink: #162638;
  --muted: #657386;
  --paper: #f2f5f7;
  --surface: #ffffff;
  --line: #d8e0e7;
  --line-dark: #aebac7;
  --cyan: #11a5a8;
  --cyan-soft: #dff4f3;
  --red: #d94a4a;
  --amber: #e6a23c;
  --wake: #d59055;
  --n1: #9bcbd0;
  --n2: #4b93ad;
  --n3: #234f79;
  --rem: #d94a4a;
  --shadow: 0 12px 34px rgba(11, 23, 38, .08);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--paper); }
body { margin: 0; min-height: 100vh; color: var(--ink); background: var(--paper); font: 16px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
button { color: inherit; font: inherit; }
button:focus-visible, canvas:focus-visible { outline: 3px solid #f4bd52; outline-offset: 3px; }

.topbar { min-height: 92px; display: flex; align-items: center; justify-content: space-between; gap: 2rem; padding: 1.2rem clamp(1rem, 3vw, 2.8rem); color: white; background: var(--navy); border-bottom: 3px solid var(--cyan); }
.topbar h1 { margin: .08rem 0 0; font-size: clamp(1.4rem, 2.3vw, 2rem); font-weight: 650; letter-spacing: -.025em; }
.institute-logo { display: block; width: clamp(250px, 32vw, 440px); max-height: 60px; object-fit: contain; object-position: right center; }
.kicker, .section-label { margin: 0; color: var(--cyan); font-size: .76rem; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }
.workspace { display: grid; grid-template-columns: minmax(210px, .72fr) minmax(540px, 2.35fr) minmax(250px, .9fr); gap: 1rem; width: min(1800px, 100%); margin: 0 auto; padding: 1rem; min-height: calc(100vh - 92px); }
.panel-shell { min-width: 0; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); border-radius: 8px; }
.patient-rail, .metrics-rail { align-self: stretch; padding: 1rem; }
.panel-heading, .chart-header, .subheading-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.panel-heading { padding-bottom: .9rem; border-bottom: 1px solid var(--line); }
.panel-heading h2, .chart-header h2 { margin: .18rem 0 0; font-size: 1.05rem; font-weight: 700; }
.count-badge { min-width: 1.8rem; padding: .18rem .45rem; text-align: center; color: var(--muted); background: #edf1f4; border-radius: 99px; font-size: .8rem; font-weight: 750; }

.patient-list { display: grid; gap: .45rem; margin: .9rem 0 1.2rem; }
.patient-button { width: 100%; display: grid; grid-template-columns: 2.25rem 1fr; align-items: center; gap: .65rem; padding: .62rem; text-align: left; border: 1px solid transparent; border-radius: 6px; background: transparent; cursor: pointer; transition: background .15s, border-color .15s; }
.patient-button:hover { background: #f2f6f8; }
.patient-button.active { background: var(--cyan-soft); border-color: #97d7d5; }
.patient-avatar { width: 2.25rem; height: 2.25rem; display: grid; place-items: center; color: #fff; background: var(--navy-2); border-radius: 50%; font-size: .8rem; font-weight: 800; }
.patient-button.active .patient-avatar { background: var(--cyan); }
.patient-button strong, .patient-button small { display: block; }
.patient-button strong { font-size: .88rem; }
.patient-button small { color: var(--muted); font-size: .74rem; }
.patient-profile { padding-top: .9rem; border-top: 1px solid var(--line); }
.patient-profile h2 { margin: 0 0 .8rem; font-size: 1.2rem; }
.profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem .5rem; margin: 0; }
.profile-grid div { min-width: 0; }
.profile-grid dt { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; }
.profile-grid dd { margin: .12rem 0 0; font-size: .9rem; font-weight: 700; }

.investigation-column { min-width: 0; display: grid; grid-template-rows: minmax(310px, .9fr) minmax(310px, 1.1fr); gap: 1rem; }
.chart-card { min-height: 0; display: flex; flex-direction: column; padding: 1rem; }
.chart-header { margin-bottom: .7rem; }
.segmented-control { display: inline-flex; padding: 3px; background: #edf1f4; border-radius: 6px; }
.view-button { padding: .38rem .66rem; border: 0; border-radius: 4px; color: var(--muted); background: transparent; cursor: pointer; font-size: .78rem; font-weight: 750; }
.view-button.active { color: white; background: var(--navy-2); }
.canvas-wrap { position: relative; min-height: 0; flex: 1; }
.canvas-wrap canvas { width: 100%; height: 100%; display: block; }
.hypnogram-wrap { min-height: 240px; overflow-x: auto; overflow-y: hidden; }
.hypnogram-wrap canvas { position: absolute; inset: 0 auto 0 0; max-width: none; }
.overview-wrap { min-height: 245px; }
.marker-layer { position: absolute; inset: 0; pointer-events: none; }
.epoch-marker { position: absolute; top: 8px; width: 13px; height: 13px; padding: 0; transform: translateX(-50%); pointer-events: auto; border: 2px solid white; border-radius: 2px; background: var(--red); box-shadow: 0 1px 5px #5c111177; cursor: pointer; }
.epoch-marker:hover, .epoch-marker:focus-visible { z-index: 2; transform: translateX(-50%) scale(1.22); }
.marker-tooltip { position: absolute; z-index: 2; pointer-events: none; transform: translate(-50%, calc(-100% - 10px)); padding: .42rem .58rem; color: white; background: var(--navy); border-radius: 4px; white-space: nowrap; font-size: .76rem; box-shadow: 0 4px 14px #0003; }
.marker-tooltip::after { content: ''; position: absolute; left: calc(50% - 5px); bottom: -5px; width: 10px; height: 10px; background: var(--navy); transform: rotate(45deg); }

.score-comparison { display: grid; grid-template-columns: 1fr 1px 1fr; gap: .7rem; margin: 1rem 0; padding: .9rem .3rem; background: #f5f8fa; border-radius: 6px; }
.score-divider { background: var(--line); }
.score-block { min-width: 0; text-align: center; }
.score-block span, .score-block small, .score-block strong { display: block; }
.score-block span { color: var(--muted); font-size: .7rem; }
.score-block strong { margin: .15rem 0; font-size: 1.75rem; line-height: 1; font-variant-numeric: tabular-nums; }
.score-block small { color: var(--muted); font-size: .68rem; text-transform: uppercase; letter-spacing: .07em; }
.patient-score strong { color: var(--red); }.reference-score strong { color: var(--cyan); }
.metric-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.metric-card { padding: .62rem; border: 1px solid var(--line); border-radius: 5px; }
.metric-card strong, .metric-card span { display: block; }
.metric-card strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.metric-card span { color: var(--muted); font-size: .7rem; }
.stage-performance-section, .confusion-section { margin-top: 1.2rem; }
.subheading-row h3 { margin: 0; font-size: .9rem; }
.subheading-row span, .axis-caption { color: var(--muted); font-size: .68rem; }
.stage-performance { display: grid; gap: .28rem; margin-top: .55rem; }
.stage-result { display: grid; grid-template-columns: 2.3rem 1fr auto; align-items: center; gap: .45rem; font-size: .7rem; }
.stage-result > span:first-child { font-weight: 750; }
.stage-result > span:last-child { min-width: 5.4rem; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.stage-result-track { height: .38rem; overflow: hidden; background: #e6ebef; border-radius: 99px; }
.stage-result-fill { height: 100%; border-radius: inherit; }
.confusion-matrix { display: grid; grid-template-columns: 1.2rem 2.4rem repeat(5, 1fr); gap: 2px; margin-top: .55rem; font-size: .62rem; }
.matrix-cell { aspect-ratio: 1; display: grid; place-items: center; min-width: 0; border-radius: 2px; font-variant-numeric: tabular-nums; }
.matrix-label { color: var(--muted); font-weight: 750; }
.matrix-value { color: var(--ink); background: rgba(17, 165, 168, var(--intensity)); font-weight: 750; }
.matrix-axis-corner { grid-column: 1 / 3; grid-row: 1 / 3; }
.matrix-axis-title { display: grid; place-items: center; color: var(--muted); font-size: .62rem; font-weight: 750; letter-spacing: .02em; }
.matrix-prediction-axis { grid-column: 3 / 8; grid-row: 1; }
.matrix-expert-axis { grid-column: 1; grid-row: 3 / 8; writing-mode: vertical-rl; transform: rotate(180deg); }

.loading-screen, .error-screen { position: fixed; z-index: 20; inset: 92px 0 0; display: grid; place-content: center; justify-items: center; gap: .8rem; background: rgba(242, 245, 247, .94); color: var(--muted); }
.loading-mark { width: 2.1rem; height: 2.1rem; border: 3px solid #cbd7df; border-top-color: var(--cyan); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-screen { color: var(--ink); }
.error-screen button { padding: .5rem .9rem; border: 0; border-radius: 4px; color: white; background: var(--navy); cursor: pointer; }
[hidden] { display: none !important; }

.epoch-dialog { width: calc(100vw - 1rem); max-width: none; height: calc(100vh - 1rem); max-height: none; padding: 0; border: 0; border-radius: 9px; color: var(--ink); background: var(--surface); box-shadow: 0 28px 90px #0008; }
.epoch-dialog::backdrop { background: rgba(5, 13, 23, .78); backdrop-filter: blur(3px); }
.dialog-shell { height: 100%; display: flex; flex-direction: column; padding: 1.2rem 1.35rem 1.35rem; }
.dialog-header { display: flex; justify-content: space-between; align-items: start; padding-bottom: .75rem; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: .18rem 0 0; font-size: 1.35rem; }
.close-button { width: 2.4rem; height: 2.4rem; display: grid; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 50%; color: var(--muted); background: white; cursor: pointer; font-size: 1.65rem; line-height: 1; }
.epoch-summary { display: grid; grid-template-columns: 160px 160px 1fr; gap: .7rem; align-items: stretch; margin: .8rem 0; }
.epoch-summary > div { padding: .62rem .75rem; background: #f4f7f8; border-radius: 5px; }
.epoch-summary span, .epoch-summary strong { display: block; }
.epoch-summary span { color: var(--muted); font-size: .7rem; }
.epoch-summary strong { margin-top: .18rem; font-size: 1rem; }
.probability-summary { min-width: 0; }
#epoch-probabilities { display: grid; grid-template-columns: repeat(5, 1fr); gap: .45rem; margin-top: .35rem; }
.probability-item { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: .35rem; min-width: 0; font-size: .7rem; }
.probability-track { height: .35rem; overflow: hidden; background: #dce4e8; border-radius: 99px; }
.probability-fill { height: 100%; background: var(--cyan); }
.epoch-toolbar { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin: 0 0 .5rem; }
.epoch-legend { display: flex; gap: 1.1rem; align-items: center; color: var(--muted); font-size: .7rem; }
.epoch-legend span { width: 1rem; height: .65rem; display: inline-block; margin-right: -.8rem; border: 1px solid var(--line); }
.context-swatch { background: #eef1f3; }.target-swatch { background: white; }.saliency-swatch { background: rgba(230, 162, 60, .75); }
.explanation-picker { display: flex; align-items: center; gap: .55rem; color: var(--muted); font-size: .7rem; font-weight: 700; }
.explanation-picker select { min-width: 250px; padding: .4rem 2rem .4rem .55rem; border: 1px solid var(--line-dark); border-radius: 4px; color: var(--ink); background: white; font: inherit; }
.epoch-plots { min-height: 0; flex: 1; display: grid; grid-template-rows: 1fr 1fr; gap: .75rem; }
.epoch-plots section { min-height: 0; display: flex; flex-direction: column; }
.epoch-plots h3 { margin: 0 0 .3rem; font-size: .8rem; }
.epoch-plots canvas { width: 100%; min-height: 150px; flex: 1; display: block; border: 1px solid var(--line); border-radius: 4px; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 210px minmax(500px, 1fr); }
  .metrics-rail { grid-column: 1 / -1; display: grid; grid-template-columns: .7fr 1fr 1fr 1.4fr; gap: 1rem; align-items: start; }
  .metrics-rail .panel-heading { border: 0; }
  .stage-performance-section, .confusion-section { margin-top: 0; }
}

@media (max-width: 760px) {
  .topbar { gap: 1rem; }
  .institute-logo { width: min(52vw, 320px); }
  .workspace { display: block; padding: .65rem; }
  .patient-rail, .investigation-column, .metrics-rail { margin-bottom: .65rem; }
  .patient-list { grid-template-columns: repeat(5, minmax(120px, 1fr)); overflow-x: auto; }
  .patient-profile { display: none; }
  .investigation-column { display: block; }
  .chart-card { min-height: 340px; margin-bottom: .65rem; }
  .metrics-rail { display: block; }
  .epoch-summary { grid-template-columns: 1fr 1fr; }
  .probability-summary { grid-column: 1 / -1; }
  #epoch-probabilities { grid-template-columns: 1fr; }
  .epoch-toolbar, .epoch-legend { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
