/* ------------------------------------------------------------------ tokens */
:root {
    --accent: #115e59;
    --accent-strong: #0b5c55;
    --accent-soft: #d7f2ee;

    /* Text and icons drawn ON the accent fill. It has to flip with the theme: the light accent is
       dark enough that white wins (7.6:1 vs 3.8:1 for black), and the dark accent is bright enough
       that the opposite is true (11.3:1 vs 1.9:1). One fixed colour cannot serve both. */
    --on-accent: #ffffff;

    --bg: #f4f6f7;
    --surface: #ffffff;
    --surface-2: #eef1f3;
    --border: #dbe1e5;
    --text: #17212b;
    /* Dark enough to clear 4.5:1 on the surfaces it lands on. #61707c read as pleasantly muted
       but only managed 4.1:1 for inactive nav labels, which are not large text. */
    --text-muted: #57646f;

    --danger: #b42318;
    --danger-soft: #fde8e6;
    --warn: #8a3708;
    --ok: #087443;

    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);

    --topbar-h: 52px;

    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --accent: #2dd4bf;
        --accent-strong: #5eead4;
        --accent-soft: #10312f;
        --on-accent: #04211e;

        --bg: #0d1117;
        --surface: #161b22;
        --surface-2: #1f2630;
        --border: #2b3541;
        --text: #e6edf3;
        --text-muted: #93a3b1;

        --danger: #ff7b72;
        --danger-soft: #3a1a18;
        --warn: #e3a008;
        --ok: #3fb950;

        --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }

a { color: var(--accent); }

.muted { color: var(--text-muted); }
.small { font-size: .85rem; }

/* ------------------------------------------------------------------ shell */
.app-shell {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.app-main {
    flex: 1;
    padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}

.page {
    max-width: 880px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.blank-layout {
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: 24px;
}

/* --------------------------------------------------------------- top strip */
.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 8px;
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 12px 0;
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: saturate(160%) blur(10px);
    border-bottom: 1px solid var(--border);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    text-decoration: none;
    color: var(--text);
    font-weight: 700;
    flex: 0 0 auto;
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 26px;
    height: 26px;
    border-radius: 8px;
    background: var(--accent);
    color: var(--on-accent);
    font-size: .9rem;
}

.brand-text { font-size: .95rem; }

.topnav {
    display: flex;
    align-items: center;
    gap: 2px;
    flex: 1 1 auto;
    justify-content: center;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
}

.topnav::-webkit-scrollbar { display: none; }

.topnav-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 999px;
    text-decoration: none;
    color: var(--text-muted);
    font-size: .9rem;
    font-weight: 550;
    white-space: nowrap;
    transition: background .12s ease, color .12s ease;
}

.topnav-item:hover { background: var(--surface-2); color: var(--text); }

.topnav-item.active {
    background: var(--accent-soft);
    color: var(--accent-strong);
}

.topnav-icon { font-size: 1rem; line-height: 1; }

.account {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex: 0 0 auto;
}

.avatar {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    border-radius: 50%;
    overflow: hidden;
    background: var(--surface-2);
    border: 1px solid var(--border);
}

.avatar-img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
}

.avatar-initials {
    font-weight: 700;
    line-height: 1;
    color: var(--text-muted);
}

.identity {
    display: flex;
    align-items: center;
    gap: 14px;
}

/* ------------------------------------------------------------------ buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font: inherit;
    font-weight: 600;
    font-size: .92rem;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: filter .12s ease, background .12s ease;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:not(:disabled):hover { filter: brightness(1.06); }

.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.small { padding: 5px 10px; font-size: .82rem; }

.icon-btn {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    flex: 0 0 auto;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: .95rem;
    cursor: pointer;
}

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

/* ------------------------------------------------------------------ inputs */
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: .8rem; font-weight: 600; color: var(--text-muted); }

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.input {
    width: 100%;
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: .95rem;
}

.input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
    border-color: transparent;
}

.input.inline { flex: 1 1 auto; min-width: 0; padding: 6px 9px; font-size: .9rem; }
.input.color { padding: 3px; height: 38px; cursor: pointer; }

textarea.input { resize: vertical; }

/* switch */
.switch { display: inline-flex; align-items: center; cursor: pointer; flex: 0 0 auto; }
.switch.standalone { height: 38px; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }

.switch-track {
    width: 40px;
    height: 23px;
    border-radius: 999px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    display: block;
    position: relative;
    transition: background .15s ease;
}

.switch-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 17px;
    height: 17px;
    border-radius: 50%;
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
    transition: transform .15s ease;
}

.switch input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(17px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------------ status */
.alert {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: .9rem;
    border: 1px solid transparent;
}

.alert.error { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }

.empty {
    padding: 40px 20px;
    text-align: center;
    background: var(--surface);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
}

.empty-title { margin: 0 0 6px; font-weight: 600; }

.hint { margin: 0 0 12px; color: var(--text-muted); font-size: .88rem; }

.centered-status {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 48px 16px;
    color: var(--text-muted);
}

.spinner {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    animation: spin .8s linear infinite;
}

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

/* ------------------------------------------------------------------ timeline */
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 20px; }
.timeline-day { display: flex; flex-direction: column; gap: 8px; }

