:root {
  --bg: #f6f8fa;
  --surface: #ffffff;
  --surface-alt: #f0f3f6;
  --border: #d8dee6;
  --text: #16202b;
  --muted: #63707e;
  --accent: #0d6f8f;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --ok: #1c7a4a;
  --radius: 8px;
  --sidebar-w: 232px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1419;
    --surface: #161e26;
    --surface-alt: #1d2831;
    --border: #2c3945;
    --text: #e4ebf2;
    --muted: #8e9dab;
    --accent: #37a8cc;
    --accent-text: #06222c;
    --danger: #f2857d;
    --ok: #62c48d;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

/* A class-level `display` always beats the UA stylesheet's `[hidden] {
   display: none }` rule, so toggling `.hidden` in JS would otherwise do
   nothing visually on elements that also carry a `display` override. */
.app[hidden],
.login-screen[hidden] {
  display: none;
}

/* ---- sidebar ---------------------------------------------------------- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 18px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 18px;
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--accent), transparent 70%),
    var(--surface-alt);
  border: 2px solid var(--accent);
  flex: none;
}

.brand strong {
  display: block;
  font-size: 15px;
  letter-spacing: 0.01em;
}

.brand-sub {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.nav-item:hover {
  background: var(--surface-alt);
}

.nav-item[aria-current="true"] {
  background: var(--accent);
  color: var(--accent-text);
}

.nav-item .lock {
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.65;
}

.conn {
  margin-top: auto;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 12px;
}

.conn[data-state="ok"] {
  color: var(--ok);
}

.conn[data-state="error"] {
  color: var(--danger);
}

/* ---- main ------------------------------------------------------------- */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 18px 22px 12px;
  gap: 14px;
}

.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.toolbar h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.toolbar-title p {
  margin: 2px 0 0;
  font-size: 12px;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
}

.muted {
  color: var(--muted);
}

.search,
.field-input {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.search {
  width: 200px;
}

.search:focus,
.field-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.btn {
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.btn:hover:not(:disabled) {
  background: var(--surface-alt);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
  background: var(--accent);
}

.btn-sm {
  padding: 3px 9px;
  font-size: 12px;
}

.btn-danger {
  color: var(--danger);
}

.filters {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.filters label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

/* ---- table ------------------------------------------------------------ */

.table-wrap {
  flex: 1;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.table th,
.table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-alt);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.table tbody tr:hover {
  background: var(--surface-alt);
}

.table td.actions {
  text-align: right;
}

.table td.actions .btn + .btn {
  margin-left: 6px;
}

.mono {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12px;
}

/* A copyable cell (apiEndpoints.endpointId) keeps its value on one line and
   reveals the Copy button on hover/focus, so the column doesn't carry a row
   of buttons at rest. It stays visible whenever it has keyboard focus. */
.table td.cell-copy {
  white-space: nowrap;
}

.cell-copy-text {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12px;
}

.btn-copy {
  margin-left: 8px;
  padding: 1px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.table tbody tr:hover .btn-copy,
.btn-copy:focus-visible {
  opacity: 1;
}

.btn-copy:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Off-screen scratch textarea for the execCommand('copy') fallback. It must
   stay focusable and selectable, so display:none / visibility:hidden won't do. */
.visually-hidden-copy {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  font-size: 11px;
}

/* Stands in for Edit/Delete on a record another editor owns, so the row
   explains itself instead of just missing its buttons. */
.owned-note {
  color: var(--muted);
  font-size: 11px;
  font-style: italic;
  cursor: help;
}

.empty {
  padding: 40px;
  text-align: center;
  color: var(--muted);
}

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding-bottom: 4px;
}

/* ---- dialog ----------------------------------------------------------- */

.dialog {
  width: min(520px, 92vw);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}

.dialog::backdrop {
  background: rgb(0 0 0 / 0.45);
}

.dialog form {
  padding: 20px 22px 16px;
}

.dialog h2 {
  margin: 0 0 16px;
  font-size: 17px;
}

.form-fields {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--muted);
}

.field .req {
  color: var(--danger);
}

.field-input {
  width: 100%;
}

.field-input:disabled {
  background: var(--surface-alt);
  color: var(--muted);
}

.field-error {
  margin: 4px 0 0;
  color: var(--danger);
  font-size: 12px;
}

.field-hint {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.form-error {
  margin: 14px 0 0;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  font-size: 13px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 18px 0 0;
  padding: 0;
}

/* ---- toast ------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.22);
}

.toast[data-kind="error"] {
  background: var(--danger);
  color: #fff;
}

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

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .nav-list {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

/* ---- breadcrumbs ------------------------------------------------------ */

.crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 13px;
}

.crumbs li + li::before {
  content: "/";
  margin-right: 4px;
  color: var(--muted);
}

.crumbs li {
  display: flex;
  align-items: center;
}

.crumb {
  padding: 2px 4px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}

.crumb:hover {
  background: var(--surface-alt);
  text-decoration: underline;
}

.crumb-current {
  color: var(--muted);
  cursor: default;
  padding: 2px 4px;
}

.btn-open {
  color: var(--accent);
  border-color: var(--accent);
}

.form-context {
  margin: -8px 0 14px;
  padding: 6px 10px;
  border-radius: var(--radius);
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 12px;
}

.field[hidden] {
  display: none;
}

/* ---- multi-select and code fields ------------------------------------- */

.field-multi {
  min-height: 96px;
  padding: 4px;
}

.field-multi option {
  padding: 3px 6px;
  border-radius: 4px;
}

.field-code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  resize: vertical;
}

/* ---- archive tabs ------------------------------------------------------- */

.archive-tabs {
  display: flex;
  gap: 4px;
  padding-bottom: 2px;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  padding: 6px 12px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
}

.tab-btn[aria-current="true"] {
  color: var(--text);
  border-bottom-color: var(--accent);
}

/* ---- computed-value expression editor ---------------------------------- */

.expr-vars {
  margin-bottom: 6px;
}

.expr-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}

