:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --border: #e3e6eb;
    --text: #1b1f27;
    --muted: #667085;
    --primary: #2f5bea;
    --primary-hover: #2449c4;
    --danger-bg: #fdecec;
    --danger-text: #a12626;
    --success-bg: #e8f6ee;
    --success-text: #1d6b42;
    --radius: 10px;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12151b;
        --surface: #1b1f27;
        --border: #2c323d;
        --text: #e7e9ee;
        --muted: #98a1b2;
        --primary: #5b7ff5;
        --primary-hover: #7a97f7;
        --danger-bg: #3a1d1d;
        --danger-text: #f3a5a5;
        --success-bg: #173226;
        --success-text: #8fdcb2;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

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

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .25rem; }
.muted { color: var(--muted); margin-top: 0; }

.brand {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-weight: 700;
    font-size: 1.2rem;
    letter-spacing: -.02em;
}
.brand-mark { width: 30px; height: 30px; flex-shrink: 0; fill: var(--primary); }
.brand-os { color: var(--primary); }
.brand-env {
    font-size: .7rem;
    font-weight: 600;
    letter-spacing: .02em;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: .05rem .45rem;
}

.btn {
    display: inline-block;
    border: 1px solid transparent;
    border-radius: 8px;
    padding: .6rem 1rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: none; color: var(--muted); border-color: var(--border); padding: .35rem .75rem; }
.btn-ghost:hover { color: var(--text); }

.alert {
    background: var(--danger-bg);
    color: var(--danger-text);
    border-radius: 8px;
    padding: .65rem .85rem;
    margin-bottom: 1rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-top: 1.5rem;
}

.alert-success { background: var(--success-bg); color: var(--success-text); }

.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 1.25rem;
}
.back-link {
    display: inline-block;
    color: var(--muted);
    text-decoration: none;
    margin-bottom: .75rem;
    font-size: .9rem;
}
.back-link:hover { color: var(--text); }

.badge {
    display: inline-block;
    margin-left: .35rem;
    padding: .05rem .45rem;
    border-radius: 999px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: .75rem;
    font-weight: 600;
}
.status { font-size: .85rem; font-weight: 600; }
.status::before {
    content: "";
    display: inline-block;
    width: .5rem; height: .5rem;
    border-radius: 50%;
    margin-right: .4rem;
    vertical-align: middle;
}
.status-active { color: var(--success-text); }
.status-active::before { background: var(--success-text); }
.status-inactive { color: var(--muted); }
.status-inactive::before { background: var(--muted); }

.table-card { padding: 0; margin-top: 0; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
    text-align: left;
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.table th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tbody tr:last-child td { border-bottom: 0; }
.table .actions { text-align: right; white-space: nowrap; }
.table a { color: var(--primary); font-weight: 600; text-decoration: none; }
.table a:hover { text-decoration: underline; }

.form-narrow { max-width: 520px; margin-top: 1.25rem; }
.form fieldset {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: .75rem 1rem 1rem;
    margin: 0;
    /* Fieldsets default to min-content width, so a long <select> would push them off narrow screens. */
    min-width: 0;
    display: grid;
    gap: 1rem;
}
.form legend { font-weight: 600; padding: 0 .35rem; }
.form fieldset > p { margin: 0; }
.form small { font-weight: 400; font-size: .8rem; }
.form .checkbox { display: flex; align-items: center; gap: .5rem; }
.form .checkbox input { width: 1rem; height: 1rem; }
.field-error { color: var(--danger-text); font-weight: 500; font-size: .85rem; }
.form-actions { display: flex; gap: .5rem; align-items: center; }
.form .form-actions .btn { width: auto; margin-top: 0; }
.form .form-actions .btn-ghost { padding: .6rem 1rem; }

/* Auth pages */
body.auth {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 16px;
}
.auth-card {
    width: 100%;
    max-width: 400px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
}
.auth-card .brand { margin-bottom: 1.5rem; }

.form { display: grid; gap: 1rem; margin-top: 1.25rem; }
.form label { display: grid; gap: .35rem; font-weight: 600; font-size: .9rem; }
.form input,
.form select {
    font: inherit;
    padding: .6rem .75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
}
.form input:focus,
.form select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.form .choice { display: flex; align-items: flex-start; gap: .6rem; font-weight: 600; }
.form .choice input { width: 1rem; height: 1rem; margin: .2rem 0 0; flex-shrink: 0; }
.form .choice small.muted { display: block; margin-top: .15rem; }
.badge-recommended {
    display: inline-block;
    margin-left: .35rem;
    padding: .05rem .45rem;
    border-radius: 999px;
    background: var(--success-bg);
    color: var(--success-text);
    font-size: .7rem;
    font-weight: 600;
}
.form select { width: 100%; min-width: 0; }
.field-row { display: grid; grid-template-columns: minmax(0, 1fr) 6rem; gap: .75rem; }
.field-row input { min-width: 0; width: 100%; }
.form + .form { margin-top: 1rem; }
[hidden] { display: none !important; }
.form .btn { width: 100%; margin-top: .25rem; }

/* App shell */
body.app { display: flex; min-height: 100vh; }
.sidebar {
    width: 230px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.25rem 1rem;
}
.sidebar nav { display: grid; gap: .2rem; margin-top: 1.75rem; }
.sidebar nav a {
    color: var(--muted);
    text-decoration: none;
    padding: .5rem .75rem;
    border-radius: 8px;
    font-weight: 500;
}
.sidebar nav a:hover { color: var(--text); background: var(--bg); }
.sidebar nav a.active { color: var(--primary); background: var(--bg); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: .75rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.account-name { font-weight: 600; }
.user-menu { display: flex; align-items: center; gap: .75rem; color: var(--muted); }
.user-menu form { margin: 0; }
.btn-user {
    display: inline-flex;
    align-items: center;
    gap: .5rem;
    padding: .3rem .8rem .3rem .3rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
}
.btn-user:hover, .btn-user.active { background: var(--bg); border-color: var(--primary); }
.avatar {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: .85rem;
    font-weight: 700;
}

.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.label-row a, .auth-footer a { color: var(--primary); font-weight: 500; text-decoration: none; font-size: .85rem; }
.label-row a:hover, .auth-footer a:hover { text-decoration: underline; }
.auth-footer { margin: 1.25rem 0 0; text-align: center; }
.content { padding: 1.5rem; }

@media (max-width: 720px) {
    body.app { flex-direction: column; }
    .sidebar { width: auto; border-right: 0; border-bottom: 1px solid var(--border); padding: .75rem 16px; }
    .sidebar nav { display: flex; margin-top: .75rem; overflow-x: auto; }
    .topbar, .content { padding-left: 16px; padding-right: 16px; }
    .btn-user { padding: .3rem; }
    .btn-user-name { display: none; }
    .table thead { display: none; }
    .table, .table tbody, .table tr, .table td { display: block; }
    .table tr { padding: .75rem 1rem; border-bottom: 1px solid var(--border); }
    .table tbody tr:last-child { border-bottom: 0; }
    .table td { border: 0; padding: .15rem 0; }
    .table td[data-label="Email"], .table td[data-label="Last sign-in"] { color: var(--muted); font-size: .9rem; }
    .table .actions { text-align: left; padding-top: .4rem; }
}
