/* Inter, self-hosted (web/vendor, see scripts/vendor.mjs). Variable weight. */
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-display: swap;
    font-weight: 100 900;
    src: url('vendor/inter-latin-wght-normal.woff2') format('woff2');
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329,
        U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
    /* Medical palette */
    --primary: #0369a1;
    --primary-hover: #0284c7;
    --primary-light: #e0f2fe;
    --primary-tint: #f0f9ff;
    --accent: #0891b2;
    --success: #059669;
    --warning: #d97706;
    --danger: #dc2626;

    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #f1f5f9;

    --text: #0f172a;
    --text-muted: #475569;
    --text-soft: #64748b;
    --border: #e2e8f0;
    --border-soft: #eef2f6;

    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.06);

    --gap: 0.75rem;
    --gap-lg: 1rem;

    --header-h: 60px;
    --footer-h: 36px;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; height: 100%; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

/* ── App shell ─────────────────────────────────────────────── */
.app {
    display: grid;
    grid-template-rows: var(--header-h) 1fr var(--footer-h);
    height: 100vh;
    width: 100%;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0 1.25rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    position: relative;
    z-index: 5;
}

.app-header::after {
    content: '';
    position: absolute;
    left: 0; right: 0; bottom: -1px;
    height: 2px;
    background: linear-gradient(90deg, var(--primary), var(--accent) 40%, transparent 85%);
    opacity: 0.55;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

/* Kidney mark, as on egfr.app: white on a navy rounded square. */
.brand-mark {
    width: 32px;
    height: 32px;
    border-radius: 7px;
    background: #1f4c7d;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
}

.brand-mark svg {
    width: 19px;
    height: 19px;
}

.brand-text h1 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.01em;
    line-height: 1.1;
}

.brand-text .subtitle {
    margin: 0;
    font-size: 0.78rem;
    color: var(--text-muted);
    line-height: 1.2;
}

/* Calculation service status (polls /health) */
.backend-status {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    color: var(--text-soft);
    white-space: nowrap;
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #cbd5e1;
    flex: 0 0 auto;
}

.backend-status[data-state="up"] .status-dot { background: var(--success); }
.backend-status[data-state="down"] { color: #b91c1c; }
.backend-status[data-state="down"] .status-dot { background: var(--danger); }

.header-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.header-meta a {
    color: var(--primary);
    text-decoration: none;
}

.header-meta a:hover { text-decoration: underline; }

.link-button {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font: inherit;
    padding: 0;
}

.link-button:hover { text-decoration: underline; }

a:focus-visible,
button:focus-visible,
summary:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    border-radius: 4px;
}

/* ── Layout ────────────────────────────────────────────────── */
.layout {
    display: grid;
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    gap: var(--gap-lg);
    padding: var(--gap-lg);
    overflow: hidden;
    min-height: 0;
}

.panel {
    background: var(--surface);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    overflow: auto;
    min-height: 0;
}

.panel-form { padding: var(--gap-lg) var(--gap-lg) 0; }

.panel-results {
    display: flex;
    flex-direction: column;
    padding: var(--gap-lg);
}

/* ── Form cards ────────────────────────────────────────────── */
.card {
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 0.65rem 0.85rem 0.85rem;
    margin: 0 0 var(--gap) 0;
    background: var(--surface);
}

.card legend {
    font-weight: 600;
    font-size: 0.78rem;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 0 0.4rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.legend-aside {
    text-transform: none;
    letter-spacing: 0;
    font-weight: 400;
    color: var(--text-soft);
    font-size: 0.75rem;
}

/* ── Grid & fields ─────────────────────────────────────────── */
.grid { display: grid; gap: 0.65rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-tight { gap: 0.5rem; }

.field { display: flex; flex-direction: column; min-width: 0; }

.field label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.2rem;
    display: flex;
    align-items: baseline;
    gap: 0.35rem;
}

.unit, .hint {
    font-weight: 400;
    color: var(--text-soft);
    font-size: 0.72rem;
}

.field input,
.field select {
    padding: 0.4rem 0.55rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s, box-shadow 0.15s;
    min-width: 0;
    width: 100%;
}

.field input:focus,
.field select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

/* Field-level errors (from local checks or mimir's validation) */
.field input.invalid,
.field select.invalid {
    border-color: var(--danger);
    background: #fef2f2;
}

.field input.invalid:focus,
.field select.invalid:focus {
    box-shadow: 0 0 0 3px #fee2e2;
}

.field-error {
    margin: 0.25rem 0 0;
    font-size: 0.72rem;
    font-weight: 500;
    color: #b91c1c;
}

.field input.readonly,
.field input[readonly] {
    background: var(--surface-3);
    color: var(--text-muted);
    cursor: default;
}

.relative-time-display {
    text-align: center;
    font-weight: 500;
    color: var(--primary) !important;
}

.dose-fields { margin-top: 0.65rem; }
.dose-fields .hint { margin: 0.4rem 0 0; }

/* ── Samples ───────────────────────────────────────────────── */
.samples {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.sample-entry {
    position: relative;
    padding: 0.55rem 2rem 0.55rem 0.65rem;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-sm);
}

/* Study nurses navigate by keyboard; hide the native time-picker clock
   button so Tab moves straight to the next field instead of stopping on it. */
.sample-time::-webkit-calendar-picker-indicator {
    display: none;
}

.sample-time::-webkit-inner-spin-button {
    display: none;
}

.btn-remove {
    position: absolute;
    top: 50%;
    right: 0.4rem;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 50%;
    color: var(--text-soft);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s;
}

.btn-remove:hover {
    background: #fee2e2;
    color: var(--danger);
}

/* ── Advanced disclosure ───────────────────────────────────── */
.advanced {
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    margin-bottom: var(--gap);
    background: var(--surface-2);
}

.advanced summary {
    list-style: none;
    cursor: pointer;
    padding: 0.5rem 0.85rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-muted);
    user-select: none;
}