.day-head {
    position: sticky;
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    z-index: 5;
    padding: 4px 0;
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    background: linear-gradient(var(--bg) 70%, transparent);
}

.cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.card {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 12px 10px 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.card-time {
    flex: 0 0 auto;
    font-variant-numeric: tabular-nums;
    font-weight: 650;
    font-size: .9rem;
    color: var(--text-muted);
    padding-top: 2px;
    min-width: 44px;
}

.card-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 6px; }

.card-note {
    margin: 0;
    font-size: .9rem;
    color: var(--text-muted);
    overflow-wrap: anywhere;
}

.pill-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }

.pill {
    display: inline-flex;
    align-items: baseline;
    gap: 5px;
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    font-size: .88rem;
}

.pill-qty { font-weight: 700; color: var(--accent-strong); }
.pill-name { font-weight: 550; }
.pill-strength { color: var(--text-muted); font-size: .8rem; }

.path-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.path { font-weight: 600; }

.severity {
    padding: 1px 8px;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 700;
    border: 1px solid transparent;
}

.sev-low { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.sev-mid { background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn); }
.sev-high { background: var(--danger-soft); color: var(--danger); }

/* ------------------------------------------------------------------ modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 60;
    background: rgba(9, 15, 22, .5);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 0;
    animation: fade .12s ease;
}

@keyframes fade { from { opacity: 0; } }

.modal-sheet {
    width: 100%;
    max-height: 92dvh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-radius: 16px 16px 0 0;
    box-shadow: var(--shadow);
    animation: rise .16s ease;
}

@keyframes rise { from { transform: translateY(18px); } }

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 12px 12px 18px;
    border-bottom: 1px solid var(--border);
}

.modal-body {
    padding: 16px 18px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.modal-foot {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
}

@media (min-width: 640px) {
    .modal-backdrop { align-items: center; padding: 24px; }

    .modal-sheet {
        max-width: 480px;
        border-radius: 16px;
        max-height: 86dvh;
    }

    .modal-wide { max-width: 640px; }
}

/* ------------------------------------------------------------------ tiles */
.tile-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
}

.tile-wrap { position: relative; display: flex; }

.tile {
    --tile-accent: var(--accent);
    position: relative;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    gap: 2px;
    min-height: 66px;
    padding: 12px 40px 12px 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    text-align: left;
    font: inherit;
    cursor: pointer;
    transition: border-color .12s ease, background .12s ease, transform .06s ease;
}

.tile::before {
    content: "";
    position: absolute;
    left: 0;
    top: 10px;
    bottom: 10px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--tile-accent);
}

.tile:active { transform: scale(.985); }
.tile:hover { border-color: var(--tile-accent); }

.tile.selected {
    border-color: var(--tile-accent);
    background: color-mix(in srgb, var(--tile-accent) 12%, var(--surface));
}

.tile-name { font-weight: 600; font-size: .95rem; overflow-wrap: anywhere; }
.tile-sub { font-size: .78rem; color: var(--text-muted); }
.tile-other { border-style: dashed; }
.tile-other::before { background: var(--border); }

.tile-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    min-width: 24px;
    height: 24px;
    padding: 0 6px;
    border-radius: 999px;
    /* Deliberately the theme accent, not the drug's colour: that colour is user-chosen and could
       be anything, and no fixed foreground is readable on an arbitrary hex. The drug stays
       identifiable through the tile's left stripe and its selected tint. */
    background: var(--accent);
    color: var(--on-accent);
    display: grid;
    place-items: center;
    font-size: .8rem;
    font-weight: 700;
}

.tile-minus {
    position: absolute;
    right: 6px;
    bottom: 6px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
}

.tile-minus:hover { background: var(--surface-2); }

.summary {
    margin: 0;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    font-size: .9rem;
    font-weight: 550;
}

.chosen-path {
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    background: var(--accent-soft);
    color: var(--accent-strong);
    font-weight: 600;
    font-size: .92rem;
}

.severity-scale { display: flex; flex-wrap: wrap; gap: 6px; }

