body {
    margin: 0;
    font-family: var(--font-body);
    background: radial-gradient(900px 480px at 100% -12%, var(--bg-2), transparent 70%), var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
}
.wrap { width: 100%; max-width: 1200px; margin: 0 auto; padding: var(--s3) var(--s5) var(--s6); }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s3); margin: var(--s3) 0 var(--s4); }
.stat {
    background: var(--bg); border-radius: var(--r-lg);
    padding: var(--s4);
}
.stat .k { color: var(--muted); font-size: var(--fs-xs); font-weight: 600; }
.stat .v { margin-top: var(--s2); font-family: var(--font-display); font-weight: 800; font-size: var(--fs-lg); letter-spacing: -0.02em; }

.panel {
    background: var(--card); border: 1px solid var(--line);
    border-radius: var(--r-lg); padding: var(--s5);
}
.controls { display: grid; grid-template-columns: 1fr; gap: var(--s3); margin-bottom: var(--s4); }
.search {
    border: 1.5px solid var(--line); border-radius: var(--r-md); padding: 14px 16px; font: inherit; font-size: var(--fs-base);
    width: 100%; background: var(--bg); color: var(--text); min-height: var(--tap);
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
[data-theme="dark"] .search { background: #0c1322; }
.search::placeholder { color: var(--muted); }
.search:focus { outline: none; border-color: var(--brand); background: var(--card); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15); }
.chips { display: flex; gap: var(--s2); flex-wrap: wrap; }
.chip {
    border: 1px solid var(--line); border-radius: var(--r-pill); background: var(--card); color: var(--muted);
    padding: 9px 15px; font: inherit; font-weight: 600; cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
    min-height: var(--tap);
}
.chip:hover { border-color: var(--brand); }
.chip.active { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; border-color: transparent; }
.toolbar-row { display: flex; gap: var(--s3); align-items: center; }
.toolbar-row .btn { flex: 0 0 auto; gap: 7px; }
.toolbar-row .btn svg { width: 15px; height: 15px; flex: none; }
/* Refresh + Export stay glued together: one flex unit that wraps as a pair
   (never leaving Export alone on its own line) and hugs the right side. */
.toolbar-actions { display: flex; gap: var(--s2); align-items: center; margin-left: auto; }

.status { color: var(--muted); font-size: var(--fs-sm); margin-bottom: var(--s3); }

.links-cards { display: grid; gap: var(--s3); }
.link-card {
    border: 1px solid var(--line); border-radius: var(--r-lg); padding: var(--s4);
    background: var(--card); display: flex; flex-direction: column; gap: var(--s3);
    animation: cardIn 0.3s ease both; transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.link-card:hover { border-color: var(--brand); box-shadow: 0 4px 16px -8px rgba(37, 99, 235, 0.18); }
@keyframes cardIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .link-card { animation: none; } }
.link-card .lc-top { display: flex; flex-direction: column; gap: var(--s1); min-width: 0; }
.link-card .lc-short { color: var(--brand); font-weight: 700; text-decoration: none; font-size: var(--fs-md); word-break: break-all; }
.link-card .lc-orig { color: var(--muted); font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.link-card .lc-meta { display: flex; align-items: center; gap: var(--s3); color: var(--muted); font-size: var(--fs-sm); }
.link-card .clicks-pill {
    display: inline-block; font-size: var(--fs-xs); font-weight: 700; color: var(--brand);
    background: var(--brand-soft); padding: 3px 10px; border-radius: var(--r-pill); white-space: nowrap;
}
.link-card .lc-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s2); }
.link-card .lc-actions .btn { width: 100%; }
.link-card .lc-actions .btn.del { grid-column: 1 / -1; }

