:root {
    --bg: #f6f6f3;
    --surface: #ffffff;
    --surface-2: #f0f0ec;
    --border: #e3e3dd;
    --text: #1b1b18;
    --text-2: #55554f;
    --muted: #8a8a82;
    --accent: #3f4fd8;
    --accent-ink: #ffffff;
    --accent-soft: #e8eafc;
    --bar: #3f4fd8;
    --bar-2: #c3c8f4;
    --grid: #ececE6;
    --good: #1f7a45;
    --good-soft: #e3f3e9;
    --danger: #b42318;
    --danger-soft: #fdecea;
    --warn: #8a5a00;
    --radius: 12px;
    --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 4px 16px rgb(0 0 0 / .04);
    --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121211;
        --surface: #1b1b19;
        --surface-2: #242421;
        --border: #2f2f2b;
        --text: #ededea;
        --text-2: #b4b4ad;
        --muted: #85857e;
        --accent: #8d98ff;
        --accent-ink: #0f1030;
        --accent-soft: #262a4d;
        --bar: #8d98ff;
        --bar-2: #3a3f72;
        --grid: #2a2a27;
        --good: #6fd197;
        --good-soft: #17301f;
        --danger: #ff8a80;
        --danger-soft: #3a1b18;
        --warn: #f2c56b;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font: 14px/1.5 var(--font);
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 13px; color: var(--text-2); font-weight: 600; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* ---------- Buttons & fields ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 34px; padding: 0 14px;
    border: 1px solid var(--border); border-radius: 9px;
    background: var(--surface); color: var(--text);
    font: inherit; font-weight: 550; cursor: pointer; text-decoration: none;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 13px; border-radius: 7px; }
