:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --text: #1c1f24;
    --muted: #5f6672;
    --border: #dde0e5;
    --accent: #2563eb;
    --error: #c62828;
    --runner: #2563eb;
    --dogs: #ea580c;
    --delusion: #9333ea;
    --radius: 10px;
    color-scheme: light dark;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161a;
        --surface: #1e2127;
        --text: #e8eaed;
        --muted: #9aa1ac;
        --border: #2f333b;
        --accent: #5b8cff;
        --error: #ff6b6b;
        --runner: #5b8cff;
        --dogs: #ff8a3d;
        --delusion: #b57bff;
    }
}

* { box-sizing: border-box; }

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

body {
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.45;
}

[hidden] { display: none !important; }

h1, h2 { margin: 0; }

.muted { color: var(--muted); }

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

input, select, button {
    font: inherit;
    color: inherit;
}

input, select {
    width: 100%;
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

input:focus-visible, select:focus-visible, button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

button {
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    cursor: pointer;
}

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

button:disabled { opacity: 0.6; cursor: progress; }

.error { color: var(--error); margin: 0; }

.noscript { padding: 24px; text-align: center; }

/* Login */

.login {
    min-height: 100%;
    display: grid;
    place-items: center;
    padding: 16px;
}

.login-card {
    width: min(100%, 360px);
    display: grid;
    gap: 14px;
    padding: 28px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.login-card p { margin: 0; }

.login-card label {
    display: grid;
    gap: 6px;
    font-weight: 600;
}

/* App layout */

.app {
    height: 100%;
    display: grid;
    grid-template-columns: 360px 1fr;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
        "top top"
        "side content"
        "stats content";
}

.topbar {
    grid-area: top;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 18px; }

.account {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.account span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar {
    grid-area: side;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 16px 16px 4px;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.picker {
    display: grid;
    gap: 6px;
}

.picker label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
}

.swatch {
    width: 14px;
    height: 4px;
    border-radius: 2px;
    flex: none;
}

.swatch.runner { background: var(--runner); }
.swatch.dogs { background: var(--dogs); }
.swatch.delusion { background: var(--delusion); }

.status { margin: 0; min-height: 1.45em; }

.stats {
    grid-area: stats;
    display: grid;
    align-content: start;
    gap: 10px;
    padding: 0 16px 16px;
    min-height: 0;
    overflow-y: auto;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.stat-card {
    border: 1px solid var(--border);
    border-left-width: 4px;
    border-radius: 8px;
    padding: 10px 12px;
}

.stat-card.runner { border-left-color: var(--runner); }
.stat-card.dogs { border-left-color: var(--dogs); }
.stat-card.delusion { border-left-color: var(--delusion); }

.stat-card h3 {
    margin: 0 0 2px;
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}

.stat-card .name {
    margin: 0 0 8px;
    font-weight: 600;
    overflow-wrap: anywhere;
}

.stat-card dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 3px 12px;
    margin: 0;
}

.stat-card dt { color: var(--muted); }
.stat-card dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.content {
    grid-area: content;
    display: grid;
    grid-template-rows: 1fr auto;
    min-width: 0;
    min-height: 0;
}

.map {
    min-height: 300px;
    background: var(--border);
}

.profile {
    padding: 10px 16px 14px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

.profile h2 {
    font-size: 14px;
    margin-bottom: 6px;
}

.profile-chart {
    height: 170px;
}

.profile-chart svg {
    display: block;
    width: 100%;
    height: 100%;
}

.profile-chart .grid { stroke: var(--border); stroke-width: 1; }
.profile-chart .axis-label { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.profile-chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; }
.profile-chart .line.runner { stroke: var(--runner); }
.profile-chart .line.dogs { stroke: var(--dogs); }
.profile-chart .line.delusion { stroke: var(--delusion); }
.profile-chart .empty { margin: 0; padding-top: 8px; }

/* User administration */

.users {
    grid-column: 1 / -1;
    grid-row: 2 / -1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: grid;
    align-content: start;
    gap: 14px;
}

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

.users-head h2 { font-size: 18px; }

.user-form {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr)) auto;
    gap: 12px;
    align-items: end;
    padding: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.user-form label {
    display: grid;
    gap: 6px;
    font-weight: 600;
    min-width: 0;
}

.table-scroll {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.users-table {
    width: 100%;
    border-collapse: collapse;
}

.users-table th, .users-table td {
    padding: 9px 12px;
    text-align: left;
    white-space: nowrap;
    border-bottom: 1px solid var(--border);
}

.users-table th { color: var(--muted); font-size: 13px; font-weight: 600; }
.users-table tbody tr:last-child td { border-bottom: none; }
.users-table select { width: auto; padding: 5px 8px; }
.users-table tr.disabled td:first-child { color: var(--muted); text-decoration: line-through; }

.user-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.user-actions button { padding: 5px 10px; }
.user-actions button:disabled { cursor: not-allowed; }

.ask-dialog {
    width: min(100% - 32px, 400px);
    padding: 22px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.ask-dialog::backdrop { background: rgb(0 0 0 / 45%); }

.ask-dialog form {
    display: grid;
    gap: 12px;
}

.ask-dialog h2 { font-size: 17px; overflow-wrap: anywhere; }
.ask-dialog p { margin: 0; }

.ask-dialog label {
    display: grid;
    gap: 6px;
    font-weight: 600;
}

.ask-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* Leaflet: tooltips and markers follow the page theme */
.leaflet-container { font: inherit; }
.trail-marker-label { font-weight: 600; }

@media (max-width: 800px) {
    .app {
        height: auto;
        min-height: 100%;
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto auto;
        grid-template-areas:
            "top"
            "side"
            "content"
            "stats";
    }

    .sidebar {
        border-right: none;
        padding-bottom: 12px;
    }

    .stats {
        border-right: none;
        padding-top: 16px;
        overflow: visible;
    }

    .topbar #current-user { display: none; }

    .users { overflow: visible; }

    .user-form { grid-template-columns: 1fr; }

    .map { height: 60vh; }
}