.table-wrap { display: none; overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r-md); -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: var(--card); table-layout: fixed; }
th, td { padding: 13px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; overflow: hidden; }
th { font-size: var(--fs-xs); color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; white-space: nowrap; }
th:nth-child(3) { width: 90px; }
th:nth-child(4) { width: 150px; }
th.actions-col { width: 360px; }
th.actions-col, td.actions-col { text-align: center; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--brand-soft); }
td .s {
    display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
td .o {
    display: block; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted);
    cursor: pointer; transition: color 0.15s ease;
}
td .o:hover { color: var(--brand); }
.date { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; }
.row-actions { display: flex; gap: var(--s2); flex-wrap: nowrap; justify-content: center; }
.row-actions .tiny { padding: 0 12px; min-height: 38px; font-size: var(--fs-sm); }

.pager { display: flex; align-items: center; justify-content: center; gap: var(--s3); margin-top: var(--s4); }
.pager .btn:disabled { opacity: 0.4; cursor: not-allowed; }
.page-info { font-size: var(--fs-sm); color: var(--muted); font-weight: 600; }

.spark { margin-top: var(--s4); border: 1px solid var(--line); border-radius: var(--r-md); padding: var(--s4); }
.spark-top { display: flex; justify-content: space-between; margin-bottom: var(--s3); font-size: var(--fs-sm); font-weight: 600; }
#sparkline { width: 100%; height: 84px; display: block; border-radius: var(--r-sm); background: var(--bg); }
.empty { text-align: center; color: var(--muted); padding: 28px 10px; font-size: var(--fs-sm); }

.traffic { margin-top: var(--s4); border: 1px solid var(--line); border-radius: var(--r-md); padding: var(--s4); }
.traffic-top { display: flex; justify-content: space-between; align-items: baseline; gap: var(--s3); margin-bottom: var(--s4); font-size: var(--fs-sm); font-weight: 600; }
.traffic-sub { color: var(--muted); font-weight: 500; font-size: var(--fs-xs); }
.traffic-grid { display: grid; grid-template-columns: 1fr; gap: var(--s4); }
.traffic-grid > div { break-inside: avoid; }
@media (min-width: 761px) {
    .traffic-grid { display: block; columns: 3; column-gap: var(--s5); }
    .traffic-grid > div { margin-bottom: var(--s4); }
}
.traffic-label { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin-bottom: var(--s2); }
.traffic-label-sub { margin-top: var(--s4); }
.traffic-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px var(--s2); align-items: center; font-size: var(--fs-sm); padding: 5px 0; }
.traffic-row .tl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.traffic-row .tc { color: var(--muted); font-variant-numeric: tabular-nums; }
.traffic-bar { grid-column: 1 / -1; height: 4px; border-radius: var(--r-pill); background: var(--bg); overflow: hidden; }
.traffic-bar i { display: block; height: 100%; border-radius: inherit; background: var(--brand); }
.traffic-empty { color: var(--muted); font-size: var(--fs-sm); padding: 4px 0; }

.exp-badge {
    display: inline-block; margin-left: 4px; padding: 1px 8px; border-radius: var(--r-pill);
    background: var(--danger-soft); color: var(--danger); font-size: var(--fs-xs); font-weight: 700;
}
.exp-note { color: var(--muted); font-size: var(--fs-xs); }
td .exp-note { display: block; margin-top: 2px; }
td .exp-badge { display: block; width: fit-content; margin: 4px 0 0; }

.qr-box { position: relative; padding: var(--s5); text-align: center; }
.qr-title {
    margin: 0 0 var(--s4); font-family: var(--font-display); font-size: var(--fs-md);
    font-weight: 700; letter-spacing: -0.01em;
}
.qr-close {
    position: absolute; top: var(--s2); right: var(--s2);
    width: 34px; height: 34px; display: grid; place-items: center;
    border: 0; border-radius: var(--r-sm); background: transparent; color: var(--muted);
    font: 400 22px/1 var(--font-body); cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}
.qr-close:hover { background: var(--bg); color: var(--text); }
.qr-close:active { transform: scale(0.95); }
#qrcode-container {
    display: block; width: fit-content; max-width: 100%; margin: 0 auto var(--s4);
    background: #fff; padding: var(--s3); border-radius: var(--r-sm); border: 1px solid var(--line);
}
#qrcode-container canvas, #qrcode-container img { display: block; max-width: 100%; height: auto; }
.qr-download { width: 100%; min-height: 46px; }

