/* Autoview dashboard - shared styles. Tokens first; everything below uses them. */

:root {
    color-scheme: light;

    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-2: #f1f5f9;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --text: #0f172a;
    --text-2: #334155;
    --muted: #475569;          /* 7.6:1 on white, 7.0:1 on the tinted surfaces (was #64748b: 4.3:1 on them) */

    --primary: #1e40af;        /* white text: 8.6:1 */
    --primary-hover: #1e3a8a;
    --primary-soft: #dbeafe;
    --accent: #d97706;
    --ring: rgba(59, 130, 246, .55);

    --ok: #166534;       --ok-bg: #dcfce7;
    --warn: #92400e;     --warn-bg: #fef3c7;
    --bad: #b91c1c;      --bad-bg: #fee2e2;
    --info: #1e40af;     --info-bg: #dbeafe;
    --neutral: #475569;  --neutral-bg: #f1f5f9;

    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 1px 3px rgba(15, 23, 42, .06);

    --font: 'Fira Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --mono: 'Fira Code', ui-monospace, 'SF Mono', Menlo, monospace;
}

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

body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    min-height: 100vh;
}

h1, h2, h3, h4, h5 { font-family: var(--font); color: var(--text); font-weight: 600; }
a { color: var(--primary); }
a:hover { color: var(--primary-hover); }
code { font-family: var(--mono); font-size: .85em; color: var(--text-2); background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }
.num, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
td.num, th.num { white-space: nowrap; }
.text-muted, .form-text { color: var(--muted) !important; }
.small, small { font-size: .85rem; }

/* Visible keyboard focus everywhere */
:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; border-radius: 6px; }
.form-control:focus, .form-select:focus, .form-check-input:focus {
    border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring);
}

.skip-link {
    position: absolute; left: -999px; top: 8px; background: var(--primary); color: #fff;
    padding: 8px 14px; border-radius: var(--radius-sm); z-index: 100;
}
.skip-link:focus { left: 12px; }

/* ---------- App bar ---------- */
.appbar {
    position: sticky; top: 0; z-index: 50;
    background: rgba(255, 255, 255, .96);
    backdrop-filter: blur(6px);
    border-bottom: 1px solid var(--border);
}
.appbar-inner {
    max-width: 1200px; margin: 0 auto; padding: 0 16px;
    display: flex; align-items: center; gap: 8px; min-height: 56px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--text); text-decoration: none; margin-right: 12px; }
