:root {
  color-scheme: light;
  --background: #fafafa;
  --foreground: #18181b;
  --card: #ffffff;
  --muted: #f4f4f5;
  --muted-foreground: #71717a;
  --border: #e4e4e7;
  --sidebar: #fafafa;
  --primary: #18181b;
  --primary-foreground: #fafafa;
  --danger: #b42318;
  --ok: #067647;
  --ok-soft: #ecfdf3;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html.dark {
  color-scheme: dark;
  --background: #09090b;
  --foreground: #fafafa;
  --card: #111113;
  --muted: #18181b;
  --muted-foreground: #a1a1aa;
  --border: #27272a;
  --sidebar: #0c0c0e;
  --primary: #fafafa;
  --primary-foreground: #18181b;
  --danger: #f97066;
  --ok: #47cd89;
  --ok-soft: #052e1c;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--background);
  color: var(--foreground);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
}

button, input, textarea { font: inherit; color: inherit; }

a { color: var(--muted-foreground); text-decoration: none; }
a:hover { color: var(--foreground); }

h1, h2, p { margin: 0; }

h1 { font-size: 24px; font-weight: 500; letter-spacing: -0.03em; }
h2 { font-size: 14px; font-weight: 500; }

.muted { color: var(--muted-foreground); }
.strong { font-weight: 500; }
.danger { color: var(--danger); }

.shell { display: flex; min-height: 100vh; min-height: 100dvh; }

aside {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  width: 240px;
  height: 100vh;
  height: 100dvh;
  flex-shrink: 0;
  padding: 16px 12px;
  border-right: 1px solid var(--border);
  background: var(--sidebar);
}

.brand { padding: 0 8px 16px; }
.brand p:first-child { font-weight: 500; }

nav, .side-foot { display: flex; flex-direction: column; gap: 4px; }
.side-foot { margin-top: auto; }

nav a, .side-foot a, .side-foot button {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  width: 100%;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted-foreground);
  text-align: left;
  cursor: pointer;
}

nav a.active, .side-foot a.active { background: var(--muted); color: var(--foreground); font-weight: 500; }
nav a:hover, .side-foot a:hover, .side-foot button:hover { background: var(--muted); color: var(--foreground); }

.icon { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
html.dark .when-light, html:not(.dark) .when-dark { display: none; }
.when-light, .when-dark { display: inline-flex; align-items: center; gap: 8px; }

.main { min-width: 0; flex: 1; }
main {
  display: flex;
  flex-direction: column;
  gap: 24px;
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: 24px;
}

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap; }
.page-head h1 { overflow-wrap: anywhere; }

.card {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
}

