/* Web viewer for shared reports (PRD §7.9, design p23/p24).
   Tokens: PRD §10. No inline styles (CSP style-src 'self'); bar sizes are set
   from JS through the CSSOM, which the CSP allows. */

/* ---- Fonts (self-hosted, CE-11) ------------------------------------------ */
@font-face { font-family: 'Manrope'; src: url('../fonts/Manrope-Regular.woff2') format('woff2'); font-weight: 400; font-display: swap; }
@font-face { font-family: 'Manrope'; src: url('../fonts/Manrope-Medium.woff2') format('woff2'); font-weight: 500; font-display: swap; }
@font-face { font-family: 'Manrope'; src: url('../fonts/Manrope-SemiBold.woff2') format('woff2'); font-weight: 600; font-display: swap; }
@font-face { font-family: 'Manrope'; src: url('../fonts/Manrope-Bold.woff2') format('woff2'); font-weight: 700; font-display: swap; }
@font-face { font-family: 'Fraunces'; src: url('../fonts/Fraunces-SemiBold.woff2') format('woff2'); font-weight: 600; font-display: swap; }
/* CJK faces are only downloaded when a CJK glyph is rendered. */
@font-face { font-family: 'Noto Sans JP'; src: url('../fonts/NotoSansJP-Regular.woff2') format('woff2'); font-weight: 100 550; font-display: swap; unicode-range: U+3000-30FF, U+3400-4DBF, U+4E00-9FFF, U+F900-FAFF, U+FF00-FFEF; }
@font-face { font-family: 'Noto Sans JP'; src: url('../fonts/NotoSansJP-Bold.woff2') format('woff2'); font-weight: 551 900; font-display: swap; unicode-range: U+3000-30FF, U+3400-4DBF, U+4E00-9FFF, U+F900-FAFF, U+FF00-FFEF; }
@font-face { font-family: 'Noto Sans SC'; src: url('../fonts/NotoSansSC-Regular.woff2') format('woff2'); font-weight: 100 550; font-display: swap; unicode-range: U+3000-303F, U+3400-4DBF, U+4E00-9FFF, U+F900-FAFF, U+FF00-FFEF; }
@font-face { font-family: 'Noto Sans SC'; src: url('../fonts/NotoSansSC-Bold.woff2') format('woff2'); font-weight: 551 900; font-display: swap; unicode-range: U+3000-303F, U+3400-4DBF, U+4E00-9FFF, U+F900-FAFF, U+FF00-FFEF; }

/* ---- Tokens (PRD §10) ----------------------------------------------------- */
:root {
  color-scheme: light dark;
  --bg: #F4F0E8;
  --surface: #FFFFFF;
  --border: #E3DCD0;
  --rule: #ECE6DC;
  --text: #1F1B17;
  --text-2: #6B6259;
  --text-3: #8A8078;
  --accent: #4E3A6E;
  --accent-text: #4E3A6E;
  --on-accent: #FFFFFF;
  --accent-tint: #E9E3F1;
  --warning-tint: #F5EEDC;
  --warning-text: #7A5310;
  --alert: #A22020;
  --alert-bg: #FBEDEC;
  --alert-row: #FBEDEC;
  --track: #EFEBE2;
  --mild: #2F7A5B;
  --moderate: #B8781A;
  --strong: #C4552B;
  --severe: #8E2A2A;
  --on-moderate: #1F1B17;
  --quit: #2F7A5B;
  --relieved: #D9A14A;
  --none: #8E2A2A;
  --worse: #5A2A6E;
  --radius-card: 14px;
  --radius-chip: 20px;
  --font-body: 'Manrope', 'Noto Sans JP', 'Noto Sans SC', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-display: 'Fraunces', Georgia, 'Noto Sans JP', 'Noto Sans SC', serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171411;
    --surface: #221E1A;
    --border: #3A332C;
    --rule: #2E2925;
    --text: #EEE7DD;
    --text-2: #B5AA9D;
    --text-3: #7D7469;
    --accent: #6E58A0;
    --accent-text: #C9B8EC;
    --accent-tint: #2C2535;
    --warning-tint: #3A3020;
    --warning-text: #E4C58A;
    --alert: #E07A72;
    --alert-bg: #3A2222;
    --alert-row: #33201F;
    --track: #2E2925;
  }
}

