/* ---------- Tokens ---------- */
:root {
  --canvas: #eef1ee;      /* cool paper grey */
  --surface: #ffffff;
  --ink: #1e2724;         /* slate ink */
  --muted: #5b6863;
  --line: #d3dad6;
  --line-strong: #b4bfba;
  --live: #2c6a4f;        /* spruce: active codes, primary actions */
  --live-soft: #e2efe8;
  --paused: #a8700f;      /* marigold: paused codes */
  --paused-soft: #fbf0d9;
  --danger: #a83232;
  --danger-soft: #f8e4e4;
  --focus: #1d6fd1;

  --font: "Public Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --step--1: 0.8125rem;
  --step-0: 0.9375rem;
  --step-1: 1.125rem;
  --step-2: 1.5rem;
  --step-3: 2rem;

  --r-control: 6px;
  --r-panel: 10px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  font-size: var(--step-0);
  line-height: 1.5;
  color: var(--ink);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: var(--step-3); font-weight: 800; letter-spacing: -0.02em; }
h2 { font-size: var(--step-1); font-weight: 600; }
h3 { font-size: var(--step-0); font-weight: 600; }
p { margin: 0; }
a { color: var(--live); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: var(--step--1); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.rail {
  background: var(--ink);
  color: #dfe7e3;
  padding: 1.25rem 0.75rem;
  display: flex; flex-direction: column; gap: 1.5rem;
  position: sticky; top: 0; height: 100vh;
}
.brand { padding: 0 0.5rem; display: flex; gap: 0.6rem; align-items: center; }
.brand img { width: 28px; height: 28px; }
.brand strong { display: block; color: #fff; font-size: var(--step-0); }
.brand span { display: block; font-size: var(--step--1); color: #9fb0a9; }
.rail nav { display: flex; flex-direction: column; gap: 2px; }
.rail nav a {
  color: #dfe7e3; text-decoration: none; padding: 0.55rem 0.75rem;
  border-radius: var(--r-control); font-weight: 500;
}
.rail nav a:hover { background: rgba(255, 255, 255, 0.06); }
.rail nav a[aria-current="page"] { background: rgba(255, 255, 255, 0.12); color: #fff; box-shadow: inset 3px 0 0 #7fc4a3; }
.rail .who { margin-top: auto; padding: 0.75rem; border-top: 1px solid rgba(255, 255, 255, 0.12); font-size: var(--step--1); }
.rail .who strong { display: block; color: #fff; font-size: var(--step-0); }
.rail .who button { margin-top: 0.5rem; }
.org-picker { padding: 0 0.5rem; }
.org-picker label { color: #9fb0a9; font-size: var(--step--1); display: block; margin-bottom: 0.25rem; }
.org-picker select { width: 100%; background: #2b3632; color: #fff; border-color: #445550; }

main { padding: 2rem clamp(1rem, 3vw, 2.5rem) 4rem; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.page-head p { margin-top: 0.35rem; max-width: 60ch; }

.topbar { display: none; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .rail {
    position: static; height: auto; padding: 0.75rem 1rem;
    display: grid; grid-template-columns: 1fr auto; grid-template-areas: "brand who" "picker picker" "nav nav";
    gap: 0.6rem 1rem; align-items: center;
  }
  .brand { grid-area: brand; padding: 0; }
  .org-picker { grid-area: picker; padding: 0; }
  .rail nav { grid-area: nav; flex-direction: row; gap: 0.25rem; overflow-x: auto; margin: 0 -0.25rem; }
  .rail nav a { padding: 0.4rem 0.7rem; white-space: nowrap; }
  .rail .who { grid-area: who; margin: 0; border: 0; padding: 0; text-align: right; display: flex; gap: 0.75rem; align-items: baseline; }
  .rail .who strong { font-size: var(--step--1); }
  .rail .who .who-email { display: none; }
  .rail .who div { margin: 0; }
  .rail .who button { margin: 0; }
  main { padding: 1.25rem 1rem 3rem; }
  h1 { font-size: var(--step-2); }
}

/* ---------- Controls ---------- */
button, .btn {
  font: inherit; font-weight: 600; font-size: var(--step-0);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  padding: 0.5rem 0.9rem; border-radius: var(--r-control); cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 0.4rem; line-height: 1.2;
}
button:hover, .btn:hover { border-color: var(--ink); }
button.primary, .btn.primary { background: var(--live); border-color: var(--live); color: #fff; }
button.primary:hover, .btn.primary:hover { background: #235a42; }
button.danger { color: var(--danger); border-color: #d9a5a5; }
button.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; padding-inline: 0.5rem; }
button.ghost:hover { background: rgba(0, 0, 0, 0.05); }
.rail button.ghost { color: #dfe7e3; padding: 0; }
.rail button.ghost:hover { background: none; text-decoration: underline; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.small { padding: 0.3rem 0.6rem; font-size: var(--step--1); }

label { font-weight: 600; font-size: var(--step--1); display: block; margin-bottom: 0.3rem; }
.hint { font-weight: 400; color: var(--muted); font-size: var(--step--1); margin-top: 0.3rem; }
input, select, textarea {
  font: inherit; color: var(--ink); width: 100%;
  border: 1px solid var(--line-strong); border-radius: var(--r-control);
  padding: 0.55rem 0.7rem; background: var(--surface);
}
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(29, 111, 209, 0.35); outline-offset: 0; border-color: var(--focus); }
input[type="color"] { padding: 2px; height: 2.4rem; width: 3.2rem; cursor: pointer; }
.field { margin-bottom: 1rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.slug-input { display: flex; align-items: stretch; }
.slug-input select { width: auto; border-radius: var(--r-control) 0 0 var(--r-control); border-right: 0; background: var(--canvas); max-width: 55%; }
.slug-input input { border-radius: 0 var(--r-control) var(--r-control) 0; }
.slug-input .slash { display: grid; place-items: center; padding: 0 0.4rem; border: 1px solid var(--line-strong); border-inline: 0; background: var(--canvas); color: var(--muted); }
.check { color: var(--live); } .taken { color: var(--danger); }

.segmented { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--r-control); overflow: hidden; }
.segmented button { border: 0; border-radius: 0; font-weight: 500; }
.segmented button + button { border-left: 1px solid var(--line-strong); }
.segmented button[aria-pressed="true"] { background: var(--ink); color: #fff; }

/* ---------- Panels & lists ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 1.25rem; }
.panel + .panel { margin-top: 1rem; }
.panel h2 { margin-bottom: 0.9rem; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 0.9rem; }
.panel-head h2 { margin: 0; }

.toolbar { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.toolbar input[type="search"] { max-width: 22rem; }

.code-list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); overflow: hidden; }
.code-row {
  display: grid; grid-template-columns: 64px minmax(0, 1.4fr) minmax(0, 1.6fr) 90px 80px;
  gap: 1rem; align-items: center; padding: 0.85rem 1.1rem; color: inherit; text-decoration: none;
  border-top: 1px solid var(--line);
}
.code-row:first-child { border-top: 0; }
.code-row:hover { background: #f7f9f8; }
.code-row .thumb { width: 64px; height: 64px; border: 1px solid var(--line); border-radius: 4px; padding: 3px; background: #fff; }
.code-row .thumb canvas, .code-row .thumb svg { width: 100% !important; height: 100% !important; display: block; }
.code-row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code-row .short { color: var(--live); font-weight: 500; font-size: var(--step--1); }
.code-row .dest { font-size: var(--step--1); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code-row .owner { font-size: var(--step--1); color: var(--muted); }
.code-row .scans { text-align: right; }
.code-row .scans strong { display: block; font-size: var(--step-1); }
.code-row .scans span { font-size: var(--step--1); color: var(--muted); }
.list-head { display: grid; grid-template-columns: 64px minmax(0, 1.4fr) minmax(0, 1.6fr) 90px 80px; gap: 1rem; padding: 0.6rem 1.1rem; font-size: var(--step--1); color: var(--muted); border-bottom: 1px solid var(--line); background: #f7f9f8; }
.list-head span:last-child { text-align: right; }

@media (max-width: 820px) {
  .list-head { display: none; }
  .code-row { grid-template-columns: 56px 1fr auto; grid-template-areas: "thumb main scans" "thumb dest status"; row-gap: 0.25rem; }
  .code-row .thumb { grid-area: thumb; width: 56px; height: 56px; }
  .code-row .main { grid-area: main; min-width: 0; }
  .code-row .dest { grid-area: dest; }
  .code-row .status-cell { grid-area: status; justify-self: end; }
  .code-row .scans { grid-area: scans; }
}

.pill { display: inline-flex; align-items: center; gap: 0.35rem; font-size: var(--step--1); font-weight: 600; padding: 0.15rem 0.55rem; border-radius: 999px; }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.active { background: var(--live-soft); color: var(--live); }
.pill.paused { background: var(--paused-soft); color: var(--paused); }
.pill.pending { background: var(--paused-soft); color: var(--paused); }
.pill.failed, .pill.disabled, .pill.suspended { background: var(--danger-soft); color: var(--danger); }

table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; font-size: var(--step--1); color: var(--muted); font-weight: 600; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--line); }
table.data td { padding: 0.7rem 0.6rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.actions { text-align: right; white-space: nowrap; }
.table-wrap { overflow-x: auto; }

.empty { padding: 3rem 1.5rem; text-align: center; background: var(--surface); border: 1px dashed var(--line-strong); border-radius: var(--r-panel); }
.empty h2 { margin-bottom: 0.4rem; }
.empty p { color: var(--muted); margin: 0 auto 1.2rem; max-width: 42ch; }

/* ---------- Code editor ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 1.5rem; align-items: start; }
@media (max-width: 1080px) { .editor { grid-template-columns: 1fr; } .proof-col { order: -1; } }
.proof-col { position: sticky; top: 1.5rem; }
@media (max-width: 1080px) { .proof-col { position: static; } }

/* The print proof: the one memorable element. */
.proof {
  background: #fff;
  border-radius: 2px;
  padding: 2.25rem 2rem 1.6rem;
  position: relative;
  box-shadow: 0 1px 0 var(--line), 0 18px 36px -24px rgba(30, 39, 36, 0.45);
  text-align: center;
}
.proof::before, .proof::after, .proof .marks::before, .proof .marks::after {
  content: ""; position: absolute; width: 18px; height: 18px; border: 0 solid var(--line-strong);
}
.proof::before { top: 10px; left: 10px; border-top-width: 1px; border-left-width: 1px; }
.proof::after { top: 10px; right: 10px; border-top-width: 1px; border-right-width: 1px; }
.proof .marks::before { bottom: 10px; left: 10px; border-bottom-width: 1px; border-left-width: 1px; }
.proof .marks::after { bottom: 10px; right: 10px; border-bottom-width: 1px; border-right-width: 1px; }
.proof .qr-stage { width: 100%; max-width: 260px; margin: 0 auto; aspect-ratio: 1; }
.proof .qr-stage canvas, .proof .qr-stage svg { width: 100% !important; height: auto !important; display: block; }
.proof .printed-url { margin-top: 1rem; font-weight: 800; font-size: var(--step-1); letter-spacing: -0.01em; word-break: break-all; }
.proof .printed-title { color: var(--muted); font-size: var(--step--1); margin-top: 0.15rem; min-height: 1.2em; }
.proof.is-paused .qr-stage { opacity: 0.35; }
.proof .paused-flag { position: absolute; top: 46%; left: 50%; transform: translate(-50%, -50%) rotate(-8deg); background: var(--paused-soft); color: var(--paused); border: 2px solid var(--paused); font-weight: 800; padding: 0.3rem 0.9rem; border-radius: 4px; }
.proof-caption { font-size: var(--step--1); color: var(--muted); margin: 0.75rem 0.25rem 0; }
.downloads { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-top: 1rem; }
.downloads button { justify-content: center; white-space: nowrap; }
.downloads button:last-child { grid-column: 1 / -1; }
.segmented button { white-space: nowrap; }
.panel-head { flex-wrap: wrap; }

.where-now { background: var(--live-soft); border: 1px solid #bcd9ca; border-radius: var(--r-panel); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.where-now.is-paused { background: var(--paused-soft); border-color: #ecd09a; }
.where-now .label { font-size: var(--step--1); color: var(--muted); }
.where-now a { font-weight: 600; word-break: break-all; }

.swatches { display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; }
.swatches .field { margin: 0; }
.style-options { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.75rem; }
.logo-row { display: flex; gap: 0.75rem; align-items: center; }
.logo-row img { width: 44px; height: 44px; object-fit: contain; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.contrast-warning { color: var(--danger); font-size: var(--step--1); margin-top: 0.5rem; }

.save-bar { display: flex; gap: 0.75rem; align-items: center; justify-content: flex-end; margin-top: 1rem; }
.save-bar .dirty { margin-right: auto; color: var(--muted); font-size: var(--step--1); }

/* ---------- Stats ---------- */
.stat-line { display: flex; gap: 2rem; flex-wrap: wrap; margin-bottom: 1rem; }
.stat-line div strong { display: block; font-size: var(--step-2); font-weight: 800; letter-spacing: -0.02em; }
.stat-line div span { font-size: var(--step--1); color: var(--muted); }
.chart { width: 100%; height: 120px; display: block; }
.chart rect.bar { fill: var(--live); }
.chart rect.bar:hover { fill: var(--ink); }
.chart line { stroke: var(--line-strong); vector-effect: non-scaling-stroke; }
.chart-peak { margin-bottom: 0.35rem; }
.chart-axis { display: flex; justify-content: space-between; margin-top: 0.35rem; }
.chart-empty { padding: 2rem 0; text-align: center; border: 1px dashed var(--line); border-radius: var(--r-control); }
.breakdowns { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1.25rem; margin-top: 1.25rem; }
.breakdown li { display: grid; grid-template-columns: 1fr auto; gap: 0.5rem; padding: 0.25rem 0; font-size: var(--step--1); position: relative; }
.breakdown ul { list-style: none; margin: 0.5rem 0 0; padding: 0; }
.breakdown .bar-bg { grid-column: 1 / -1; height: 4px; background: var(--canvas); border-radius: 2px; overflow: hidden; }
.breakdown .bar-bg i { display: block; height: 100%; background: var(--live); }

.history li { padding: 0.6rem 0; border-top: 1px solid var(--line); font-size: var(--step--1); list-style: none; }
.history ul { margin: 0; padding: 0; }
.history li:first-child { border-top: 0; }
.history .to { font-weight: 600; word-break: break-all; }
.history .from { color: var(--muted); text-decoration: line-through; word-break: break-all; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login { width: 100%; max-width: 380px; }
.login .brand { padding: 0; margin-bottom: 1.5rem; }
.login .brand strong { color: var(--ink); font-size: var(--step-1); }
.login h1 { font-size: var(--step-2); margin-bottom: 0.4rem; }
.login p.lead { color: var(--muted); margin-bottom: 1.5rem; }
.login button.primary { width: 100%; justify-content: center; padding: 0.7rem; }

.error-box { background: var(--danger-soft); color: var(--danger); border-radius: var(--r-control); padding: 0.6rem 0.8rem; margin-bottom: 1rem; font-size: var(--step--1); font-weight: 500; }
.note-box { background: #f4f7f5; border: 1px solid var(--line); border-radius: var(--r-control); padding: 0.8rem 1rem; font-size: var(--step--1); }
.note-box code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; background: #e8edea; padding: 0.05rem 0.3rem; border-radius: 3px; }

/* ---------- Dialog & toast ---------- */
dialog { border: 0; border-radius: var(--r-panel); padding: 0; width: min(480px, calc(100vw - 2rem)); box-shadow: 0 24px 48px -12px rgba(30, 39, 36, 0.4); }
dialog::backdrop { background: rgba(30, 39, 36, 0.45); }
dialog form { padding: 1.4rem; }
dialog h2 { margin-bottom: 1rem; }
dialog .actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.25rem; }

#toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translate(-50%, 1rem); background: var(--ink); color: #fff; padding: 0.65rem 1rem; border-radius: var(--r-control); font-weight: 500; opacity: 0; pointer-events: none; transition: opacity 0.18s, transform 0.18s; max-width: calc(100vw - 2rem); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }

.loading { color: var(--muted); padding: 2rem 0; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