.sev-chip {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.sev-chip.selected { outline: 2px solid currentColor; outline-offset: -2px; font-weight: 800; }

/* ------------------------------------------------------------------ settings */
.tabs {
    display: inline-flex;
    gap: 4px;
    padding: 3px;
    border-radius: 999px;
    background: var(--surface-2);
    align-self: flex-start;
}

.tab {
    padding: 6px 16px;
    border: none;
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    font: inherit;
    font-size: .9rem;
    font-weight: 600;
    cursor: pointer;
}

.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.panel {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.panel-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.panel-head p { margin: 2px 0 0; }

.rows { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }

.row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}

.row:last-child { border-bottom: none; }
.row.inactive .row-main { opacity: .5; }

.row-swatch { width: 8px; height: 32px; border-radius: 4px; flex: 0 0 auto; }
.row-main { flex: 1 1 auto; min-width: 0; }
.row-title { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; font-weight: 600; }
.row-sub { font-size: .8rem; font-weight: 500; color: var(--text-muted); }
.row-note { margin: 2px 0 0; font-size: .85rem; color: var(--text-muted); }

/* tree */
.tree { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.tree-node { border-bottom: 1px solid var(--border); }
.tree-node:last-child { border-bottom: none; }

.tree-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 0;
    min-height: 40px;
}

.tree-row.child { padding-left: 34px; border-top: 1px dashed var(--border); }
.tree-row.add { padding-top: 10px; }
.tree-row.root { border-top: 1px solid var(--border); padding-top: 14px; }

.tree-children { list-style: none; margin: 0 0 6px; padding: 0; }

.disclosure {
    width: 26px;
    height: 26px;
    flex: 0 0 auto;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    font-size: .8rem;
}

.disclosure:hover { background: var(--surface-2); }

.tree-name { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; font-size: .95rem; }
.tree-row.child .tree-name { font-size: .9rem; color: var(--text-muted); }

.tree-count {
    flex: 0 0 auto;
    min-width: 22px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: .75rem;
    font-weight: 700;
    text-align: center;
}

/* ------------------------------------------------------------------ vault */
.unlock {
    display: grid;
    place-items: center;
    padding: 8vh 4px 4px;
}

.unlock-card {
    width: 100%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 26px 22px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-align: center;
}

.unlock-card .field { text-align: left; }
.unlock-card form { display: flex; flex-direction: column; gap: 14px; }

.brand-mark.large {
    width: 46px;
    height: 46px;
    border-radius: 14px;
    font-size: 1.4rem;
    margin: 0 auto;
    background: var(--accent-soft);
}

.btn.block { width: 100%; }

.unlock-links {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}

.linkish {
    border: none;
    background: none;
    padding: 0;
    font: inherit;
    font-size: .85rem;
    color: var(--accent);
    cursor: pointer;
    text-decoration: underline;
}

.alert.warn {
    background: color-mix(in srgb, var(--warn) 14%, transparent);
    color: var(--warn);
    border-color: color-mix(in srgb, var(--warn) 35%, transparent);
    text-align: left;
}

.mode-card {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface-2);
}

.mode-card.on {
    border-color: color-mix(in srgb, var(--accent) 45%, transparent);
    background: var(--accent-soft);
}

.mode-icon { font-size: 1.5rem; line-height: 1.2; flex: 0 0 auto; }

.recovery-code {
    margin: 0;
    padding: 14px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    border: 1px dashed var(--border);
    font-family: ui-monospace, "SF Mono", Consolas, monospace;
    font-size: 1.05rem;
    letter-spacing: .06em;
    text-align: center;
    overflow-wrap: anywhere;
    user-select: all;
}

.check {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    font-size: .9rem;
    cursor: pointer;
}

.check input { margin-top: 3px; flex: 0 0 auto; }

/* ------------------------------------------------------------------ narrow */
@media (max-width: 560px) {
    .brand-text { display: none; }
    .topnav-item { padding: 6px 10px; font-size: .82rem; }
    .app-main { padding: 12px 12px calc(28px + env(safe-area-inset-bottom)); }
    .field-row { grid-template-columns: 1fr; }
    .page-head .btn { flex: 1 1 100%; }
    .tile-grid { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); }
    .card-time { min-width: 40px; font-size: .85rem; }
}

/* Nav labels are worth more than the sign-out button; Settings carries sign-out too. */
@media (max-width: 430px) {
    .topbar { gap: 4px; padding-inline: 8px; }
    .topnav-item { padding: 6px 7px; gap: 4px; font-size: .78rem; }
    .account-signout { display: none; }
}

/* Last resort on very small phones: icons carry the navigation. */
@media (max-width: 330px) {
    .topnav-label { display: none; }
    .topnav-item { padding: 6px 12px; }
    .topnav-icon { font-size: 1.15rem; }
}

/* -------------------------------------------------- boot & error (template) */
.loading-progress {
    position: relative;
    display: block;
    width: 8rem;
    height: 8rem;
    margin: 20vh auto 1rem auto;
}

.loading-progress circle {
    fill: none;
    stroke: var(--surface-2);
    stroke-width: .6rem;
    transform-origin: 50% 50%;
    transform: rotate(-90deg);
}

.loading-progress circle:last-child {
    stroke: var(--accent);
    stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%;
    transition: stroke-dasharray .05s ease-in-out;
}

.loading-progress-text {
    position: absolute;
    text-align: center;
    font-weight: bold;
    inset: calc(20vh + 3.25rem) 0 auto 0;
    color: var(--text-muted);
}

.loading-progress-text:after {
    content: var(--blazor-load-percentage-text, "Loading");
}

#blazor-error-ui {
    background: #ffeb3b;
    color: #17212b;
    bottom: 0;
    box-shadow: 0 -1px 2px rgba(0, 0, 0, .2);
    display: none;
    left: 0;
    padding: .7rem 1.25rem .7rem 1.25rem;
    position: fixed;
    width: 100%;
    z-index: 1000;
}

#blazor-error-ui .dismiss {
    cursor: pointer;
    position: absolute;
    right: .75rem;
    top: .5rem;
}