.advanced summary::-webkit-details-marker { display: none; }

.advanced summary::after {
    content: '▾';
    font-size: 0.75rem;
    color: var(--text-soft);
    transition: transform 0.2s;
}

.advanced[open] summary::after { transform: rotate(180deg); }

.advanced-icon { color: var(--text-soft); }

.advanced-summary {
    margin-left: auto;
    font-weight: 400;
    font-size: 0.75rem;
    color: var(--text-soft);
}

.advanced[open] .advanced-summary { display: none; }

.advanced > .grid { padding: 0 0.85rem 0.75rem; }

/* ── Buttons ───────────────────────────────────────────────── */
.form-actions {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    position: sticky;
    bottom: 0;
    margin: 0.5rem calc(-1 * var(--gap-lg)) 0;
    padding: 0.75rem var(--gap-lg) var(--gap-lg);
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.85), var(--surface) 35%);
    border-top: 1px solid var(--border-soft);
    z-index: 2;
}

.btn-primary,
.btn-ghost {
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
}

.btn-primary {
    background: var(--primary);
    color: white;
    flex: 1;
}

.btn-primary:hover { background: var(--primary-hover); }

.btn-primary:active { transform: translateY(1px); }

.btn-primary:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--primary-light);
}

.btn-ghost {
    background: var(--surface);
    color: var(--text-muted);
    border-color: var(--border);
}

.btn-ghost:hover {
    background: var(--surface-2);
    color: var(--text);
    border-color: var(--text-soft);
}

.btn-sm {
    padding: 0.35rem 0.7rem;
    font-size: 0.8rem;
    white-space: nowrap;
}

#addSample {
    align-self: flex-start;
    margin-top: 0.25rem;
}

/* ── Results panel ─────────────────────────────────────────── */
.empty-state {
    margin: auto;
    text-align: center;
    color: var(--text-soft);
    padding: 2rem 1rem;
    max-width: 320px;
}

.empty-icon {
    width: 56px;
    height: 56px;
    color: var(--primary);
    opacity: 0.4;
    margin-bottom: 0.75rem;
}

.empty-state h2 {
    margin: 0 0 0.4rem;
    font-size: 1rem;
    color: var(--text);
    font-weight: 600;
}

.empty-state p {
    margin: 0;
    font-size: 0.85rem;
}

.loading-spinner {
    margin: auto;
    text-align: center;
    padding: 2rem;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
    margin: 0 auto 0.75rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.spinner-text {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 500;
}

.error-message {
    background: #fef2f2;
    color: #991b1b;
    padding: 0.65rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid #fecaca;
    font-size: 0.85rem;
    margin-bottom: var(--gap);
}

.warning-message {
    background: #fffbeb;
    color: #92400e;
    padding: 0.55rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid #fde68a;
    font-size: 0.82rem;
}

.error-message::before {
    content: '⚠ ';
    font-weight: 600;
}

.results {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex: 1;
    min-height: 0;
}

.results-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.75rem;
}

.results-meta {
    margin: 0.15rem 0 0;
    font-size: 0.75rem;
    color: var(--text-soft);
}

.results-header h2 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
}

.results-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.65rem;
}

.result-card {
    background: linear-gradient(135deg, var(--primary-tint) 0%, var(--surface) 100%);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 0.8rem 0.95rem 0.75rem;
    position: relative;
    overflow: hidden;
}

.result-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), var(--accent));
}

.result-card h3 {
    margin: 0 0 0.2rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-soft);
    font-weight: 600;
}

