/*
 * Adminpanelen. Desktop first: 1280–1920 px är huvudfallet (fast vänstermeny, innehållet använder bredden, täta
 * tabeller, flera kolumner). Under 900 px: menyn blir en knapp med utfällbar panel, kolumnerna staplas, tabeller rullar
 * i sidled i sin ruta (eller blir kort med .table-cards), och knappar och fält blir stora nog att trycka på.
 * Ljust och mörkt följer systemet. Inga inline-stilar (CSP): bara klasserna här.
 *
 * Klasserna för delarna (se också docs/ADMIN.md):
 *   Sida       .page-head (h1 + .page-actions), .toolbar (sök, filter, knappar på en rad), .section-title
 *   Rutnät     .grid-cols (2 kolumner), .grid-cols-3, .grid-cols-4, .grid-sidebar (smal + bred), .span-2, .span-all
 *   Kort       .card > .card-head (h2 + .card-actions) + .card-body; .card-flush (tabell kant i kant)
 *   Siffror    .stat-grid > .stat (.stat-label, .stat-value, .stat-sub; .stat-ok/-warn/-bad)
 *   Tabeller   .table-wrap > table.table (.table-cards = kort på mobil, td[data-label]); .num, .nowrap, .truncate,
 *              tr.is-clickable, tr.is-muted
 *   Märken     .badge + .badge-neutral/-ok/-warn/-bad/-info
 *   Knappar    .btn, .btn-primary, .btn-danger, .btn-small, .btn-link, .btn-icon
 *   Formulär   .form, .field (label > span + input), .field-row, .input, .select, .textarea, .check
 *   Flikar     .tabs > .tab-list > .tab(.is-active) + .tab-panel
 *   Övrigt     .kv (dl med dt/dd), .muted, .mono, .pre (rå text, JSON), .alert(-error/-ok/-info), .empty,
 *              .pager, .masked, .chart (grafens ruta), .map (kartans ruta), .mail-frame (sandbox-iframe)
 */

:root {
    color-scheme: light dark;
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #dfe3e8;
    --border-strong: #c7cdd5;
    --text: #1c2430;
    --text-muted: #5f6b7a;
    --accent: #2f6f4f;
    --accent-strong: #245a3f;
    --accent-soft: #e3f0e9;
    --on-accent: #ffffff;
    --ok: #237a45;
    --ok-soft: #e1f3e7;
    --warn: #8a5a00;
    --warn-soft: #fdf0d5;
    --bad: #b42318;
    --bad-soft: #fde7e5;
    --info: #1f5fa8;
    --info-soft: #e3eefb;
    --focus: #3b82f6;
    --sidebar-w: 232px;
    --radius: 6px;
    --gap: 16px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111418;
        --surface: #191d23;
        --surface-2: #20252c;
        --border: #2c333c;
        --border-strong: #3a434e;
        --text: #e5e9ee;
        --text-muted: #9aa5b1;
        --accent: #5fb98a;
        --accent-strong: #79c99e;
        --accent-soft: #1d3328;
        --on-accent: #0d1a13;
        --ok: #5fc185;
        --ok-soft: #173023;
        --warn: #e5b350;
        --warn-soft: #3a2e14;
        --bad: #f07b70;
        --bad-soft: #3d1c1a;
        --info: #77aef0;
        --info-soft: #182a40;
        --focus: #60a5fa;
        --shadow: none;
    }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 var(--font);
    overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
p { margin: 0 0 8px; }

[hidden] { display: none !important; }

/* Skalet */

.shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100vh;
}

.topbar { display: none; }

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 16px 12px;
    overflow-y: auto;
}

.brand {
    color: var(--text);
    font-weight: 700;
    font-size: 15px;
    letter-spacing: 0.01em;
}
.brand:hover { text-decoration: none; }
.brand-side { display: block; padding: 4px 10px 16px; }

.nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.nav-list a {
    display: block;
    padding: 7px 10px;
    border-radius: var(--radius);
    color: var(--text);
    font-weight: 500;
}
.nav-list a:hover { background: var(--surface-2); text-decoration: none; }
.nav-list a.is-active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 650; }

