:root {
  --bg: #050505;
  --surface: #101010;
  --surface-2: #181818;
  --border: #262626;
  --text: #f2f2f2;
  --muted: #9a9a9a;
  --accent: #ff4a0a;
  --accent-soft: rgba(255, 74, 10, 0.14);
  --success: #2ebd6b;
  --success-soft: rgba(46, 189, 107, 0.14);
  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, 0.14);
  --warn: #e8a317;
  --warn-soft: rgba(232, 163, 23, 0.14);
  --info: #5b9dff;
  --info-soft: rgba(91, 157, 255, 0.14);
  --radius: 12px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
}

* { box-sizing: border-box; }
html { color-scheme: dark; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.5rem; line-height: 1.25; }
h2 { font-size: 1.05rem; }
a { color: var(--accent); text-underline-offset: 2px; }
code, .mono { font-family: var(--mono); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.icon { flex: none; vertical-align: -3px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.skip-link { position: absolute; left: -999px; top: 8px; background: var(--accent); color: #000; padding: 8px 12px; border-radius: 8px; z-index: 100; }
.skip-link:focus { left: 8px; }
.noscript { padding: 24px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 252px minmax(0, 1fr); min-height: 100vh; }
.nav { overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border); padding: 14px 10px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.nav-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 4px 10px 12px; color: var(--text); font-size: 1.02rem; }
.nav-brand small { display: block; font-weight: 500; font-size: 0.7rem; color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; }
.nav-brand .logo { width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: #000; display: grid; place-items: center; font-weight: 800; flex: none; }
.nav-group { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.09em; color: #6b6b6b; padding: 14px 10px 4px; }
.nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.nav-list a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; color: var(--muted); text-decoration: none; min-height: 38px; }
.nav-list a:hover { background: var(--surface-2); color: var(--text); }
.nav-list a[aria-current='page'] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-count { margin-left: auto; font-size: 0.7rem; background: var(--accent); color: #000; border-radius: 999px; padding: 0 7px; font-weight: 700; min-width: 20px; text-align: center; }
.main { min-width: 0; display: flex; flex-direction: column; }
.view { padding: 24px 28px 72px; max-width: 1320px; width: 100%; outline: none; }

.topbar { position: sticky; top: 0; z-index: 30; min-height: 56px; display: flex; align-items: center; gap: 12px; padding: 0 24px; background: rgba(5, 5, 5, 0.9); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
.crumbs { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.85rem; min-width: 0; white-space: nowrap; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.crumbs b { color: var(--text); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.search-btn { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 0 12px; height: 36px; width: min(380px, 38vw); color: var(--muted); cursor: pointer; margin-left: auto; font: inherit; font-size: 0.88rem; }
.search-btn:hover { border-color: #3a3a3a; color: var(--text); }
.search-btn kbd, .palette kbd { font: 0.72rem var(--mono); border: 1px solid #333; border-radius: 5px; padding: 0 5px; margin-left: auto; color: var(--muted); background: var(--surface); }
.search-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.env { font-size: 0.72rem; border: 1px solid var(--success); color: var(--success); background: var(--success-soft); padding: 2px 9px; border-radius: 999px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.env i { width: 6px; height: 6px; border-radius: 50%; background: var(--success); display: block; }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); display: inline-grid; place-items: center; color: var(--muted); cursor: pointer; }
.menu-btn { display: none; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: #202020; display: inline-grid; place-items: center; font-weight: 700; font-size: 0.74rem; flex: none; border: 1px solid #333; }
.account { position: relative; }
.account-btn { display: flex; align-items: center; gap: 8px; background: none; border: 0; color: var(--text); cursor: pointer; font: inherit; padding: 2px 4px; border-radius: 10px; }
.account-btn:hover { background: var(--surface-2); }
.account-name { display: flex; flex-direction: column; text-align: left; line-height: 1.2; }
.account-name b { font-size: 0.82rem; }
.account-name small { color: var(--muted); font-size: 0.72rem; }
.account-menu { position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; background: var(--surface); border: 1px solid #333; border-radius: 12px; box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6); padding: 6px; z-index: 50; }
.account-head { padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.menu-item { display: flex; align-items: center; gap: 10px; width: 100%; background: none; border: 0; color: var(--text); padding: 9px 10px; border-radius: 8px; font: inherit; cursor: pointer; text-align: left; }
.menu-item:hover { background: var(--surface-2); }
.rail-scrim { display: none; }

@media (max-width: 960px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .nav { position: fixed; left: 0; top: 0; bottom: 0; width: 260px; z-index: 60; transform: translateX(-102%); transition: transform 0.18s; box-shadow: 20px 0 50px rgba(0, 0, 0, 0.6); }
  body.rail-open .nav { transform: none; }
  body.rail-open .rail-scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); z-index: 55; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 0 12px; gap: 8px; }
  .search-btn { width: auto; flex: 1; }
  .search-btn kbd, .env, .account-name { display: none; }
  .view { padding: 20px 16px 56px; }
}
@media (prefers-reduced-motion: reduce) { .nav { transition: none; } }

/* ---------- command palette (Ctrl+K) ---------- */
.palette-scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); z-index: 90; }
.palette { position: fixed; left: 50%; top: 12vh; transform: translateX(-50%); width: min(620px, 94vw); background: var(--surface); border: 1px solid #333; border-radius: 14px; z-index: 91; box-shadow: 0 30px 80px #000; overflow: hidden; }
.palette-input { width: 100%; background: none; border: 0; border-bottom: 1px solid var(--border); padding: 16px 18px; color: var(--text); font: 1rem var(--font); outline: none; border-radius: 0; min-height: 0; }
.palette-list { max-height: 56vh; overflow-y: auto; padding-bottom: 8px; }
.palette-group { padding: 10px 18px 4px; font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em; color: #6b6b6b; }
.palette-item { display: flex; gap: 10px; align-items: center; width: 100%; padding: 9px 18px; background: none; border: 0; color: var(--muted); font: inherit; cursor: pointer; text-align: left; }
.palette-item.is-active { background: var(--accent-soft); color: var(--text); }
.palette-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-empty { padding: 18px; }

/* ---------- headings / layout ---------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.group-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 24px 0 10px; font-weight: 600; }
.small-title { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 14px 0 8px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); margin-bottom: 16px; }
.row-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0 16px; }
.back-link { display: inline-flex; align-items: center; gap: 4px; margin-bottom: 14px; color: var(--muted); text-decoration: none; }
.back-link:hover { color: var(--text); }
.user-head h1 { overflow-wrap: anywhere; }
.user-head .muted { overflow-wrap: anywhere; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; min-width: 0; }
.grid-2 .card { margin-bottom: 0; }
.card-title { margin-bottom: 12px; }
.card > .muted.small { margin-bottom: 12px; }

.metric { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 6px; position: relative; min-width: 0; }
.metric-wide { grid-column: 1 / -1; }
.metric-label { font-size: 0.85rem; font-weight: 500; color: var(--muted); }
.metric-value { font-size: 2rem; font-weight: 650; line-height: 1.1; font-variant-numeric: tabular-nums; }
.metric-link { color: inherit; text-decoration: none; }
.metric-link:hover { border-color: var(--accent); }
.metric-link > .icon { position: absolute; right: 14px; top: 16px; color: var(--muted); }
.metric-err { border-color: var(--danger); }
.metric-error { color: var(--danger); font-weight: 600; }
.err-text { display: block; font-size: 0.8rem; color: var(--text); overflow-wrap: anywhere; background: var(--surface-2); padding: 8px; border-radius: 8px; margin-top: 4px; white-space: pre-wrap; }
.hint { color: var(--muted); font-size: 0.85rem; margin-top: 6px; }

/* ---------- key/value ---------- */
.kv { margin: 0; display: grid; gap: 0; }
.kv-row { display: grid; grid-template-columns: minmax(110px, 38%) minmax(0, 1fr); gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.kv-row:last-child { border-bottom: 0; }
.kv dt { color: var(--muted); font-size: 0.88rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; }
.card .table-wrap { background: transparent; }
.table { width: 100%; border-collapse: collapse; min-width: 320px; }
.table th { text-align: left; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table-plain td { padding: 9px 14px; }
.row-link { cursor: pointer; }
.row-link:hover, .row-link:focus-visible { background: var(--surface-2); }
.row-link:focus-visible { outline-offset: -2px; }
.row-chev { color: var(--muted); width: 32px; }
.td-actions { text-align: right; white-space: nowrap; }
@media (max-width: 600px) { .hide-sm { display: none; } .table td, .table th { padding-left: 10px; padding-right: 10px; } }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.field label, .field > label { font-size: 0.88rem; font-weight: 550; }
input[type='text'], input[type='tel'], input[type='url'], input[type='email'], input[type='search'], textarea, select {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font: inherit; min-height: 42px;
}
textarea { resize: vertical; }
input::placeholder, textarea::placeholder { color: #5f5f5f; }
input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.has-error input, .has-error textarea, .has-error select { border-color: var(--danger); }
.field-hint { color: var(--muted); font-size: 0.8rem; }
.field-error { color: var(--danger); font-size: 0.82rem; min-height: 0; }
.field-error:empty { display: none; }
.toolbar { display: flex; gap: 12px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .field { flex: 1 1 260px; margin-bottom: 0; }
.toolbar-actions { display: flex; gap: 8px; padding-top: 25px; flex-wrap: wrap; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.login-links { margin-top: 12px; }

.switch { display: inline-flex; align-items: center; gap: 12px; cursor: pointer; margin-bottom: 14px; min-height: 32px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track { width: 42px; height: 24px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; position: relative; flex: none; transition: background 0.15s; }
.switch-track::after { content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: var(--muted); transition: transform 0.15s, background 0.15s; }
.switch input:checked + .switch-track { background: var(--accent-soft); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: var(--accent); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 42px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; text-decoration: none; }
.btn:hover:not(:disabled) { border-color: #3a3a3a; background: #202020; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #0a0a0a; }
.btn-primary:hover:not(:disabled) { background: #ff6030; border-color: #ff6030; }
.btn-danger { color: var(--danger); background: var(--danger-soft); border-color: rgba(229, 72, 77, 0.4); }
.btn-danger:hover:not(:disabled) { background: rgba(229, 72, 77, 0.24); border-color: var(--danger); }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 0.88rem; }
.more { margin: 12px 0; display: flex; }

/* ---------- badges, notices, states ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 0.76rem; font-weight: 600; border: 1px solid var(--border); color: var(--muted); background: var(--surface-2); white-space: nowrap; }
.badge-success { color: var(--success); background: var(--success-soft); border-color: rgba(46, 189, 107, 0.35); }
.badge-accent { color: var(--accent); background: var(--accent-soft); border-color: rgba(255, 74, 10, 0.4); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }

.notice { display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); margin-bottom: 14px; }
.notice .icon { margin-top: 3px; }
.notice p + p, .notice strong + p { margin-top: 4px; color: var(--muted); font-size: 0.9rem; }
.notice-error { border-color: rgba(229, 72, 77, 0.5); background: var(--danger-soft); }
.notice-error .icon { color: var(--danger); }
.notice-info .icon { color: var(--info); }
.notice-warning .icon { color: var(--warn); }

.state { padding: 24px 18px; border: 1px dashed var(--border); border-radius: var(--radius); display: flex; flex-direction: column; gap: 6px; align-items: flex-start; margin: 8px 0; }
.state p { color: var(--muted); }
.state-loading { flex-direction: row; align-items: center; gap: 10px; border-style: solid; color: var(--muted); }
.state-error { border: 1px solid rgba(229, 72, 77, 0.5); background: var(--danger-soft); width: 100%; }
.state-error strong { color: var(--danger); }
.state-error p { color: var(--text); }
.state-error .hint { color: var(--muted); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .switch-track, .switch-track::after { transition: none; } }

/* ---------- banner preview ---------- */
.banner { display: flex; gap: 12px; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); align-items: flex-start; }
.banner .icon { margin-top: 2px; }
.banner-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
.banner-text p { color: var(--text); opacity: 0.88; }
.banner-link { font-weight: 600; margin-top: 4px; }
.banner-info { background: var(--info-soft); border-color: rgba(91, 157, 255, 0.45); }
.banner-info .icon, .banner-info .banner-link { color: var(--info); }
.banner-warning { background: var(--warn-soft); border-color: rgba(232, 163, 23, 0.5); }
.banner-warning .icon, .banner-warning .banner-link { color: var(--warn); }
.preview { min-height: 40px; display: flex; flex-direction: column; gap: 8px; }

/* ---------- inbox ---------- */
.segmented { display: inline-flex; gap: 4px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.seg { background: transparent; border: 0; color: var(--muted); padding: 8px 14px; border-radius: 8px; font: inherit; cursor: pointer; min-height: 36px; }
.seg:hover { color: var(--text); }
.seg.is-active { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }
.item-list { display: flex; flex-direction: column; gap: 8px; }
.item { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.item-new { border-left: 3px solid var(--accent); }
.item-head { width: 100%; display: flex; gap: 12px; align-items: center; background: transparent; border: 0; color: inherit; font: inherit; text-align: left; padding: 14px; cursor: pointer; }
.item-head:hover { background: var(--surface-2); }
.item-head:focus-visible { outline-offset: -2px; }
.item-head > .icon { color: var(--muted); transition: transform 0.15s; }
.item-head.is-open > .icon { transform: rotate(90deg); }
.item-main { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-sub { color: var(--muted); font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-preview { color: var(--muted); font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: 0.8; }
.item-head.is-open .item-preview { display: none; }
.item-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.item-body { padding: 4px 16px 16px 42px; border-top: 1px solid var(--border); }
.message-text { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin: 12px 0; }
.item-actions { display: flex; gap: 8px; flex-wrap: wrap; }
@media (max-width: 600px) { .item-body { padding-left: 16px; } .item-meta .small { display: none; } }

/* ---------- chart + raw ---------- */
.chart { width: 100%; height: auto; max-width: 640px; display: block; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis { fill: var(--muted); font-size: 10px; font-family: var(--font); }
.chart-bar { fill: #7a2a10; }
.chart-bar-hit { fill: var(--accent); }
.chart-bar-empty { fill: var(--border); }
.chart-goal { stroke: var(--success); stroke-width: 1.5; stroke-dasharray: 5 4; }
.raw summary { cursor: pointer; font-weight: 600; }
.json { margin: 12px 0 0; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px; overflow: auto; max-height: 420px; font: 0.8rem/1.5 var(--mono); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px 16px; gap: 14px; }
.login > * { width: 100%; max-width: 400px; }
.login-brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 1.1rem; }
.login-brand .icon { color: var(--accent); }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.login-card .btn-primary { width: 100%; }
.login-msg:empty { display: none; }
.login-msg .notice { margin: 0; }

/* ---------- dialog + toast ---------- */
.dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; max-width: min(440px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
.dialog h2 { margin-bottom: 8px; }
.dialog p { color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.toasts { position: fixed; bottom: 16px; left: 16px; right: 16px; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 200; pointer-events: none; }
.toast { display: flex; gap: 10px; align-items: center; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; max-width: 520px; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5); overflow-wrap: anywhere; }
.toast-success .icon { color: var(--success); }
.toast-error { border-color: var(--danger); }
.toast-error .icon { color: var(--danger); }