:lang(zh) { --font-body: 'Manrope', 'Noto Sans SC', 'Noto Sans JP', system-ui, sans-serif; --font-display: 'Fraunces', 'Noto Sans SC', 'Noto Sans JP', serif; }
:lang(ja), :lang(zh) { line-height: 1.7; }
:lang(ja) .report-title, :lang(zh) .report-title, :lang(ja) .state-title, :lang(zh) .state-title { font-family: var(--font-body); font-weight: 700; }

/* ---- Base ----------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 14px/1.45 var(--font-body);
  -webkit-font-smoothing: antialiased;
}
h1, h2, p, dl, dd, ul { margin: 0; }
ul { padding: 0; list-style: none; }
button { font: inherit; color: inherit; }
.muted { color: var(--text-2); }
.icon { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---- Top bar -------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 8px 30px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
/* Brand symbol (public/brand/, light/dark via <picture> + prefers-color-scheme). */
.app-mark { display: flex; flex: none; }
.app-mark img { display: block; width: 28px; height: 28px; }
.app-name { font-weight: 600; font-size: 15px; white-space: nowrap; }
.topbar-sub { color: var(--text-2); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; justify-content: flex-end; }
.pill-available {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 999px;
  background: var(--warning-tint); color: var(--warning-text);
  font-size: 12.5px; font-weight: 600; white-space: nowrap;
}
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 36px; padding: 7px 16px; border-radius: 10px;
  font-size: 13.5px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn-outline { background: var(--surface); border: 1px solid var(--border); }
.btn-primary { background: var(--accent); color: var(--on-accent); border: 1px solid var(--accent); }
.btn:focus-visible, .pill:focus-visible { outline: 3px solid var(--accent-text); outline-offset: 2px; }

/* ---- Page ----------------------------------------------------------------- */
.page { max-width: 900px; margin: 0 auto; padding: 32px 30px 40px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); padding: 18px 20px; }
.section-title { font-size: 11px; font-weight: 700; letter-spacing: 0.8px; text-transform: uppercase; color: var(--text-2); margin-bottom: 14px; }

