:root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --text: #1d2430;
    --muted: #6a7382;
    --border: #e1e5eb;
    --accent: #1f6feb;
    --danger: #c62828;
    --ok-bg: #e5f5ea; --ok: #1e7a3c;
    --info-bg: #e7f0fd; --info: #1f5fbf;
    --warn-bg: #fff4e0; --warn: #9a5b00;
    --bad-bg: #fdeaea; --bad: #b3261e;
    --idle-bg: #eef0f3; --idle: #555d6b;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    color: var(--text);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12151a; --surface: #1a1f26; --text: #e6e9ee; --muted: #98a1ae; --border: #2a313b;
        --accent: #6ea8ff; --danger: #ff6b6b;
        --ok-bg: #173523; --ok: #7fd69a; --info-bg: #182a45; --info: #8db8ff;
        --warn-bg: #3a2c12; --warn: #f0c060; --bad-bg: #3d1a1a; --bad: #ff8a80;
        --idle-bg: #262c35; --idle: #aab2be;
    }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 48px; }
h1 { font-size: 1.5rem; margin: 0 0 16px; }
h2 { font-size: 1.1rem; margin: 32px 0 12px; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.small { font-size: 0.85rem; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.topbar { display: flex; align-items: center; gap: 24px; padding: 12px 16px; background: var(--surface);
    border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.topbar .brand { font-weight: 600; color: var(--text); }
.topbar nav { display: flex; gap: 16px; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: var(--muted); }
.topbar nav a.active { color: var(--text); font-weight: 600; }

table { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--surface); border: 1px solid var(--border);
    border-radius: 8px; overflow: hidden; }
/* Narrow screens: the table scrolls sideways instead of overflowing the page. */
@media (max-width: 760px) {
    table { display: block; overflow-x: auto; }
}
th, td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: none; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; }
.status-queued, .status-expired { background: var(--idle-bg); color: var(--idle); }
.status-dispatched, .status-accepted { background: var(--warn-bg); color: var(--warn); }
.status-sent { background: var(--info-bg); color: var(--info); }
.status-delivered { background: var(--ok-bg); color: var(--ok); }
.status-failed { background: var(--bad-bg); color: var(--bad); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px;
    display: flex; flex-direction: column; gap: 4px; }
.tile-label { color: var(--muted); font-size: 0.85rem; }
.tile-value { font-size: 1.8rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile-note { color: var(--muted); font-size: 0.78rem; }

/* ---- Overview chart (palette validated for colour-vision deficiencies, light and dark) ---- */
:root {
    --series-delivered: #0ca30c;
    --series-sent: #2a78d6;
    --series-failed: #d03b3b;
    --series-expired: #eda100;
    --chart-grid: #e8ebef;
}
@media (prefers-color-scheme: dark) {
    :root {
        --series-sent: #3987e5;
        --series-expired: #c98500;
        --chart-grid: #2a313b;
    }
}
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px; margin-top: 16px; }
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.chart-head h2 { margin: 0; }
.legend { list-style: none; display: flex; gap: 16px; margin: 0; padding: 0; flex-wrap: wrap; font-size: 0.85rem; color: var(--muted); }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: currentColor; margin-right: 2px; }
.swatch.series-delivered { background: var(--series-delivered); }
.swatch.series-sent { background: var(--series-sent); }
.swatch.series-failed { background: var(--series-failed); }
.swatch.series-expired { background: var(--series-expired); }
.chart-empty { margin: 24px 0; text-align: center; }
.chart-wrap { position: relative; }
.chart { display: block; width: 100%; height: auto; overflow: visible; }
.chart-grid line { stroke: var(--chart-grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart-axis-label, .chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart path.series-delivered { fill: var(--series-delivered); }
.chart path.series-sent { fill: var(--series-sent); }
.chart path.series-failed { fill: var(--series-failed); }
.chart path.series-expired { fill: var(--series-expired); }
.chart-hit { fill: transparent; }
.chart-bar { outline: none; cursor: default; }
.chart-bar.active .chart-hit, .chart-bar:focus-visible .chart-hit { fill: var(--chart-grid); opacity: 0.6; }
.chart-tooltip { position: absolute; top: -8px; transform: translateY(-100%); min-width: 160px; pointer-events: none;
    background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px;
    padding: 8px 10px; font-size: 0.82rem; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); z-index: 2; }
.chart-tooltip table { border: none; display: table; width: 100%; margin-top: 4px; background: none; }
.chart-tooltip td { padding: 2px 0; border: none; }
.chart-tooltip .tip-total td { border-top: 1px solid var(--border); padding-top: 4px; font-weight: 600; }

input, select { font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
    background: var(--surface); color: var(--text); }
button { font: inherit; padding: 7px 14px; border: none; border-radius: 6px; background: var(--accent);
    color: #fff; cursor: pointer; }
button.small { padding: 4px 10px; font-size: 0.85rem; }
button.danger { background: var(--danger); }
button.link { background: none; color: var(--muted); padding: 0; }
.filters, .inline-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 16px; }
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.inline-form input { min-width: 260px; }

.notice { background: var(--info-bg); color: var(--text); border-radius: 8px; padding: 12px 16px; margin-bottom: 16px; }
.notice-error { background: var(--bad-bg); }
.secret { display: block; margin-top: 8px; padding: 8px; background: var(--surface); border-radius: 6px;
    word-break: break-all; user-select: all; }

.details { display: grid; grid-template-columns: max-content 1fr; gap: 6px 24px; background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; padding: 16px; margin: 0 0 16px; }
.details dt { color: var(--muted); }
.details dd { margin: 0; word-break: break-word; }

.pager { display: flex; gap: 16px; justify-content: center; margin-top: 16px; color: var(--muted); }

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }

