:root {
  color-scheme: dark;

  --bg: #1e1e1e;
  --surface: #252525;
  --surface-2: #2d2d2d;
  --surface-3: #373737;
  --border: #343434;
  --border-strong: #4a4a4a;
  --text: #efefef;
  --text-dim: #b3b3b3;
  --text-faint: #8f8f8f;
  /* Neutral defaults. A studio's own colours come from /studio.css, which the
     server writes from branding.colors in studio.json and which loads after
     this file. Accent: buttons, links and focus. Title: page titles. */
  --accent: #5b8def;
  --accent-hover: #7ca4f2;
  --accent-soft: rgba(91, 141, 239, 0.16);
  --accent-ink: #1e1e1e;
  --title: #ff9f6b;
  --warn: #e8b35a;
  --warn-soft: rgba(232, 179, 90, 0.14);
  --danger: #ff7262;
  --danger-soft: rgba(255, 114, 98, 0.14);

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  --text-xs: 11px;
  --text-sm: 12.5px;
  --text-md: 14px;
  --text-lg: 17px;
  --text-xl: 21px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --radius-sm: 4px;
  --radius: 7px;
  --radius-lg: 11px;
  --hit: 36px;
  --rail-w: 76px;
  --page-max: 1320px;
  --transition: 120ms ease;
}

@media (pointer: coarse) {
  :root { --hit: 44px; }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow-x: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-md);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4, p { margin: 0; }
img, svg { max-width: 100%; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, kbd { font-family: var(--font-mono); font-size: 0.93em; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* App shell */

.boot, .login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--space-4);
}

.shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr);
}

.skip-link {
  position: fixed;
  top: var(--space-2);
  left: var(--space-2);
  z-index: 350;
  min-height: var(--hit);
  padding: 0 var(--space-3);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transform: translateY(-200%);
}

.skip-link:focus { transform: none; }

.nav {
  position: relative;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-3);
  min-height: 0;
  padding: var(--space-3) var(--space-2);
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.nav-logo {
  display: block;
  width: 36px;
  height: 36px;
  margin: 0 auto;
}

.nav-items {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  overflow-y: auto;
  scrollbar-width: none;
}

.nav-item {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 52px;
  padding: var(--space-1);
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--text-dim);
  font: inherit;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.nav-item:hover {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}

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

.nav-icon {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.nav-slot {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-1);
}

.nav-slot:empty { display: none; }

.nav-user { flex: none; }
.nav-user[aria-current="page"] { color: var(--accent); }

.nav-avatar {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface-3);
  color: var(--text);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.nav-user[aria-current="page"] .nav-avatar { background: var(--accent-soft); color: var(--accent); }

/* A profile photo inside any round face: the nav's, a card's, a comment's. */
.has-photo { overflow: hidden; }
.face-photo {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  object-fit: cover;
}

.user-menu {
  position: absolute;
  left: calc(100% + var(--space-2));
  bottom: var(--space-2);
  width: 230px;
  padding: var(--space-1);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}

.user-menu-who {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--space-1);
}

.user-menu-name { font-weight: 600; }

.user-menu-item {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: var(--hit);
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.user-menu-item:hover { background: var(--surface-2); text-decoration: none; }

.nav-label {
  max-width: 100%;
  overflow: hidden;
  font-size: var(--text-xs);
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.view {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  outline: none;
}

.module-root { min-height: 100%; }

.view-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}

@media (max-width: 767.98px) {
  .shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .view { grid-row: 1; }

  .nav {
    grid-row: 2;
    flex-direction: row;
    gap: 0;
    padding: var(--space-1) var(--space-1) calc(var(--space-1) + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    border-right: 0;
  }

  .nav-logo { display: none; }

  .nav-items {
    flex-direction: row;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .nav-items .nav-item { flex: 1 0 64px; }
  .nav-slot { flex-direction: row; }
  .nav-user { flex: 0 0 60px; }

  .user-menu {
    left: auto;
    right: var(--space-2);
    bottom: calc(100% + var(--space-2));
  }
}

/* Too narrow for every item at its usual width, so the pages share what is left. */
@media (max-width: 411.98px) {
  .nav-items { gap: 0; }

  .nav-items .nav-item {
    flex-shrink: 1;
    min-width: 0;
    padding-inline: 0;
  }

  .nav-label { font-size: 10px; }
  .nav-user { flex-basis: 44px; }
  .nav-user .nav-label { display: none; }
}

/* Login */

.login-card {
  width: min(340px, 100%);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: var(--space-5);
}

.login-logo { align-self: center; }

/* First-run wizard */
.login-card.wizard { width: min(560px, 100%); }
.wizard-form { display: flex; flex-direction: column; gap: var(--space-4); min-width: 0; }
.wizard-step { margin: 0; text-align: center; }
.wizard-row { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: flex-end; }
.wizard-row > .field { flex: 1 1 140px; }
.wizard-list { display: flex; flex-direction: column; gap: var(--space-3); }
.wizard-item {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.wizard-item > .btn { align-self: flex-start; }
.wizard-key { text-transform: uppercase; }
.wizard-check { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; }
.wizard-colour { width: 100%; min-height: 36px; padding: 2px; cursor: pointer; }
.wizard-preview {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
}
.wizard-preview-title { color: var(--title); font-size: var(--text-lg); font-weight: 600; flex: 1 1 100%; }
.wizard-preview-link { color: var(--accent); }
.wizard-module { margin: 0; padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius-sm); min-width: 0; }
.wizard-module-title { padding: 0 var(--space-1); font-weight: 600; }
.wizard-module > p { margin: var(--space-1) 0 var(--space-2); }
.wizard-settings { display: flex; flex-direction: column; gap: var(--space-2); }
.wizard-never > .input { flex: 1 1 120px; }
.wizard-review { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-1) var(--space-3); margin: 0; }
.wizard-review dt { color: var(--text-dim); }
.wizard-review dd { margin: 0; overflow-wrap: anywhere; }
.wizard-notes { margin: 0; padding-left: var(--space-4); color: var(--warn); }
.login-title { color: var(--title); font-size: var(--text-xl); font-weight: 600; text-align: center; }
.login-code { font-family: var(--font-mono); letter-spacing: 0.04em; }
#auth-message { padding: var(--space-3); }
#auth-message .state { padding: var(--space-4) var(--space-2); }
.state-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); }

