:root {
  color-scheme: light dark;
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-muted: #f8f9fb;
  --border: #e2e5eb;
  --border-strong: #cdd2db;
  --text: #1b1f27;
  --text-muted: #5c6472;
  --accent: #3b5bdb;
  --accent-hover: #2f4ac0;
  --accent-soft: #e8edff;
  --accent-contrast: #ffffff;
  --sidebar: #141a26;
  --sidebar-text: #c9d0dc;
  --sidebar-active: #252e40;
  --ok: #1f7a4d;
  --ok-soft: #e3f4ea;
  --warn: #8a5a00;
  --warn-soft: #fff3d6;
  --error: #b42318;
  --error-soft: #fde8e6;
  --info: #2851a3;
  --info-soft: #e5edfb;
  --muted-soft: #eceef2;
  --focus: #6b8afd;
  --radius: 12px;
  --radius-small: 8px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131b;
    --surface: #171c26;
    --surface-muted: #1c2230;
    --border: #2a3140;
    --border-strong: #3a4355;
    --text: #e7eaf0;
    --text-muted: #9aa3b2;
    --accent: #7b94ff;
    --accent-hover: #93a7ff;
    --accent-soft: #232c4a;
    --accent-contrast: #0f131b;
    --sidebar: #0b0e14;
    --sidebar-text: #aab3c2;
    --sidebar-active: #1d2433;
    --ok: #5fd39a;
    --ok-soft: #173327;
    --warn: #f2c14e;
    --warn-soft: #3a2f12;
    --error: #ff8a80;
    --error-soft: #3d1c1a;
    --info: #9bb8ff;
    --info-soft: #1d2a47;
    --muted-soft: #242a36;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}

[hidden] { display: none !important; }

h1, h2, p { margin: 0; }
h1 { font-size: 1.6rem; line-height: 1.25; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; font-weight: 620; }
a { color: var(--accent); }
code { font-family: var(--mono); font-size: 0.9em; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
h1:focus, main:focus, .alert:focus { outline: none; }

.muted { color: var(--text-muted); }
.small { font-size: 0.85rem; }
.strong { font-weight: 600; }

.visually-hidden, .offscreen {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: 1rem; top: -3rem;
  z-index: 100;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-small);
  background: var(--accent);
  color: var(--accent-contrast);
}
.skip-link:focus { top: 1rem; }

/* Buttons */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 2.5rem;
  padding: 0.5rem 1rem;
  border: 1px solid transparent;
  border-radius: var(--radius-small);
  font: inherit;
  font-weight: 560;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 120ms, border-color 120ms, color 120ms;
}
.button:disabled { opacity: 0.55; cursor: not-allowed; }
.button-small { min-height: 2rem; padding: 0.3rem 0.75rem; font-size: 0.875rem; }
.button-block { width: 100%; }
.button-primary { background: var(--accent); color: var(--accent-contrast); }
.button-primary:hover:not(:disabled) { background: var(--accent-hover); }
.button-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.button-secondary:hover:not(:disabled) { background: var(--surface-muted); }
.button-ghost { background: transparent; color: var(--accent); }
.button-ghost:hover:not(:disabled) { background: var(--accent-soft); }
.button-danger { background: var(--error); color: #fff; }
.button-danger:hover:not(:disabled) { filter: brightness(0.92); }
.button-sidebar { background: transparent; color: var(--sidebar-text); border-color: #3a4355; }
.button-sidebar:hover { background: var(--sidebar-active); color: #fff; }

.link-button {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  text-decoration: underline;
  cursor: pointer;
}

/* Boot and login */
.boot, .login {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1.5rem;
}
.boot { display: flex; justify-content: center; align-items: center; gap: 0.75rem; color: var(--text-muted); }

.login-card {
  display: grid;
  gap: 1rem;
  width: min(100%, 26rem);
  padding: 2rem;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand { display: flex; align-items: center; gap: 0.6rem; }
.brand-mark { width: 1.75rem; height: 1.75rem; flex: none; color: var(--accent); }
.brand-name { font-size: 1.2rem; font-weight: 700; letter-spacing: 0.02em; }
.brand-login { margin-bottom: 0.5rem; }

/* Shell */
.shell {
  display: grid;
  grid-template-columns: 15rem minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
  height: 100vh;
  padding: 1.5rem 1rem;
  background: var(--sidebar);
  color: var(--sidebar-text);
}
.sidebar .brand-name { color: #fff; }
.sidebar .brand-mark { color: #8fa4ff; }

.nav ul { display: grid; gap: 0.25rem; margin: 0; padding: 0; list-style: none; }
.nav a {
  display: block;
  padding: 0.55rem 0.75rem;
  border-radius: var(--radius-small);
  color: var(--sidebar-text);
  font-weight: 540;
  text-decoration: none;
}
.nav a:hover { background: var(--sidebar-active); color: #fff; }
.nav a[aria-current="page"] { background: var(--sidebar-active); color: #fff; box-shadow: inset 3px 0 0 #8fa4ff; }

.account { display: grid; gap: 0.2rem; margin-top: auto; padding-top: 1rem; border-top: 1px solid #262e3d; }
.account-name { color: #fff; font-weight: 600; overflow-wrap: anywhere; }
.account-role { margin-bottom: 0.5rem; font-size: 0.85rem; }

.content {
  display: block;
  width: 100%;
  max-width: 78rem;
  padding: 2rem clamp(1rem, 3vw, 2.5rem) 4rem;
}

.view { display: grid; gap: 1.25rem; }

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
}
.page-header .muted { margin-top: 0.3rem; max-width: 48rem; }
.page-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.breadcrumb a { color: var(--text-muted); text-decoration: none; font-size: 0.9rem; }
.breadcrumb a:hover { color: var(--accent); }

/* Cards */
.card {
  display: grid;
  gap: 1rem;
  padding: 1.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card-header { display: flex; justify-content: space-between; gap: 1rem; }
.card-header .hint { margin-top: 0.2rem; }
.card-danger { border-color: color-mix(in srgb, var(--error) 35%, var(--border)); }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.stat { gap: 0.2rem; padding: 1rem 1.25rem; }
.stat-label { color: var(--text-muted); font-size: 0.85rem; font-weight: 540; }
.stat-value { font-size: 1.75rem; font-weight: 680; font-variant-numeric: tabular-nums; }
.tone-ok { color: var(--ok); }
.tone-warn { color: var(--warn); }
.tone-muted { color: var(--text-muted); }

/* Forms */
.field { display: grid; gap: 0.3rem; align-content: start; min-width: 0; }
.field label { font-weight: 560; font-size: 0.9rem; }
.hint { color: var(--text-muted); font-size: 0.85rem; }
.field-error { color: var(--error); font-size: 0.85rem; font-weight: 540; }

input[type="text"], input[type="url"], input[type="search"], select, textarea {
  width: 100%;
  min-height: 2.5rem;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
textarea { resize: vertical; font-family: var(--mono); font-size: 0.85rem; word-break: break-all; }
input[type="url"], [data-role="userAgent"] { font-family: var(--mono); font-size: 0.85rem; }
input::placeholder, textarea::placeholder { color: var(--text-muted); opacity: 0.75; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input[readonly] { background: var(--surface-muted); color: var(--text-muted); }
[aria-invalid="true"] { border-color: var(--error) !important; }

.field-check {
  grid-template-columns: auto 1fr;
  align-items: center;
  column-gap: 0.6rem;
}
.field-check input { width: 1.15rem; height: 1.15rem; margin: 0; accent-color: var(--accent); }
.field-check .hint { grid-column: 2; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.filters .field { flex: 1 1 11rem; }
.filters .field-grow { flex: 3 1 16rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 1rem;
}
.field-wide { grid-column: 1 / -1; }

.disclosure { gap: 0; }
.disclosure summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  list-style: none;
}
.disclosure summary::-webkit-details-marker { display: none; }
.disclosure summary::before {
  content: "";
  width: 0.5rem; height: 0.5rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 120ms;
}
.disclosure[open] summary::before { transform: rotate(45deg); }
.disclosure[open] summary { margin-bottom: 1rem; }

/* Repeated source rows */
.rows { display: grid; gap: 0.75rem; }
.row {
  position: relative;
  display: grid;
  gap: 0.75rem;
  margin: 0;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface-muted);
  min-width: 0;
}
.row-title { padding: 0 0.35rem; font-weight: 620; font-size: 0.9rem; }
.row-grid {
  display: grid;
  grid-template-columns: minmax(10rem, 1fr) minmax(14rem, 2.5fr) minmax(9rem, 1fr);
  gap: 0.75rem;
}
.field-full { grid-column: 2 / -1; }
.row-remove { justify-self: end; color: var(--error); }
.row-remove:hover:not(:disabled) { background: var(--error-soft); }
.empty-rows { padding: 0.75rem 1rem; border: 1px dashed var(--border-strong); border-radius: var(--radius-small); text-align: center; }
#add-exit, #add-entry { justify-self: start; }

/* Editor layout */
.editor-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 19rem;
  align-items: start;
  gap: 1.25rem;
}
.editor-form { display: grid; gap: 1rem; min-width: 0; }
.editor-side { position: sticky; top: 1rem; display: grid; gap: 1rem; }
.editor-layout:has(.editor-side[hidden]) { grid-template-columns: minmax(0, 1fr); }

.savebar {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(6px);
  box-shadow: 0 -4px 12px rgb(16 24 40 / 6%);
}
.dirty-indicator { margin-right: auto; color: var(--warn); font-size: 0.9rem; font-weight: 560; }

.details {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.5rem 1rem;
  margin: 0;
}
.details dt { color: var(--text-muted); font-size: 0.9rem; }
.details dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.details .status-detail { grid-column: 1 / -1; text-align: left; }

.links { display: grid; gap: 0.75rem; margin: 0; padding: 0; list-style: none; }
.link-row { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
.link-label { font-weight: 580; }
.link-row .hint { overflow-wrap: anywhere; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -1.25rem -1.25rem; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.75rem 1.25rem; border-top: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { color: var(--text-muted); font-size: 0.8rem; font-weight: 600; letter-spacing: 0.03em; text-transform: uppercase; background: var(--surface-muted); }
.table tbody tr:hover { background: var(--surface-muted); }
.table .numeric { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }
.table .actions .button + .button { margin-left: 0.35rem; }
.row-link { color: var(--text); font-weight: 600; text-decoration: none; }
.row-link:hover { color: var(--accent); text-decoration: underline; }
.row-disabled td { color: var(--text-muted); }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge::before { content: ""; width: 0.45rem; height: 0.45rem; border-radius: 50%; background: currentColor; }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-error { background: var(--error-soft); color: var(--error); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-muted { background: var(--muted-soft); color: var(--text-muted); }

/* States and alerts */
.state:empty { display: none; }
.state {
  display: grid;
  justify-items: center;
  gap: 0.75rem;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
  text-align: center;
}
.state[data-kind="error"] { color: var(--error); }
.state[data-kind="loading"] { grid-auto-flow: column; justify-content: center; align-items: center; }

.alert {
  display: grid;
  gap: 0.6rem;
  padding: 0.85rem 1rem;
  border: 1px solid transparent;
  border-radius: var(--radius-small);
}
.alert-error { background: var(--error-soft); border-color: color-mix(in srgb, var(--error) 30%, transparent); color: var(--error); }
.alert-warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 30%, transparent); color: var(--warn); }
.alert-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.error-list { display: grid; gap: 0.25rem; margin: 0; padding-left: 1.25rem; }
.error-list:empty { display: none; }

.spinner {
  width: 1.1rem; height: 1.1rem;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  z-index: 50;
  max-width: min(90vw, 28rem);
  padding: 0.7rem 1.1rem;
  border-radius: var(--radius-small);
  background: #1b2130;
  color: #fff;
  box-shadow: 0 8px 24px rgb(0 0 0 / 20%);
  opacity: 0;
  transform: translate(-50%, 0.5rem);
  pointer-events: none;
  transition: opacity 160ms, transform 160ms;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
.toast[data-tone="error"] { background: var(--error); }
.toast:empty { display: none; }

/* Dialogs */
.dialog {
  width: min(92vw, 30rem);
  padding: 1.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(0 0 0 / 25%);
}
.dialog::backdrop { background: rgb(10 14 22 / 55%); }
.dialog form { display: grid; gap: 1rem; }
.dialog-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.5rem; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}

/* Tablet: side panel moves under the form. */
@media (max-width: 1080px) {
  .editor-layout { grid-template-columns: minmax(0, 1fr); }
  .editor-side { position: static; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); align-items: start; }
}

@media (max-width: 860px) {
  .row-grid { grid-template-columns: 1fr 1fr; }
  .row-grid .field-grow, .field-full { grid-column: 1 / -1; }
}

/* Mobile: sidebar becomes a top bar and tables become labelled cards. */
@media (max-width: 720px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    flex-flow: row wrap;
    align-items: center;
    gap: 0.75rem 1rem;
    height: auto;
    padding: 0.75rem 1rem;
  }
  .nav { order: 3; flex-basis: 100%; }
  .nav ul { grid-auto-flow: column; justify-content: start; }
  .nav a[aria-current="page"] { box-shadow: inset 0 -2px 0 #8fa4ff; }
  .account { display: flex; align-items: center; gap: 0.75rem; margin: 0 0 0 auto; padding: 0; border: 0; }
  .account-name { font-size: 0.9rem; }
  .account-role { display: none; }
  .account .button { margin: 0; }
  .content { padding-top: 1.25rem; }
  h1 { font-size: 1.35rem; }
  .row-grid { grid-template-columns: 1fr; }
  .row-grid .field-full { grid-column: auto; }
  .savebar { margin: 0 -0.25rem; }

  .table-wrap { margin: 0; overflow: visible; }
  .responsive-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .responsive-table, .responsive-table tbody, .responsive-table tr, .responsive-table td { display: block; }
  .responsive-table tr {
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-small);
  }
  .responsive-table tr + tr { margin-top: 0.75rem; }
  .responsive-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: 0.35rem 0;
    border: 0;
    text-align: right;
  }
  .responsive-table td::before { content: attr(data-label); color: var(--text-muted); font-size: 0.85rem; font-weight: 560; text-align: left; }
  .responsive-table td.actions { justify-content: flex-end; padding-top: 0.6rem; }
  .responsive-table td.actions::before { content: none; }
  .table .numeric { text-align: right; }
}