.result-value {
    margin: 0;
    font-size: 1.9rem;
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
    font-weight: 700;
    color: var(--primary);
    letter-spacing: -0.02em;
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.result-unit {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-soft);
    letter-spacing: 0;
}

.result-ci {
    margin: 0.2rem 0 0;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.results-plot-container,
.results-table-container {
    background: var(--surface);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 0.65rem 0.85rem;
}

.plot-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.25rem 1rem;
    margin-bottom: 0.4rem;
}

.plot-legend {
    display: flex;
    gap: 0.85rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0.72rem;
    color: var(--text-soft);
}

.plot-legend li { display: flex; align-items: center; gap: 0.35rem; }
.plot-legend li[hidden] { display: none; }

.key { display: inline-block; flex: 0 0 auto; }
.key-obs { width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--accent); background: var(--surface); }
.key-line { width: 16px; height: 2px; border-radius: 1px; background: var(--primary); }
.key-band { width: 14px; height: 9px; border-radius: 2px; background: rgba(3, 105, 161, 0.25); }

.results-plot-container h3 {
    margin: 0;
    font-size: 0.78rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

#concentrationChart {
    width: 100%;
    position: relative;
}

#concentrationChart svg {
    display: block;
    width: 100%;
    height: auto;
}

.d3-tooltip {
    position: absolute;
    pointer-events: none;
    background: rgba(15, 23, 42, 0.92);
    color: #f1f5f9;
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 11px;
    line-height: 1.4;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: opacity 0.15s ease;
    white-space: nowrap;
    z-index: 10;
}

.d3-tooltip .tt-soft { color: #94a3b8; }

.results-table-container summary {
    cursor: pointer;
    font-size: 0.78rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    list-style: none;
}

.results-table-container summary::-webkit-details-marker { display: none; }

.results-table-container summary::after {
    content: ' ▾';
    color: var(--text-soft);
}

.results-table-container[open] summary::after { content: ' ▴'; }

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 0.5rem;
    font-size: 0.85rem;
}

th {
    text-align: left;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid var(--border);
}

td {
    padding: 0.35rem 0.5rem;
    border-bottom: 1px solid var(--border-soft);
    color: var(--text);
}

tbody tr:last-child td { border-bottom: none; }

tbody tr:hover { background: var(--surface-2); }

/* ── Footer ────────────────────────────────────────────────── */
.app-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1.25rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    font-size: 0.72rem;
    color: var(--text-soft);
    gap: 1rem;
}

.footer-links {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.app-footer a {
    color: var(--primary);
    text-decoration: none;
}

.app-footer a:hover { text-decoration: underline; }

/* ── About dialog ──────────────────────────────────────────── */
.about-dialog {
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 0;
    max-width: 540px;
    width: calc(100% - 2rem);
    box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
}

.about-dialog::backdrop {
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(2px);
}

.about-dialog form { padding: 1.25rem 1.5rem 1rem; }

.about-dialog h2 {
    margin: 0 0 0.75rem;
    font-size: 1.1rem;
    color: var(--text);
}

.about-dialog h3 {
    margin: 0.85rem 0 0.25rem;
    font-size: 0.78rem;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.about-dialog p {
    margin: 0 0 0.5rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    line-height: 1.5;
}

.about-dialog a { color: var(--primary); }

.about-dialog menu {
    margin: 1rem 0 0;
    padding: 0;
    list-style: none;
    display: flex;
    justify-content: flex-end;
}

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 960px) {
    body { overflow: auto; }
    .app { height: auto; min-height: 100vh; grid-template-rows: var(--header-h) 1fr auto; }
    .app-footer { flex-wrap: wrap; padding: 0.5rem 1.25rem; }
    .layout { grid-template-columns: 1fr; overflow: visible; }
    .panel { overflow: visible; }
    /* Calculation service status (polls /health) */
.backend-status {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    color: var(--text-soft);
    white-space: nowrap;
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #cbd5e1;
    flex: 0 0 auto;
}

.backend-status[data-state="up"] .status-dot { background: var(--success); }
.backend-status[data-state="down"] { color: #b91c1c; }
.backend-status[data-state="down"] .status-dot { background: var(--danger); }

.header-meta { display: none; }
}

@media (max-width: 600px) {
    /* Keep only the dot on phones; the label stays for screen readers. */
    .backend-status .status-label {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
        white-space: nowrap;
    }
    .grid-3 { grid-template-columns: 1fr 1fr; }
    .results-summary { grid-template-columns: 1fr; }
    .form-actions { flex-wrap: wrap; }
    .footer-links > [aria-hidden="true"] { display: none; }
    .footer-links { gap: 0.25rem 0.75rem; }
    .app-footer {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
        padding: 0.5rem 1rem;
        height: auto;
    }
    .app { grid-template-rows: var(--header-h) 1fr auto; }
}