/* Over everything: a session ended, and the page waits underneath */

.session-ended {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: grid;
  place-items: center;
  padding: var(--space-4);
  overflow-y: auto;
  background: rgba(30, 30, 30, 0.82);
}

/* Page layout for modules */

.page {
  width: 100%;
  max-width: var(--page-max);
  margin: 0 auto;
  padding: var(--space-5);
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.page-title { color: var(--title); font-size: var(--text-xl); font-weight: 600; }

@media (max-width: 767.98px) {
  .page { padding: var(--space-4); }
}

.stack { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); min-width: 0; }
.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  gap: var(--space-3);
}

/* Cards */

.card {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
}

.card-title { font-size: var(--text-md); font-weight: 600; }
.card-body { padding: var(--space-4); }

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--hit);
  padding: 0 var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.btn:hover:not(:disabled) { background: var(--surface-3); text-decoration: none; }
.btn:disabled { opacity: 0.5; cursor: default; }

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

.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: none; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.btn-danger { border-color: var(--danger); background: none; color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); }
.btn-icon { width: var(--hit); padding: 0; }
.btn-sm { min-height: 28px; padding: 0 var(--space-2); font-size: var(--text-sm); }
.btn-block { width: 100%; }

@media (pointer: coarse) {
  .btn-sm { min-height: 36px; }
}

/* Forms */

.field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.label { color: var(--text-dim); font-size: var(--text-sm); }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--hit);
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.input:disabled { color: var(--text-faint); cursor: not-allowed; }
.textarea { padding: var(--space-2) var(--space-3); resize: vertical; }
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }

@media (pointer: coarse) {
  /* iOS zooms the page when a focused field is under 16px. */
  .input, .select, .textarea { font-size: 16px; }
}

/* Tabs */

.tabs {
  display: flex;
  gap: var(--space-1);
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
}

.tab {
  flex: none;
  min-height: var(--hit);
  padding: 0 var(--space-3);
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-dim);
  font: inherit;
  line-height: var(--hit);
  cursor: pointer;
}

.tab:hover { color: var(--text); text-decoration: none; }
.tab[aria-selected="true"], .tab[aria-current="page"] { border-bottom-color: var(--accent); color: var(--text); }

/* Tables and numbers */

.table-wrap { max-width: 100%; overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table th, .table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.table th { color: var(--text-faint); font-weight: 500; white-space: nowrap; }
/* A row header reads like the rest of its row; it is only there for screen readers. */
.table tbody th { color: inherit; font-weight: inherit; white-space: normal; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num, .num { font-variant-numeric: tabular-nums; }
.table .num { text-align: right; white-space: nowrap; }
.tabular { font-variant-numeric: tabular-nums; }

/* Small pieces */

.badge {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--space-2);
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text-dim);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

.notice {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-sm);
}

.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice-danger { background: var(--danger-soft); color: var(--danger); }
.notice-ok { background: var(--accent-soft); color: var(--accent); }

.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.small { font-size: var(--text-sm); }
.warn { color: var(--warn); }
.danger { color: var(--danger); }
.accent { color: var(--accent); }

.truncate {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Loading, empty and error states */

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

.spinner-lg { width: 28px; height: 28px; border-width: 3px; }

@keyframes spin {
  to { transform: rotate(360deg); }
}

.skeleton {
  min-height: 1em;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  animation: pulse 1400ms ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.55; }
}

.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-6) var(--space-4);
  color: var(--text-dim);
  text-align: center;
}

.state-title { color: var(--text); font-size: var(--text-lg); font-weight: 600; }
.state-text { max-width: 46ch; overflow-wrap: anywhere; }
.state .btn { margin-top: var(--space-2); }
.state-error .state-title { color: var(--warn); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
  .spinner { animation-duration: 2400ms; }
  .skeleton { animation: none; }
}
