:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #0d1318;
  --surface: #151d24;
  --surface-2: #1d2731;
  --line: #26323d;
  --line-strong: #3a4957;
  --text: #e9eff2;
  --muted: #95a4b0;
  --accent: #9ef6d0;
  --accent-strong: #5bd0a2;
  --accent-ink: #072018;
  --link: #9bceff;
  --warn: #f2bb7e;
  --danger: #ff9d9d;
  --ok-bg: rgba(91, 208, 162, .12);
  --ok-line: rgba(91, 208, 162, .32);
  --warn-bg: rgba(242, 187, 126, .1);
  --warn-line: rgba(242, 187, 126, .32);
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(1100px 420px at 50% -240px, rgba(91, 208, 162, .07), transparent), var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
}

a { color: var(--link); text-underline-offset: 3px; }
a:hover { color: var(--accent); }

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 6px; font-size: 1.75rem; line-height: 1.2; letter-spacing: -.02em; }
h2 { margin-bottom: 4px; font-size: 1.2rem; line-height: 1.3; letter-spacing: -.01em; }
h3 { margin-bottom: 6px; font-size: 1rem; line-height: 1.35; }
p { margin-bottom: 12px; }
code { color: var(--accent); font-family: var(--mono); font-size: .9em; }
.hint, .muted { color: var(--muted); font-size: .9rem; }
p.hint { margin-bottom: 10px; }
.lede { margin-bottom: 0; color: var(--muted); }
.lede strong { color: var(--text); font-weight: 600; overflow-wrap: anywhere; }

/* Buttons: neutral by default, one .button-primary per card. */
.button, button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 9px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: .95rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease, color .15s ease;
}
.button:hover, button:hover { border-color: var(--accent-strong); color: var(--text); }
.button-primary { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }
.button-primary:hover { border-color: #c4ffe5; background: #c4ffe5; color: var(--accent-ink); }
.button-ghost { border-color: transparent; background: transparent; color: var(--text); }
.button-ghost:hover { border-color: transparent; background: var(--surface-2); color: var(--text); }
.button-danger { border-color: rgba(255, 157, 157, .45); background: transparent; color: var(--danger); }
.button-danger:hover { border-color: var(--danger); background: rgba(255, 100, 100, .1); color: var(--danger); }
.link-button { display: inline; min-height: 0; padding: 0; border: 0; background: none; color: var(--link); font-size: inherit; font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }
.link-button:hover { background: none; color: var(--accent); }

button:focus-visible, a:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.inline-form { margin: 0; }
.card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin: 16px 0; }

/* Login and error pages. */
.card {
  width: min(460px, calc(100% - 32px));
  margin: clamp(32px, 14vh, 140px) auto 48px;
  padding: 32px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
}
.card .brand { display: inline-flex; margin-bottom: 28px; }
.card h1 { font-size: 1.5rem; }

/* Page frame. */
.page-shell { width: min(1000px, calc(100% - 32px)); margin: 0 auto 64px; }
.page-shell--home { width: min(1000px, calc(100% - 32px)); }
.page-topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px; padding: 16px 0; border-bottom: 1px solid var(--line); }
.brand { display: inline-flex; align-items: center; gap: 9px; color: var(--text); font-size: 1rem; font-weight: 700; letter-spacing: -.01em; text-decoration: none; }
.brand::before { width: 12px; height: 12px; border-radius: 4px; background: var(--accent-strong); content: ""; }
.brand:hover { color: var(--text); }
.topnav { display: flex; flex-wrap: wrap; gap: 4px; font-size: .92rem; }
.topnav a { padding: 6px 10px; border-radius: 8px; color: var(--muted); text-decoration: none; }
.topnav a:hover { color: var(--text); }
.topnav a[aria-current=page] { background: var(--surface-2); color: var(--text); }
.topbar-user { display: flex; align-items: center; gap: 14px; margin-left: auto; font-size: .9rem; }
.topbar-user .who { color: var(--muted); }
.topbar-user a { color: var(--text); text-decoration: none; }
.topbar-user a:hover { color: var(--accent); }
.page-intro { padding: 28px 0 18px; }
.page-intro--split { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 18px 30px; }