.brand:hover { color: var(--text); }
.brand-mark {
    width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff;
    display: inline-flex; align-items: center; justify-content: center; font-size: 1rem;
}
.nav-links { display: flex; gap: 2px; flex: 1 1 auto; overflow-x: auto; scrollbar-width: none; }
.nav-links::-webkit-scrollbar { display: none; }
.nav-link-item {
    display: inline-flex; align-items: center; gap: 8px; white-space: nowrap;
    padding: 8px 12px; min-height: 40px; border-radius: var(--radius-sm);
    color: var(--text-2); font-weight: 500; text-decoration: none;
    transition: background-color .15s ease, color .15s ease;
}
.nav-link-item:hover { background: var(--surface-2); color: var(--text); }
.nav-link-item[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.logout-form { margin-left: auto; }

/* ---------- Layout ---------- */
.page { max-width: 1200px; margin: 0 auto; padding: 24px 16px 56px; }
.page-header { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-title { font-size: 1.6rem; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.page-sub { color: var(--muted); margin: 2px 0 0; }
.back-link { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); text-decoration: none; font-weight: 500; margin-bottom: 6px; min-height: 32px; }
.back-link:hover { color: var(--primary); }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-header {
    background: transparent; border-bottom: 1px solid var(--border);
    padding: 14px 20px; font-weight: 600; display: flex; align-items: center; gap: 10px;
    border-radius: var(--radius) var(--radius) 0 0 !important;
}
.card-header .bi { color: var(--primary); font-size: 1.05rem; }
.card-header .hint { margin-left: auto; font-weight: 400; color: var(--muted); font-size: .85rem; }
.card-body { padding: 20px; }
.card.danger { border-color: #fecaca; }
.card.danger .card-header { color: var(--bad); border-bottom-color: #fecaca; }
.card.danger .card-header .bi { color: var(--bad); }

/* Collapsible section header (detail page) */
.section-toggle {
    display: flex; align-items: center; gap: 10px; width: 100%; min-height: 48px;
    background: none; border: 0; padding: 14px 20px; color: inherit; font: inherit; font-weight: 600; text-align: left; cursor: pointer;
    border-radius: var(--radius);
}
.section-toggle:hover { background: var(--surface-2); }
.section-toggle .bi:first-child { color: var(--primary); font-size: 1.05rem; }
.section-toggle .chevron { margin-left: auto; transition: transform .2s ease; color: var(--muted); }
.section-toggle[aria-expanded="true"] .chevron { transform: rotate(180deg); }
.section-toggle[aria-expanded="true"] { border-bottom: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0; }
.section-toggle .hint { font-weight: 400; color: var(--muted); font-size: .85rem; }
.card.danger .section-toggle { color: var(--bad); }
.card.danger .section-toggle .bi:first-child { color: var(--bad); }

/* ---------- Buttons ---------- */
.btn { border-radius: var(--radius-sm); font-weight: 500; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: background-color .15s ease, border-color .15s ease, color .15s ease; }
.btn-sm { min-height: 34px; padding: 4px 12px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover, .btn-primary:focus-visible { background: var(--primary-hover); border-color: var(--primary-hover); color: #fff; }
.btn-primary:disabled { background: var(--primary); border-color: var(--primary); opacity: .5; }
.btn-outline-secondary { color: var(--text-2); border-color: var(--border-strong); background: var(--surface); }
.btn-outline-secondary:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-strong); }
.btn-outline-primary { color: var(--primary); border-color: var(--primary); }
.btn-outline-primary:hover { background: var(--primary); color: #fff; }
.btn-outline-danger { color: var(--bad); border-color: #fca5a5; background: var(--surface); }
.btn-outline-danger:hover { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger { background: var(--bad); border-color: var(--bad); }
.btn-icon { min-width: 40px; padding-left: 0; padding-right: 0; }
.btn-sm.btn-icon { min-width: 34px; }

/* ---------- Forms ---------- */
.form-label { font-weight: 500; font-size: .875rem; color: var(--text-2); margin-bottom: 4px; }
.form-control, .form-select { border-radius: var(--radius-sm); border-color: var(--border-strong); min-height: 40px; color: var(--text); }
.form-control-sm { min-height: 34px; }
.form-control::placeholder { color: #94a3b8; }
.form-control:disabled, .form-select:disabled { background: var(--surface-2); }
.form-check-input:checked { background-color: var(--primary); border-color: var(--primary); }
.form-switch { padding-left: 3.1em; min-height: 1.6em; }
.form-switch .form-check-input { width: 2.5em; height: 1.35em; margin-left: -3.1em; cursor: pointer; }
.form-switch .form-check-label { cursor: pointer; }
.field-narrow { max-width: 200px; }
.field-medium { max-width: 320px; }

/* ---------- Alerts ---------- */
.alert { border-radius: var(--radius); border: 1px solid transparent; padding: 12px 16px; display: flex; gap: 10px; align-items: flex-start; }
.alert .bi { margin-top: 2px; flex-shrink: 0; }
.alert-success { background: var(--ok-bg); color: var(--ok); border-color: #bbf7d0; }
.alert-danger  { background: var(--bad-bg); color: var(--bad); border-color: #fecaca; }
.alert-warning { background: var(--warn-bg); color: var(--warn); border-color: #fde68a; }
.alert-info    { background: var(--info-bg); color: var(--info); border-color: #bfdbfe; }
.alert-secondary { background: var(--neutral-bg); color: var(--neutral); border-color: var(--border); }
.alert code { background: rgba(255,255,255,.6); }
.alert .btn-close { margin-left: auto; padding: 12px; }

/* ---------- Pills / badges ---------- */
.pill {
    display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
    padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600; line-height: 1.6;
}
.pill-ok { background: var(--ok-bg); color: var(--ok); }
.pill-bad { background: var(--bad-bg); color: var(--bad); }
.pill-warn { background: var(--warn-bg); color: var(--warn); }
.pill-info { background: var(--info-bg); color: var(--info); }
.pill-muted { background: var(--neutral-bg); color: var(--neutral); }
.badge { border-radius: 6px; font-weight: 600; }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 20px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.kpi-label { color: var(--muted); font-size: .8rem; font-weight: 500; text-transform: uppercase; letter-spacing: .04em; display: flex; align-items: center; gap: 6px; }
.kpi-value { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 1.75rem; font-weight: 600; line-height: 1.2; margin-top: 4px; }
.kpi-sub { color: var(--muted); font-size: .8rem; }
.kpi.is-bad .kpi-value { color: var(--bad); }
.kpi.is-ok .kpi-value { color: var(--ok); }

/* ---------- Meter (progress) ---------- */
.meter { height: 8px; background: var(--border); border-radius: 999px; overflow: hidden; min-width: 90px; }
.meter > span { display: block; height: 100%; border-radius: 999px; background: var(--primary); transition: width .3s ease; }
.meter.ok > span { background: #16a34a; }
.meter.bad > span { background: #dc2626; }
.meter.lg { height: 12px; }

/* ---------- Tables ---------- */
.table { --bs-table-bg: transparent; color: var(--text); margin-bottom: 0; }
.table > :not(caption) > * > * { padding: 12px 14px; border-bottom-color: var(--border); }
.table thead th { background: var(--surface-2); color: var(--text-2); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table-hover > tbody > tr:hover > * { background: #f8fafc; }
.table-sm > :not(caption) > * > * { padding: 8px 12px; }
/* position: relative keeps absolutely positioned bits (visually-hidden header cells) inside the scroller - without it they
   stretched the whole page sideways on phones. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; position: relative; }
.table-scroll:focus-visible { outline-offset: -3px; }
.table-scroll.tall { max-height: 320px; overflow-y: auto; }
.table-scroll.tall thead th { position: sticky; top: 0; z-index: 1; }

/* Toolbar above the posts table */
.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; padding: 14px 20px; border-bottom: 1px solid var(--border); }
.search { position: relative; flex: 1 1 240px; max-width: 360px; }
.search .bi { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { padding-left: 36px; }
.filter-select { width: auto; min-width: 9.5rem; min-height: 38px; }
.toolbar-chips { padding-top: 10px; padding-bottom: 10px; }
a.chip { text-decoration: none; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-top: 1px solid var(--border); }
.pager .page-link { min-width: 38px; min-height: 38px; display: inline-flex; align-items: center; justify-content: center; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
    display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 4px 12px;
    border: 1px solid var(--border-strong); border-radius: 999px; background: var(--surface);
    color: var(--text-2); font-weight: 500; font-size: .85rem; cursor: pointer;
    transition: background-color .15s ease, border-color .15s ease;
}
.chip:hover { background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip .count { font-family: var(--mono); font-size: .75rem; opacity: .8; }

.post-id { font-family: var(--mono); font-weight: 600; color: var(--text); text-decoration: none; }
.post-id:hover { color: var(--primary); text-decoration: underline; }
.ext-link { color: var(--muted); font-size: .85rem; text-decoration: none; }
.ext-link:hover { color: var(--primary); }
.progress-cell { min-width: 170px; }
.progress-cell .meter { margin-top: 6px; }
.progress-body { min-width: 0; }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty .bi { font-size: 2rem; color: var(--border-strong); display: block; margin-bottom: 8px; }
.empty strong { color: var(--text-2); display: block; font-size: 1.05rem; }

/* ---------- Stat blocks (detail page) ---------- */
.stat { padding: 16px 18px; }
.stat-label { color: var(--muted); font-size: .8rem; font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 1.5rem; font-weight: 600; line-height: 1.25; }
.stat-sub { color: var(--muted); font-size: .85rem; }

.preview-frame { border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); background: #fff; max-width: 420px; margin: 0 auto; min-height: 120px; }

.account-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; }
.account-card.is-active { border-color: #86efac; box-shadow: 0 0 0 3px #dcfce7; }
.balance { font-family: var(--mono); font-weight: 600; background: var(--ok-bg); color: var(--ok); border-radius: 999px; padding: 4px 14px; display: inline-flex; align-items: center; gap: 6px; }

.anchor-nav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.anchor-nav a { display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 4px 12px; border: 1px solid var(--border-strong); border-radius: 999px; background: var(--surface); color: var(--text-2); font-weight: 500; font-size: .85rem; text-decoration: none; }
.anchor-nav a:hover { background: var(--surface-2); color: var(--primary); }
.stack > * + * { margin-top: 16px; }

/* ---------- Auth pages ---------- */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 10px 30px -12px rgba(15, 23, 42, .18); padding: 32px 28px; }
.auth-card .brand { margin-bottom: 18px; }
.auth-card h1 { font-size: 1.4rem; font-weight: 700; margin: 0 0 4px; }
.auth-card .sub { color: var(--muted); margin-bottom: 22px; }
.recovery-code { font-family: var(--mono); font-size: 1.6rem; font-weight: 600; letter-spacing: .12em; text-align: center; background: var(--surface-2); border: 1px dashed var(--border-strong); border-radius: var(--radius); padding: 14px; user-select: all; }

/* ---------- Responsive ---------- */
@media (max-width: 991.98px) {
    .kpis { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
}
@media (max-width: 720px) {
    body { font-size: 16px; }
    .appbar-inner { padding-top: 8px; padding-bottom: 6px; row-gap: 4px; }
    .brand { order: 1; margin-right: 0; }
    .logout-form { order: 2; margin-left: auto; }
    .nav-links { order: 3; flex: 1 1 100%; }
    .page { padding: 16px 12px 40px; }
    .page-title { font-size: 1.35rem; }
    .kpi-value { font-size: 1.4rem; }
    .field-narrow, .field-medium { max-width: none; }
    .card-body { padding: 16px; }
    .toolbar { padding: 12px 16px; }
    .search { max-width: none; }
    .filter-select { flex: 1 1 44%; min-width: 0; }
    .pager { justify-content: center; padding: 12px 16px; }

    /* Wide tables become a stack of cards instead of scrolling sideways (each cell carries a data-label). */
    .posts-table thead, .stack-table thead { display: none; }
    .posts-table, .posts-table tbody, .posts-table tr, .posts-table td,
    .stack-table, .stack-table tbody, .stack-table tr, .stack-table td { display: block; width: 100%; }
    .posts-table tr, .stack-table tr { padding: 12px 16px; border-bottom: 1px solid var(--border); }
    .posts-table td, .stack-table td { border: 0 !important; padding: 4px 0 !important; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; }
    .posts-table td::before, .stack-table td::before { content: attr(data-label); color: var(--muted); font-size: .78rem; font-weight: 500; text-transform: uppercase; letter-spacing: .04em; flex-shrink: 0; text-align: left; }
    .posts-table td.cell-action, .stack-table td.cell-action { justify-content: flex-end; padding-top: 10px !important; }
    .posts-table td.cell-action::before, .stack-table td.cell-action::before { display: none; }
    .stack-table tr { display: flex; flex-direction: column; }
    .stack-table td[data-primary] { order: -1; font-size: 1rem; padding-bottom: 8px !important; }
    .stack-table td[data-primary]::before { display: none; }
    .posts-table .progress-cell { min-width: 0; }
    .posts-table .progress-body { flex: 1; max-width: 62%; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; scroll-behavior: auto !important; }
}


.key-input { width: 12rem; min-width: 0; }
.stack-table td[data-label^="Panel"] { min-width: 230px; }   /* keep service names from wrapping into 5 lines */
.section-heading { margin: 0; font-size: inherit; font-weight: inherit; line-height: inherit; }

/* ---------- Accessibility and touch (audited with the UI/UX guidelines) ---------- */
html { scroll-padding-top: 76px; }                 /* the sticky header must never hide the control that has keyboard focus */
a, button, summary, input, select, textarea, label { touch-action: manipulation; }   /* no 300 ms tap delay */
code { font-size: max(.875em, 12px); }             /* Bootstrap's .875em fell to 11px inside small text */
.post-id, .ext-link, .back-link { display: inline-flex; align-items: center; min-height: 24px; }   /* standalone links: 24 px target minimum */
.form-check { min-height: 24px; }
.form-check-input { margin-top: .2em; }
.form-check-label { line-height: 24px; }
.post-id { min-width: 24px; justify-content: center; }
summary { min-height: 24px; line-height: 24px; cursor: pointer; }
.link-target { display: inline-flex; align-items: center; min-height: 24px; }   /* a standalone link (not inside a sentence) */

/* Phones and touch screens: every control is at least 44 px tall (and wide enough), with room between neighbours. */
@media (max-width: 720px), (pointer: coarse) {
    .btn, .btn-sm, .form-control, .form-control-sm, .form-select, .form-select-sm { min-height: 44px; }
    input, select, textarea, .form-control, .form-select, .form-select-sm, .form-control-sm { font-size: 16px; }   /* iOS zooms into smaller fields */
    .btn-icon, .btn-sm.btn-icon { min-width: 44px; }
    .nav-link-item, .brand, .back-link, .post-id, .ext-link, .link-target, .anchor-nav a, summary, .skip-link { min-height: 44px; }
    summary { display: flex; align-items: center; padding-block: 2px; }
    .post-id { min-width: 44px; }
    .dropdown-toggle-split { min-width: 44px; }
    .brand { padding-right: 4px; }
    .chip { min-height: 44px; padding: 6px 14px; }
    .pager .page-link { min-width: 44px; min-height: 44px; }
    .form-check { min-height: 44px; display: flex; align-items: center; gap: .5rem; padding-left: 0; }
    .form-check .form-check-input { float: none; margin: 0; flex-shrink: 0; width: 1.35rem; height: 1.35rem; }
    .form-switch .form-check-input { width: 2.75rem; height: 1.5rem; margin-left: 0; }
    .dropdown-item { min-height: 44px; display: flex; align-items: center; }
    .btn-group > .btn + .btn, .toolbar { gap: 8px; }
    .table td .btn, .table td a.btn, .table td button { margin-block: 2px; }
}
