/* ===== Migraine Center v2 — mobile-first styles (blueprint §8 palette) ===== */
:root {
    --green: #4CAF50;
    --blue: #3d4fa8;
    --blue2: #4a2d6d;
    --blue-grad: linear-gradient(135deg, #3d4fa8, #4a2d6d);
    --secondary-blue: #2196F3;
    --red: #c41e3a;
    --orange: #fb8c00;
    --text: #333;
    --text-2: #666;
    --muted: #6b6b6b;
    --page: #f4f4f4;
    --section: #f9f9f9;
    --card: #fff;
    --ok-bg: #d4edda; --ok-text: #0a3622;
    --err-bg: #f8d7da; --err-text: #5a0a0f;
    --info-bg: #e3f2fd; --info-text: #0d47a1;
    --warn-bg: #fff3cd; --warn-text: #663d00;
    --nav-h: 60px;
    --bar-h: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { overflow-x: hidden; }               /* never scroll sideways on small viewports */
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--page);
    color: var(--text);
    overflow-wrap: break-word;
    overflow-x: hidden;
    max-width: 100%;
    -webkit-text-size-adjust: 100%;
}

/* ===== Accessibility helpers ===== */
/* Visually hidden but available to screen readers */
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
/* Skip link — first focusable element, hidden until focused (SC 2.4.1) */
.skip-link {
    position: absolute; left: 8px; top: -60px; z-index: 100;
    background: var(--blue); color: #fff; padding: .6rem 1rem;
    border-radius: 0 0 8px 8px; text-decoration: none; transition: top .15s ease;
}
.skip-link:focus { top: 0; }
/* Visible focus indicator, ≥3:1 contrast (SC 2.4.7 / 1.4.11) */
:focus-visible { outline: 3px solid #1a237e; outline-offset: 2px; }
.app-bar :focus-visible { outline-color: #fff; }
/* Respect reduced-motion preference (spec §3.8 rule 58) */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}

/* App shell */
.app-bar {
    position: sticky; top: 0; z-index: 20;
    height: var(--bar-h); display: flex; align-items: center; gap: .5rem;
    padding: 0 1rem; color: #fff; background: var(--blue-grad);
    padding-top: env(safe-area-inset-top);
}
/* Brand may shrink & ellipsize so the action buttons (theme, settings, sign out) are
   NEVER pushed off the right edge on narrow phones. */
.app-bar .brand { font-weight: 600; font-size: 1.02rem; line-height: 1.12; display: inline-flex; flex-direction: column; justify-content: center; min-width: 0; flex: 0 1 auto; }
.app-bar .brand-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-bar .brand-domain { font-size: .6rem; font-weight: 400; opacity: .85; letter-spacing: .02em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-bar .spacer { flex: 1 1 0; min-width: 0; }
/* Action controls keep their size; only the brand gives way. */
.app-bar .icon-btn, .app-bar .env-badge { flex: 0 0 auto; }
.env-badge { font-size: .68rem; text-transform: uppercase; letter-spacing: .04em; padding: .12rem .4rem; border-radius: 4px; background: rgba(255,255,255,.25); }
.env-dev { background: #c62828; } .env-stg { background: #f9a825; color:#222;} .env-prd { background: #2e7d32; }
.icon-btn { background: transparent; border: 0; color: #fff; font-size: .9rem; cursor: pointer; padding: .4rem .6rem; border-radius: 6px; min-height: 44px; min-width: 44px; }
.icon-btn:hover { background: rgba(255,255,255,.15); }

main { max-width: 780px; margin: 0 auto; padding: 1rem 1rem calc(var(--nav-h) + 1.5rem); min-height: calc(100dvh - var(--bar-h)); }

/* Bottom navigation (thumb-friendly) */
.bottom-nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    display: flex; background: var(--card); border-top: 1px solid #e2e2e2;
}
.bottom-nav a {
    flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; text-decoration: none; color: var(--muted); font-size: .68rem; padding: .3rem 0; overflow: hidden;
}
.bottom-nav a .ic { font-size: 1.25rem; line-height: 1; }
.bottom-nav a.active { color: var(--blue); }

/* Cards & tiles */
.card { background: var(--card); border-radius: 12px; box-shadow: 0 1px 3px rgba(0,0,0,.08); padding: 1rem; margin-bottom: 1rem; }
.card h2 { margin: 0 0 .5rem; font-size: 1rem; }
.tile-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.tile-grid > * { min-width: 0; }
.tile { border: 0; border-radius: 12px; color: #fff; padding: 1.1rem 1rem; text-align: left; text-decoration: none; display: block; box-shadow: 0 2px 6px rgba(0,0,0,.12); }
.tile .t-title { font-weight: 600; font-size: 1.02rem; } .tile .t-sub { font-size: .78rem; opacity: .9; }
.tile.new { background: #e53935; } .tile.history { background: #1e88e5; }
.tile.insights { background: #43a047; } .tile.settings { background: var(--orange); }

/* Stat pills */
.stat-row { display: flex; gap: .6rem; flex-wrap: wrap; }
.stat { flex: 1 1 30%; min-width: 90px; background: var(--section); border-radius: 10px; padding: .6rem .7rem; }
.stat .v { font-size: 1.4rem; font-weight: 700; } .stat .l { font-size: .72rem; color: var(--text-2); }
.badge { display: inline-block; padding: .12rem .5rem; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.badge.green { background: #e6f4ea; color: #1b5e20; } .badge.yellow { background: #fff8e1; color: #8d6e00; } .badge.red { background: #fdecea; color: #b71c1c; }

/* Forms */
label { display: block; font-size: .85rem; color: var(--text-2); margin: .6rem 0 .25rem; }
input, select, textarea, .btn { font-size: 1rem; }
input, select, textarea {
    width: 100%; max-width: 100%; min-width: 0; padding: .6rem .7rem; border: 1px solid #ccc; border-radius: 8px; background: #fff;
}
/* Native date/time controls have a wide intrinsic min-width; keep them inside their box. */
input[type="datetime-local"], input[type="date"], input[type="time"] { min-width: 0; width: 100%; }
textarea { min-height: 80px; resize: vertical; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; border: 0; border-radius: 8px; padding: .7rem 1rem; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 44px; }
.btn-primary { background: var(--blue); color: #fff; } .btn-primary:disabled { opacity: .6; }
.btn-green { background: var(--green); color: #fff; }
.btn-ghost { background: var(--section); color: var(--text); border: 1px solid #ddd; }
.btn-block { width: 100%; }
.btn-danger { background: var(--red); color: #fff; }
.row2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .6rem; }
.row2 > * { min-width: 0; }
/* On small phones, give each field its own full-width row so nothing is cramped or clipped. */
@media (max-width: 560px) {
    .row2 { grid-template-columns: 1fr; }
}

/* Alerts */
.alert { padding: .7rem .9rem; border-radius: 8px; font-size: .9rem; margin-bottom: .8rem; }
.alert-err { background: var(--err-bg); color: var(--err-text); }
.alert-ok { background: var(--ok-bg); color: var(--ok-text); }
.alert-info { background: var(--info-bg); color: var(--info-text); }
.alert-warn { background: var(--warn-bg); color: var(--warn-text); }

/* Analytics metric cards (Insights overview) */
.insight-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: .8rem; margin-bottom: .6rem; }
.metric { border-left: 4px solid var(--blue); }
.metric-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .5rem; }
.metric-head h2 { margin: 0; font-size: 1rem; }
.metric-chip { flex: 0 0 auto; min-width: 2rem; height: 2rem; padding: 0 .5rem; display: inline-flex; align-items: center; justify-content: center; border-radius: 6px; color: #fff; font-weight: 700; font-size: .85rem; }
.metric-chip.green { background: #2e7d32; } .metric-chip.yellow { background: #f5820b; } .metric-chip.red { background: #c62828; }
.metric-num { font-size: 2.3rem; font-weight: 800; color: var(--blue); line-height: 1.15; margin: .3rem 0; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.metric-delta { font-size: 1.1rem; font-weight: 700; padding: .1rem .5rem; border-radius: 6px; }
.metric-unit { font-size: 1rem; color: var(--text-2); font-weight: 600; }
.metric-row { display: flex; justify-content: space-between; align-items: center; font-size: .85rem; padding: .35rem 0; border-top: 1px solid #eee; }

/* Location chip */
.loc-chip { display: inline-flex; align-items: center; gap: .4rem; background: var(--info-bg); color: var(--info-text); border-radius: 999px; padding: .35rem .7rem; font-size: .85rem; }

/* History list */
.mig-item { display: flex; align-items: center; gap: .6rem; padding: .7rem 0; border-bottom: 1px solid #eee; text-decoration: none; color: var(--text); }
.mig-item .sev { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; background: var(--secondary-blue); flex: 0 0 auto; }
.mig-item .meta { flex: 1; min-width: 0; }
.mig-item .when { font-weight: 600; font-size: .92rem; } .mig-item .sub { font-size: .78rem; color: var(--text-2); }
.dot-active { width: 8px; height: 8px; border-radius: 50%; background: var(--red); display: inline-block; margin-right: 4px; }

/* Auth screen */
.auth-wrap { max-width: 400px; margin: 0 auto; padding-top: 2rem; }
.auth-logo { text-align: center; margin-bottom: 1rem; }
.auth-logo .lg { font-size: 1.4rem; font-weight: 700; color: var(--blue); margin: 0; }
.link { color: var(--blue); text-decoration: none; } .link:hover { text-decoration: underline; }

/* Footer + cookie notice */
footer { text-align: center; color: var(--muted); font-size: .78rem; padding: 1.5rem 0 .5rem; }
footer a { color: var(--text-2); }
.cookie {
    position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); z-index: 25;
    background: #263238; color: #eceff1; font-size: .82rem; padding: .7rem 1rem; display: flex; gap: .7rem; align-items: center; justify-content: center; flex-wrap: wrap;
}
.cookie.no-nav { bottom: 0; }

.spin { width: 22px; height: 22px; border: 3px solid #ddd; border-top-color: var(--blue); border-radius: 50%; animation: sp 1s linear infinite; margin: 1.5rem auto; }
@keyframes sp { to { transform: rotate(360deg); } }
.text-muted { color: var(--muted); } .text-center { text-align: center; }
.mt { margin-top: .8rem; } .small { font-size: .8rem; }

/* Responsive report tables — a real <table> on wide screens and in print,
   reflowed to stacked label/value rows on narrow phones so columns never
   wrap word-by-word. Give each <td> a data-label matching its column header. */
.rtable { width: 100%; border-collapse: collapse; font-size: .85rem; }
.rtable thead tr { text-align: left; border-bottom: 1px solid #ddd; }
.rtable th, .rtable td { padding: .35rem .4rem; }
.rtable tbody tr { border-bottom: 1px solid #f0f0f0; }
@media screen and (max-width: 560px) {
    /* Hide the header row visually but keep it for screen readers. */
    .rtable thead { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }
    .rtable tbody tr { display: block; padding: .5rem 0; }
    .rtable tbody td { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; padding: .15rem 0; }
    .rtable tbody td::before { content: attr(data-label); font-weight: 600; color: var(--text-2); }
    .rtable tbody td:first-child { font-weight: 600; font-size: 1rem; margin-bottom: .1rem; }
    .rtable tbody td:first-child::before { content: none; }
}

/* Desktop niceties */
@media (min-width: 720px) {
    .tile-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* Print (Doctor Visit Summary) */
@media print {
    .app-bar, .bottom-nav, footer, .cookie, .no-print, .btn { display: none !important; }
    main { max-width: 100%; padding: 0; }
    .card { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; }
    body { background: #fff; }
}

/* ===== Blazor WASM boot indicator + error UI ===== */
/* Without these, the boot <svg> renders as a solid black circle and the
   error banner shows on every load (missing display:none). */
.loading-progress {
    position: relative; display: block; width: 8rem; height: 8rem; margin: 20vh auto 1rem auto;
}
.loading-progress circle {
    fill: none; stroke: #e0e0e0; stroke-width: .5rem; transform-origin: 50% 50%; transform: rotate(-90deg);
}
.loading-progress circle:last-child {
    stroke: var(--blue);
    stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * .8), 500%;
    transition: stroke-dasharray .05s ease-in-out;
}
.loading-progress-text {
    position: absolute; text-align: center; font-weight: 600; inset: calc(20vh + 3.3rem) 0 auto 0; color: var(--text-2);
}
.loading-progress-text:after { content: var(--blazor-load-percentage-text, "Loading"); }

#blazor-error-ui {
    display: none; position: fixed; left: 0; bottom: 0; width: 100%; z-index: 1000;
    color: var(--err-text); background: var(--err-bg);
    padding: .6rem 1.25rem .6rem .9rem; box-shadow: 0 -1px 3px rgba(0,0,0,.2);
}
#blazor-error-ui .reload { color: var(--err-text); }
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: .75rem; top: .5rem; }

/* ============================================================================
   Comfort / photo-sensitive mode — warm, dimmed, low-blue-light, LARGER targets.
   Design constraint: stays WCAG 2.1 AA. Every text/background pairing below keeps
   >=4.5:1 (normal text) or >=3:1 (large text / UI component boundaries) — the mode
   is "gentle & dim," NOT genuinely low-contrast.
   ============================================================================ */
html[data-mc-theme="comfort"] {
    /* Warm accent replaces blue (cuts blue light); amber reads AA on the dark surfaces. */
    --green: #7fc98a;
    --blue: #f2ab63;
    --blue2: #e79a54;
    --blue-grad: linear-gradient(135deg, #3a2c20, #2b2019);
    --secondary-blue: #e79a54;
    --red: #f0817b;
    --orange: #f2ab63;
    /* Warm off-white text on warm near-black surfaces (no pure #fff glare). */
    --text: #f4eadc;
    --text-2: #dccdbb;
    --muted: #cabaa7;
    --page: #1b1611;
    --section: #2a221b;
    --card: #271f19;
    --ok-bg: #24361f;  --ok-text: #cfe9bf;
    --err-bg: #3a201f; --err-text: #f6bdb6;
    --info-bg: #2d2a1c; --info-text: #eddaad;
    --warn-bg: #342919; --warn-text: #f2d29e;
    /* Gentle overall enlargement of every rem-based size → bigger touch targets. */
    font-size: 17px;
}
/* Surfaces that use hard-coded colours (not the variables) need explicit overrides. */
html[data-mc-theme="comfort"] body { background: var(--page); color: var(--text); }
html[data-mc-theme="comfort"] .app-bar { color: var(--text); }
html[data-mc-theme="comfort"] .icon-btn { color: var(--text); }
html[data-mc-theme="comfort"] .card { border: 1px solid #372c22; box-shadow: none; }
html[data-mc-theme="comfort"] input,
html[data-mc-theme="comfort"] select,
html[data-mc-theme="comfort"] textarea {
    background: #322820; color: var(--text); border-color: #5b4a3a;
}
html[data-mc-theme="comfort"] input::placeholder,
html[data-mc-theme="comfort"] textarea::placeholder { color: #b0a292; }
/* Amber buttons need dark text to stay AA (white on amber fails). */
html[data-mc-theme="comfort"] .btn-primary { color: #241a10; }
html[data-mc-theme="comfort"] .btn-green { color: #16240f; }
html[data-mc-theme="comfort"] .btn-ghost { border-color: #4a3d31; }
html[data-mc-theme="comfort"] .skip-link { color: #241a10; }
/* Dividers / borders that were near-white. */
html[data-mc-theme="comfort"] .bottom-nav { border-top-color: #372c22; }
html[data-mc-theme="comfort"] .metric-row,
html[data-mc-theme="comfort"] .mig-item { border-color: #372c22; }
html[data-mc-theme="comfort"] .mig-item { color: var(--text); }
html[data-mc-theme="comfort"] .mig-item .sev { color: #241a10; }
/* Bright dashboard tiles → dimmed, desaturated, still distinct; white text stays AA. */
html[data-mc-theme="comfort"] .tile.new { background: #8f3b3b; }
html[data-mc-theme="comfort"] .tile.history { background: #3d5a60; }
html[data-mc-theme="comfort"] .tile.insights { background: #3f5b3a; }
html[data-mc-theme="comfort"] .tile.settings { background: #7d5326; }
/* Focus ring must be light on the dark ground (dark-blue ring would vanish). */
html[data-mc-theme="comfort"] :focus-visible { outline-color: #ffcf9e; }
/* Larger, easier hit targets in comfort mode. */
html[data-mc-theme="comfort"] .btn,
html[data-mc-theme="comfort"] input,
html[data-mc-theme="comfort"] select,
html[data-mc-theme="comfort"] textarea { min-height: 48px; }
html[data-mc-theme="comfort"] .icon-btn { min-height: 48px; min-width: 48px; }
html[data-mc-theme="comfort"] .loading-progress circle { stroke: #4a3d31; }
html[data-mc-theme="comfort"] .loading-progress circle:last-child { stroke: var(--blue); }

/* ===== Bottom-sheet / modal chooser (Quick Start stub, etc.) ===== */
.mc-sheet-backdrop {
    position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45);
    display: flex; align-items: flex-end; justify-content: center;
    padding-bottom: env(safe-area-inset-bottom);
}
.mc-sheet {
    background: var(--card); color: var(--text); width: 100%; max-width: 460px;
    border-radius: 16px 16px 0 0; padding: 1.15rem 1.15rem 1.3rem;
    box-shadow: 0 -6px 28px rgba(0,0,0,.35);
}
.mc-sheet h2 { margin: 0 0 .2rem; font-size: 1.2rem; }
.mc-sheet-sub { color: var(--text-2); font-size: .92rem; margin-bottom: 1rem; }
.mc-sheet .btn { width: 100%; margin-bottom: .55rem; }
.mc-sheet-comfort {
    display: flex; align-items: center; justify-content: space-between; gap: .6rem;
    border-top: 1px solid #eee; margin-top: .5rem; padding-top: .6rem;
}
html[data-mc-theme="comfort"] .mc-sheet-comfort { border-top-color: #372c22; }
@media (min-width: 560px) {
    .mc-sheet-backdrop { align-items: center; }
    .mc-sheet { border-radius: 16px; }
}

/* ===== "Update available" refresh banner (injected by index.html on SW update) ===== */
.mc-update-banner {
    position: fixed; left: 50%; transform: translateX(-50%);
    bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px); z-index: 40;
    background: var(--blue); color: #fff; border: 0; border-radius: 999px;
    padding: .75rem 1.3rem; font-size: .9rem; font-weight: 700; cursor: pointer;
    min-height: 44px; box-shadow: 0 4px 16px rgba(0,0,0,.32); max-width: calc(100% - 2rem);
}
.mc-update-banner:disabled { opacity: .8; }
html[data-mc-theme="comfort"] .mc-update-banner { color: #241a10; }

/* ===== Theme toggle controls ===== */
.theme-toggle-btn { font-size: 1.05rem; }
.theme-switch {
    display: inline-flex; align-items: center; gap: .6rem; background: transparent;
    border: 0; cursor: pointer; padding: .5rem .3rem; min-height: 44px;
    color: var(--text-2); font-size: .92rem;
}
.theme-switch-track {
    position: relative; flex: 0 0 auto; width: 46px; height: 26px; border-radius: 999px;
    background: #8a8a8a; box-shadow: inset 0 0 0 1px rgba(120,120,120,.7); transition: background .15s;
}
.theme-switch.on .theme-switch-track { background: var(--orange); }
.theme-switch-thumb {
    position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
    background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.4); transition: left .15s;
}
.theme-switch.on .theme-switch-thumb { left: 23px; }
.theme-switch-label { font-weight: 600; }