.report-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 20px; }
.report-head-main { flex: 1 1 440px; min-width: 0; }
.kicker { color: var(--accent-text); font-size: 11px; font-weight: 700; letter-spacing: 0.8px; text-transform: uppercase; margin-bottom: 8px; }
.report-title { font: 600 31px/1.08 var(--font-display); letter-spacing: -0.3px; max-width: 480px; }
.report-sub { color: var(--text-2); font-size: 13px; margin-top: 10px; }
.patient-card { flex: 0 1 370px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; padding: 16px; }
.patient-field dt { color: var(--text-2); font-size: 12.5px; }
.patient-field dd { font-size: 12.5px; font-weight: 600; overflow-wrap: anywhere; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi { padding: 16px 16px 14px; min-height: 100px; }
.kpi-value { font: 600 34px/1.05 var(--font-display); letter-spacing: -0.3px; margin-bottom: 8px; }
.kpi-suffix { font-size: 17px; color: var(--text-2); }
.kpi-label { font-size: 13.5px; font-weight: 600; }
.kpi-note { font-size: 12px; color: var(--text-2); margin-top: 3px; }
.kpi-alert { background: var(--alert-bg); border: 2px solid var(--alert); padding: 15px 15px 13px; }
.kpi-alert .kpi-value, .kpi-note-alert { color: var(--alert); }
.kpi-note-alert { font-weight: 700; font-size: 11.5px; }

.row { display: grid; gap: 12px; margin-bottom: 12px; }
.row-main { grid-template-columns: minmax(0, 2.05fr) minmax(0, 1fr); align-items: start; }
.row-patterns { grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* Monthly chart */
.chart { display: flex; align-items: flex-end; gap: 10px; height: 170px; padding: 0 0 0; border-bottom: 1px solid var(--text); margin-bottom: 14px; }
.bar-slot { flex: 1; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 6px; }
.bar-value { font-size: 12.5px; font-weight: 700; }
.bar-value.is-alert { color: var(--alert); }
.bar { width: 100%; min-height: 2px; background: var(--accent); border-radius: 3px 3px 0 0; }
.bar-alert { background: var(--alert); }
.bar-partial { background: repeating-linear-gradient(135deg, var(--accent) 0 8px, #7A6A99 8px 16px); }
.bar-alert.bar-partial { background: repeating-linear-gradient(135deg, var(--alert) 0 8px, #C66 8px 16px); }
.legend { color: var(--text-2); font-size: 11.5px; margin-top: 10px; }

/* Tables */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.table th, .table td { text-align: left; vertical-align: top; padding: 8px 5px; }
.table thead th { font-weight: 600; border-bottom: 1px solid var(--text); white-space: nowrap; vertical-align: bottom; }
.table tbody th { font-weight: 500; }
.table tbody tr { border-bottom: 1px solid var(--rule); }
.table .num { text-align: right; }
.months-table tbody th, .months-table td { padding-top: 9px; padding-bottom: 9px; }

.row-alert { background: var(--alert-row); color: var(--alert); }
.row-alert th { font-weight: 700; }
.row-alert td:nth-last-child(-n+2) { font-weight: 700; }
.months-table th:first-child { padding-left: 0; }
.months-table .num:last-child { padding-right: 0; }
.row-alert th:first-child { padding-left: 6px; }

/* Medications */
.med { padding: 2px 0 14px; border-bottom: 1px solid var(--rule); margin-bottom: 14px; }
.med:last-child { margin-bottom: 0; }
.med-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 7px; }
.med-name { font-size: 13.5px; font-weight: 700; }
.med-doses { color: var(--text-2); font-size: 13px; white-space: nowrap; }
.stack { display: flex; height: 9px; border-radius: 3px; overflow: hidden; background: var(--track); margin-bottom: 8px; }
.seg { display: block; height: 100%; }
.seg-quit { background: var(--quit); }
.seg-relieved { background: var(--relieved); }
.seg-none { background: var(--none); }
.seg-worse { background: var(--worse); }
.med-stats { color: var(--text-2); font-size: 11.5px; }
.preventive { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.preventive-title { margin: 0 0 6px; font-size: 11px; font-weight: 700; letter-spacing: 0.8px; text-transform: uppercase; color: var(--text-2); }
.preventive-list { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; font-size: 13px; }
.preventive-list .med-name { display: inline; }
.preventive-period { color: var(--text-2); }
.med-effects { font-size: 12px; margin-top: 6px; }

/* Pattern bars */
.hbars { display: grid; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(40px, 1fr) 22px; align-items: center; gap: 10px; font-size: 12.5px; }
.hbar-label { overflow-wrap: anywhere; }
.hbar-track { height: 6px; border-radius: 3px; background: var(--track); overflow: hidden; }
.hbar-fill { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.hbar-count { text-align: right; font-weight: 600; }

/* Notes */
.notes-card { margin-bottom: 12px; }
.notes { display: grid; gap: 8px; font-size: 13px; }
.notes li { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 12px; }
.note-date { color: var(--text-2); }
.note-text { white-space: pre-wrap; overflow-wrap: anywhere; }

/* Episodes */
.episodes-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.episodes-head .section-title { margin-bottom: 0; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.pill {
  min-height: 32px; padding: 5px 13px; border-radius: var(--radius-chip);
  border: 1px solid var(--border); background: var(--surface);
  font-size: 12.5px; font-weight: 500; cursor: pointer;
}
.pill[aria-pressed='true'] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.episodes-table { min-width: 720px; }
.episodes-table th:first-child, .episodes-table td:first-child { padding-left: 0; }
.ep-date { font-weight: 700 !important; white-space: nowrap; }
.ep-schedule { min-width: 150px; }
.badge {
  display: inline-grid; place-items: center; min-width: 21px; height: 21px; padding: 0 4px;
  border-radius: 5px; color: #FFF; font-weight: 700; font-size: 12px;
}
.sev-mild { background: var(--mild); }
.sev-moderate { background: var(--moderate); color: var(--on-moderate); }
.sev-strong { background: var(--strong); }
.sev-severe { background: var(--severe); }
.no-match { padding: 14px 0 4px; }

.report-foot { color: var(--text-2); font-size: 12px; padding: 18px 2px 0; display: grid; gap: 4px; }
.foot-strong { color: var(--text); font-weight: 600; }

/* ---- States (expired / damaged / landing) --------------------------------- */
.state { min-height: calc(100vh - 57px); display: grid; place-items: center; padding: 40px 20px; }
.state-card {
  width: min(450px, 100%); text-align: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 32px 30px 34px;
}
.state-icon {
  width: 48px; height: 48px; margin: 0 auto 14px; border-radius: 50%;
  display: grid; place-items: center; background: var(--warning-tint); color: #8A5A10;
}
.state-icon .icon { width: 22px; height: 22px; }
.state-error .state-icon { background: var(--alert-bg); color: var(--alert); }
.state-info .state-icon { background: var(--accent-tint); color: var(--accent-text); }
.state-title { font: 600 23px/1.2 var(--font-display); margin-bottom: 12px; }
.state-body { color: var(--text-2); font-size: 14.5px; line-height: 1.5; }
.state-note { color: var(--text-2); font-size: 12px; line-height: 1.5; margin-top: 12px; }
.loading { color: var(--text-2); }

/* ---- Narrow screens ------------------------------------------------------- */
@media screen and (max-width: 760px) {
  .topbar { padding: 8px 16px; flex-wrap: wrap; }
  .topbar-actions { flex-wrap: wrap; flex: 1 1 100%; }
  .topbar-sub { display: none; }
  .topbar-actions { width: 100%; justify-content: flex-start; }
  .page { padding: 22px 16px 32px; }
  .report-head { flex-direction: column; align-items: stretch; }
  .report-head-main, .patient-card { flex-basis: auto; }
  .report-title { font-size: 27px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .row-main, .row-patterns { grid-template-columns: minmax(0, 1fr); }
  .months-table thead th { white-space: normal; }
  .notes li { grid-template-columns: 1fr; gap: 0; }
}
@media screen and (max-width: 420px) {
  .patient-card { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 28px; }
  .btn, .pill-available { flex: 1 1 auto; justify-content: center; }
}

/* ---- Print / "Descargar PDF" (CE-7, §8 letter pages) ---------------------- */
@page { size: letter; margin: 12mm 12mm 15mm; }
/* ---- Head map (CE-5, MC-9, design h03) ------------------------------------ */
.location-card { display: grid; grid-template-columns: minmax(0, 1fr) minmax(220px, 280px); gap: 24px; margin-bottom: 16px; }
.side-block .section-title + .side-rows { margin-top: 4px; }
.side-block .legend + .section-title { margin-top: 18px; }
.location-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 16px; }
.zone-hint { font-size: 13px; }
.head-views { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin: 10px 0 12px; }
.head-view { margin: 0; border: 1px solid var(--border); border-radius: 12px; padding: 8px 8px 6px; background: var(--surface); }
.head-frame { position: relative; }
.head-svg { display: block; width: 100%; height: auto; }
.head-view figcaption { text-align: center; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-top: 2px; }
.head-mark { position: absolute; bottom: 0; font-size: 11px; font-weight: 700; color: var(--text-2); }
.head-mark-l { left: 2px; }
.head-mark-r { right: 2px; }
.zone { stroke: #FFFFFF; stroke-width: 5; cursor: pointer; }
.zone-empty { stroke: #8F84A6; stroke-width: 4; stroke-dasharray: 12 9; fill: rgba(255, 255, 255, 0.001); }
.zone:hover { stroke: var(--text); stroke-width: 7; }
.zone.is-selected { stroke: #1F1B17; stroke-width: 10; stroke-dasharray: none; }
.zone-num { font: 700 120px Manrope, system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.location-foot { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 16px; }
.zone-detail { font-size: 15px; min-height: 1.4em; margin: 0; }
.heat-legend { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-2); margin: 0; }
.heat-legend-bar { width: 100px; height: 8px; border-radius: 4px; border: 1px solid var(--border); background: linear-gradient(90deg, #FFFFFF, #DCD4EA, #3B2A5E); }
.heat-note { margin-top: 6px; }
.zone-list { margin-top: 10px; font-size: 13px; }
.zone-list summary { cursor: pointer; color: var(--accent-text); font-weight: 600; min-height: 32px; }
.zone-list ul { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-wrap: wrap; gap: 6px; }
.zone-list .pill[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.side-block { border-left: 1px solid var(--rule); padding-left: 24px; }
.side-rows { list-style: none; padding: 0; margin: 0 0 8px; display: grid; gap: 10px; }
.side-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 14px; }
.side-value { font-weight: 700; }
.side-track { grid-column: 1 / -1; height: 10px; border-radius: 5px; background: var(--track); overflow: hidden; }
.side-fill { display: block; height: 100%; border-radius: 5px; }
.side-right { background: #3B2A5E; }
.side-both { background: repeating-linear-gradient(135deg, #7A6A99 0 6px, #A99BC4 6px 10px); }
.side-left { background: #DCD4EA; }
.side-central { background: transparent; }
@media screen and (prefers-color-scheme: dark) {
  .side-right { background: #8F7BC0; }
}
.zone-months { margin: 0; display: grid; gap: 6px; font-size: 14px; }
.zone-months div { display: flex; justify-content: space-between; gap: 12px; }
.zone-months dd { margin: 0; font-weight: 700; text-align: right; }
@media (max-width: 900px) {
  .location-card { grid-template-columns: minmax(0, 1fr); }
  .side-block { border-left: 0; padding-left: 0; }
}
@media (max-width: 560px) {
  .head-views { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media print {
  :root {
    color-scheme: light;
    --bg: #FFFFFF; --surface: #FFFFFF; --border: #E3DCD0; --rule: #DAD5CB;
    --text: #1F1B17; --text-2: #6B6259; --accent: #4E3A6E; --accent-text: #4E3A6E;
    --alert: #A22020; --alert-bg: #FBEDEC; --alert-row: #FBEDEC; --track: #EFEBE2;
  }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { background: #FFF; font-size: 10pt; }
  .topbar { display: none; }
  .page { max-width: none; padding: 0; }
  .card { border-radius: 8px; padding: 12px 14px; break-inside: avoid; }
  .report-head { margin-bottom: 12px; }
  .report-title { font-size: 22pt; max-width: none; }
  .kpis { grid-template-columns: repeat(4, 1fr); }
  /* Page 1: summary + monthly; page 2: medications + patterns (like §8.2). */
  .row-main { grid-template-columns: minmax(0, 1fr); }
  .meds-card { break-before: page; }
  .meds-card .med { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); column-gap: 18px; }
  .meds-card .med-head { grid-column: 1 / -1; }
  .months-table thead th { white-space: normal; }
  .row-patterns { grid-template-columns: repeat(3, 1fr); }
  .chart { height: 150px; }
  .table-scroll { overflow: visible; }
  .filters, .no-match { display: none; }
  /* Page 3: pain location (heat map), like the PDF (§8.2). */
  .location-card { break-before: page; grid-template-columns: minmax(0, 1fr) 270px; }
  .zone-list, .zone-hint { display: none; }
  .episodes-card { break-before: page; border: 0; padding: 0; }
  .episodes-table { min-width: 0; font-size: 9pt; }
  .episodes-table th, .episodes-table td { padding-top: 5px; padding-bottom: 5px; }
  .episodes-table thead { display: table-header-group; }
  .episodes-table tr { break-inside: avoid; }
  .report-foot { break-inside: avoid; }
}
