:root {
    --bg: #f5f4fb;
    --surface: #ffffff;
    --surface-alt: #f6f4fc;
    --border: #e7e3f3;
    --text: #1e1b2e;
    --text-muted: #6f6a85;
    --text-faint: #a6a1ba;

    --primary: #6d5bd0;
    --primary-hover: #5b49c2;
    --primary-soft: #efebfc;
    --primary-soft-hover: #e3ddf9;

    --success-bg: #e5f7ea;
    --success-text: #1c8a44;
    --warn-bg: #fff4dc;
    --warn-text: #9a6a00;
    --error-bg: #fce8e8;
    --error-text: #c22b2b;

    --radius-lg: 18px;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow-sm: 0 1px 2px rgba(30, 20, 60, 0.05);
    --shadow: 0 2px 8px rgba(30, 20, 60, 0.06), 0 12px 28px rgba(30, 20, 60, 0.07);
    --shadow-lg: 0 24px 60px rgba(20, 10, 45, 0.22);

    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #16141f;
        --surface: #1e1c2a;
        --surface-alt: #262436;
        --border: #322f45;
        --text: #f1eefb;
        --text-muted: #a49fbf;
        --text-faint: #716c8c;

        --primary: #9686f0;
        --primary-hover: #ab9ffb;
        --primary-soft: #2c2745;
        --primary-soft-hover: #383159;

        --success-bg: #163826;
        --success-text: #6fe0a0;
        --warn-bg: #3a2f0e;
        --warn-text: #f0c05a;
        --error-bg: #3a1f22;
        --error-text: #f28b8b;

        --shadow: 0 2px 8px rgba(0, 0, 0, 0.25), 0 12px 28px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.55);
    }
}

* { box-sizing: border-box; }

html { font-size: 18px; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    margin: 0;
    padding: 0;
    min-height: 100vh;
    line-height: 1.55;
    font-size: 1.05rem;
    -webkit-font-smoothing: antialiased;
}

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

/* ---------- Top navigation ---------- */
.topnav {
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topnav-inner {
    max-width: 1240px;
    margin: 0 auto;
    padding: 0.85em 1.8em;
    display: flex;
    align-items: center;
    gap: 2em;
}
.brand {
    display: flex;
    align-items: center;
    gap: 0.6em;
    font-weight: 700;
    color: var(--text);
    text-decoration: none;
    font-size: 1.05em;
    white-space: nowrap;
}
.brand img { height: 36px; width: auto; background: #fff; border-radius: 6px; padding: 3px 6px; }
.nav-links {
    display: flex;
    gap: 0.3em;
    flex: 1;
    flex-wrap: wrap;
}
.nav-links a {
    color: var(--text-muted);
    text-decoration: none;
    font-weight: 500;
    font-size: 1em;
    padding: 0.5em 0.9em;
    border-radius: var(--radius-sm);
    transition: background 0.15s, color 0.15s;
}
.nav-links a:hover { background: var(--surface-alt); color: var(--text); }
.nav-links a.active { background: var(--primary-soft); color: var(--primary); }
.nav-user {
    display: flex;
    align-items: center;
    gap: 0.6em;
    white-space: nowrap;
}
.avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9em;
    font-weight: 700;
}
.nav-user-name {
    font-size: 0.95em;
    color: var(--text-muted);
    font-weight: 500;
}
.nav-logout {
    font-size: 0.9em;
    color: var(--text-faint);
    text-decoration: none;
    border: 1px solid var(--border);
    padding: 0.35em 0.7em;
    border-radius: var(--radius-sm);
    transition: color 0.15s, border-color 0.15s;
}
.nav-logout:hover { color: var(--error-text); border-color: var(--error-text); }

/* ---------- Page shell ---------- */
.page {
    max-width: 1240px;
    margin: 0 auto;
    padding: 2.6em 1.8em 4.5em;
}
.page-narrow {
    max-width: 540px;
    padding-top: 4em;
}
.auth-shell {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}
.auth-shell .page { padding-top: 0; }

h1 {
    color: var(--text);
    font-size: 1.9em;
    font-weight: 800;
    letter-spacing: -0.02em;
    margin: 0 0 0.9em;
}
.auth-shell h1 {
    text-align: center;
    color: var(--primary);
}
.auth-logo {
    display: block;
    margin: 0 auto 1.2em;
    height: 66px;
    width: auto;
    background: #fff;
    border-radius: 12px;
    padding: 10px 16px;
}

/* ---------- Cards & forms ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 2em;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    max-width: 580px;
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 1.2em;
    margin: 0 auto 1.8em;
}
label {
    display: block;
    margin-top: 0;
    font-weight: 600;
    font-size: 0.95em;
    color: var(--text-muted);
}
select, input[type="text"], input[type="date"], input[type="password"], input[type="email"], input[type="number"] {
    width: 100%;
    padding: 0.7em 0.85em;
    margin-top: 0.4em;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-alt);
    color: var(--text);
    font-size: 1.05em;
    font-family: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}
select:focus, input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}
.hint {
    font-weight: normal;
    color: var(--text-faint);
    font-size: 0.92em;
}

/* ---------- Buttons ---------- */
.btn, button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4em;
    padding: 0.75em 1.4em;
    background: var(--primary);
    border: none;
    border-radius: var(--radius-sm);
    color: #fff;
    font-weight: 600;
    font-size: 1.03em;
    font-family: inherit;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}
