:root {
  --bg-app: #faf9f8;
  --bg-titlebar: #ffffff;
  --bg-sidebar: #f3f2f1;
  --bg-panel: #ffffff;
  --bg-inset: #ffffff;
  --bg-hover: #edebe9;
  --bg-code: #f6f5f4;
  --border: #e1dfdd;
  --border-strong: #8a8886;
  --border-subtle: #edebe9;

  --text-primary: #201f1e;
  --text-secondary: #323130;
  --text-muted: #605e5c;
  --text-faint: #8a8886;
  --text-on-accent: #ffffff;

  --accent-blue: #0078d4;
  --accent-blue-hover: #106ebe;
  --accent-blue-light: #eff6fc;
  --accent-green: #107c10;
  --accent-green-light: #dff6dd;
  --accent-red: #d13438;
  --accent-red-light: #fde7e9;
  --accent-red-border: #f1707b;
  --accent-red-strong: #a4262c;
  --accent-yellow-text: #8a6d00;
  --accent-yellow-light: #fff4ce;
  --accent-purple: #5c2e91;
  --accent-purple-light: #f2edf9;
  --accent-orange: #ca5010;
  --accent-orange-light: #fdf0e4;

  --radius: 4px;
  --radius-lg: 6px;
  --shadow-card: 0 1.6px 3.6px rgba(0,0,0,0.08), 0 0.3px 0.9px rgba(0,0,0,0.06);
  --shadow-modal: 0 6.4px 14.4px rgba(0,0,0,0.13), 0 1.2px 3.6px rgba(0,0,0,0.1);
}

/* Dark theme: applied when the OS prefers dark AND the user hasn't
   explicitly chosen light, OR when the user explicitly chose dark —
   the explicit data-theme attribute always wins over the media query. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-app: #1f1f1f;
    --bg-titlebar: #232323;
    --bg-sidebar: #252423;
    --bg-panel: #2b2b2b;
    --bg-inset: #1f1f1f;
    --bg-hover: #3b3a39;
    --bg-code: #262625;
    --border: #3b3a39;
    --border-strong: #605e5c;
    --border-subtle: #323130;

    --text-primary: #f3f2f1;
    --text-secondary: #d2d0ce;
    --text-muted: #a19f9d;
    --text-faint: #797775;

    --accent-blue: #479ef5;
    --accent-blue-hover: #6cb1f7;
    --accent-blue-light: rgba(71,158,245,0.16);
    --accent-green: #92c353;
    --accent-green-light: rgba(146,195,83,0.16);
    --accent-red: #f1707b;
    --accent-red-light: rgba(241,112,123,0.14);
    --accent-red-border: #f1707b;
    --accent-red-strong: #ff9aa2;
    --accent-yellow-text: #e3c05b;
    --accent-yellow-light: rgba(227,192,91,0.16);
    --accent-purple: #b28ddb;
    --accent-purple-light: rgba(178,141,219,0.16);
    --accent-orange: #f7a06b;
    --accent-orange-light: rgba(247,160,107,0.16);

    --shadow-card: 0 1.6px 3.6px rgba(0,0,0,0.4), 0 0.3px 0.9px rgba(0,0,0,0.3);
    --shadow-modal: 0 6.4px 20px rgba(0,0,0,0.55), 0 1.2px 3.6px rgba(0,0,0,0.4);
  }
}
:root[data-theme="dark"] {
  --bg-app: #1f1f1f;
  --bg-titlebar: #232323;
  --bg-sidebar: #252423;
  --bg-panel: #2b2b2b;
  --bg-inset: #1f1f1f;
  --bg-hover: #3b3a39;
  --bg-code: #262625;
  --border: #3b3a39;
  --border-strong: #605e5c;
  --border-subtle: #323130;

  --text-primary: #f3f2f1;
  --text-secondary: #d2d0ce;
  --text-muted: #a19f9d;
  --text-faint: #797775;

  --accent-blue: #479ef5;
  --accent-blue-hover: #6cb1f7;
  --accent-blue-light: rgba(71,158,245,0.16);
  --accent-green: #92c353;
  --accent-green-light: rgba(146,195,83,0.16);
  --accent-red: #f1707b;
  --accent-red-light: rgba(241,112,123,0.14);
  --accent-red-border: #f1707b;
  --accent-red-strong: #ff9aa2;
  --accent-yellow-text: #e3c05b;
  --accent-yellow-light: rgba(227,192,91,0.16);
  --accent-purple: #b28ddb;
  --accent-purple-light: rgba(178,141,219,0.16);
  --accent-orange: #f7a06b;
  --accent-orange-light: rgba(247,160,107,0.16);

  --shadow-card: 0 1.6px 3.6px rgba(0,0,0,0.4), 0 0.3px 0.9px rgba(0,0,0,0.3);
  --shadow-modal: 0 6.4px 20px rgba(0,0,0,0.55), 0 1.2px 3.6px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Inter', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ── Top command bar ─────────────────────────────────────────────────────── */