footer { text-align: center; color: var(--muted); font-size: var(--fs-sm); padding: var(--s5) var(--s4) calc(var(--s6) + env(safe-area-inset-bottom, 0px)); line-height: var(--lh-body); }

@media (min-width: 761px) {
    .stats { grid-template-columns: repeat(4, 1fr); }
    .controls { grid-template-columns: 1fr auto auto; align-items: center; }
    .panel { padding: var(--s6); }
}
@media (min-width: 900px) {
    .links-cards { display: none; }
    .table-wrap { display: block; }
}
@media (max-width: 760px) {
    .wrap { padding-left: var(--s4); padding-right: var(--s4); }
}

/* Auth gate: covers the dashboard until the session resolves, so signed-out
   visitors never glimpse the layout — they get a fading "sign in" note instead. */
#authGate {
    position: fixed;
    inset: 0;
    z-index: 960;
    display: grid;
    place-items: center;
    background: var(--bg);
    transition: opacity 0.35s ease;
}
#authGate.hide { opacity: 0; pointer-events: none; }
.auth-gate-msg {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    padding: var(--s3) var(--s5);
    font-weight: 600;
    font-size: var(--fs-base);
    color: var(--text);
    box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 0.4s ease, transform 0.4s ease;
}
#authGate.show-msg .auth-gate-msg { opacity: 1; transform: none; }

/* Demo workspace banner */
.demo-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    flex-wrap: wrap;
    margin: var(--s2) 0 var(--s3);
    padding: var(--s3) var(--s4);
    border: 1px solid var(--brand);
    border-radius: var(--r-md);
    background: var(--brand-soft);
    font-size: var(--fs-sm);
    line-height: 1.45;
}
.demo-copy { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.demo-pill {
    display: inline-block;
    padding: 2px 9px;
    border-radius: var(--r-pill);
    background: var(--brand);
    color: #fff;
    font-size: var(--fs-xs);
    font-weight: 700;
    letter-spacing: 0.02em;
    white-space: nowrap;
}

/* API key management */
.api-box { width: min(560px, 100%); }
.api-key-list {
    display: grid;
    gap: var(--s2);
    max-height: 230px;
    overflow-y: auto;
    margin-bottom: var(--s3);
}
.api-key-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    padding: var(--s3);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--bg);
}
.api-key-main { min-width: 0; }
.api-key-name { font-weight: 600; font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.api-key-meta { margin-top: 2px; color: var(--muted); font-size: var(--fs-xs); }
.api-key-meta code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.8em;
    color: var(--text);
}
.api-key-revoked { color: var(--muted); font-size: var(--fs-xs); font-weight: 600; }
.api-key-empty { color: var(--muted); font-size: var(--fs-sm); padding: var(--s3) 0; }
.api-key-create { display: flex; gap: var(--s2); }
.api-key-create .input { flex: 1; min-width: 0; }
.api-key-new {
    margin-top: var(--s3);
    padding: var(--s3);
    border: 1px solid var(--ok-line);
    border-radius: var(--r-md);
    background: var(--ok-bg);
}
.api-key-new-title { color: var(--ok-text); font-size: var(--fs-xs); font-weight: 700; margin-bottom: var(--s2); }
.api-key-value-row { display: flex; align-items: center; gap: var(--s2); }
.api-key-value-row code {
    flex: 1;
    min-width: 0;
    padding: 8px 10px;
    border-radius: var(--r-sm);
    background: var(--card);
    border: 1px solid var(--ok-line);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: var(--fs-xs);
    word-break: break-all;
}

@media (min-width: 1000px) {
    .traffic-grid { column-gap: var(--s6); }
}
@media (max-width: 760px) {
    .demo-banner { align-items: flex-start; }
    .api-key-create { flex-direction: column; }
    .api-key-create .btn { width: 100%; }
}