.btn:hover, button:hover { background: var(--primary-hover); }
.btn:active, button:active { transform: translateY(1px); }
.btn-secondary, dialog button[type="button"] {
    background: var(--primary-soft);
    color: var(--primary);
}
.btn-secondary:hover, dialog button[type="button"]:hover { background: var(--primary-soft-hover); }
.button-row {
    display: flex;
    justify-content: space-between;
    gap: 1em;
    margin-top: 0.3em;
}
.button-row > * { flex: 1; }
.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.9em;
    max-width: 460px;
    margin: 0 auto;
}
.action-btn {
    padding: 0.4em 0.85em;
    font-size: 0.9em;
    margin: 0.15em 0.25em 0.15em 0;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 0.7em;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    padding: 0.8em 1em;
    border-radius: var(--radius-sm);
}
.checkbox-row input {
    width: 20px;
    height: 20px;
    accent-color: var(--primary);
    margin: 0;
}

/* ---------- Flash messages ---------- */
.flash {
    padding: 0.9em 1.1em;
    border-radius: var(--radius-sm);
    margin-bottom: 1.2em;
    font-weight: 600;
    font-size: 1em;
}
.flash.ok { background: var(--success-bg); color: var(--success-text); }
.flash.dry { background: var(--warn-bg); color: var(--warn-text); }
.flash.error { background: var(--error-bg); color: var(--error-text); }

/* ---------- Dashboard (main menu) ---------- */
.status-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1.2em;
    margin-bottom: 2em;
}
.status-grid .card { margin: 0; max-width: none; }
.status-grid h3 {
    margin: 0 0 0.3em;
    font-size: 1.15em;
    font-weight: 700;
    color: var(--text);
}
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    gap: 1.2em;
}
.dash-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.7em;
    text-decoration: none;
    color: var(--text);
    box-shadow: var(--shadow-sm);
    transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
    display: flex;
    flex-direction: column;
    gap: 0.5em;
}
.dash-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow);
    border-color: var(--primary-soft-hover);
}
.dash-card .dash-icon {
    width: 50px;
    height: 50px;
    border-radius: var(--radius-sm);
    background: var(--primary-soft);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
}
.dash-card .dash-icon svg { width: 26px; height: 26px; }
.dash-card h3 {
    margin: 0.2em 0 0;
    font-size: 1.2em;
    font-weight: 700;
}
.dash-card p {
    margin: 0;
    font-size: 0.95em;
    color: var(--text-muted);
}
.dash-card.secondary .dash-icon { background: var(--surface-alt); color: var(--text-muted); }