.titlebar {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-titlebar);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 600; font-size: 14.5px; color: var(--text-primary); }
.brand-sub { color: var(--text-muted); font-size: 12.5px; }
.spacer { flex: 1; }
.whoami { font-size: 12.5px; color: var(--text-muted); font-family: ui-monospace, monospace; }

.session-timer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-app);
}
.session-timer-text {
  font-size: 11.5px;
  font-family: ui-monospace, monospace;
  color: var(--text-muted);
  white-space: nowrap;
}
.session-timer.warn .session-timer-text { color: var(--accent-yellow-text); font-weight: 600; }
.session-timer.critical .session-timer-text { color: var(--accent-red); font-weight: 700; }
.session-extend-btn {
  padding: 3px 10px;
  font-size: 11px;
  min-height: 0;
}

.hidden { display: none !important; }

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
}
.theme-toggle:hover { background: var(--bg-hover); }
.theme-toggle svg { width: 16px; height: 16px; }

/* ── App shell: sidebar + content ────────────────────────────────────────── */
.shell {
  display: flex;
  min-height: calc(100vh - 48px);
}

.sidebar {
  width: 224px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 12px 8px;
  overflow-y: auto;
}
.sidebar-group { margin-bottom: 16px; }
.sidebar-group-label {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 6px 12px 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  margin: 1px 0;
  font-size: 13px;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  border-left: 3px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  cursor: pointer;
}
.nav-item:hover { background: var(--bg-hover); }
.nav-item.active {
  background: var(--accent-blue-light);
  border-left-color: var(--accent-blue);
  color: var(--accent-blue);
  font-weight: 600;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 24px 28px 40px;
}
.main > .card + .card { margin-top: 16px; }

.center-card-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 48px);
  padding: 16px;
}
.center-card { width: 380px; max-width: 100%; }

.card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 22px 24px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Page header + toolbar (title/description + right-aligned actions) ──── */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.page-header h1 { margin: 0 0 4px; }
.page-header .hint { margin: 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; flex-shrink: 0; }

.section-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 0 0 10px;
}

.search-input-wrap { position: relative; max-width: 280px; }
.search-input-wrap input[type=text] { margin-bottom: 0; padding-left: 30px; }
.search-input-wrap svg {
  position: absolute;
  left: 9px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  color: var(--text-faint);
  pointer-events: none;
}

/* ── Stat tiles (dashboard-style summary numbers) ────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat-tile {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 16px 18px;
}
.stat-tile .stat-value { font-size: 24px; font-weight: 700; color: var(--text-primary); line-height: 1.2; }
.stat-tile .stat-label { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }

/* ── Key/value list (Account / Setup Guide style detail rows) ───────────── */
.kv-list { display: flex; flex-direction: column; }
.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 13px;
}
.kv-row:last-child { border-bottom: none; }
.kv-row .kv-key { color: var(--text-muted); }
.kv-row .kv-val { color: var(--text-primary); text-align: right; font-family: ui-monospace, monospace; font-size: 12.5px; }

h1 { font-size: 19px; margin: 0 0 6px; font-weight: 600; }
h2 { font-size: 14.5px; margin: 0 0 4px; font-weight: 600; }
h3 { font-size: 13px; margin: 0 0 4px; font-weight: 600; color: var(--text-secondary); }
p.hint { color: var(--text-muted); font-size: 12.5px; margin: 0 0 18px; line-height: 1.5; }

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.link-toggle {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-blue);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 14px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.link-toggle:hover { color: var(--accent-blue-hover); }

