:root {
  color-scheme: light;
  --bg: oklch(0.975 0.006 235);
  --surface: oklch(0.995 0.004 235);
  --surface-2: oklch(0.955 0.008 235);
  --surface-3: oklch(0.925 0.011 235);
  --text: oklch(0.235 0.018 245);
  --muted: oklch(0.505 0.018 245);
  --border: oklch(0.875 0.012 235);
  --accent: oklch(0.535 0.145 194);
  --accent-hover: oklch(0.475 0.135 194);
  --accent-soft: oklch(0.925 0.045 194);
  --success: oklch(0.52 0.125 155);
  --success-soft: oklch(0.945 0.035 155);
  --warning: oklch(0.62 0.135 78);
  --warning-soft: oklch(0.955 0.04 78);
  --danger: oklch(0.54 0.18 25);
  --danger-soft: oklch(0.95 0.035 25);
  --sidebar: oklch(0.205 0.022 247);
  --sidebar-muted: oklch(0.69 0.018 247);
  --sidebar-active: oklch(0.305 0.035 225);
  --shadow: 0 14px 34px color-mix(in oklch, var(--text) 8%, transparent);
  --radius: 7px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: oklch(0.155 0.012 238);
  --surface: oklch(0.205 0.014 238);
  --surface-2: oklch(0.245 0.016 238);
  --surface-3: oklch(0.305 0.018 238);
  --text: oklch(0.925 0.008 225);
  --muted: oklch(0.69 0.014 230);
  --border: oklch(0.32 0.018 238);
  --accent: oklch(0.665 0.125 190);
  --accent-hover: oklch(0.72 0.115 190);
  --accent-soft: oklch(0.275 0.045 190);
  --success: oklch(0.7 0.13 155);
  --success-soft: oklch(0.255 0.038 155);
  --warning: oklch(0.76 0.13 78);
  --warning-soft: oklch(0.275 0.04 78);
  --danger: oklch(0.7 0.15 25);
  --danger-soft: oklch(0.275 0.045 25);
  --sidebar: oklch(0.125 0.014 242);
  --sidebar-muted: oklch(0.7 0.014 235);
  --sidebar-active: oklch(0.245 0.032 215);
  --shadow: 0 18px 42px oklch(0.07 0.012 240 / 0.42);
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body { margin: 0; min-width: 320px; min-height: 100vh; color: var(--text); background: var(--bg); font-size: 14px; line-height: 1.45; letter-spacing: 0; }
button, input, select, textarea { font: inherit; color: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }
[hidden] { display: none !important; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 28px; background: var(--bg); }
.login-panel { width: min(100%, 390px); padding: 34px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.login-panel h1 { margin: 32px 0 22px; font-size: 1.45rem; line-height: 1.2; }
.login-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.login-brand { color: var(--text) !important; }
.login-brand span { color: var(--muted) !important; }
.login-form { display: grid; gap: 12px; }
.login-form label { font-weight: 650; font-size: 0.84rem; }
.form-error { margin: 0; color: var(--danger); font-size: 0.84rem; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 224px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; padding: 22px 14px 16px; background: var(--sidebar); color: oklch(0.95 0.006 247); }
.brand-lockup { display: flex; align-items: center; gap: 11px; min-height: 40px; padding: 0 8px; }
.brand-lockup > div { display: grid; line-height: 1.15; }
.brand-lockup strong { font-size: 0.93rem; }
.brand-lockup span { margin-top: 4px; color: var(--sidebar-muted); font-size: 0.72rem; }
.brand-signal { width: 32px; height: 32px; flex: 0 0 32px; display: grid; place-items: center; margin: 0 !important; border-radius: 8px; color: oklch(0.985 0.003 194) !important; background: var(--accent); font-size: 0.65rem !important; font-weight: 800; line-height: 1; }
.primary-nav { display: grid; gap: 3px; margin-top: 34px; }
.nav-button { width: 100%; min-height: 40px; display: flex; align-items: center; gap: 11px; padding: 0 12px; border: 0; border-radius: 6px; text-align: left; color: var(--sidebar-muted); background: transparent; font-weight: 600; transition: color 160ms ease, background 160ms ease; }
.nav-button svg, .sidebar-action svg { width: 17px; height: 17px; flex: 0 0 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-button:hover { color: oklch(0.96 0.005 247); background: color-mix(in oklch, var(--sidebar-active) 55%, transparent); }
.nav-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.nav-button.active { color: oklch(0.975 0.004 247); background: var(--sidebar-active); }
.sidebar-footer { margin-top: auto; display: grid; gap: 12px; padding: 14px 8px 2px; border-top: 1px solid oklch(0.32 0.018 247); }
.service-state { display: flex; align-items: center; gap: 8px; color: var(--sidebar-muted); font-size: 0.77rem; }
.status-dot { width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 3px color-mix(in oklch, var(--success) 18%, transparent); }
.status-dot.down { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in oklch, var(--danger) 18%, transparent); }
.sidebar-action { width: 100%; min-height: 34px; display: flex; align-items: center; gap: 9px; padding: 0; border: 0; color: var(--sidebar-muted); background: transparent; font-size: 0.77rem; font-weight: 600; text-align: left; }
.sidebar-action:hover { color: oklch(0.96 0.005 247); }
.sidebar-action:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.main-area { min-width: 0; padding: 0 28px 48px; }
.topbar { min-height: 88px; display: flex; align-items: center; justify-content: space-between; gap: 18px; border-bottom: 1px solid var(--border); }
.topbar h1 { margin: 0; font-size: 1.35rem; line-height: 1.2; }
.updated-at { margin: 5px 0 0; color: var(--muted); font-size: 0.78rem; }
.toolbar, .page-actions, .section-heading, .form-actions, .inline-form, .secret-panel { display: flex; align-items: center; gap: 10px; }
.page { display: none; padding-top: 26px; }
.page.active { display: block; }
.page-actions { justify-content: space-between; margin-bottom: 18px; }
.page-actions h2, .section-heading h2 { margin: 0; font-size: 1rem; line-height: 1.25; }
.page-actions p, .section-heading p { margin: 5px 0 0; color: var(--muted); font-size: 0.8rem; }

.button { min-height: 36px; display: inline-flex; align-items: center; justify-content: center; padding: 0 14px; border: 1px solid transparent; border-radius: 6px; font-weight: 650; font-size: 0.82rem; transition: background 160ms ease, border-color 160ms ease, color 160ms ease, box-shadow 160ms ease; }
.button:focus-visible, .text-button:focus-visible, .table-action:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.button.primary { color: oklch(0.985 0.003 194); background: var(--accent); border-color: var(--accent); }
.button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.button.secondary { color: var(--text); background: var(--surface); border-color: var(--border); }
.button.secondary:hover { background: var(--surface-2); border-color: var(--surface-3); }
.button.wide { width: 100%; }
.icon-button { width: 36px; height: 36px; display: inline-grid; flex: 0 0 36px; place-items: center; padding: 0; border: 1px solid var(--border); border-radius: 6px; color: var(--muted); background: var(--surface); transition: background 160ms ease, border-color 160ms ease, color 160ms ease; }
.icon-button:hover { color: var(--text); background: var(--surface-2); border-color: var(--surface-3); }
.icon-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.theme-icon { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.theme-icon-sun { display: none; }
html[data-theme="dark"] .theme-icon-moon { display: none; }
html[data-theme="dark"] .theme-icon-sun { display: block; color: var(--warning); }
.mobile-theme-toggle { display: none; }
.text-button { display: inline-flex; align-items: center; padding: 0; border: 0; color: var(--accent-hover); background: transparent; font-weight: 650; font-size: 0.8rem; }
.text-button:hover { color: var(--accent); }
.table-action { min-height: 30px; padding: 0 8px; border: 0; border-radius: 5px; color: var(--accent-hover); background: transparent; font-size: 0.76rem; font-weight: 650; }
.table-action:hover { background: var(--accent-soft); }
.table-action.danger { color: var(--danger); }
.table-action.danger:hover { background: var(--danger-soft); }

.loading-bar { position: fixed; z-index: 20; top: 0; left: 224px; right: 0; height: 2px; overflow: hidden; background: color-mix(in oklch, var(--accent) 16%, transparent); }
.loading-bar span { display: block; width: 34%; height: 100%; background: var(--accent); animation: load 1.05s infinite cubic-bezier(.76, 0, .24, 1); }
@keyframes load { from { transform: translateX(-110%); } to { transform: translateX(390%); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; } }

.alert { margin-top: 16px; padding: 10px 12px; border: 1px solid; border-radius: 6px; font-size: 0.82rem; }
.alert.error { color: var(--danger); background: var(--danger-soft); border-color: color-mix(in oklch, var(--danger) 25%, var(--border)); }
.metric-strip { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.metric { min-width: 0; padding: 18px 18px 17px; border-right: 1px solid var(--border); }
.metric:first-child { padding-left: 0; }
.metric:last-child { border-right: 0; }
.metric-label { display: block; color: var(--muted); font-size: 0.75rem; font-weight: 600; }
.metric-value { display: block; margin-top: 5px; font-size: 1.28rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.metric-note { display: block; min-height: 1.15em; margin-top: 4px; color: var(--muted); font-size: 0.7rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.two-column-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 22px; margin-top: 26px; }
.data-section { min-width: 0; }
.section-heading { justify-content: space-between; min-height: 48px; margin-bottom: 10px; }

.table-frame { width: 100%; overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; min-width: 680px; border-collapse: collapse; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; white-space: nowrap; }
th { color: var(--muted); background: var(--surface-2); font-size: 0.71rem; font-weight: 700; text-transform: uppercase; }
td { font-size: 0.79rem; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in oklch, var(--accent-soft) 38%, var(--surface)); }
.actions-column { text-align: right; }
td.actions-column { display: flex; justify-content: flex-end; gap: 2px; }
.cell-main { display: block; max-width: 280px; overflow: hidden; text-overflow: ellipsis; font-weight: 650; }
.cell-sub { display: block; max-width: 280px; margin-top: 2px; overflow: hidden; color: var(--muted); font-size: 0.7rem; text-overflow: ellipsis; }
.code-cell { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.72rem; }
.empty-row td { height: 130px; color: var(--muted); text-align: center; }

.badge { display: inline-flex; align-items: center; min-height: 22px; padding: 0 8px; border-radius: 999px; color: var(--muted); background: var(--surface-2); font-size: 0.7rem; font-weight: 700; }
.badge.success { color: var(--success); background: var(--success-soft); }
.badge.warning { color: color-mix(in oklch, var(--warning) 82%, var(--text)); background: var(--warning-soft); }
.badge.danger { color: var(--danger); background: var(--danger-soft); }
.badge.info { color: var(--accent-hover); background: var(--accent-soft); }

input, select, textarea { width: 100%; min-height: 38px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; outline: 0; background: var(--surface); transition: border-color 150ms ease, box-shadow 150ms ease; }
textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.78rem; line-height: 1.5; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in oklch, var(--accent) 14%, transparent); }
input:disabled { color: var(--muted); background: var(--surface-2); }
label { display: grid; gap: 6px; color: var(--muted); font-size: 0.76rem; font-weight: 650; }
.check-label { display: flex; align-items: center; gap: 8px; min-height: 38px; color: var(--text); }
.check-label input { width: 16px; min-height: 16px; accent-color: var(--accent); }
.field-hint { color: var(--muted); font-size: 0.68rem; font-weight: 500; }
.editor-panel { margin-bottom: 18px; padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.form-actions { justify-content: flex-end; padding-top: 4px; }
.inline-form { width: min(100%, 520px); margin-bottom: 16px; }
.inline-form input { flex: 1; }
.secret-panel { justify-content: space-between; margin-bottom: 16px; padding: 13px 14px; background: var(--warning-soft); border: 1px solid color-mix(in oklch, var(--warning) 25%, var(--border)); border-radius: var(--radius); }
.secret-panel > div { min-width: 0; display: grid; gap: 5px; }
.secret-panel code { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.settings-form { max-width: 980px; display: grid; gap: 18px; }
fieldset { margin: 0; padding: 18px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
legend { padding: 0 6px; font-size: 0.82rem; font-weight: 750; }
.route-editors { display: grid; gap: 22px; }
.route-editor { min-width: 0; }
.route-editor + .route-editor { padding-top: 20px; border-top: 1px solid var(--border); }
.route-editor-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 10px; }
.route-editor-heading h3 { margin: 0; font-size: 0.86rem; }
.route-editor-heading p { margin: 4px 0 0; color: var(--muted); font-size: 0.72rem; }
.route-list { display: grid; gap: 8px; }
.route-list:empty::before { content: attr(data-empty); min-height: 54px; display: grid; place-items: center; padding: 12px; border: 1px dashed var(--border); border-radius: 6px; color: var(--muted); background: var(--bg); font-size: 0.75rem; }
.route-row { display: grid; grid-template-columns: minmax(0, 1fr) 18px minmax(0, 1fr) auto; align-items: end; gap: 8px; }
.route-row.vision { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; }
.route-arrow { align-self: center; color: var(--muted); font-size: 0.9rem; text-align: center; }
.route-remove { width: 36px; height: 38px; align-self: end; display: grid; place-items: center; padding: 0; border: 1px solid var(--border); border-radius: 6px; color: var(--danger); background: var(--surface); }
.route-remove:hover { background: var(--danger-soft); border-color: color-mix(in oklch, var(--danger) 28%, var(--border)); }
.route-remove:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.route-remove svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.compact-control { display: flex; align-items: center; gap: 8px; }
.compact-control select { width: 76px; min-height: 34px; }

.toast-region { position: fixed; z-index: 100; right: 20px; bottom: 20px; display: grid; gap: 8px; width: min(360px, calc(100vw - 40px)); }
.toast { padding: 11px 13px; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; box-shadow: var(--shadow); font-size: 0.8rem; }
.toast.error { color: var(--danger); border-color: color-mix(in oklch, var(--danger) 30%, var(--border)); }

@media (max-width: 1100px) {
  .metric-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metric:nth-child(3) { border-right: 0; }
  .metric:nth-child(n+4) { border-top: 1px solid var(--border); }
  .two-column-layout { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  body { padding-bottom: 0; }
  .app-shell { display: block; min-height: 0; height: 100vh; height: 100dvh; overflow: hidden; }
  .sidebar { position: fixed; z-index: 40; top: auto; right: 0; bottom: 0; left: 0; width: auto; height: calc(66px + env(safe-area-inset-bottom)); display: block; padding: 0 0 env(safe-area-inset-bottom); border-top: 1px solid color-mix(in oklch, var(--sidebar-muted) 24%, transparent); }
  .sidebar > .brand-lockup, .sidebar-footer { display: none; }
  .primary-nav { height: 100%; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0; margin: 0; }
  .nav-button { min-width: 0; min-height: 66px; flex-direction: column; justify-content: center; gap: 4px; padding: 4px 2px 3px; border-radius: 0; text-align: center; font-size: 0.66rem; }
  .nav-button svg { width: 16px; height: 16px; flex-basis: 16px; }
  .main-area { height: calc(100vh - 66px); height: calc(100dvh - 66px - env(safe-area-inset-bottom)); overflow-y: auto; overscroll-behavior: contain; padding: 0 14px 30px; }
  .topbar { min-height: 74px; }
  .topbar h1 { font-size: 1.15rem; }
  .mobile-theme-toggle { display: inline-grid; }
  .loading-bar { left: 0; }
  .page { padding-top: 18px; }
  .metric-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { padding: 14px 12px; }
  .metric:first-child { padding-left: 12px; }
  .metric:nth-child(2n) { border-right: 0; }
  .metric:nth-child(3) { border-right: 1px solid var(--border); }
  .metric:nth-child(n+3) { border-top: 1px solid var(--border); }
  .metric:last-child { grid-column: span 2; border-right: 0; }
  .page-actions { align-items: flex-start; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2, .span-3 { grid-column: auto; }
  .inline-form { align-items: stretch; }
  .secret-panel { align-items: flex-start; }
  .login-panel { padding: 26px 22px; }
  .empty-row td { text-align: left; white-space: normal; }
  .route-row, .route-row.vision { grid-template-columns: minmax(0, 1fr) auto; }
  .route-arrow { display: none; }
  .route-row label { grid-column: 1; }
  .route-remove { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
}

@media (max-width: 430px) {
  .toolbar .button { padding: 0 11px; }
  .updated-at { max-width: 205px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .page-actions { gap: 12px; }
  .page-actions > div { min-width: 0; }
  .inline-form, .secret-panel { flex-direction: column; }
  .inline-form .button, .secret-panel .button { width: 100%; }
  .route-editor-heading { align-items: flex-start; }
  .route-editor-heading .button { flex: 0 0 auto; padding: 0 10px; }
  .toast-region { right: 12px; bottom: 78px; width: calc(100vw - 24px); }
}
