:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1c1c1a;
  --ink-2: #57554f;
  --ink-3: #8f8c84;
  --line: #e6e3dc;
  --accent: #1f6f54;
  --accent-ink: #ffffff;
  --accent-soft: #e3f0ea;
  --c2: #c9772b;
  --c3: #5b6ab8;
  --c4: #b4507a;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --good: #1f7a3f;
  --bad: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 20, 15, .05), 0 2px 8px rgba(20, 20, 15, .04);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141412;
    --surface: #1d1d1a;
    --surface-2: #232320;
    --ink: #eeece6;
    --ink-2: #b6b3aa;
    --ink-3: #7f7c74;
    --line: #2f2e2a;
    --accent: #4fb88f;
    --accent-ink: #0d1f18;
    --accent-soft: #1d3329;
    --c2: #e09a55;
    --c3: #8b97dc;
    --c4: #d77ea2;
    --danger: #ef7d74;
    --danger-soft: #3a1f1c;
    --good: #5cc584;
    --bad: #ef7d74;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.45rem; font-weight: 650; }
h2 { font-size: 1.05rem; font-weight: 620; }
h3 { font-size: .8rem; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); font-size: .85rem; }
.mono { font-family: var(--mono); font-size: .85em; }
[hidden] { display: none !important; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit; font-weight: 550; font-size: .9rem;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: .5rem .85rem; border-radius: 9px; cursor: pointer;
  display: inline-flex; align-items: center; gap: .4rem; text-decoration: none;
  transition: background .12s, border-color .12s;
}
button:hover, .btn:hover { background: var(--surface-2); border-color: var(--ink-3); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.07); }
button.danger { color: var(--danger); }
button.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
button.small, .btn.small { padding: .28rem .55rem; font-size: .8rem; border-radius: 7px; }
button.ghost { border-color: transparent; background: transparent; }
button:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 9px; padding: .55rem .7rem; width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
label { display: block; font-size: .85rem; font-weight: 550; color: var(--ink-2); margin-bottom: .3rem; }
.field { margin-bottom: .9rem; }
.hint { font-size: .8rem; color: var(--ink-3); margin-top: .3rem; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.error { color: var(--danger); font-size: .88rem; min-height: 1.2em; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; padding: .4rem .75rem; font-size: .85rem; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--ink); color: var(--bg); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem 1rem; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 2rem 1.6rem; box-shadow: var(--shadow);
}
.login-card h1 { margin-bottom: .3rem; }
.login-card p { margin: 0 0 1.4rem; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 650; }
.brand-dot { width: 22px; height: 22px; border-radius: 7px; background: var(--accent); position: relative; flex: none; }
.brand-dot::after { content: ""; position: absolute; inset: 6px; border-radius: 50%; border: 2px solid var(--accent-ink); }

/* ---------- app shell ---------- */
.topbar {
  display: flex; align-items: center; gap: .8rem; padding: .8rem 1.25rem;
  border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; z-index: 5;
}
.shell { display: grid; grid-template-columns: 270px 1fr; min-height: calc(100vh - 57px); }
.sidebar { border-right: 1px solid var(--line); padding: 1rem; background: var(--surface-2); }
.sidebar ul { list-style: none; margin: .8rem 0 0; padding: 0; }
.sidebar li button {
  width: 100%; text-align: left; justify-content: space-between; border: 1px solid transparent;
  background: transparent; padding: .55rem .65rem; margin-bottom: .15rem;
}
.sidebar li button:hover { background: var(--surface); border-color: var(--line); }
.sidebar li button.on { background: var(--surface); border-color: var(--line); box-shadow: var(--shadow); }
.sidebar .count { font-size: .78rem; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.main { padding: 1.5rem 1.75rem 3rem; min-width: 0; }
.page { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }

.tabs { display: flex; gap: .2rem; border-bottom: 1px solid var(--line); margin: 1.1rem 0 1.4rem; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; padding: .55rem .8rem; color: var(--ink-2); }
.tabs button.on { color: var(--ink); border-bottom-color: var(--ink); }

/* ---------- cards / stats ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow); min-width: 0; }
.card > h3 { margin-bottom: .8rem; }
.grid { display: grid; gap: 1rem; }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.kpi .v { font-size: 1.9rem; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.15; margin-top: .25rem; }
.kpi .d { font-size: .82rem; margin-top: .25rem; color: var(--ink-3); }
.up { color: var(--good); } .down { color: var(--bad); }
.chart-box { position: relative; height: 240px; }
.chart-box.short { height: 190px; }

.bars { display: grid; gap: .7rem; }
.bar-row .top { display: flex; justify-content: space-between; gap: .5rem; font-size: .9rem; margin-bottom: .3rem; }
.bar-row .top span:last-child { font-variant-numeric: tabular-nums; font-weight: 600; }
.bar-track { height: 8px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--line); overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 99px; }
.tag { display: inline-block; font-size: .72rem; font-weight: 600; padding: .1rem .45rem; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2); text-transform: capitalize; }

.split { display: flex; height: 10px; border-radius: 99px; overflow: hidden; margin: .6rem 0 .5rem; background: var(--surface-2); }
.split > div { height: 100%; }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .85rem; color: var(--ink-2); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: .35rem; vertical-align: 0; }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; font-size: .75rem; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .05em; padding: 0 .6rem .5rem; border-bottom: 1px solid var(--line); }
td { padding: .6rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

/* links table */
.link-card { display: grid; grid-template-columns: 1fr auto; gap: .9rem 1.2rem; padding: 1rem 0; border-bottom: 1px solid var(--line); }
.link-card:last-child { border-bottom: 0; }
.link-card .urls { display: grid; gap: .35rem; margin-top: .55rem; }
.urlrow { display: flex; align-items: center; gap: .5rem; font-size: .85rem; min-width: 0; }
.urlrow b { width: 34px; flex: none; font-size: .72rem; color: var(--ink-3); letter-spacing: .04em; }
.urlrow code { font-family: var(--mono); font-size: .82rem; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: .15rem .4rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.dest { font-size: .85rem; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.link-side { text-align: right; display: grid; gap: .4rem; justify-items: end; align-content: start; }
.link-side .big { font-size: 1.3rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.off { opacity: .55; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--ink-2); }
.empty h2 { margin-bottom: .4rem; color: var(--ink); }

/* dialogs */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 1.5rem; width: min(480px, calc(100vw - 2rem)); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(20, 20, 15, .45); }
dialog h2 { margin-bottom: 1rem; }
.keybox { font-family: var(--mono); font-size: 1.35rem; letter-spacing: .08em; text-align: center; padding: 1rem; background: var(--accent-soft); border-radius: 10px; margin: .8rem 0; user-select: all; }
.qr-preview { display: grid; place-items: center; background: #fff; border-radius: 10px; padding: 1rem; border: 1px solid var(--line); }
.qr-preview svg { width: 220px; height: 220px; }

.toast { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: .55rem 1rem; border-radius: 99px; font-size: .88rem; z-index: 50; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .two { grid-template-columns: 1fr; }
  .shell { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); }
  .main { padding: 1.2rem 1rem 3rem; }
}
@media (max-width: 520px) {
  .kpi .v { font-size: 1.5rem; }
  .link-card { grid-template-columns: 1fr; }
  .link-side { justify-items: start; text-align: left; grid-auto-flow: column; justify-content: start; align-items: center; }
  .topbar { padding: .7rem 1rem; }
}