input[type=text], input[type=email], input[type=password], select {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-inset);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13.5px;
  margin-bottom: 14px;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 1px var(--accent-blue);
}

textarea {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-inset);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  margin-bottom: 14px;
  resize: vertical;
}
textarea:focus { outline: none; border-color: var(--accent-blue); box-shadow: 0 0 0 1px var(--accent-blue); }
textarea.mono-input { font-family: ui-monospace, monospace; font-size: 12.5px; }

input[type=file] {
  display: block;
  width: 100%;
  padding: 8px 0;
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 14px;
}
input[type=file]::file-selector-button {
  padding: 7px 14px;
  margin-right: 12px;
  background: var(--bg-app);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 12.5px;
  cursor: pointer;
}
input[type=file]::file-selector-button:hover { background: var(--bg-hover); }

button {
  font-family: inherit;
  cursor: pointer;
}

.btn-primary {
  padding: 8px 16px;
  background: var(--accent-blue);
  border: 1px solid var(--accent-blue);
  border-radius: var(--radius);
  color: var(--text-on-accent);
  font-size: 13px;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-blue-hover); border-color: var(--accent-blue-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-ghost {
  padding: 7px 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 12.5px;
}
.btn-ghost:hover { background: var(--bg-hover); }

.btn-danger {
  padding: 6px 12px;
  background: var(--bg-panel);
  border: 1px solid var(--accent-red);
  border-radius: var(--radius);
  color: var(--accent-red);
  font-size: 12px;
  font-weight: 600;
}
.btn-danger:hover { background: var(--accent-red-light); }

.btn-success {
  padding: 6px 12px;
  background: var(--bg-panel);
  border: 1px solid var(--accent-green);
  border-radius: var(--radius);
  color: var(--accent-green);
  font-size: 12px;
  font-weight: 600;
}
.btn-success:hover { background: var(--accent-green-light); }

.error-banner {
  padding: 9px 13px;
  background: var(--accent-red-light);
  border: 1px solid var(--accent-red-border);
  border-left: 3px solid var(--accent-red);
  border-radius: var(--radius);
  color: var(--accent-red-strong);
  font-size: 12.5px;
  margin-bottom: 14px;
}

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left;
  font-size: 10.5px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
td {
  padding: 10px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-secondary);
}
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg-app); }

.badge {
  display: inline-block;
  font-size: 10.5px;
  padding: 2px 9px;
  border-radius: 20px;
  font-weight: 600;
}
.badge-active { background: var(--accent-green-light); color: var(--accent-green); }
.badge-pending { background: var(--accent-yellow-light); color: var(--accent-yellow-text); }
.badge-admin { background: var(--accent-blue-light); color: var(--accent-blue); }
.badge-neutral { background: var(--bg-hover); color: var(--text-muted); }
.badge-danger { background: var(--accent-red-light); color: var(--accent-red); }

.truncate {
  display: inline-block;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

/* ── HTTP method badges (API docs page) ──────────────────────────────────── */
.method-badge {
  display: inline-block;
  min-width: 52px;
  text-align: center;
  font-size: 10.5px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: var(--radius);
  font-family: ui-monospace, monospace;
}
.method-GET { background: var(--accent-blue-light); color: var(--accent-blue); }
.method-POST { background: var(--accent-green-light); color: var(--accent-green); }
.method-DELETE { background: var(--accent-red-light); color: var(--accent-red); }
.method-PUT, .method-PATCH { background: var(--accent-orange-light); color: var(--accent-orange); }

.empty-state {
  padding: 30px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 16px;
}
.modal {
  width: 360px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 64px);
  overflow-y: auto;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  padding: 22px;
}
.checklist { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.checklist label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 400;
}
.checklist input { width: auto; margin: 0; }

.mono { font-family: ui-monospace, monospace; font-size: 12px; color: var(--text-faint); }

pre.mono {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin: 8px 0 14px;
  overflow-x: auto;
}

.code-block {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 8px 0 14px;
  overflow-x: auto;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre;
}

.qr-wrap {
  display: inline-block;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px;
  margin: 4px 0 14px;
}
.qr-wrap svg { display: block; width: 176px; height: 176px; max-width: 100%; height: auto; aspect-ratio: 1; }

/* ── Tool grid: multiple focused-task cards side by side on wide screens ─── */
.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 16px;
  align-items: start;
}
.tool-grid .card { margin-top: 0 !important; }