.expr-var-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-alt);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.expr-var-chip-name {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12px;
}

/* What the alias actually reads, e.g. "Chamber 1 Depth · msw". Capped so one
   long description can't stretch the row past the dialog. */
.expr-var-chip-detail {
  max-width: 22ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 10px;
}

.expr-var-chip:hover {
  border-color: var(--accent);
}

.expr-var-chip:hover .expr-var-chip-name {
  color: var(--accent);
}

/* The textarea sits transparently on top of a normal-flow highlight div that
   mirrors its text with ${alias} tokens colored — the classic highlighted-
   textarea overlay trick, kept in sync on every input/scroll event. */
.expr-editor {
  position: relative;
}

.expr-highlight,
.expr-input {
  box-sizing: border-box;
  margin: 0;
  width: 100%;
  min-height: 72px;
  padding: 7px 10px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  border-radius: var(--radius);
}

.expr-highlight {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
  pointer-events: none;
}

.expr-input {
  position: absolute;
  inset: 0;
  resize: none;
  border: 1px solid transparent;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  overflow: auto;
}

.expr-var {
  color: #7c3aed;
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  .expr-var {
    color: #c4b5fd;
  }
}

.expr-test {
  margin-top: 8px;
}

.expr-test-panel {
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-alt);
}

.expr-test-inputs {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.expr-test-input {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.expr-test-input input {
  width: 110px;
}

.expr-test-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
}

.expr-test-result {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
}

.expr-test-result[data-kind="ok"] {
  color: var(--ok);
}

.expr-test-result[data-kind="error"] {
  color: var(--danger);
}

.table td.actions .btn-open + .btn-open {
  margin-left: 6px;
}

.btn-clone {
  color: var(--accent);
}

/* ---- login -------------------------------------------------------------- */

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-card {
  width: min(340px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 28px 26px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.login-card h1 {
  margin: 0;
  font-size: 18px;
}

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.whoami {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  font-size: 12px;
  color: var(--muted);
}

.whoami span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
