@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

: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: 56px;
    --footer-h: 32px;
}

* { 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);
    z-index: 5;
}

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

.brand-mark {
    width: 32px;
    height: 32px;
    color: var(--primary);
    flex: 0 0 auto;
}

.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;
}

.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; }

/* ── 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); }

.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 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);
}

.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;
    margin-top: 0.5rem;
}

.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: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;
}

#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);
}

.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: center;
}

.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.65rem 0.85rem;
    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.5rem;
    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;
}

.credibility-note {
    margin: -0.25rem 0 0;
    font-size: 0.75rem;
    color: var(--text-soft);
    text-align: center;
}

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

.results-plot-container h3 {
    margin: 0 0 0.4rem;
    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;
}

.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);
}

/* ── 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;
    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; }
    .layout { grid-template-columns: 1fr; overflow: visible; }
    .panel { overflow: visible; }
    .header-meta { display: none; }
}

@media (max-width: 600px) {
    .grid-3 { grid-template-columns: 1fr 1fr; }
    .results-summary { grid-template-columns: 1fr; }
    .form-actions { flex-wrap: wrap; }
    .app-footer {
        flex-direction: column;
        gap: 0.15rem;
        padding: 0.4rem 1rem;
        height: auto;
    }
    .app { grid-template-rows: var(--header-h) 1fr auto; }
}