.kpis, .charts { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.charts { grid-template-columns: minmax(0, 1.4fr) minmax(280px, 1fr); }
.charts .card, .kpi { padding: 16px; }
.charts .card { min-width: 0; overflow: hidden; }
.charts .muted, .kpi .muted:last-child { margin-top: 4px; font-size: 12px; }
.figure {
  margin-top: 8px;
  font-size: 24px;
  font-weight: 500;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

.chart { display: block; width: 100%; height: 220px; margin-top: 12px; }
.chart .grid { stroke: var(--border); }
.chart .line { fill: none; stroke: var(--foreground); stroke-width: 1.5; }
.chart .area, .chart .bar { fill: var(--foreground); }
.chart .area { opacity: 0.08; }
.chart text { fill: var(--muted-foreground); font-size: 11px; font-family: var(--sans); }

.table-head, .split, .form-actions, .bulk, .toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.table-head { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.scroll { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; min-width: 640px; }
.users { min-width: 760px; }
th, td { padding: 12px 16px; text-align: left; vertical-align: middle; }
th { padding-top: 8px; padding-bottom: 8px; font-size: 12px; font-weight: 500; color: var(--muted-foreground); }
tbody tr { border-top: 1px solid var(--border); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.clip { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.quiet { color: var(--muted-foreground); }
.quiet .clip { color: var(--foreground); }
.check { width: 40px; padding-right: 0; }
.actions, .row-actions.wrap { display: flex; justify-content: flex-end; gap: 4px; flex-wrap: wrap; }
.row-actions.wrap { justify-content: flex-start; }
a.row-link { color: inherit; font-weight: 500; }
a.row-link:hover { text-decoration: underline; }
.back { font-size: 13px; margin-top: -12px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.fact, .pad { padding: 16px; }
.fact p:nth-child(2) { margin-top: 4px; font-weight: 500; }
.fact .muted:last-child { margin-top: 4px; font-size: 12px; }
.split-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.split-2 > * { min-width: 0; }
.danger-zone { border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }
.checkline { display: flex; align-items: center; gap: 10px; min-height: 36px; }
.watch { padding: 16px; }
.watch ul { margin: 12px 0 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; }
.watch a { color: var(--foreground); }

.badge {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 12px;
  color: var(--muted-foreground);
}
.badge.ok { border-color: transparent; background: var(--ok-soft); color: var(--ok); font-weight: 500; }
.badge.warn { border-color: transparent; background: #fff7ed; color: #c2410c; font-weight: 500; }
html.dark .badge.warn { background: #431407; color: #fdba74; }
.stack-tight { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.hint { font-size: 12px; color: var(--muted-foreground); }

.toolbar { flex-wrap: wrap; justify-content: flex-start; }
.toolbar select { width: auto; }
.search { position: relative; flex: 1; min-width: 220px; }
.search .icon { position: absolute; top: 10px; left: 10px; color: var(--muted-foreground); }
.search input { padding-left: 32px; }

select, input[type="text"], input[type="search"], input[type="password"], textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  outline: none;
}
select, input[type="text"], input[type="search"], input[type="password"] { height: 36px; padding: 0 12px; }
select { background: var(--card); }
textarea { resize: none; padding: 8px 12px; background: var(--background); }
input:focus, textarea:focus, select:focus { box-shadow: 0 0 0 2px color-mix(in srgb, var(--foreground) 15%, transparent); outline: none; }

.chip, .primary, .text, .icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  padding: 0 12px;
  color: var(--muted-foreground);
  cursor: pointer;
}
.chip.on, .primary {
  border-color: transparent;
  background: var(--primary);
  color: var(--primary-foreground);
}
.primary { font-weight: 500; }
.primary.small { height: 32px; }
.text { border-color: transparent; background: transparent; font-weight: 500; color: var(--foreground); }
.text:hover, .chip:hover { background: var(--muted); }
.text.danger { color: var(--danger); }
button:disabled { opacity: 0.6; }

.bulk {
  margin-bottom: -8px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--muted);
}
.row-actions { display: flex; gap: 8px; }

.notice {
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  border-radius: 12px;
  background: var(--card);
  padding: 12px 16px;
  color: var(--danger);
}
.quiet-note { border-color: var(--border); color: var(--foreground); }

.ready {
  max-width: 420px;
  border-radius: 12px;
  background: var(--primary);
  padding: 20px;
  color: var(--primary-foreground);
}
.ready h2 { margin-top: 12px; font-size: 30px; letter-spacing: -0.03em; }
.ready p:last-child { margin-top: 8px; opacity: 0.8; }
.ready p:first-child { font-size: 12px; opacity: 0.7; }

.gift-card { max-width: 440px; padding: 20px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.plans { display: flex; flex-direction: column; gap: 8px; }
.plan {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
}
.plan:has(input:checked) { border-color: var(--foreground); }
.plan input { position: absolute; opacity: 0; pointer-events: none; }
.plan small { color: var(--muted-foreground); }
.field { display: flex; flex-direction: column; gap: 6px; }
.icon-button { width: 32px; height: 32px; padding: 0; font-size: 20px; line-height: 1; }

.only-narrow { display: none; }
.select-line {
  display: none;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  font-size: 14px;
}

.login {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) 24px max(24px, env(safe-area-inset-bottom));
}
.login-card { width: min(100%, 384px); padding: 24px; }
.login-card h1 { margin-top: 4px; }
.kicker { color: var(--muted-foreground); font-size: 14px; }

.empty, .empty-block { padding: 40px 24px; text-align: center; }
.empty-block .muted { margin-top: 4px; }
.dashed { border-style: dashed; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 16px 1fr auto; gap: 16px; }
.timeline li { position: relative; }
.timeline li::before {
  content: "";
  position: absolute;
  left: 7px;
  top: 16px;
  bottom: 0;
  width: 1px;
  background: var(--border);
}
.timeline li.last::before { content: none; }
.dot {
  position: relative;
  z-index: 1;
  width: 8px;
  height: 8px;
  margin: 6px auto 0;
  border-radius: 99px;
  background: var(--foreground);
}
.event { display: flex; gap: 12px; min-width: 0; padding-bottom: 20px; }
.timeline li.last .event { padding-bottom: 0; }
.event .icon { margin-top: 2px; color: var(--muted-foreground); }
time { font-size: 12px; color: var(--muted-foreground); }

input[type="checkbox"] { accent-color: var(--foreground); width: 16px; height: 16px; }

@media (max-width: 980px) {
  .charts { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
  .shell { flex-direction: column; }
  aside {
    z-index: 5;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "brand foot" "nav nav";
    align-items: center;
    gap: 4px 8px;
    width: 100%;
    height: auto;
    padding: calc(10px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .brand {
    grid-area: brand;
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 0 4px;
  }
  nav {
    grid-area: nav;
    flex-direction: row;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  nav a {
    flex: 0 0 auto;
    justify-content: center;
    min-height: 44px;
    padding: 0 10px;
    white-space: nowrap;
  }
  .side-foot {
    grid-area: foot;
    flex-direction: row;
    margin-top: 0;
  }
  .side-foot form { display: block; }
  .side-foot a, .side-foot button { width: auto; min-height: 44px; padding: 0 10px; }
  main {
    padding: 16px max(16px, env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    gap: 16px;
  }
  .toolbar .search { flex-basis: 100%; min-width: 0; }
  .bulk { flex-wrap: wrap; }
  .row-actions { margin-left: auto; }
}

@media (max-width: 720px) {
  .only-narrow { display: flex; }
  table.stack { min-width: 0; }
  table.stack thead { display: none; }
  table.stack,
  table.stack tbody,
  table.stack tr,
  table.stack td { display: block; width: auto; }
  table.stack tr { padding: 12px 14px 8px; border-top: 1px solid var(--border); }
  table.stack td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 0;
    text-align: right;
  }
  table.stack td::before {
    content: attr(data-label);
    color: var(--muted-foreground);
    font-size: 12px;
    font-weight: 500;
    text-align: left;
  }
  table.stack td.clip {
    display: block;
    max-width: none;
    overflow: visible;
    text-align: left;
    white-space: normal;
    overflow-wrap: anywhere;
    font-weight: 500;
    padding-top: 2px;
    padding-bottom: 8px;
  }
  table.stack td.clip::before,
  table.stack td.check::before,
  table.stack td.actions::before { content: none; }
  table.stack td.check { justify-content: flex-start; padding-top: 0; }
  table.stack td.actions { justify-content: flex-end; padding-top: 8px; padding-bottom: 4px; }
  table.users.stack tr {
    display: grid;
    grid-template-columns: 28px minmax(0, 1fr);
    column-gap: 8px;
  }
  table.users.stack td.check { grid-column: 1; grid-row: 1; padding-top: 2px; }
  table.users.stack td.clip { grid-column: 2; grid-row: 1; align-self: center; }
  table.users.stack td:not(.check):not(.clip) { grid-column: 1 / -1; }
  table.stack .num { text-align: right; }
  .check, td:first-child { width: auto; }
  .gift-card, .ready { max-width: none; }
  .form-actions { flex-direction: column-reverse; align-items: stretch; }
  .form-actions .primary, .form-actions .text { width: 100%; }
  .timeline li { grid-template-columns: 16px minmax(0, 1fr); }
  .timeline time { grid-column: 2; margin-top: -8px; }
  .event { padding-bottom: 2px; }
  .split-2 { grid-template-columns: 1fr; }
  .chart { height: 180px; }
  .chart .tick-minor { display: none; }
  input, textarea, select { font-size: 16px; }
}

@media (max-width: 420px) {
  .side-foot .when-light > span,
  .side-foot .when-dark > span,
  .side-foot > a > span,
  .side-foot form button > span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }
  h1 { font-size: 22px; }
}

@media (pointer: coarse) {
  .chip, .primary, .plan, nav a, .side-foot a, .side-foot button { min-height: 44px; }
}