/* ---------- Result box ---------- */
.result-box {
    max-width: 580px;
    width: 100%;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.2em;
    font-family: "SF Mono", Consolas, monospace;
    font-size: 0.88em;
    white-space: pre-wrap;
    word-break: break-all;
    margin: 0 auto 1.6em;
    color: var(--text-muted);
}

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 1em; }
th, td {
    text-align: left;
    padding: 0.75em 0.6em;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
th {
    color: var(--text-muted);
    font-size: 0.85em;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-weight: 700;
}
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-alt); }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-wrap table { min-width: max-content; }
.table-wrap td, .table-wrap th { white-space: nowrap; }

.comp-table-wrap { overflow-x: hidden; }
.comp-table { table-layout: fixed; font-size: 0.88em; }
.comp-table th, .comp-table td { white-space: normal; word-break: break-word; padding: 0.55em 0.5em; vertical-align: top; }
.comp-actions { display: flex; flex-direction: column; gap: 0.3em; align-items: flex-start; }
.comp-actions .action-btn { margin: 0; }
.comp-detail-row { display: none; }
.comp-detail-row.open { display: table-row; }
.comp-detail-row td { background: var(--surface-alt); padding: 0.9em 1em; }
.comp-detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.5em 1.2em; font-size: 0.95em; }

.badge {
    display: inline-block;
    padding: 0.3em 0.7em;
    border-radius: 999px;
    font-size: 0.88em;
    font-weight: 700;
    white-space: nowrap;
}
.badge-ok { background: var(--success-bg); color: var(--success-text); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge-error { background: var(--error-bg); color: var(--error-text); }
.badge-count { background: var(--primary-soft); color: var(--primary); }
.badge-muted { background: var(--surface-alt); color: var(--text-faint); }

.status-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.8em;
    padding: 0.5em 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.98em;
    overflow-x: auto;
}
.status-row:last-child { border-bottom: none; }
.status-row .label { color: var(--text-muted); white-space: nowrap; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 0.4em; flex-wrap: wrap; }
.tab {
    padding: 0.5em 1.1em;
    border-radius: 999px;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.98em;
    color: var(--text-muted);
    background: var(--surface-alt);
    border: 1px solid transparent;
    transition: background 0.15s, color 0.15s;
}
.tab:hover { background: var(--primary-soft); color: var(--primary); }
.tab.active { background: var(--primary); color: #fff; }

/* ---------- Modals ---------- */
dialog.modal {
    border: none;
    border-radius: var(--radius-lg);
    padding: 2em;
    box-shadow: var(--shadow-lg);
    width: 90%;
    max-width: 500px;
    background: var(--surface);
    color: var(--text);
}
dialog.modal::backdrop { background: rgba(20, 12, 40, 0.5); backdrop-filter: blur(2px); }
dialog.modal h3 { margin: 0 0 1em; color: var(--text); font-size: 1.3em; }
dialog.modal form { display: flex; flex-direction: column; gap: 1em; }
dialog.modal label { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6em; margin-top: 0.3em; }
.modal-actions button { padding: 0.65em 1.3em; }

/* ---------- Utility ---------- */
.hstack { display: flex; align-items: center; gap: 1em; flex-wrap: wrap; }

@media (max-width: 640px) {
    .topnav-inner { flex-wrap: wrap; row-gap: 0.5em; padding: 0.7em 1em; gap: 0.6em; }
    .nav-links {
        order: 3;
        flex: 1 1 100%;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0.2em;
        padding-bottom: 0.15em;
        scrollbar-width: none;
    }
    .nav-links::-webkit-scrollbar { display: none; }
    .nav-links a {
        flex: 0 0 auto;
        white-space: nowrap;
        padding: 0.45em 0.75em;
        font-size: 0.92em;
    }
    .nav-user-name { display: none; }
    .page { padding: 1.4em 1em 3em; }
}