/* ---- Brand in the top bar ---- */
.topbar .brand { display: inline-flex; align-items: center; gap: 10px; }
.topbar .brand img { width: 28px; height: 28px; }

/* ---- Sign-in page: form on the left, brand panel on the right ---- */
body.auth { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); min-height: 100vh; background: var(--surface); }
.auth-form-side { display: flex; align-items: center; justify-content: center; padding: 48px 24px; }
.auth-form-wrap { width: 100%; max-width: 400px; }
.auth-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 40px; }
.auth-brand div { display: flex; flex-direction: column; line-height: 1.15; }
.auth-brand-name { font-size: 1.25rem; font-weight: 600; letter-spacing: -0.01em; }
.auth-brand-sub { font-size: 0.85rem; color: var(--accent); }
.auth h1 { font-size: 1.6rem; margin: 0 0 6px; letter-spacing: -0.01em; }
.auth-form-wrap > .muted { margin: 0 0 28px; }
.auth-form { gap: 18px; }
.auth-form label { color: var(--text); font-weight: 600; font-size: 0.9rem; gap: 6px; }
.auth-form .required { color: var(--danger); }
.auth-form input { width: 100%; padding: 11px 12px; font-weight: 400; }
.auth-form input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
.password-field { position: relative; display: block; }
.password-field input { padding-right: 44px; }
.password-toggle { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); background: none; color: var(--muted);
    padding: 6px; display: grid; place-items: center; }
.password-toggle:hover, .password-toggle[aria-pressed="true"] { color: var(--text); }
.auth-submit { margin-top: 6px; padding: 11px 14px; font-weight: 600; }
.auth-submit:hover { filter: brightness(1.08); }