.btn.icon { width: 30px; padding: 0; }
.btn.block { width: 100%; }
.btn:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
    outline: 2px solid var(--accent); outline-offset: 2px;
}

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-weight: 600; font-size: 13px; }
input[type=text], input[type=url], input[type=password], input[type=search], input:not([type]), textarea, select {
    width: 100%; padding: 8px 10px;
    border: 1px solid var(--border); border-radius: 9px;
    background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
.prefixed { display: flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; background: var(--surface); }
.prefixed .prefix { padding: 8px 0 8px 10px; color: var(--muted); white-space: nowrap; font-family: var(--mono); font-size: 13px; align-self: center; }
.prefixed input { border: 0; border-radius: 0; padding-left: 2px; font-family: var(--mono); font-size: 13px; min-width: 0; }
.prefixed:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.prefixed input:focus-visible { outline: none; }
.err { color: var(--danger); min-height: 0; }
.err:empty { display: none; }
.warn { color: var(--warn); }
.switch { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; margin: 4px 0 14px; color: var(--text-2); }
.switch input { margin-top: 3px; accent-color: var(--accent); }
.alert { padding: 10px 12px; border-radius: 9px; background: var(--danger-soft); color: var(--danger); margin-bottom: 14px; }

/* ---------- Layout ---------- */
.topbar {
    position: sticky; top: 0; z-index: 5;
    display: flex; align-items: center; gap: 12px;
    padding: 10px 24px; background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.topbar-actions form { margin: 0; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; text-decoration: none; }
.brand-mark {
    display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 8px;
    background: var(--accent); color: var(--accent-ink); font-size: 15px;
}
.brand .host { font-weight: 500; color: var(--muted); font-size: 13px; font-family: var(--mono); }
.container { max-width: 1120px; margin: 0 auto; padding: 24px; }
.section-head { display: flex; align-items: center; gap: 12px; margin: 28px 0 12px; flex-wrap: wrap; }
.section-head:first-child { margin-top: 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 16px 18px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }

.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(4, 1fr); }
.two { grid-template-columns: 2fr 1fr; }
.three { grid-template-columns: repeat(3, 1fr); }

.kpi .label { color: var(--text-2); font-size: 12.5px; font-weight: 550; }
.kpi .value { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .sub { color: var(--muted); font-size: 12px; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; padding: 2px; background: var(--surface); }
.segmented button {
    border: 0; background: transparent; color: var(--text-2); font: inherit; font-size: 13px;
    padding: 4px 10px; border-radius: 7px; cursor: pointer;
}
.segmented button[aria-pressed=true] { background: var(--accent-soft); color: var(--text); font-weight: 600; }

/* ---------- Links table ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; margin-left: auto; }
.toolbar input { width: 240px; }
.links { width: 100%; border-collapse: collapse; }
.links th {
    text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
    padding: 10px 14px; border-bottom: 1px solid var(--border);
}
.links td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.links tr:last-child td { border-bottom: 0; }
.links tr.inactive td:not(:last-child) { opacity: .55; }
.links .num { text-align: right; font-variant-numeric: tabular-nums; }
.slug-cell { display: flex; align-items: center; gap: 6px; }
.slug-link { font-family: var(--mono); font-weight: 600; text-decoration: none; }
.slug-link:hover { text-decoration: underline; }
.dest { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.dest .title { color: var(--text); font-weight: 550; display: block; overflow: hidden; text-overflow: ellipsis; }
.dest a { text-decoration: none; }
.dest a:hover { text-decoration: underline; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.pill {
    display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 99px;
    font-size: 12px; font-weight: 600; border: 0; cursor: pointer; font-family: inherit;
}
.pill.on { background: var(--good-soft); color: var(--good); }
.pill.off { background: var(--surface-2); color: var(--muted); }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty h2 { color: var(--text); margin-bottom: 6px; }

/* ---------- Charts ---------- */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .bar { fill: var(--bar); }
.chart .bar-u { fill: var(--bar-2); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart g.col:hover .bar { filter: brightness(1.15); }
.chart g.col:hover .hover-bg { fill: var(--surface-2); }
.spark { display: block; }
.spark rect { fill: var(--bar); }
.spark rect.zero { fill: var(--grid); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.hbars { display: grid; gap: 8px; }
.hbar { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; font-size: 13px; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .count { font-variant-numeric: tabular-nums; color: var(--text-2); }
.hbar .track { grid-column: 1 / -1; height: 6px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.hbar .fill { height: 100%; border-radius: 4px; background: var(--bar); }
.hbar a { text-decoration: none; }
.hbar a:hover { text-decoration: underline; }

.tooltip {
    position: fixed; z-index: 50; pointer-events: none;
    background: var(--text); color: var(--bg); padding: 6px 9px; border-radius: 8px;
    font-size: 12px; line-height: 1.4; box-shadow: 0 4px 14px rgb(0 0 0 / .2); white-space: nowrap;
}
.tooltip b { font-variant-numeric: tabular-nums; }

/* ---------- Detail ---------- */
.back { display: inline-flex; gap: 6px; align-items: center; color: var(--text-2); text-decoration: none; margin-bottom: 14px; }
.back:hover { color: var(--text); }
.detail-head { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 18px; }
.detail-head .short { font-family: var(--mono); font-size: 20px; font-weight: 700; word-break: break-all; }
.detail-head .to { color: var(--text-2); word-break: break-all; margin-top: 4px; }
.detail-head .to a { color: var(--accent); }
.detail-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.timeline { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.timeline li { display: grid; gap: 2px; padding-left: 14px; border-left: 2px solid var(--border); }
.timeline li:first-child { border-left-color: var(--accent); }
.timeline .when { font-size: 12px; color: var(--muted); }
.timeline .url { word-break: break-all; font-size: 13px; }
.timeline .old { text-decoration: line-through; color: var(--muted); }
.mini-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.mini-table th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.mini-table td { padding: 7px 8px; border-bottom: 1px solid var(--border); }
.mini-table tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

/* ---------- Dialogs / toast ---------- */
.dialog {
    width: min(520px, calc(100vw - 32px)); border: 1px solid var(--border); border-radius: 16px;
    padding: 22px; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .25);
}
.dialog.narrow { width: min(400px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(10 10 10 / .45); }
.dialog h2 { margin-bottom: 16px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.qr-box { background: #fff; border-radius: 12px; padding: 16px; display: grid; place-items: center; border: 1px solid var(--border); }
.qr-box svg { width: 100%; max-width: 280px; height: auto; }
.qr-url { text-align: center; color: var(--text-2); word-break: break-all; margin: 10px 0; }
.toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
    background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-weight: 550;
}

/* ---------- Auth pages ---------- */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card {
    width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border);
    border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.auth-card .brand { margin-bottom: 2px; }
.auth-card > .muted { margin: 0 0 22px; font-family: var(--mono); font-size: 12px; }
.auth-card h1 { margin-bottom: 8px; }
.auth-card form { margin-top: 18px; }
.auth-card.center { text-align: center; }
.big-code { font-size: 54px; font-weight: 800; color: var(--accent); letter-spacing: -0.04em; line-height: 1; margin-bottom: 10px; }
.auth-card.center .btn { margin-top: 10px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .kpis { grid-template-columns: repeat(2, 1fr); }
    .two, .three { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
    .container { padding: 16px; }
    .topbar { padding: 10px 16px; }
    .brand .host { display: none; }
    .toolbar { width: 100%; margin-left: 0; }
    .toolbar input { width: 100%; }
    .links thead { display: none; }
    .links, .links tbody, .links tr, .links td { display: block; width: 100%; }
    .links tr { padding: 12px 14px; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; }
    .links tr:last-child { border-bottom: 0; }
    .links td { padding: 0; border: 0; }
    .links td.c-dest { grid-column: 1 / -1; }
    .links td.c-dest .dest { max-width: 100%; }
    .links td.c-spark, .links td.c-total { display: none; }
    .links td.c-actions { grid-column: 1 / -1; }
    .row-actions { justify-content: flex-start; }
    .kpi .value { font-size: 24px; }
}