/* ── Stepper (Setup Guide-style horizontal step nav) ─────────────────────── */
.stepper {
  display: flex;
  gap: 4px;
  margin-bottom: 18px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.step {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 10px;
  font-size: 12.5px;
  color: var(--text-muted);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}
.step:hover { background: var(--bg-hover); }
.step .step-num {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--bg-hover);
  color: var(--text-faint);
  font-size: 10.5px;
  font-weight: 700;
  flex-shrink: 0;
}
.step.active {
  color: var(--accent-blue);
  border-color: var(--accent-blue);
  background: var(--accent-blue-light);
  font-weight: 600;
}
.step.active .step-num { background: var(--accent-blue); color: var(--text-on-accent); }

/* ── API docs page: collapsible endpoint entries ─────────────────────────── */
.endpoint {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 10px;
  overflow: hidden;
}
.endpoint summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  cursor: pointer;
  list-style: none;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-panel);
}
.endpoint summary::-webkit-details-marker { display: none; }
.endpoint summary:hover { background: var(--bg-hover); }
.endpoint summary .ep-path { font-family: ui-monospace, monospace; font-size: 12.5px; }
.endpoint summary .ep-auth { margin-left: auto; font-size: 10.5px; color: var(--text-faint); }
.endpoint-body { padding: 4px 16px 16px; border-top: 1px solid var(--border-subtle); }
.endpoint-body p { font-size: 12.5px; color: var(--text-secondary); line-height: 1.6; margin: 10px 0; }
.endpoint-body h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-faint); margin: 14px 0 6px; }

/* ── Mobile / narrow viewport: sidebar collapses to a horizontal scroller ── */
@media (max-width: 800px) {
  .shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 6px 8px;
    gap: 2px;
  }
  .sidebar-group { display: flex; margin-bottom: 0; gap: 2px; }
  .sidebar-group-label { display: none; }
  .nav-item {
    white-space: nowrap;
    flex-shrink: 0;
    border-left: none;
    border-bottom: 2px solid transparent;
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .nav-item.active { border-left-color: transparent; border-bottom-color: var(--accent-blue); }

  .main { padding: 20px; }
}

@media (max-width: 640px) {
  .titlebar {
    height: auto;
    flex-wrap: wrap;
    padding: 10px 14px;
    row-gap: 6px;
  }
  .brand-sub { display: none; }
  .whoami {
    order: 3;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .main { padding: 14px 12px; }

  .card { padding: 16px; }

  h1 { font-size: 17px; }
  h2 { font-size: 14px; }

  .center-card { width: 100%; max-width: 340px; }

  .page-header { flex-direction: column; align-items: stretch; }
  .toolbar { width: 100%; }
  .toolbar > * { flex: 1; }
  .search-input-wrap { max-width: none; }

  table { font-size: 12px; }
  th, td { padding: 8px 6px; }

  .modal { width: calc(100vw - 32px); padding: 18px; }

  .stat-grid { grid-template-columns: repeat(2, 1fr); }

  button, .btn-primary, .btn-ghost, .btn-danger, .btn-success {
    min-height: 40px;
  }
  input[type=text], input[type=email], input[type=password], select, textarea {
    min-height: 40px;
    font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
  }
}

@media (max-width: 380px) {
  .brand { font-size: 13px; }
  .main { padding: 10px 8px; }
  .card { padding: 12px; border-radius: var(--radius); }
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-tile { padding: 12px; }
}

/* Touch devices: comfortable tap targets regardless of viewport width
   (a docked tablet or a large-screen touch laptop still benefits). */
@media (hover: none) and (pointer: coarse) {
  .nav-item { padding: 10px 12px; }
  button, .btn-primary, .btn-ghost, .btn-danger, .btn-success, .theme-toggle {
    min-height: 40px;
  }
}

/* Button rows (e.g. table action cells, modal footers) wrap instead of
   overflowing horizontally on narrow screens — these are built with inline
   flex styles in admin.js, so this targets them structurally by position
   rather than needing per-instance edits. */
@media (max-width: 640px) {
  td[style*="display:flex"],
  .card div[style*="display:flex"],
  .modal div[style*="display:flex"] {
    flex-wrap: wrap;
  }
}