.auth-hero { position: relative; overflow: hidden; color: #fff;
    background: linear-gradient(150deg, #3b8bff 0%, #1d4ed8 55%, #1a3aa8 100%);
    display: flex; align-items: center; padding: 48px clamp(32px, 6vw, 96px); }
.auth-hero-art { position: absolute; inset: 0; width: 100%; height: 100%; color: #fff; pointer-events: none; }
.auth-hero-content { position: relative; max-width: 520px; }
.auth-hero-brand { display: flex; align-items: center; gap: 10px; font-size: 1.25rem; font-weight: 600; margin-bottom: 28px; }
.auth-hero h2 { font-size: clamp(1.5rem, 2.4vw, 2rem); line-height: 1.25; margin: 0 0 14px; letter-spacing: -0.01em; }
.auth-hero p { margin: 0 0 24px; opacity: 0.92; line-height: 1.5; }
.auth-hero ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 16px; line-height: 1.5; }
.auth-hero li { opacity: 0.95; }

@media (max-width: 860px) {
    body.auth { grid-template-columns: 1fr; }
    .auth-hero { display: none; }
}

/* ---- Device pairing ---- */
button.secondary { background: none; color: var(--accent); border: 1px solid var(--border); }
.pairing { display: grid; grid-template-columns: minmax(220px, 320px) 1fr; gap: 32px; align-items: start;
    background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 24px; }
/* A QR code must stay dark on light to be readable, in dark mode too. */
.pairing-qr { background: #fff; border-radius: 8px; padding: 12px; }
.pairing-qr svg { display: block; width: 100%; height: auto; }
.pairing ol { margin: 0 0 16px; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; line-height: 1.5; }
.pairing-link { word-break: break-all; user-select: all; font-size: 0.8rem; }
@media (max-width: 760px) { .pairing { grid-template-columns: 1fr; } }

/* ---- Empty states: the component stays in place, with a line that says why it is empty ---- */
.empty-row td { text-align: center; color: var(--muted); padding: 28px 12px; white-space: normal; }
.chart-empty-note { position: absolute; inset: 0 0 28px 40px; display: grid; place-items: center; margin: 0; text-align: center;
    color: var(--muted); font-size: 0.9rem; pointer-events: none; }
.pager { align-items: center; }
.pager-link { padding: 6px 12px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.pager-link:hover { text-decoration: none; border-color: var(--accent); color: var(--accent); }
.pager-link.disabled { color: var(--muted); opacity: 0.55; pointer-events: none; }
.pager-info { font-variant-numeric: tabular-nums; }

/* ---- Page header with its main action on the right ---- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin-bottom: 4px; }
.page-head p { margin: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- Row actions ---- */
td.actions { white-space: nowrap; text-align: right; }
td.actions form { display: inline; margin: 0; }
td.actions > * + * { margin-left: 6px; }
button.danger-solid { background: var(--danger); color: #fff; }

/* ---- Numbered pagination ---- */
.pager { flex-wrap: wrap; }
.pager-pages { display: inline-flex; gap: 4px; }
.pager-number { min-width: 34px; height: 34px; padding: 0 8px; display: inline-grid; place-items: center; border-radius: 6px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text); font-variant-numeric: tabular-nums; }
.pager-number:hover { text-decoration: none; border-color: var(--accent); color: var(--accent); }
.pager-number.current { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.pager-info { flex-basis: 100%; text-align: center; font-size: 0.85rem; }

/* ---- Dialogs (see js/ui.js) ---- */
dialog.modal { padding: 0; border: none; background: transparent; color: var(--text); max-width: min(560px, calc(100vw - 24px));
    width: 100%; max-height: calc(100vh - 32px); overflow: visible; }
dialog.modal.modal-small { max-width: min(420px, calc(100vw - 24px)); }
dialog.modal.modal-wide { max-width: min(880px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(8, 12, 24, .55); backdrop-filter: blur(3px); }
dialog.modal[open] { animation: modal-in .22s cubic-bezier(.2, .7, .2, 1); }
dialog.modal[open]::backdrop { animation: modal-fade .22s ease; }
@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { dialog.modal[open], dialog.modal[open]::backdrop { animation: none; } }
.modal-box { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 24px 64px rgba(8, 12, 24, .3);
    max-height: calc(100vh - 32px); overflow: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px 0; }
.modal-head h2 { margin: 0; font-size: 1.15rem; }
.modal-close { font-size: 1.5rem; line-height: 1; padding: 4px 8px !important; border-radius: 8px; color: var(--muted); }
.modal-close:hover { background: var(--bg); color: var(--text); }
.modal-body { padding: 14px 20px 4px; }
.modal-body > p { margin: 0; }
.modal-body label input, .modal-body label select { width: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 16px 20px 18px; }
.confirm-text { line-height: 1.5; }
body.is-busy { cursor: progress; }

/* ---- A wide table scrolls sideways inside its own frame instead of widening the page ---- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 8px; }
.table-wrap > table { min-width: 100%; overflow: visible; }
@media (max-width: 760px) {
    .table-wrap > table { display: table; }
}
/* The row actions stay in view on the right while a wide table scrolls. */
.table-wrap th.actions, .table-wrap td.actions { position: sticky; right: 0; box-shadow: -10px 0 12px -12px rgba(16, 24, 48, .35); }
.table-wrap td.actions { background: var(--surface); }
.table-wrap th.actions { background: inherit; }

/* ---- Webhooks ---- */
.event-badge { background: var(--idle-bg); color: var(--idle); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 500; margin: 1px 4px 1px 0; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-start; }
.head-actions form { margin: 0; }
form.inline { display: inline; margin-left: 10px; }
.details dd form.inline button { font-size: 0.85rem; color: var(--accent); }
.payload summary { cursor: pointer; color: var(--accent); font-size: 0.85rem; }
.payload pre { margin: 8px 0 0; max-width: 520px; white-space: pre-wrap; word-break: break-all; font-size: 0.78rem;
    background: var(--bg); border-radius: 8px; padding: 10px; }
.code-block { margin: 8px 0 0; padding: 12px 14px; border-radius: 8px; background: #0b1020; color: #dbe3ff; font-size: 0.82rem; overflow-x: auto; }
.webhook-help h2, .danger-zone h2 { margin: 0 0 6px; font-size: 1rem; }
.webhook-help p, .danger-zone p { margin: 0; }
.danger-zone { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
    border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); margin-top: 24px; }
.checks { border: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.checks legend { padding: 0; margin-bottom: 6px; font-weight: 600; font-size: 0.88rem; color: var(--text); }
.checks label, .check-line { display: flex !important; flex-direction: row !important; align-items: center; gap: 10px; font-weight: 400 !important; cursor: pointer; }
.checks input, .check-line input { width: auto !important; accent-color: var(--accent); }
a.button-secondary { display: inline-block; padding: 4px 10px; border: 1px solid var(--border); border-radius: 6px; color: var(--accent); font-size: 0.85rem; }
a.button-secondary:hover { text-decoration: none; border-color: var(--accent); }