.badge { display: inline-flex; flex: none; align-items: center; gap: 6px; padding: 2px 9px; border: 1px solid var(--line-strong); border-radius: 999px; color: var(--muted); font-size: .76rem; font-weight: 600; line-height: 1.5; white-space: nowrap; }
.badge::before { width: 6px; height: 6px; border-radius: 50%; background: currentColor; content: ""; }
.badge--ok { border-color: var(--ok-line); background: var(--ok-bg); color: var(--accent); }
.badge--warn { border-color: var(--warn-line); background: var(--warn-bg); color: var(--warn); }

.notice, .warning, .flash { margin: 16px 0 0; padding: 10px 14px; border: 1px solid var(--ok-line); border-radius: 10px; background: var(--ok-bg); font-size: .94rem; }
.warning { border-color: var(--warn-line); background: var(--warn-bg); }
.warning p { margin-bottom: 10px; }
.notice + .warning, .warning + .warning { margin-top: 8px; }
.flash { margin: 0 0 12px; }
.sheet-body > .notice:first-child { margin-top: 0; }

/* Home: one row per service, instructions in dialogs. */
.service-list { display: grid; gap: 12px; }
.service { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; align-items: center; gap: 14px 18px; padding: 18px 20px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.service-icon { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; background: var(--surface-2); color: var(--accent); }
.icon { display: block; width: 22px; height: 22px; }
.service-title { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.service-title h2 { margin: 0; font-size: 1.05rem; }
.service-line { margin: 3px 0 0; color: var(--muted); font-size: .9rem; }
.service-line.is-warn { color: var(--warn); }
.service-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.secret { color: var(--text); font-size: .84rem; overflow-wrap: anywhere; }

.menu { position: relative; }
.menu-button { display: grid; place-items: center; width: 40px; height: 40px; border: 1px solid var(--line-strong); border-radius: 10px; background: var(--surface-2); color: var(--text); cursor: pointer; list-style: none; }
.menu-button::-webkit-details-marker { display: none; }
.menu-button:hover, .menu[open] > .menu-button { border-color: var(--accent-strong); }
.menu-button .icon { stroke-width: 3.2; }
.menu-panel { position: absolute; top: calc(100% + 6px); right: 0; z-index: 20; width: min(300px, calc(100vw - 32px)); padding: 6px; border: 1px solid var(--line-strong); border-radius: 12px; background: var(--surface-2); box-shadow: 0 14px 36px rgba(0, 0, 0, .45); }
.menu-item { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 100%; min-height: 0; padding: 9px 11px; border: 0; border-radius: 8px; background: transparent; color: var(--danger); font-size: .92rem; text-align: left; }
.menu-item:hover { border-color: transparent; background: rgba(255, 255, 255, .06); color: var(--danger); }
.menu-item small { color: var(--muted); font-size: .8rem; font-weight: 400; line-height: 1.35; }

dialog.sheet { width: min(620px, calc(100% - 32px)); max-height: calc(100dvh - 48px); padding: 0; overflow: auto; border: 1px solid var(--line-strong); border-radius: 16px; background: var(--surface); color: var(--text); box-shadow: 0 24px 64px rgba(0, 0, 0, .5); }
dialog.sheet:focus { outline: none; }
.sheet-body > .notice + .steps { margin-top: 18px; }
dialog.sheet::backdrop { background: rgba(5, 9, 12, .72); }
.sheet-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 14px 14px 22px; border-bottom: 1px solid var(--line); background: var(--surface); }
.sheet-head h2 { margin: 0; font-size: 1.1rem; }
.sheet-body { padding: 18px 22px 22px; }
.icon-button { width: 38px; min-height: 38px; padding: 0; border-color: transparent; background: transparent; color: var(--muted); }
.icon-button:hover { border-color: var(--line-strong); color: var(--text); }
.sheet-note { padding-top: 14px; border-top: 1px solid var(--line); }
.sheet-note h3 { font-size: .95rem; }
.plain-list { margin: 6px 0 10px; padding-left: 18px; color: var(--muted); font-size: .9rem; }
.plain-list li { margin-bottom: 4px; }

label { display: block; margin: 18px 0 8px; color: var(--muted); font-size: .88rem; font-weight: 600; }
.keyrow { display: flex; flex-wrap: wrap; gap: 8px; }
.keyrow input { flex: 1 1 200px; min-width: 0; min-height: 40px; padding: 9px 12px; border: 1px solid var(--line-strong); border-radius: 10px; background: var(--bg); color: var(--text); font-family: var(--mono); font-size: .84rem; }

.steps { margin: 4px 0 8px; padding: 0; list-style: none; counter-reset: step; }
.steps > li { position: relative; padding: 0 0 20px 42px; counter-increment: step; }
.steps > li::before { position: absolute; top: -3px; left: 0; display: grid; place-items: center; width: 28px; height: 28px; border: 1px solid var(--line-strong); border-radius: 50%; background: var(--surface-2); font-size: .85rem; font-weight: 700; content: counter(step); }
.steps > li:not(:last-child)::after { position: absolute; top: 30px; bottom: 4px; left: 14px; width: 1px; background: var(--line); content: ""; }
.steps > li:last-child { padding-bottom: 6px; }
.steps p.hint:last-child { margin-bottom: 0; }
.step-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 8px 0 4px; }
.subscribe { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: start; gap: 16px; }
.qr { width: 136px; margin: 0; }
.qr figcaption { margin-top: 6px; font-size: .8rem; line-height: 1.35; }
.inet-qr { display: block; width: 136px; height: auto; border-radius: 10px; background: #fff; image-rendering: pixelated; }

.howto { margin-top: 8px; }
.howto > summary { display: inline-flex; align-items: center; gap: 8px; color: var(--link); font-size: .9rem; font-weight: 600; cursor: pointer; list-style: none; }
.howto > summary::-webkit-details-marker { display: none; }
.howto > summary::before { width: 6px; height: 6px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(-45deg); transition: transform .15s ease; content: ""; }
.howto[open] > summary::before { transform: rotate(45deg); }
.howto > summary:hover { color: var(--accent); }
.client-list { margin: 10px 0; padding: 0; list-style: none; font-size: .9rem; }
.client-list li { display: grid; grid-template-columns: 110px 1fr; gap: 2px 14px; padding: 8px 0; border-top: 1px solid var(--line); }
.client-list li > .hint { grid-column: 2; }

/* Admin pages. */
.panel { margin-bottom: 16px; padding: 20px 22px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.panel-heading h2 { margin: 0; }
.panel-status { color: var(--accent-strong); font-size: .82rem; }
.panel-status.is-error { color: var(--danger); }
.hint-list { margin: 4px 0 0; padding-left: 18px; color: var(--muted); font-size: .88rem; }
.hint-list li { margin-bottom: 4px; }

.admin-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.admin-table th { padding: 12px 10px 8px; color: var(--muted); font-size: .78rem; font-weight: 600; text-align: left; }
.admin-table td { padding: 10px; border-top: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
.admin-table .badge { margin-right: 4px; }
.cell-check { width: 32px; }
.admin-table input[type=checkbox] { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--accent-strong); }

.period-switch { display: inline-flex; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 11px; background: var(--surface); }
.period-button { min-width: 84px; min-height: 0; padding: 8px 13px; border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--muted); font-size: .86rem; }
.period-button:hover { border-color: transparent; background: var(--surface-2); color: var(--text); }
.period-button.is-active { border-color: var(--ok-line); background: var(--ok-bg); color: var(--accent); }

.stats-summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.summary-card { padding: 18px 20px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.summary-card--accent { border-color: var(--ok-line); background: rgba(48, 111, 93, .2); }
.summary-label { display: block; margin-bottom: 10px; color: var(--muted); font-size: .84rem; font-weight: 600; }
.summary-card strong { display: block; font-family: var(--mono); font-size: clamp(1.4rem, 3vw, 1.9rem); letter-spacing: -.04em; }
.summary-card--accent strong { color: var(--accent); }
.summary-card small { display: block; margin-top: 8px; color: var(--muted); font-size: .8rem; }

.usage-list { display: grid; gap: 3px; padding-top: 6px; }
.usage-row { padding: 16px 2px; border-bottom: 1px solid var(--line); }
.usage-row:last-child { border-bottom: 0; }
.usage-row-top, .usage-row-bottom { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.usage-row-top { align-items: start; }
.usage-row-bottom { margin-top: 12px; }
.usage-key { display: flex; flex: 1 1 auto; align-items: baseline; gap: 11px; min-width: 0; }
.usage-key-name { min-width: 0; overflow: hidden; font-size: .94rem; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.usage-key-prefix { flex: 0 0 auto; color: var(--muted); font-family: var(--mono); font-size: .76rem; }
.usage-meter { position: relative; flex: 1; height: 7px; overflow: hidden; border-radius: 99px; background: var(--surface-2); }
.usage-meter-fill { display: block; height: 100%; min-width: 0; border-radius: inherit; background: var(--accent-strong); transition: width .45s cubic-bezier(.2, .8, .2, 1); }
.usage-value { display: flex; align-items: baseline; justify-content: end; gap: 8px; min-width: 180px; }
.usage-tokens { font-family: var(--mono); font-size: .92rem; }
.usage-unit { color: var(--muted); font-size: .76rem; }
.usage-cost { color: var(--warn); font-family: var(--mono); font-size: .76rem; }
.stats-skeleton { height: 69px; margin: 3px 0; border-radius: 8px; background: linear-gradient(90deg, rgba(255, 255, 255, .03), rgba(255, 255, 255, .085), rgba(255, 255, 255, .03)); background-size: 240% 100%; animation: shimmer 1.7s ease-in-out infinite; }
.empty-state { margin: 0; padding: 30px 8px; color: var(--muted); text-align: center; }
.empty-state--error { color: var(--danger); }
.stats-footnote { margin: 0 2px; color: var(--muted); font-size: .8rem; }

@keyframes shimmer { 0% { background-position: 120% 0; } 100% { background-position: -80% 0; } }

@media (max-width: 760px) {
  /* Icon beside the title, text and actions at full width. */
  .service { grid-template-columns: 36px minmax(0, 1fr); gap: 0 12px; padding: 16px; }
  .service-main { display: contents; }
  .service-icon { width: 36px; height: 36px; border-radius: 10px; }
  .service-icon .icon { width: 20px; height: 20px; }
  .service-line { grid-column: 1 / -1; }
  .service-title + .service-line { margin-top: 10px; }
  .service-actions { grid-column: 1 / -1; flex-wrap: wrap; margin-top: 14px; }
  .service-actions > .button-primary, .service-actions > .is-primary { order: -1; flex: 1 1 100%; }
  .service-actions > .button-ghost { flex: 1 1 auto; justify-content: flex-start; padding-left: 4px; }
  .service-actions form button { width: 100%; }
  .stats-summary { grid-template-columns: 1fr; }
  .admin-table thead { display: none; }
  .admin-table tr { display: grid; grid-template-columns: 32px 1fr; gap: 4px 0; padding: 12px 0; border-top: 1px solid var(--line); }
  .admin-table td { display: block; grid-column: 2; padding: 0; border-top: 0; }
  .admin-table td.cell-check { grid-row: 1 / span 3; grid-column: 1; width: auto; }
  .admin-table--log tr { grid-template-columns: 1fr; }
  .admin-table--log td { grid-column: 1; }
  .admin-table td[data-label]::before { margin-right: 6px; color: var(--muted); font-size: .8rem; content: attr(data-label) ":"; }
}

@media (max-width: 520px) {
  h1 { font-size: 1.5rem; }
  .card { margin-top: 24px; padding: 24px 20px; }
  .page-intro { padding: 24px 0 16px; }
  .panel { padding: 18px 16px; border-radius: 14px; }
  .topbar-user .who { display: none; }
  .topnav { order: 3; width: 100%; margin-left: -10px; }
  dialog.sheet { width: 100%; max-width: none; height: 100%; max-height: none; margin: 0; border: 0; border-radius: 0; }
  .sheet-body { padding: 16px; }
  .subscribe { grid-template-columns: 1fr; }
  .qr { display: none; }
  .keyrow button { flex: 1 1 0; }
  .client-list li { grid-template-columns: 1fr; }
  .client-list li > .hint { grid-column: 1; }
  .usage-row-top { flex-wrap: wrap; gap: 8px; }
  .usage-key { display: block; flex-basis: 100%; }
  .usage-key-name { display: block; white-space: normal; overflow-wrap: break-word; }
  .usage-key-prefix { display: block; margin-top: 4px; }
  .usage-row-bottom { display: block; }
  .usage-value { justify-content: start; min-width: 0; margin-top: 10px; }
  .period-switch { display: flex; width: 100%; }
  .period-button { flex: 1; min-width: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