.sidebar-foot {
    margin-top: auto;
    padding: 12px 10px 0;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.admin-name { color: var(--text-muted); font-size: 13px; overflow-wrap: anywhere; }

.nav-backdrop { display: none; }

.view {
    min-width: 0;
    padding: 20px 28px 48px;
    display: flex;
    flex-direction: column;
    gap: var(--gap);
}
.view:focus { outline: none; }

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.section-title { font-size: 13px; font-weight: 650; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar .input { width: 280px; max-width: 100%; }
.toolbar .spacer { flex: 1; }

/* Rutnät */

.grid-cols, .grid-cols-3, .grid-cols-4, .grid-sidebar { display: grid; gap: var(--gap); align-items: start; }
.grid-cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-sidebar { grid-template-columns: minmax(260px, 340px) minmax(0, 1fr); }
.span-2 { grid-column: span 2; }
.span-all { grid-column: 1 / -1; }

@media (max-width: 1280px) {
    .grid-cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Kort */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    min-width: 0;
}
.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
}
.card-actions { display: flex; gap: 6px; align-items: center; }
.card-body { padding: 12px 14px; min-width: 0; }
.card-flush > .card-body { padding: 0; }
.card-flush .table-wrap { border: 0; border-radius: 0; }

/* Siffror */

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    gap: 12px;
}
.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 14px;
    min-width: 0;
}
.stat-label { color: var(--text-muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.stat-value { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
.stat-sub { color: var(--text-muted); font-size: 12px; }
.stat-ok { border-left: 3px solid var(--ok); }
.stat-warn { border-left: 3px solid var(--warn); }
.stat-bad { border-left: 3px solid var(--bad); }

/* Tabeller */

.table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    max-width: 100%;
}
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td {
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}
.table th {
    position: sticky;
    top: 0;
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
.table tr.is-clickable { cursor: pointer; }
.table tr.is-muted td { color: var(--text-muted); }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.truncate { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Märken */

.badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    line-height: 18px;
    white-space: nowrap;
}
.badge-neutral { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }
.badge-info { background: var(--info-soft); color: var(--info); }

/* Knappar */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 32px;
    padding: 5px 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-weight: 550;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger:hover { filter: brightness(0.95); background: var(--bad); }
.btn-small { min-height: 26px; padding: 2px 8px; font-size: 13px; }
.btn-link { border-color: transparent; background: transparent; color: var(--accent); padding-left: 4px; padding-right: 4px; }
.btn-link:hover { background: transparent; text-decoration: underline; }
.btn-icon { width: 36px; padding: 0; }

/* Formulär */

.form { display: flex; flex-direction: column; gap: 12px; }
.form-inline { display: inline; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--text-muted); }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row > .field { flex: 1 1 200px; }
.input, .select, .textarea,
.field input, .field select, .field textarea {
    width: 100%;
    min-height: 32px;
    padding: 5px 10px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font: inherit;
}
.textarea, .field textarea { min-height: 120px; resize: vertical; }
.check { display: inline-flex; gap: 6px; align-items: center; }

/* Flikar */

.tabs { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.tab-list {
    display: flex;
    gap: 2px;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    scrollbar-width: thin;
}
.tab {
    padding: 7px 12px;
    border: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    color: var(--text-muted);
    font: inherit;
    font-weight: 550;
    cursor: pointer;
    white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent-strong); border-bottom-color: var(--accent); }
.tab-panel { min-width: 0; }

/* Övrigt */

.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 16px; margin: 0; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.muted { color: var(--text-muted); }
.mono { font-family: var(--mono); font-size: 12.5px; }
.pre {
    font-family: var(--mono);
    font-size: 12.5px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 12px;
    margin: 0;
    max-height: 70vh;
    overflow: auto;
}
.alert { padding: 8px 12px; border-radius: var(--radius); margin: 0; border: 1px solid transparent; }
.alert-error { background: var(--bad-soft); color: var(--bad); border-color: var(--bad); }
.alert-ok { background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }
.alert-info { background: var(--info-soft); color: var(--info); border-color: var(--info); }
.empty { color: var(--text-muted); padding: 16px; text-align: center; margin: 0; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: flex-end; padding: 8px 0; }
.masked { font-family: var(--mono); }
.chart { width: 100%; min-height: 220px; }
.map { width: 100%; height: 480px; border-radius: var(--radius); border: 1px solid var(--border); }
.mail-frame { width: 100%; min-height: 480px; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }

/* Inloggningen */

.auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.auth-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 24px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.auth-card .btn-primary { min-height: 40px; }
.code-input { font-family: var(--mono); font-size: 22px !important; letter-spacing: 0.3em; text-align: center; }

/* Mobil och smal skärm: fungerar, men panelen är byggd för skrivbordet. */

@media (max-width: 900px) {
    body { font-size: 15px; }

    .shell { display: block; }

    .topbar {
        display: flex;
        position: sticky;
        top: 0;
        z-index: 20;
        align-items: center;
        gap: 10px;
        height: 52px;
        padding: 0 12px;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
    }
    .nav-toggle-bars,
    .nav-toggle-bars::before,
    .nav-toggle-bars::after {
        display: block;
        width: 18px;
        height: 2px;
        background: var(--text);
        border-radius: 1px;
        position: relative;
    }
    .nav-toggle-bars::before, .nav-toggle-bars::after { content: ""; position: absolute; left: 0; }
    .nav-toggle-bars::before { top: -6px; }
    .nav-toggle-bars::after { top: 6px; }

    .sidebar {
        position: fixed;
        z-index: 40;
        top: 0;
        left: 0;
        width: min(300px, 85vw);
        height: 100vh;
        transform: translateX(-100%);
        transition: transform 0.18s ease;
        box-shadow: 0 0 24px rgba(0, 0, 0, 0.25);
    }
    .nav-open .sidebar { transform: none; }
    .nav-open .nav-backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.35); }
    .nav-list a { padding: 11px 12px; }

    .view { padding: 14px 12px 40px; }

    .grid-cols, .grid-cols-3, .grid-cols-4, .grid-sidebar { grid-template-columns: minmax(0, 1fr); }
    .span-2 { grid-column: auto; }
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .stat-value { font-size: 20px; }

    .btn { min-height: 40px; padding: 8px 14px; }
    .btn-small { min-height: 34px; padding: 4px 10px; }
    .btn-icon { width: 40px; padding: 0; }
    .input, .select, .textarea, .field input, .field select, .field textarea { min-height: 42px; font-size: 16px; }
    .toolbar .input { width: 100%; }
    .tab { padding: 10px 12px; }
    .truncate { max-width: 60vw; }

    .table-cards thead { display: none; }
    .table-cards, .table-cards tbody, .table-cards tr, .table-cards td { display: block; width: 100%; }
    .table-cards tr { padding: 8px 12px; border-bottom: 1px solid var(--border); }
    .table-cards tbody tr:last-child { border-bottom: 0; }
    .table-cards td { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border: 0; text-align: right; }
    .table-cards td::before { content: attr(data-label); color: var(--text-muted); font-weight: 600; text-align: left; flex: none; }
    .table-cards td.num { white-space: normal; }

    .kv { grid-template-columns: minmax(0, 1fr); gap: 0; }
    .kv dd { margin-bottom: 8px; }
    .map { height: 320px; }
}
