/* MoonPaper design system.
   - Spacing: 4px base, used in steps of 4 and 8 only (--s1 … --s9).
   - Type: Fraunces (soft, rounded serifs) for headings, Nunito (rounded)
     for everything else, JetBrains Mono for code. Sizes come
     from the ramp below and scale with the reader's text-size setting.
   - Shape: controls are pills, containers are soft rectangles. Three radii.
   - Colour: ink on warm cream. Orange marks the current choice only.
     Primary actions are ink, not colour. */
:root {
  --font: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Consolas, monospace;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px;
  --s6: 32px; --s7: 48px; --s8: 64px; --s9: 96px;
  --page: 1080px;
  --measure: 720px;

  /* Type ramp in rem, so the text-size setting (html font-size) scales it. */
  --t-xs: .75rem;     /* 12 */
  --t-sm: .8125rem;   /* 13 */
  --t-ui: .875rem;    /* 14 */
  --t-base: .9375rem; /* 15 */
  --t-md: 1rem;       /* 16 */
  --t-lg: 1.125rem;   /* 18 */
  --t-xl: 1.375rem;   /* 22 */
  --t-2xl: 2rem;      /* 32 */
  --lh-tight: 1.2;
  --lh-ui: 1.5;
  --lh-read: 1.7;

  --r-sm: 8px;    /* code, thumbnails */
  --r-md: 14px;   /* text areas, rows, notes, toasts */
  --r-lg: 20px;   /* cards, dialogs */
  --r-pill: 999px;/* buttons, inputs, chips, segmented controls */

  --ease: cubic-bezier(.2, 0, 0, 1);
  --dur: 160ms;

  --bg: #f3efe8;
  --surface: #fbf9f5;
  --raised: #ffffff;
  --ink: #1c1a17;
  --muted: #6b645b;
  /* Roleplay actions and your own messages: softer than --ink, but still at
     APCA Lc 75+ so long passages stay easy to read (--muted is ~Lc 70). */
  --narration: #5b554d;
  --line: #e6e0d6;
  --line-strong: #8c8478;
  --fill: #ece6dc;
  --fill-strong: #e1d9cc;

  /* Primary action: ink. */
  --primary: #1c1a17;
  --primary-hover: #3a3631;
  --on-primary: #fbf9f5;

  /* Current choice, links, focus. */
  --accent: #ad4318;
  --accent-hover: #8f3712;
  --on-accent: #ffffff;
  --accent-soft: #f6e3d8;

  --danger: #a3301f;
  --ok: #3d6b34;

  --shadow-1: 0 1px 2px rgba(28, 26, 23, .05);
  --shadow-2: 0 12px 32px rgba(28, 26, 23, .14);
  --scrim: rgba(28, 26, 23, .45);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #161412;
  --surface: #1f1c19;
  --raised: #262320;
  --ink: #f2ede5;
  --muted: #a9a197;
  --narration: #d5cfc6;
  --line: #2f2b27;
  --line-strong: #7a7268;
  --fill: #2e2a26;
  --fill-strong: #3a3530;

  --primary: #f2ede5;
  --primary-hover: #ffffff;
  --on-primary: #1c1a17;

  --accent: #ef8a5a;
  --accent-hover: #f5a17a;
  --on-accent: #1a1210;
  --accent-soft: #3a261c;

  --danger: #ef9b87;
  --ok: #9ec293;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-2: 0 12px 32px rgba(0, 0, 0, .5);
  --scrim: rgba(0, 0, 0, .55);
  color-scheme: dark;
}

/* Moon Cell colours: violet ink and a rose accent, after BB (Moon Cancer).
   Same roles as the default palette, so every component follows. */
:root[data-palette="moon"] {
  --bg: #f1edf5;
  --surface: #faf8fc;
  --raised: #ffffff;
  --ink: #1e1828;
  --muted: #655c72;
  --narration: #5a5166;
  --line: #e4ddec;
  --line-strong: #877d94;
  --fill: #ebe5f2;
  --fill-strong: #dfd6ea;
  --primary: #2b1e3f;
  --primary-hover: #3e2d58;
  --on-primary: #faf8fc;
  --accent: #a8234b;
  --accent-hover: #8a1a3c;
  --on-accent: #ffffff;
  --accent-soft: #f5dde5;
  --shadow-1: 0 1px 2px rgba(30, 24, 40, .06);
  --shadow-2: 0 12px 32px rgba(30, 24, 40, .16);
  --scrim: rgba(30, 24, 40, .45);
}
:root[data-palette="moon"][data-theme="dark"] {
  --bg: #131019;
  --surface: #1b1723;
  --raised: #221d2c;
  --ink: #efeaf6;
  --muted: #a89fb6;
  --narration: #d3ccdc;
  --line: #2c2638;
  --line-strong: #776d88;
  --fill: #29233a;
  --fill-strong: #352d48;
  --primary: #efeaf6;
  --primary-hover: #ffffff;
  --on-primary: #1e1828;
  --accent: #f07ea2;
  --accent-hover: #f59ab8;
  --on-accent: #1a0f14;
  --accent-soft: #3b1f2c;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, .35);
  --shadow-2: 0 12px 32px rgba(0, 0, 0, .55);
  --scrim: rgba(8, 5, 12, .6);
}


/* Text size (Settings > Appearance). Applied before paint by theme-init.js. */
html { font-size: 100%; }
:root[data-text="sm"] { font-size: 93.75%; }
:root[data-text="lg"] { font-size: 112.5%; }
:root[data-text="xl"] { font-size: 125%; }
:root { --chat-scale: 1; }
/* How far in from the message box's edge its text starts. The line under the
   box, and on phones the + button's icon, start at the same place. */
:root { --composer-inset: 17.5px; }
@media (max-width: 600px) { :root { --composer-inset: 20.5px; } }
:root[data-chat-text="sm"] { --chat-scale: .9; }
:root[data-chat-text="lg"] { --chat-scale: 1.15; }
:root[data-chat-text="xl"] { --chat-scale: 1.3; }

* { box-sizing: border-box; margin: 0; padding: 0; }
html { background: var(--bg); }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font);
  font-size: var(--t-base); line-height: var(--lh-ui); -webkit-font-smoothing: antialiased;
  min-height: 100svh; display: flex; flex-direction: column;
}
img { display: block; max-width: 100%; }
a { color: var(--accent); text-underline-offset: 3px; }
@media (hover: hover) { a:hover { color: var(--accent-hover); } }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
/* Selected text in the theme's own colour, not the system blue. */
::selection { background: color-mix(in srgb, var(--accent) 32%, transparent); color: var(--ink); }

h1, h2, h3 { font-family: var(--font-display); font-weight: 500; line-height: var(--lh-tight); letter-spacing: -.01em; }
h1 { font-size: var(--t-2xl); }
h2 { font-size: var(--t-xl); }
h3 { font-size: var(--t-md); }
small { font-size: var(--t-sm); }

.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;
}
.skip-link {
  position: absolute; left: var(--s4); top: -100px; z-index: 100;
  background: var(--primary); color: var(--on-primary); padding: var(--s2) var(--s4);
  border-radius: var(--r-pill); text-decoration: none;
}
.skip-link:focus { top: var(--s2); }
.wrap { width: 100%; max-width: var(--page); margin: 0 auto; padding: 0 var(--s5); }
@media (max-width: 600px) { .wrap { padding: 0 var(--s4); } }

/* ---------- Header ---------- */
.site-header {
  position: sticky; top: 0; z-index: 40;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.2) blur(12px); -webkit-backdrop-filter: saturate(1.2) blur(12px);
  border-bottom: 1px solid var(--line);
}
.header-inner { display: flex; align-items: center; gap: var(--s4); min-height: 64px; }
.brand {
  font-family: var(--font-display); font-size: var(--t-lg); font-weight: 600;
  color: var(--ink); text-decoration: none; white-space: nowrap;
}
@media (hover: hover) { .brand:hover { color: var(--ink); } }
.brand { display: inline-flex; align-items: center; gap: var(--s2); }
.brand-mark { width: 24px; height: 24px; flex: none; }
.brand-mark .moon { fill: var(--ink); }
.brand-mark .star { fill: var(--accent); }

/* Back and Home: one pill, always top left, only away from the home page. */
.nav-pair {
  display: inline-flex; align-items: center; flex: none; padding: 2px;
  background: var(--fill); border-radius: var(--r-pill);
}
.nav-pair .icon-btn { width: 36px; height: 36px; color: var(--ink); }
@media (hover: hover) { .nav-pair .icon-btn:hover { background: var(--raised); } }
.nav-pair-sep { width: 1px; height: 16px; margin: 0 var(--s1); background: var(--line-strong); opacity: .5; transition: opacity var(--dur) var(--ease); }
/* A highlighted button is its own divider; the line would cut into it. */
.nav-pair:has(.icon-btn:hover:not(:disabled), .icon-btn[aria-current="page"], .icon-btn:focus-visible) .nav-pair-sep { opacity: 0; }

.main-nav { flex: 1; min-width: 0; }
.main-nav ul { list-style: none; display: flex; gap: var(--s1); overflow-x: auto; scrollbar-width: none; }
.main-nav ul::-webkit-scrollbar { display: none; }
.main-nav a {
  display: inline-flex; align-items: center; height: 36px; padding: 0 var(--s4);
  font-size: var(--t-ui); color: var(--muted); text-decoration: none; white-space: nowrap;
  border-radius: var(--r-pill); transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
@media (hover: hover) { .main-nav a:hover { color: var(--ink); background: var(--fill); } }
.main-nav a[aria-current="page"] { color: var(--on-primary); background: var(--primary); font-weight: 500; }

.header-status {
  display: inline-flex; align-items: center; gap: var(--s2); max-width: 240px; height: 36px;
  font-size: var(--t-sm); color: var(--muted); text-decoration: none; padding: 0 var(--s3); border-radius: var(--r-pill);
}
@media (hover: hover) { .header-status:hover { background: var(--fill); color: var(--ink); } }
.header-status .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* On wide screens the status gives up space before the menu does. */
@media (min-width: 1000px) {
  /* The menu keeps its size; the status takes the free space and shrinks first. */
  .header-inner { gap: var(--s3); }
  .main-nav { flex: 0 0 auto; }
  .header-status { flex: 0 1 auto; min-width: 0; margin-left: auto; }
}
/* Mid-width: the dot alone; the words stay for screen readers and the tooltip. */
@media (min-width: 1000px) and (max-width: 1199px) {
  .header-status { padding: 0 var(--s3); }
  .header-status .label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); flex: none; }
.status-dot.on { background: var(--ok); }
.status-dot.off { background: var(--danger); }

.icon-toggle {
  flex: none; width: 36px; height: 36px; border-radius: var(--r-pill); border: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; color: var(--muted); cursor: pointer;
}
@media (hover: hover) { .icon-toggle:hover { background: var(--fill); color: var(--ink); } }
.icon-toggle svg { width: 18px; height: 18px; }
.theme-toggle .icon-sun, :root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }

@media (max-width: 999px) { .header-status { display: none; } }
@media (max-width: 700px) {
  .header-inner { flex-wrap: wrap; gap: var(--s2) var(--s3); padding-top: var(--s2); min-height: 0; }
  .brand { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
  .main-nav { order: 3; flex-basis: 100%; margin: 0 calc(-1 * var(--s4)); padding: 0 var(--s4) var(--s2); }
}

/* ---------- Page scaffolding ---------- */
main { flex: 1; }
main:focus { outline: none; }
/* Vertical only: the horizontal gutter belongs to .wrap, which this shares an element with. */
.page { padding-block: var(--s7) var(--s9); }
.lead { color: var(--muted); max-width: 68ch; }
.page > h1 + .lead { margin: var(--s2) 0 var(--s6); }
.row-between { display: flex; flex-wrap: wrap; gap: var(--s4); align-items: flex-start; justify-content: space-between; }
.section { margin-top: var(--s7); }
.section > h2 { margin-bottom: var(--s4); }
.stack { display: grid; gap: var(--s4); }
.stack-lg { display: grid; gap: var(--s5); }

.page-head { display: flex; flex-wrap: wrap; gap: var(--s4); align-items: flex-end; justify-content: space-between; margin-bottom: var(--s6); }
.page-head .lead { margin-top: var(--s2); }
.page-head-tight { margin-bottom: var(--s4); }
.page-note { margin-bottom: var(--s5); }
h2.sub {
  font-family: var(--font); font-size: var(--t-xs); font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); margin: var(--s7) 0 var(--s4);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  height: 40px; padding: 0 var(--s5); border-radius: var(--r-pill);
  border: 1.5px solid var(--ink); background: transparent; color: var(--ink);
  font-size: var(--t-ui); font-weight: 500; text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
@media (hover: hover) { .btn:hover { background: var(--fill); color: var(--ink); } }
.btn svg { width: 18px; height: 18px; flex: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
@media (hover: hover) { .btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); color: var(--on-primary); } }
.btn-ghost { background: var(--fill); border-color: var(--fill); }
@media (hover: hover) { .btn-ghost:hover { background: var(--fill-strong); border-color: var(--fill-strong); } }
.btn-quiet { border-color: transparent; background: none; color: var(--muted); }
@media (hover: hover) { .btn-quiet:hover { background: var(--fill); color: var(--ink); } }
.btn-danger { color: var(--danger); border-color: var(--danger); }
@media (hover: hover) { .btn-danger:hover { background: var(--danger); border-color: var(--danger); color: var(--surface); } }
.btn-quiet.btn-danger { border-color: transparent; }
@media (hover: hover) { .btn-quiet.btn-danger:hover { background: var(--fill); color: var(--danger); } }
.btn-sm { height: 32px; padding: 0 var(--s4); font-size: var(--t-sm); }
.btn:disabled, .btn[aria-disabled="true"] { cursor: not-allowed; color: var(--muted); background: var(--fill); border-color: var(--fill); }
/* Busy: keeps its width, swaps the label for a spinner. */
.btn.is-loading { position: relative; color: transparent; pointer-events: none; }
.btn.is-loading::after {
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--on-primary); border-right-color: transparent; animation: spin .7s linear infinite;
}
.btn:not(.btn-primary).is-loading::after { border-color: var(--ink); border-right-color: transparent; }
@keyframes spin { to { transform: rotate(360deg); } }
.actions { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.actions-end { justify-content: flex-end; }
.actions-col { flex-direction: column; align-items: stretch; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 36px; height: 36px; border: 0; border-radius: var(--r-pill);
  background: none; color: var(--muted); cursor: pointer; text-decoration: none;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
@media (hover: hover) { .icon-btn:hover { background: var(--fill); color: var(--ink); } }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:disabled { color: var(--line-strong); cursor: not-allowed; background: none; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: var(--t-ui); text-decoration: none; }
@media (hover: hover) { .link-btn:hover { color: var(--accent-hover); text-decoration: underline; } }

/* Segmented control: radios that look like one pill. */
.segmented {
  align-self: flex-start; max-width: 100%;
  display: inline-flex; flex-wrap: wrap; gap: 2px; padding: 3px;
  border: 1.5px solid var(--ink); border-radius: var(--r-pill);
}
.segmented label { position: relative; display: inline-flex; cursor: pointer; }
.segmented input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; margin: 0; cursor: pointer; }
/* Outside, a segmented control is exactly as tall as a text field:
   31 + 3 + 3 padding + 1.5 + 1.5 border = 40 (35 on touch screens = 44). */
.segmented span {
  display: inline-flex; align-items: center; height: 31px; padding: 0 var(--s4);
  border-radius: var(--r-pill); font-size: var(--t-ui); font-weight: 500; color: var(--ink);
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
@media (hover: hover) { .segmented label:hover span { background: var(--fill); } }
.segmented input:checked + span { background: var(--primary); color: var(--on-primary); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
/* On a phone the choices share the full width in one row instead of wrapping. */
@media (max-width: 480px) {
  .segmented { display: flex; flex-wrap: nowrap; width: 100%; }
  .segmented label { flex: 1 1 0; min-width: 0; }
  .segmented span { width: 100%; justify-content: center; padding: 0 var(--s2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}

/* ---------- Surfaces ---------- */
.card { background: var(--surface); border-radius: var(--r-lg); padding: var(--s5); box-shadow: var(--shadow-1); }
/* Stacked cards sit 24px apart everywhere; in a grid the gap already does it. */
.card + .card { margin-top: var(--s5); }
.form-grid > .card + .card { margin-top: 0; }
.card > .page-head:last-child { margin-bottom: 0; }
.card-title { font-size: var(--t-lg); margin-bottom: var(--s1); }
.card > .card-title + .lead, .card > h2 + .lead { margin-bottom: var(--s5); }
.card-tight { padding: var(--s4); }
@media (max-width: 600px) { .card { padding: var(--s4); } }

.toolbar { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; margin-bottom: var(--s5); }
.toolbar .search { flex: 1 1 260px; }

.bot-grid { display: grid; gap: var(--s4); grid-template-columns: repeat(auto-fill, minmax(208px, 1fr)); }
.bot-card {
  position: relative; display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border-radius: var(--r-lg); box-shadow: var(--shadow-1);
  transition: box-shadow var(--dur) var(--ease);
}
@media (hover: hover) { .bot-card:hover { box-shadow: var(--shadow-1), 0 0 0 1.5px var(--line-strong); } }
.bot-card:focus-within { box-shadow: 0 0 0 2px var(--accent); }
.bot-card > .portrait { aspect-ratio: 1; height: auto; }
.bot-card-body { padding: var(--s3) var(--s4) var(--s3); flex: 1; display: flex; flex-direction: column; gap: var(--s1); }
.bot-card-name { font-size: var(--t-md); font-weight: 600; }
.bot-card-name a { color: inherit; text-decoration: none; }
.bot-card-name a:focus-visible { outline: none; }
.bot-card-name a::after { content: ""; position: absolute; inset: 0; z-index: 1; }
.bot-card-tagline {
  color: var(--muted); font-size: var(--t-ui); display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.bot-card-meta { margin-top: auto; padding-top: var(--s2); display: flex; align-items: center; justify-content: space-between; gap: var(--s2); }
.bot-card-meta .when { font-size: var(--t-sm); color: var(--muted); }
.bot-card-actions { position: relative; z-index: 2; display: flex; }
.new-card {
  display: flex; align-items: center; justify-content: center; min-height: 224px;
  border: 1.5px dashed var(--line-strong); border-radius: var(--r-lg); color: var(--muted);
  text-decoration: none; font-size: var(--t-ui); font-weight: 500;
  transition: color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
@media (hover: hover) { .new-card:hover { color: var(--ink); border-color: var(--ink); } }

.chips { display: flex; flex-wrap: wrap; gap: var(--s2); }
.chip {
  display: inline-flex; align-items: center; height: 24px; padding: 0 var(--s3);
  font-size: var(--t-xs); font-weight: 500; letter-spacing: .02em; color: var(--muted); background: var(--fill); border-radius: var(--r-pill);
}
button.chip { height: 32px; padding: 0 var(--s4); font-size: var(--t-sm); cursor: pointer; border: 0; transition: background var(--dur) var(--ease), color var(--dur) var(--ease); }
@media (hover: hover) { button.chip:hover { color: var(--ink); background: var(--fill-strong); } }
button.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* Avatars */
.avatar {
  flex: none; border-radius: 50%; overflow: hidden; background: var(--fill);
  display: grid; place-items: center; width: var(--size, 40px); height: var(--size, 40px);
}
.avatar img, .portrait img { width: 100%; height: 100%; object-fit: cover; }
.monogram {
  display: grid; place-items: center; width: 100%; height: 100%;
  background: var(--fill); color: var(--muted); font-family: var(--font-display);
  font-size: calc(var(--size, 40px) * .42); font-weight: 500; line-height: 1;
}
.portrait { display: block; width: 100%; height: 100%; background: var(--fill); overflow: hidden; }
.portrait .monogram { font-size: 48px; }
.avatar-edit { display: flex; align-items: center; gap: var(--s4); flex-wrap: wrap; }
.avatar-edit .avatar { --size: 96px; }
.avatar-hint { flex-basis: 100%; }
/* A bot's picture that opens its editor. */
.avatar-link { display: inline-flex; flex: none; border-radius: 50%; align-self: start; text-decoration: none; }
.list-item .avatar-link, .chat-sidebar-bot .avatar-link { align-self: center; }
.avatar-link .avatar { transition: box-shadow var(--dur) var(--ease); }
@media (hover: hover) { .avatar-link:hover .avatar { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--line-strong); } }

/* ---------- Forms ---------- */
.form-grid { display: grid; gap: var(--s5); }
.form-row { display: grid; gap: var(--s4); grid-template-columns: repeat(auto-fit, minmax(208px, 1fr)); }
.form-row-end { align-items: end; }
.field { display: flex; flex-direction: column; gap: var(--s2); min-width: 0; }
.field > label:not(.check), .field-label {
  font-size: var(--t-ui); font-weight: 500; display: flex; justify-content: space-between; gap: var(--s2); align-items: baseline;
}
.field .count { font-size: var(--t-sm); font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.hint { font-size: var(--t-sm); color: var(--muted); }
code {
  font-family: var(--mono); font-size: .85em;
  background: var(--fill); padding: 1px 6px; border-radius: var(--r-sm);
}
input[type="text"], input[type="url"], input[type="password"], input[type="search"], input[type="number"], select, textarea {
  width: 100%; height: 40px; padding: 0 var(--s4); font-size: var(--t-base);
  background: var(--raised); border: 1.5px solid var(--line-strong); border-radius: var(--r-pill); color: var(--ink);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
textarea { height: auto; min-height: 88px; padding: var(--s3) var(--s4); line-height: var(--lh-read); resize: vertical; border-radius: var(--r-md); }
textarea.tall { min-height: 200px; }
textarea.mono, input.mono { font-family: var(--mono); font-size: var(--t-ui); }
@media (hover: hover) { input:hover, select:hover, textarea:hover { border-color: var(--ink); } }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: none; border-color: var(--ink); box-shadow: 0 0 0 3px var(--accent-soft); }
input[aria-invalid="true"] { border-color: var(--danger); }
.error-text { font-size: var(--t-sm); color: var(--danger); }
input[type="range"] { width: 100%; height: 32px; padding: 0; border: 0; background: none; accent-color: var(--ink); }
input[type="checkbox"], input[type="radio"] { width: 18px; height: 18px; accent-color: var(--ink); }
.check { display: flex; gap: var(--s3); align-items: flex-start; justify-content: flex-start; padding: var(--s2) 0; cursor: pointer; font-weight: 400; }
.check input { margin-top: 3px; flex: none; }
.check small { display: block; color: var(--muted); }
.input-group { display: flex; gap: var(--s2); }
.input-group > input, .input-group > select { flex: 1; min-width: 0; }
.slider-row { display: flex; gap: var(--s3); align-items: center; }
.slider-row input[type="number"] { width: 88px; flex: none; }
.slider-field .slider-row { margin-top: var(--s1); }
.search input { padding-left: var(--s6); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238c8478' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='6'/%3E%3Cpath d='m20 20-4.5-4.5'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-size: 16px; background-position: 14px center; }
select {
  appearance: none; padding-right: var(--s7); cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238c8478' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-size: 16px; background-position: calc(100% - 16px) center;
}
.w-auto { width: auto; }
fieldset { border: 0; }
fieldset.field > legend { margin-bottom: var(--s2); }

details.more { border-top: 1px solid var(--line); padding-top: var(--s4); }
details.more > summary {
  cursor: pointer; font-size: var(--t-ui); font-weight: 500; color: var(--ink); list-style: none;
  display: flex; align-items: center; gap: var(--s2); padding: var(--s1) 0;
}
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::after {
  content: "+"; margin-left: auto; display: grid; place-items: center; width: 24px; height: 24px;
  border-radius: 50%; background: var(--fill); font-size: var(--t-md); line-height: 1; color: var(--ink);
}
details.more[open] > summary::after { content: "\2212"; }
details.more[open] > * + * { margin-top: var(--s4); }
.card > details.more:first-child { border-top: 0; padding-top: 0; }
/* Stacked sections: the dividing line sits 16px from what is above and below it. */
details.more + details.more { margin-top: var(--s4); }

.editor-layout { display: grid; gap: var(--s5); grid-template-columns: minmax(0, 1fr) 280px; align-items: start; }
.editor-aside { position: sticky; top: 80px; display: grid; gap: var(--s4); }
@media (max-width: 900px) { .editor-layout { grid-template-columns: 1fr; } .editor-aside { position: static; } }

.save-bar {
  position: sticky; bottom: var(--s4); z-index: 5; display: flex; flex-wrap: wrap; gap: var(--s3);
  justify-content: space-between; align-items: center; padding: var(--s2) var(--s2) var(--s2) var(--s5);
  background: var(--raised); border-radius: var(--r-pill); box-shadow: var(--shadow-2);
}
.save-state { font-size: var(--t-sm); color: var(--muted); }
/* Save status sits right under the button it describes, centred on it. */
.save-under { margin: calc(-1 * var(--s1)) 0 var(--s1); text-align: center; }
.save-under:empty { display: none; }
/* A destructive button stands apart from the everyday ones. */
.danger-apart { margin-top: var(--s3); }
.save-pill {
  position: sticky; bottom: var(--s5); width: fit-content; margin: var(--s4) 0 0 auto;
  padding: var(--s2) var(--s4); background: var(--raised); border-radius: var(--r-pill); box-shadow: var(--shadow-2);
  font-size: var(--t-sm);
}
.save-pill:empty { visibility: hidden; }

/* ---------- Lists ---------- */
.list { list-style: none; display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s2); }
.list-item {
  display: flex; align-items: center; gap: var(--s3); padding: var(--s3) var(--s4); min-width: 0;
  background: var(--surface); border-radius: var(--r-md); box-shadow: var(--shadow-1);
}
.list-item.is-active { background: var(--fill); }
.list-item .grow { flex: 1; min-width: 0; overflow: hidden; }
.list-item .title { font-weight: 500; }
.list-item .sub { color: var(--muted); font-size: var(--t-ui); font-weight: 400; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty {
  text-align: center; padding: var(--s8) var(--s4);
  border: 1.5px dashed var(--line-strong); border-radius: var(--r-lg); color: var(--muted);
}
.empty h2 { color: var(--ink); margin-bottom: var(--s2); }
.empty p { max-width: 46ch; margin: 0 auto var(--s4); }
.empty .actions { justify-content: center; }

.note {
  display: flex; gap: var(--s3); align-items: flex-start;
  padding: var(--s3) var(--s4); background: var(--fill); border-radius: var(--r-md); font-size: var(--t-ui);
}
.note svg { width: 18px; height: 18px; flex: none; margin-top: 2px; color: var(--muted); }
.note.bad { background: var(--accent-soft); }
.note.bad svg { color: var(--danger); }

/* ---------- Loading ---------- */
.skeleton { display: block; border-radius: var(--r-sm); background: var(--fill); animation: pulse 1.4s ease-in-out infinite; }
.skeleton-card { border-radius: var(--r-lg); aspect-ratio: 3 / 4; }
.skeleton-title { width: 220px; height: 32px; }
.skeleton-line { width: min(480px, 80%); height: 14px; margin-top: var(--s3); }
@keyframes pulse { 50% { opacity: .55; } }
.fade-in { animation: fade-in 180ms var(--ease) both; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } }

/* ---------- Dialogs and toasts ---------- */
dialog {
  margin: auto; padding: 0; border: 0; border-radius: var(--r-lg);
  background: var(--raised); color: var(--ink); box-shadow: var(--shadow-2);
  width: min(520px, calc(100vw - 32px)); max-height: calc(100svh - 32px);
}
dialog[open] { animation: fade-in 180ms var(--ease) both; }
dialog.wide { width: min(820px, calc(100vw - 32px)); }
dialog::backdrop { background: var(--scrim); }
.dialog-body { padding: var(--s5); display: grid; gap: var(--s4); }
.dialog-body h2 { font-size: var(--t-xl); }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--s2); }
.dialog-actions .push { margin-right: auto; }
/* On a phone, a third, quieter button moves under the main pair. With only
   two buttons they share one row. */
@media (max-width: 480px) {
  .dialog-actions:has(> :nth-child(3)) .push { order: 1; flex-basis: 100%; margin: var(--s1) 0 0; }
}
.prompt-preview {
  white-space: pre-wrap; font-family: var(--mono); font-size: var(--t-sm); line-height: 1.65;
  background: var(--surface); border-radius: var(--r-md);
  padding: var(--s4); max-height: 58svh; overflow: auto;
}
.prompt-preview .role {
  display: inline-block; margin: var(--s4) 0 var(--s1); font-family: var(--font);
  font-size: var(--t-xs); font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: .08em;
}
.prompt-preview .role:first-child { margin-top: 0; }

.toasts { position: fixed; right: var(--s5); bottom: var(--s5); z-index: 90; display: grid; gap: var(--s2); width: min(360px, calc(100vw - 32px)); }
.toast {
  display: flex; align-items: center; gap: var(--s3); padding: var(--s2) var(--s2) var(--s2) var(--s4);
  background: var(--primary); color: var(--on-primary); border-radius: var(--r-md);
  box-shadow: var(--shadow-2); font-size: var(--t-ui); animation: fade-in 180ms var(--ease) both;
}
.toast .msg-text { flex: 1; }
.toast .icon-btn { color: inherit; opacity: .7; }
@media (hover: hover) { .toast .icon-btn:hover { opacity: 1; background: color-mix(in srgb, var(--on-primary) 12%, transparent); } }
.toast .link-btn { color: inherit; font-weight: 600; text-decoration: underline; }
.toast.error { background: var(--danger); color: #ffffff; }
:root[data-theme="dark"] .toast.error { color: #1a1210; }
@media (max-width: 600px) { .toasts { right: var(--s4); left: var(--s4); bottom: var(--s4); width: auto; } }

/* ---------- Chat ---------- */
body.in-chat { height: 100svh; height: 100dvh; overflow: hidden; }
/* In a chat only the messages scroll: the page itself never moves or bounces. */
html:has(body.in-chat) { overflow: hidden; overscroll-behavior: none; }
body.in-chat main { min-height: 0; display: flex; }
body.in-chat .site-footer { display: none; }
.chat-layout { flex: 1; min-height: 0; width: 100%; display: grid; grid-template-columns: 272px minmax(0, 1fr); }
.chat-sidebar { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--line); }
.chat-sidebar-head { padding: var(--s4); display: grid; gap: var(--s4); border-bottom: 1px solid var(--line); }
.chat-sidebar-bot { display: flex; align-items: center; gap: var(--s3); }
.chat-sidebar-bot .avatar { --size: 48px; }
.chat-sidebar-bot .grow { min-width: 0; }
.chat-sidebar-bot .name { font-family: var(--font-display); font-size: var(--t-lg); font-weight: 500; line-height: var(--lh-tight); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Rows line up with the New chat button above them (16px in from each side). */
.chat-list { list-style: none; flex: 1; overflow-y: auto; padding: var(--s2) var(--s4); display: grid; align-content: start; gap: 2px; }
.chat-list li { border-radius: var(--r-md); }
@media (hover: hover) { .chat-list li:hover { background: var(--fill); } }
.chat-list li.is-active { background: var(--fill); }
.chat-list li { display: flex; align-items: center; gap: var(--s1); padding-right: var(--s1); }
.chat-list a { display: block; flex: 1; min-width: 0; padding: var(--s2) var(--s3); color: var(--ink); text-decoration: none; border-radius: var(--r-md); }
/* Delete shows on the row under the pointer or focus; always on touch screens. */
.chat-list .chat-del { opacity: 0; transition: opacity var(--dur) var(--ease); }
.chat-list li:focus-within .chat-del { opacity: 1; }
@media (hover: hover) { .chat-list li:hover .chat-del { opacity: 1; } }
@media (hover: none) { .chat-list .chat-del { opacity: 1; } }
.chat-list .t { display: block; font-size: var(--t-ui); font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .d { font-size: var(--t-sm); color: var(--muted); }

.chat-main { position: relative; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-topbar { display: flex; align-items: center; gap: var(--s2); min-height: 64px; padding: var(--s2) var(--s4); border-bottom: 1px solid var(--line); background: var(--bg); }
.chat-topbar .avatar-link { align-self: center; }
/* The title box clips long names, so it carries 4px of room on each side for
   the focus ring of the name and the rename field (drawn slightly outside). */
.chat-topbar .title {
  flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 4px 4px 4px calc(var(--s2) + 4px); margin: -4px -4px -4px calc(-1 * (var(--s2) + 4px));
  pointer-events: none;
}
.chat-topbar .title > * { pointer-events: auto; }
.chat-topbar .title small { display: block; font-weight: 400; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
/* The chat name renames in place. The pencil shows on hover and focus. */
.title-line { display: flex; align-items: center; gap: var(--s2); min-width: 0; }
.title-edit {
  flex: 0 1 auto; display: inline-flex; align-items: center; gap: var(--s1); min-width: 0; height: 32px;
  margin-left: calc(-1 * var(--s2)); padding: 0 var(--s2); border: 0; border-radius: var(--r-sm);
  background: none; color: inherit; font: inherit; cursor: text;
}
.title-edit > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.title-edit svg { flex: none; width: 14px; height: 14px; color: var(--muted); opacity: 0; transition: opacity var(--dur) var(--ease); }
@media (hover: hover) { .title-edit:hover { background: var(--fill); } }
.title-edit:focus-visible svg { opacity: 1; }
@media (hover: hover) { .title-edit:hover svg { opacity: 1; } }
@media (hover: none) { .title-edit svg { opacity: 1; } }
/* Same height, font and text position as the name it replaces, so nothing moves. */
.chat-topbar .title-input {
  height: 32px; width: min(320px, 100%); margin-left: calc(-1 * var(--s2)); padding: 0 calc(var(--s2) - 1.5px);
  font: inherit; font-weight: 600; border-radius: var(--r-sm);
}
.only-mobile { display: none; }
/* Hide chats (wide screens): the chat list folds away and the conversation takes the room.
   The bot's picture moves into the top bar so Edit bot stays one click away. */
:root[data-chat-sidebar="hidden"] .chat-layout { grid-template-columns: minmax(0, 1fr); }
:root[data-chat-sidebar="hidden"] .chat-sidebar { display: none; }
:root[data-chat-sidebar="hidden"] .chat-topbar > .avatar-link.only-mobile { display: inline-flex; }

.chat-log { flex: 1; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; padding: var(--s6) var(--s5) var(--s4); }
.chat-log-inner { max-width: var(--measure); margin: 0 auto; display: grid; gap: var(--s4); }
/* One picture size for everyone in a chat, faces included. */
.msg { --pic: 96px; display: grid; grid-template-columns: var(--pic) minmax(0, 1fr); gap: var(--s4); }
.msg > .avatar, .msg > .avatar-link > .avatar { --size: var(--pic) !important; }
/* Picture size (Chat look, or Settings → Appearance). */
:root[data-chat-pic="md"] .msg { --pic: 72px; }
:root[data-chat-pic="sm"] .msg { --pic: 48px; gap: var(--s3); }
/* Novel layout: no pictures beside messages, just names and text, like a book. */
:root[data-chat-layout="novel"] .msg { grid-template-columns: minmax(0, 1fr); }
:root[data-chat-layout="novel"] .msg > .avatar, :root[data-chat-layout="novel"] .msg > .avatar-link { display: none; }
/* A hairline keeps pictures crisp against any background. */
.msg > .avatar, .msg > .avatar-link > .avatar, .msg .expr-face { box-shadow: 0 0 0 1px var(--line); }
@media (max-width: 600px) {
  .msg { --pic: 40px; gap: var(--s3); }
  :root[data-chat-pic="md"] .msg { --pic: 36px; }
  :root[data-chat-pic="sm"] .msg { --pic: 32px; }
}
.msg-head { display: flex; align-items: baseline; gap: var(--s2); margin-bottom: var(--s1); }
.msg-name { font-size: var(--t-ui); font-weight: 600; }
.msg-time { font-size: var(--t-xs); color: var(--muted); }
.msg-body { font-family: var(--chat-font, var(--font)); font-size: calc(var(--t-md) * var(--chat-scale)); line-height: var(--lh-read); overflow-wrap: anywhere; }
/* Chat font (Chat look, or Settings → Appearance): system faces, nothing extra to load. */
:root[data-chat-font="serif"] { --chat-font: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif; }
:root[data-chat-font="plain"] { --chat-font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
/* Clear: made for low vision and tired eyes; I, l and 1, O and 0 never look alike. */
:root[data-chat-font="clear"] { --chat-font: "Atkinson Hyperlegible Next", system-ui, sans-serif; }
/* Light text on a dark screen blooms and looks thinner, so it gets a little more weight. */
:root[data-theme="dark"] .msg-body { font-weight: 450; }
/* Long passages read faster upright; actions keep their softer colour either way. */
:root[data-chat-actions="upright"] .msg-body em { font-style: normal; }
/* About 70 characters a line (the comfortable range is 45 to 75), measured
   in em so it holds at every message size. */
.msg-body { max-width: 33em; }
:root[data-chat-font="serif"] .msg-body { max-width: 30em; } /* serif fallbacks run narrow */
.msg-body > * + * { margin-top: .75em; }
.msg.user .msg-body { color: var(--narration); }
.msg-body em { font-style: italic; color: var(--narration); }
.msg.user .msg-body em { color: inherit; }
.msg-body pre { background: var(--surface); padding: var(--s3); border-radius: var(--r-sm); overflow-x: auto; }
.msg-body ul, .msg-body ol { padding-left: 1.3em; }
.msg-body blockquote { border-left: 2px solid var(--line-strong); padding-left: var(--s3); color: var(--narration); }
.msg-body.error { color: var(--danger); }
.msg-body .caret { display: inline-block; width: 7px; height: 1.05em; background: var(--ink); border-radius: 2px; vertical-align: text-bottom; margin-left: 2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.msg-body details.thinking { font-size: var(--t-ui); color: var(--muted); }
.msg-body details.thinking summary { cursor: pointer; }
.msg-body details.thinking div { white-space: pre-wrap; padding-left: var(--s3); border-left: 2px solid var(--line); margin-top: var(--s2); }
/* Pulled left by half the space around an icon (36px button, 18px icon), so
   the first icon lines up with the message text above it. */
.msg-foot { display: flex; align-items: center; gap: var(--s1); flex-wrap: wrap; min-height: 36px; margin-top: var(--s1); margin-left: -9px; }
/* Kept in the tab order at opacity 0, so keyboard focus reveals them. */
.msg-tools { display: flex; opacity: 0; transition: opacity var(--dur) var(--ease); }
.msg:focus-within .msg-tools, .msg.is-last .msg-tools { opacity: 1; }
@media (hover: hover) { .msg:hover .msg-tools { opacity: 1; } }
/* Touch screens have no hover. Tools show on the latest reply and on the
   message last tapped; the rest stay out of the way while reading. */
@media (hover: none) {
  .msg-tools { opacity: 1; }
  .msg:not(.is-last):not(.tools-open):not(:focus-within) .msg-tools { display: none; }
  .msg:not(.is-last):not(.tools-open):not(:focus-within) .msg-foot { min-height: 0; }
  .msg:not(.is-last):not(.tools-open):not(:focus-within) .msg-foot:not(:has(.swipes, .msg-meta)) { display: none; }
}
.msg-meta { font-size: var(--t-sm); color: var(--muted); margin-left: auto; display: flex; align-items: center; gap: var(--s2); }
.swipes { display: inline-flex; align-items: center; font-size: var(--t-sm); color: var(--muted); font-variant-numeric: tabular-nums; }
.msg-edit textarea { min-height: 112px; }
.msg-edit .actions { margin-top: var(--s2); justify-content: flex-end; }

.composer { background: var(--bg); padding: var(--s2) var(--s5) var(--s4); }
.composer-inner { max-width: var(--measure); margin: 0 auto; }
.composer-inner > .note { margin-bottom: var(--s2); }
.composer-box {
  /* The send button sits 6px in from the edge, and the corner radius is its
     radius plus that gap plus the border (20 + 6 + 1.5, capped at half the
     54px height), so the button's curve follows the box's corner. */
  display: flex; align-items: flex-end; gap: var(--s2); padding: 6px 6px 6px var(--s2);
  background: var(--raised); border: 1.5px solid var(--line-strong); border-radius: 27px;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.composer-box:focus-within { border-color: var(--ink); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer-box textarea {
  font-family: var(--chat-font, var(--font));
  min-height: 40px; max-height: 40svh; border: 0; background: none; resize: none;
  /* Text starts --composer-inset in from the box's outer edge (1.5 border + 8 box padding + this). */
  padding: var(--s2) var(--s2) var(--s2) calc(var(--composer-inset) - 9.5px);
  /* One line is exactly as tall as the 40px buttons beside it, so the send
     button sits the same 6px from the top, right and bottom of the box. */
  padding-block: max(0px, calc((40px - 1lh) / 2));
  border-radius: 0; font-size: calc(var(--t-md) * var(--chat-scale));
}
.composer-box textarea:focus-visible { box-shadow: none; }
.send-btn {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: var(--primary); color: var(--on-primary); cursor: pointer; display: grid; place-items: center;
  transition: background var(--dur) var(--ease);
}
@media (hover: hover) { .send-btn:hover { background: var(--primary-hover); } }
.send-btn:disabled { background: var(--fill); color: var(--muted); cursor: not-allowed; }
.send-btn svg { width: 20px; height: 20px; }
.send-btn.stop { background: var(--danger); color: var(--surface); }
.composer-foot { display: flex; flex-wrap: wrap; gap: var(--s3); justify-content: space-between; align-items: center; margin-top: var(--s2); padding: 0 var(--s2) 0 var(--composer-inset); font-size: var(--t-sm); color: var(--muted); }
/* The persona picker is a quiet filled pill, not a boxed field: it is a
   setting beside the message box, not something to type into. */
.composer-foot select {
  width: auto; height: 32px; font-size: var(--t-sm); padding-left: var(--s3); padding-right: var(--s6);
  border-color: transparent; background-color: var(--fill); background-position: calc(100% - 12px) center;
}
@media (hover: hover) { .composer-foot select:hover { border-color: transparent; background-color: var(--fill-strong); } }
.persona-pick { display: inline-flex; align-items: center; gap: var(--s2); }
.persona-pick > span { white-space: nowrap; }
.persona-group { display: inline-flex; align-items: center; gap: var(--s1); }
.composer-foot .persona-edit { width: 32px; height: 32px; }
.composer-foot .persona-edit svg { width: 16px; height: 16px; }
/* Write my reply: sits beside Send, quieter than it. */
.composer-tool { width: 40px; height: 40px; }
.composer-tool.is-active { background: var(--fill); color: var(--ink); }
.draft-bar {
  display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s2); min-height: 40px;
  padding: var(--s1) var(--s1) var(--s1) var(--s4); background: var(--fill); border-radius: var(--r-pill);
  font-size: var(--t-sm); color: var(--ink); animation: fade-in 180ms var(--ease) both;
}
.draft-bar .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer-box textarea[readonly] { color: var(--muted); }

@media (max-width: 860px) {
  .chat-layout { grid-template-columns: 1fr; }
  .chat-sidebar { position: fixed; inset: 0 auto 0 0; width: min(304px, 86vw); z-index: 60; transform: translateX(-100%); transition: transform 200ms var(--ease); }
  .chat-sidebar.is-open { transform: none; box-shadow: var(--shadow-2); }
  .only-mobile { display: inline-flex; }
  .only-desktop { display: none; }
  :root[data-chat-sidebar="hidden"] .chat-sidebar { display: flex; }
  .scrim { position: fixed; inset: 0; z-index: 55; background: var(--scrim); }
  .chat-log { padding: var(--s4) var(--s4) var(--s2); }
  .chat-log-inner { gap: var(--s5); }
  .composer { padding: var(--s2) var(--s4) var(--s4); }
}

/* ---------- Lore ---------- */
.lore-layout { display: grid; gap: var(--s5); grid-template-columns: 240px minmax(0, 1fr); align-items: start; }
.lore-nav { position: sticky; top: 80px; max-height: calc(100svh - 112px); overflow-y: auto; }
.lore-nav h3 { font-family: var(--font); font-size: var(--t-xs); font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: var(--s4) 0 var(--s1); }
.lore-nav h3:first-of-type { margin-top: 0; }
.lore-nav ul { list-style: none; }
.lore-nav a { display: flex; align-items: center; gap: var(--s2); padding: var(--s2) var(--s3); color: var(--ink); text-decoration: none; border-radius: var(--r-pill); font-size: var(--t-ui); }
@media (hover: hover) { .lore-nav a:hover { background: var(--fill); } }
.lore-nav a[aria-current] { background: var(--primary); color: var(--on-primary); font-weight: 500; }
.lore-nav a.off { color: var(--muted); }
.lore-nav input[type="search"] { margin-bottom: var(--s3); }
.prose { font-size: var(--t-md); line-height: var(--lh-read); max-width: 68ch; }
.prose > * + * { margin-top: var(--s4); }
.prose ul, .prose ol { padding-left: 1.3em; }
@media (max-width: 800px) { .lore-layout { grid-template-columns: 1fr; } .lore-nav { position: static; max-height: none; } }

.site-footer { border-top: 1px solid var(--line); padding: var(--s5) 0; color: var(--muted); font-size: var(--t-sm); }
.site-footer .wrap { display: flex; flex-wrap: wrap; gap: var(--s3); justify-content: space-between; }

.shortcut-list { padding-left: 1.2em; display: grid; gap: var(--s1); }

.setup-steps { list-style: none; display: grid; gap: var(--s1); }
.setup-steps li { display: flex; align-items: center; gap: var(--s3); padding: var(--s2) 0; }
.setup-steps li + li { border-top: 1px solid var(--line); }
.setup-steps .grow { flex: 1; }
.step-mark {
  flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  background: var(--fill); color: var(--muted); font-size: var(--t-sm); font-weight: 500;
}
.setup-steps .is-done { color: var(--muted); }
.setup-steps .is-done .step-mark { background: var(--primary); color: var(--on-primary); }
.setup-steps .is-done .step-mark svg { width: 14px; height: 14px; }
.tag-filter { margin: calc(-1 * var(--s2)) 0 var(--s5); }
/* Series come before tags: a step up in size, and chosen in the primary colour. */
.series-filter { margin: calc(-1 * var(--s2)) 0 var(--s3); }
.series-filter + .tag-filter { margin-top: 0; }
.series-filter button.chip { height: 36px; font-size: var(--t-ui); gap: var(--s2); }
.series-filter button.chip[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }
.chip .n { font-variant-numeric: tabular-nums; opacity: .72; }
/* On a phone, many series would stack into a tall block; they scroll sideways instead. */
@media (max-width: 600px) {
  .series-filter {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none;
    margin-inline: calc(-1 * var(--s4)); padding-inline: var(--s4); scroll-padding-inline: var(--s4);
    /* Room for the chips' 4px tap borders, which a scrolling row would clip. */
    padding-block: 4px; margin-block: calc(-1 * var(--s2) - 4px) calc(var(--s3) - 4px);
  }
  .series-filter::-webkit-scrollbar { display: none; }
  .series-filter button.chip { flex: none; white-space: nowrap; }
}
/* A heading over each series when every bot is shown. */
.series-head {
  display: flex; align-items: baseline; gap: var(--s2); margin-top: var(--s4);
  font-family: var(--font-display); font-size: var(--t-lg); font-weight: 500; line-height: var(--lh-tight);
}
.series-head:first-child { margin-top: 0; }
.series-head .n { font-family: var(--font); font-size: var(--t-sm); color: var(--muted); font-variant-numeric: tabular-nums; }
.span-all { grid-column: 1 / -1; }

@media (max-width: 600px) {
  .list-item { flex-wrap: wrap; }
  .list-item .grow { flex: 1 1 55%; }
  .list-item .actions { flex-basis: 100%; justify-content: flex-end; }
  .page-head .actions { width: 100%; }
  .page-head .actions .btn { flex: 1; }
}

/* Chat on a phone: the chat owns the screen, so the site nav steps aside. */
@media (max-width: 700px) {
  body.in-chat .main-nav, body.in-chat .header-status { display: none; }
  body.in-chat .header-inner { padding-bottom: var(--s2); }
}
@media (max-width: 600px) {
  #composer-hint { display: none; }
  .chat-topbar .hide-narrow { display: none; }
  /* The bot's face is in every message and the sidebar; the name needs the room. */
  .chat-topbar > .avatar, .chat-topbar > .avatar-link { display: none; }
  .chat-topbar { min-height: 56px; padding: var(--s1) var(--s2); gap: var(--s1); }
}

/* ---------- Text size preview (Settings) ---------- */
.text-preview { padding: var(--s4); background: var(--bg); border-radius: var(--r-md); }
.text-preview .msg-body { max-width: 60ch; }

/* ---------- Crop dialog ---------- */
.crop-stage {
  position: relative; overflow: hidden; touch-action: none; cursor: grab;
  /* Width follows the height limit, so the frame always keeps the picture's shape. */
  width: min(100%, calc(46svh * var(--crop-aspect, 1))); aspect-ratio: var(--crop-aspect, 1); margin: 0 auto;
  background: var(--fill); border-radius: var(--r-md);
}
.crop-stage:active { cursor: grabbing; }
.crop-stage img { position: absolute; max-width: none; user-select: none; -webkit-user-drag: none; }
.crop-ring { position: absolute; inset: 0; pointer-events: none; box-shadow: inset 0 0 0 1px var(--line-strong); border-radius: inherit; }
.crop-stage.is-round .crop-ring {
  border-radius: 50%; margin: auto; width: min(100%, 100cqh);
  box-shadow: inset 0 0 0 2px var(--raised), 0 0 0 9999px rgb(from var(--bg) r g b / .55);
}
.crop-stage.is-round { container-type: size; }

/* ---------- Picture pickers ---------- */
.bg-edit { display: grid; gap: var(--s3); }
.bg-thumb {
  display: grid; place-items: center; width: 100%; max-width: 320px; aspect-ratio: 16 / 9;
  overflow: hidden; border-radius: var(--r-md); background: var(--fill);
  color: var(--muted); font-size: var(--t-sm);
}
.bg-thumb img { width: 100%; height: 100%; object-fit: cover; }
.bg-edit .pick-hint { flex-basis: auto; }

/* ---------- Chat background ---------- */
.chat-bg {
  position: absolute; inset: 0; z-index: 0; pointer-events: none;
  background-size: cover; background-position: center;
}
.chat-bg[hidden] { display: none; }
.chat-bg::after { content: ""; position: absolute; inset: 0; background: var(--bg); opacity: var(--bg-dim, .86); }
.chat-main.has-bg > *:not(.chat-bg) { position: relative; z-index: 1; }
.chat-main.has-bg .chat-topbar, .chat-main.has-bg .composer { background: color-mix(in srgb, var(--bg) 90%, transparent); }
/* Words keep a plain background of their own. */
.chat-main.has-bg .msg-body {
  background: var(--surface); border-radius: var(--r-md); padding: var(--s3) var(--s4); box-shadow: var(--shadow-1);
}
/* Name, tools and details line up with the words inside the card (16px in). */
.chat-main.has-bg .msg-head { padding-inline: var(--s4); }
.chat-main.has-bg .msg-foot { margin-left: calc(var(--s4) - 9px); padding-right: var(--s4); }

/* ---------- Read mode ----------
   For reading in company: only the story and the box you type in. No header,
   chat list, top bar, names, pictures, bubbles, tools or tags. The button in the
   box (or Alt+B) brings everything back. Notes that need you, like a missing
   connection or a reply cut off, still show. The message box keeps + (Direct
   the next reply, Move the plot, Dice), Write my reply, and a Regenerate button for
   the last reply with its versions; Ideas stays out. */
:root[data-chat-read="on"] body.in-chat .site-header,
:root[data-chat-read="on"] .chat-sidebar,
:root[data-chat-read="on"] .chat-bg,
:root[data-chat-read="on"] .chat-topbar,
:root[data-chat-read="on"] .msg > .avatar,
:root[data-chat-read="on"] .msg > .avatar-link,
:root[data-chat-read="on"] .msg-head,
:root[data-chat-read="on"] .msg[data-id] .msg-foot,
:root[data-chat-read="on"] .earlier .hint,
:root[data-chat-read="on"] .milestone,
:root[data-chat-read="on"] .composer-foot,
:root[data-chat-read="on"] #suggest { display: none !important; }
#read-exit, #read-regen { display: none; }
:root[data-chat-read="on"] #read-regen { display: inline-flex; }
:root[data-chat-read="on"] #read-exit { display: inline-flex; color: var(--muted); }
:root[data-chat-read="on"] .chat-layout { grid-template-columns: minmax(0, 1fr); }
:root[data-chat-read="on"] .chat-main.has-bg .composer { background: var(--bg); }
:root[data-chat-read="on"] .msg { grid-template-columns: minmax(0, 1fr); }
:root[data-chat-read="on"] .chat-log-inner { gap: .9em; }
/* One voice: both sides in the same ink, no cards. Font, size, line length and
   how actions look follow Chat look / Settings → Appearance. */
:root[data-chat-read="on"] .msg-body,
:root[data-chat-read="on"] .chat-main.has-bg .msg-body {
  margin-inline: auto; background: none; box-shadow: none; padding: 0; border-radius: 0;
  color: var(--ink); text-align: justify; hyphens: auto;
}
:root[data-chat-read="on"] .msg.user .msg-body { color: var(--ink); }
/* Narrow columns justify into gaps; ragged right reads better there. */
@media (max-width: 600px) { :root[data-chat-read="on"] .msg-body { text-align: left; } }

/* Author's note: a quiet reminder above the message box that a lasting instruction is on. */
.note-pill {
  display: flex; align-items: baseline; gap: var(--s2); max-width: 100%; margin-bottom: var(--s2); padding: 4px var(--s3);
  border: 1px dashed var(--line-strong); border-radius: var(--r-pill); background: none; color: var(--muted);
  font: inherit; font-size: var(--t-sm); text-align: left; cursor: pointer;
}
.note-pill[hidden] { display: none; }
.note-pill-label { flex: none; font-weight: 600; color: var(--ink); }
.note-pill-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (hover: hover) { .note-pill:hover { border-style: solid; color: var(--ink); } }
:root[data-chat-read="on"] .note-pill { display: none !important; }

/* Phrases on the avoid list, marked in a reply. */
mark.avoided { background: color-mix(in srgb, var(--accent) 18%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; text-decoration: underline wavy color-mix(in srgb, var(--accent) 60%, transparent); text-underline-offset: 3px; }
:root[data-chat-read="on"] mark.avoided { background: none; text-decoration: none; padding: 0; }

/* ---------- Bond meter ---------- */
.bond {
  display: flex; align-items: center; gap: var(--s2); flex: none; height: 36px;
  background: none; border: 0; border-radius: var(--r-pill);
  padding: 0 var(--s3); cursor: pointer;
}
@media (hover: hover) { .bond:hover { background: var(--fill); } }
@media (hover: hover) { .bond:hover .bond-label { color: var(--ink); } }
.bond-label { font-size: var(--t-sm); color: var(--muted); }
.bond-bar { display: block; width: 72px; height: 6px; border-radius: var(--r-pill); background: var(--fill-strong); overflow: hidden; }
.bond-fill { display: block; height: 100%; width: 0; background: var(--accent); border-radius: var(--r-pill); transition: width 400ms var(--ease); }
.bond[data-level="0"] .bond-fill { background: var(--danger); }
.bond[data-level="1"] .bond-fill { background: var(--muted); }
.bond-read { font-size: var(--t-ui); color: var(--ink); }
.bond-chip { color: var(--muted); }
.bond-chip.up { color: var(--ok); }
.bond-chip.down { color: var(--danger); }
/* On a phone the label sits small above the bar, so the bar still says something. */
@media (max-width: 600px) {
  .chat-topbar .bond { flex-direction: column; align-items: stretch; justify-content: center; gap: var(--s1); height: 40px; padding: 0 var(--s2); }
  .chat-topbar .bond-label { max-width: 72px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--t-xs); line-height: 1; text-align: center; }
  .bond-bar { width: 56px; }
}

/* ---------- Lorebook bar ---------- */
.book-bar { margin-bottom: var(--s5); display: grid; gap: var(--s3); }
.book-bar .check { padding-bottom: 0; }

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

/* Each page fades in when it is ready. Opacity only, so fixed panels inside keep their place. */
.route-in { animation: route-in 180ms var(--ease); }
@keyframes route-in { from { opacity: 0; } }
.nav-pair .icon-btn:disabled { color: var(--line-strong); background: none; }
.nav-pair .icon-btn[aria-current="page"] { background: var(--raised); box-shadow: var(--shadow-1); }

/* ---------- Menus ---------- */
.menu {
  position: fixed; z-index: 80; min-width: 200px; max-width: min(320px, calc(100vw - 16px));
  padding: var(--s1); background: var(--raised); border-radius: var(--r-md); box-shadow: var(--shadow-2);
  animation: fade-in 140ms var(--ease) both;
}
.menu-item {
  display: flex; flex-direction: column; align-items: flex-start; width: 100%; gap: 2px;
  padding: var(--s2) var(--s3); border: 0; border-radius: var(--r-sm); background: none;
  color: var(--ink); font-size: var(--t-ui); text-align: left; cursor: pointer;
}
.menu-item small { color: var(--muted); font-size: var(--t-xs); }
.menu-item:focus-visible { background: var(--fill); outline: none; }
@media (hover: hover) { .menu-item:hover { background: var(--fill); outline: none; } }
.menu-item.danger { color: var(--danger); }
.menu-item:disabled { color: var(--line-strong); cursor: not-allowed; background: none; }
.menu-sep { height: 1px; margin: var(--s1) var(--s2); background: var(--line); }
/* Long menus are grouped under short headings, so the eye can skip ahead. */
.menu-heading { padding: var(--s2) var(--s3) var(--s1); font-size: var(--t-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.menu-group + .menu-group { margin-top: var(--s1); padding-top: var(--s1); border-top: 1px solid var(--line); }

/* ---------- Chat tools ---------- */
/* A small dot marks a tool that holds something (memory saved, cast added). */
.icon-btn.has-dot { position: relative; }
.icon-btn.has-dot::after {
  content: ""; position: absolute; top: 7px; right: 7px; width: 6px; height: 6px;
  border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 2px var(--bg);
}
/* Busy icon button: the icon gives way to a spinner, the size stays. */
.icon-btn.is-loading { position: relative; color: transparent; pointer-events: none; }
.icon-btn.is-loading svg { visibility: hidden; }
.icon-btn.is-loading::before {
  content: ""; position: absolute; inset: 0; margin: auto; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--muted); border-right-color: transparent; animation: spin .7s linear infinite;
}
.btn-quiet.is-loading::after { border-color: var(--muted); border-right-color: transparent; }

.direct-bar {
  display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s2); min-height: 40px;
  padding: var(--s1) var(--s1) var(--s1) var(--s4); background: var(--accent-soft); border-radius: var(--r-pill);
  animation: fade-in 180ms var(--ease) both;
}
.direct-label { font-size: var(--t-sm); font-weight: 600; color: var(--accent); flex: none; }
.direct-bar input[type="text"] {
  flex: 1; min-width: 0; height: 32px; padding: 0 var(--s2); border: 0; background: none; font-size: var(--t-ui);
}
.direct-bar input[type="text"]:focus-visible { box-shadow: none; }
.direct-bar .icon-btn { width: 32px; height: 32px; }

.check-chip { font-size: var(--t-sm); }
.check-chip.ok { color: var(--ok); }
button.check-chip.bad {
  background: none; border: 0; padding: 0; color: var(--danger); cursor: pointer;
  text-decoration: underline; text-underline-offset: 3px;
}
.issue-list { padding-left: 1.2em; display: grid; gap: var(--s1); }

/* Cast picker */
.cast-list { display: grid; gap: var(--s1); }
.cast-scroll { display: grid; gap: var(--s2); max-height: 50svh; overflow-y: auto; }
.cast-group { margin-top: var(--s2); font-family: var(--font); font-size: var(--t-xs); font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.cast-group:first-child { margin-top: 0; }
.cast-row {
  display: flex; align-items: center; gap: var(--s3); padding: var(--s2) var(--s3);
  border-radius: var(--r-md); cursor: pointer;
}
@media (hover: hover) { .cast-row:hover { background: var(--fill); } }
.cast-row .grow { flex: 1; min-width: 0; display: grid; }
.cast-row .t { font-weight: 500; }
.cast-row small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Lore suggestions */
.sugg-list { display: grid; gap: var(--s3); max-height: 56svh; overflow-y: auto; padding-right: var(--s1); }
.sugg-item { display: grid; gap: var(--s3); padding: var(--s4); background: var(--surface); border-radius: var(--r-md); }
.sugg-item .check { padding: 0; font-weight: 500; }
#sugg .skeleton-line { width: 100%; }

/* A bond level change, shown between messages. */
.milestone { display: flex; align-items: center; gap: var(--s3); font-size: var(--t-sm); color: var(--muted); }
.milestone::before, .milestone::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.milestone span { padding: var(--s1) var(--s3); border-radius: var(--r-pill); background: var(--fill); color: var(--ink); font-weight: 500; }
.milestone.up span { background: var(--accent-soft); }

/* Bond levels in the bot editor */
.bond-levels { display: grid; gap: var(--s3); }
.bond-level { display: grid; grid-template-columns: 40px minmax(0, 1fr); gap: var(--s3); align-items: start; }
.bond-at { padding-top: var(--s2); font-size: var(--t-sm); font-variant-numeric: tabular-nums; color: var(--muted); text-align: right; }
.bond-level textarea { min-height: 64px; }

/* ---------- Bond chart ---------- */
.bond-chart { display: grid; gap: var(--s2); }
.bond-chart-plot { position: relative; }
.bond-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.bond-chart .grid { stroke: var(--line); stroke-width: 1; }
.bond-chart .axis { fill: var(--muted); font-size: 13px; font-family: var(--font); }
.bond-chart .value { fill: var(--ink); font-size: 13px; font-weight: 600; font-family: var(--font); }
.bond-chart .series { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.bond-chart .end, .bond-chart .hover-dot { fill: var(--accent); stroke: var(--raised); stroke-width: 2; }
.bond-chart .crosshair { stroke: var(--line-strong); stroke-width: 1; }
.bond-chart .hit { fill: transparent; cursor: crosshair; outline: none; }
.bond-chart .hit:focus-visible { stroke: var(--accent); stroke-width: 2; }
.chart-tip {
  position: absolute; z-index: 2; pointer-events: none; display: grid; gap: 2px;
  padding: var(--s2) var(--s3); background: var(--primary); color: var(--on-primary);
  border-radius: var(--r-sm); font-size: var(--t-sm); white-space: nowrap; box-shadow: var(--shadow-2);
}
.chart-tip small { opacity: .75; font-size: var(--t-xs); }
.chart-table table { width: 100%; border-collapse: collapse; font-size: var(--t-sm); }
.chart-table th, .chart-table td { text-align: left; padding: var(--s1) var(--s2); border-bottom: 1px solid var(--line); }
.chart-table th { color: var(--muted); font-weight: 500; }

/* ---------- Reply ideas ---------- */
.suggest-bar {
  display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s2); min-height: 40px;
  padding: var(--s1) var(--s1) var(--s1) var(--s4); background: var(--fill); border-radius: var(--r-md);
  animation: fade-in 180ms var(--ease) both;
}
.suggest-title { font-size: var(--t-sm); font-weight: 600; flex: none; }
.suggest-list { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; gap: var(--s2); padding-block: var(--s1); }
.suggest-chip {
  height: 32px; padding: 0 var(--s4); border: 1.5px solid var(--ink); border-radius: var(--r-pill);
  background: var(--raised); color: var(--ink); font-size: var(--t-sm); font-weight: 500; cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
@media (hover: hover) { .suggest-chip:hover { background: var(--primary); color: var(--on-primary); } }
.skeleton-chip { width: 112px; height: 32px; border-radius: var(--r-pill); }
.suggest-bar .icon-btn { width: 32px; height: 32px; }

/* ---------- Recap ---------- */
.recap-card {
  margin-bottom: var(--s2); padding: var(--s3) var(--s2) var(--s3) var(--s4);
  background: var(--surface); border-radius: var(--r-md); box-shadow: var(--shadow-1);
  animation: fade-in 180ms var(--ease) both;
}
.recap-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); }
.recap-head h2 { font-size: var(--t-md); }
.recap-body { font-size: var(--t-ui); color: var(--ink); padding-right: var(--s2); max-height: 30svh; overflow-y: auto; }
.recap-body .skeleton-line { width: 100%; }

/* ---------- Messages: translation, pins, jump highlight ---------- */
.translation {
  margin-top: var(--s2); padding: var(--s2) var(--s3); border-left: 2px solid var(--accent);
  background: var(--surface); border-radius: 0 var(--r-sm) var(--r-sm) 0;
  font-size: calc(var(--t-base) * var(--chat-scale)); line-height: var(--lh-read);
}
.translation > * + * { margin-top: .6em; }
.translation-head { display: flex; justify-content: space-between; gap: var(--s2); font-size: var(--t-xs); font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.translation-head .link-btn { font-size: var(--t-xs); text-transform: none; letter-spacing: 0; }
.translation .skeleton-line { width: 100%; }
.icon-btn.is-on { color: var(--accent); }
.icon-btn.is-on svg { fill: currentColor; fill-opacity: .18; }
.pinned-chip { background: var(--accent-soft); color: var(--accent); }
.msg.flash .msg-body { animation: flash 1.6s var(--ease); border-radius: var(--r-sm); }
@keyframes flash { 0%, 40% { background: var(--accent-soft); box-shadow: 0 0 0 8px var(--accent-soft); } 100% { background: transparent; box-shadow: 0 0 0 8px transparent; } }

.pin-list { list-style: none; display: grid; gap: var(--s2); max-height: 56svh; overflow-y: auto; }
.pin-row { display: flex; align-items: center; gap: var(--s2); padding: var(--s3); background: var(--surface); border-radius: var(--r-md); }
.pin-row .grow { flex: 1; min-width: 0; font-size: var(--t-ui); }
.pin-row p { color: var(--muted); }

/* ---------- Story ---------- */
.story-out { max-height: 56svh; overflow-y: auto; padding: var(--s4) var(--s5); background: var(--surface); border-radius: var(--r-md); }
.story-out .skeleton-line { width: 100%; }
.story-out .prose h1 { font-size: var(--t-xl); }

/* ---------- Bonds dialog ---------- */
.bond-rows { display: grid; gap: var(--s4); max-height: 62svh; overflow-y: auto; }
.bond-row { display: grid; gap: var(--s3); padding: var(--s4); background: var(--surface); border-radius: var(--r-md); }
.bond-row-head { display: flex; align-items: center; gap: var(--s3); }
.bond-row-head .grow { flex: 1; min-width: 0; }
.bond-row-head h3 { font-size: var(--t-md); }
.bond-row-head small { color: var(--muted); }

/* ---------- Search ---------- */
.search-page { display: block; margin-bottom: var(--s3); }
.search-page input { height: 48px; font-size: var(--t-md); }
.search-hit { align-items: flex-start; }
.search-hit .sub-inline { font-weight: 400; color: var(--muted); font-size: var(--t-sm); }
.hit-text { color: var(--muted); font-size: var(--t-ui); margin-top: var(--s1); }
.hit-text mark { background: var(--accent-soft); color: var(--ink); border-radius: 3px; padding: 0 2px; }
.icon-toggle[aria-current="page"] { background: var(--primary); color: var(--on-primary); }
input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none; width: 16px; height: 16px; cursor: pointer;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238c8478' stroke-width='2.2' stroke-linecap='round'%3E%3Cpath d='M6 6l12 12M18 6 6 18'/%3E%3C/svg%3E") center / 16px no-repeat;
}

/* Mature content marker in the chat header */
.content-tag {
  display: inline-flex; align-items: center; height: 20px; padding: 0 var(--s2); vertical-align: 2px;
  border-radius: var(--r-pill); background: var(--accent-soft); color: var(--accent);
  font-size: var(--t-xs); font-weight: 600; letter-spacing: .02em;
}

/* Message box: the text always takes the spare room. On a phone it gets the
   full width, and the buttons move to a row underneath it. */
.composer-box textarea { flex: 1 1 auto; min-width: 0; }
@media (max-width: 600px) {
  .composer-box { flex-wrap: wrap; row-gap: 0; }
  .composer-box textarea { flex: 1 0 100%; }
  .composer-box .hide-narrow { display: none; }
  .composer-box .send-btn { margin-left: auto; }
}
.steps-list { padding-left: 1.3em; display: grid; gap: var(--s1); }

/* ---------- Usage ---------- */
.usage-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--s3); }
.usage-tile { padding: var(--s3) var(--s4); background: var(--bg); border-radius: var(--r-md); display: grid; gap: 2px; }
.usage-tile .k { font-size: var(--t-sm); color: var(--muted); }
.usage-tile .v { font-family: var(--font-display); font-size: var(--t-xl); font-weight: 600; font-variant-numeric: tabular-nums; }
.usage-tile .s { font-size: var(--t-sm); color: var(--muted); }
.usage-chart svg { display: block; width: 100%; height: auto; }
.usage-chart .grid { stroke: var(--line); stroke-width: 1; }
.usage-chart .axis { fill: var(--muted); font-size: 13px; font-family: var(--font); }
.usage-chart .bar { fill: var(--accent); }
.usage-chart .bar-hit { fill: transparent; cursor: pointer; outline: none; }
.usage-chart .bar-hit.is-active, .usage-chart .bar-hit:focus-visible { fill: color-mix(in srgb, var(--ink) 6%, transparent); }
.usage-table { width: 100%; border-collapse: collapse; font-size: var(--t-sm); font-variant-numeric: tabular-nums; }
.usage-table th, .usage-table td { text-align: left; padding: var(--s1) var(--s2); border-bottom: 1px solid var(--line); }
.usage-table th { color: var(--muted); font-weight: 500; }
.usage-table td.num, .usage-table th.num { text-align: right; }

/* ---------- Expressions ---------- */
.expr-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(136px, 1fr)); gap: var(--s4); }
.expr-slot { display: grid; gap: var(--s2); align-content: start; }
.expr-edit { display: grid; gap: var(--s2); justify-items: start; }
.expr-edit .actions { gap: var(--s1); }
.expr-edit .pick-hint:empty { display: none; }
.expr-thumb {
  display: grid; place-items: center; width: 112px; aspect-ratio: 1; overflow: hidden;
  border-radius: var(--r-md); background: var(--fill); color: var(--muted); font-size: var(--t-sm);
}
.expr-thumb img { width: 100%; height: 100%; object-fit: cover; }
.expr-thumb.is-empty { border: 1.5px dashed var(--line-strong); background: none; }
.expr-slot-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s1); min-height: 32px; }
.expr-slot-head .icon-btn { width: 32px; height: 32px; }
.expr-add { display: grid; gap: var(--s3); margin-top: var(--s5); padding-top: var(--s5); border-top: 1px solid var(--line); }
.expr-add .input-group { max-width: 360px; }
/* In chat: the face that matches the reply. The latest reply shows it large. */
.expr-face { display: block; width: var(--pic); height: var(--pic); border-radius: 50%; overflow: hidden; background: var(--fill); }
.expr-face img { width: 100%; height: 100%; object-fit: cover; }

.mood-chip { text-transform: lowercase; }

/* ---------- First-glance clarity ---------- */
/* Header tools carry a word, not just an icon, where there is room. */
.tool-label { display: none; }
@media (min-width: 1180px) {
  .chat-topbar .tool-btn { width: auto; gap: var(--s2); padding: 0 var(--s3); }
  .chat-topbar .tool-btn .tool-label { display: inline; font-size: var(--t-sm); }
  .chat-topbar .tool-btn.has-dot::after { top: 6px; right: 4px; }
}
/* Which model answers, always in view. */
.model-chip {
  display: inline-flex; align-items: center; gap: var(--s1); max-width: 220px; height: 36px; padding: 0 var(--s2) 0 var(--s3);
  border: 1.5px solid var(--line-strong); border-radius: var(--r-pill); background: none; color: var(--muted);
  font-size: var(--t-sm); cursor: pointer; transition: border-color var(--dur) var(--ease), color var(--dur) var(--ease);
}
@media (hover: hover) { .model-chip:hover { border-color: var(--ink); color: var(--ink); } }
.model-chip.is-custom { border-color: var(--accent); color: var(--accent); }
.model-chip .model-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-chip .chev { width: 14px; height: 14px; flex: none; transform: rotate(-90deg); }
.model-chip.is-loading { opacity: .6; pointer-events: none; }
/* What is happening in the background. */
.activity { display: inline-flex; align-items: center; gap: var(--s2); color: var(--ink); font-weight: 500; margin-left: auto; }
.spin { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--accent); border-right-color: transparent; animation: spin .8s linear infinite; flex: none; }
/* Working: a moon going through its phases. Still in reduced motion. */
.moon-phase { position: relative; width: 12px; height: 12px; border-radius: 50%; overflow: hidden; background: var(--accent); flex: none; }
.moon-phase::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%; background: var(--bg);
  animation: moon-phase 1.6s var(--ease) infinite;
}
@keyframes moon-phase { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) { .moon-phase::after { animation: none; transform: translateX(40%); } }
/* A cut-off reply says so, next to the fix. */
.cutoff-bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s2);
  margin-top: var(--s2); padding: var(--s2) var(--s2) var(--s2) var(--s3);
  background: var(--accent-soft); border-radius: var(--r-md); font-size: var(--t-sm);
}
.menu { max-height: min(62svh, 520px); overflow-y: auto; }

/* Journal */
.journal-list { display: grid; gap: var(--s3); max-height: 58svh; overflow-y: auto; }
.journal-entry { padding: var(--s4); background: var(--surface); border-radius: var(--r-md); }
.journal-entry header { display: flex; justify-content: space-between; gap: var(--s2); font-size: var(--t-sm); color: var(--muted); margin-bottom: var(--s2); }
.journal-entry .prose { font-family: var(--font-display); font-style: italic; font-size: var(--t-md); }

/* Swipe between versions on touch screens */
.msg.assistant { transition: transform 180ms var(--ease); touch-action: pan-y; }

/* ---------- Command palette ---------- */
.palette-btn { width: auto; padding: 0 var(--s2); gap: var(--s2); }
.palette-hint { display: none; }
@media (min-width: 1000px) {
  .palette-btn { padding: 0 7px 0 var(--s3); border: 1px solid var(--line); }
  .palette-hint { display: inline-flex; }
  /* Inside the pill the key is a pill too, the same distance from every edge. */
  .palette-hint kbd { height: 20px; padding: 0 var(--s2); border-radius: var(--r-pill); border-bottom: 0; }
}
kbd {
  display: inline-flex; align-items: center; min-width: 20px; height: 20px; padding: 0 var(--s1); justify-content: center;
  font-family: var(--mono); font-size: var(--t-xs); color: var(--muted);
  background: var(--fill); border-radius: var(--r-sm); border-bottom: 1px solid var(--line);
}
dialog.palette-dialog { width: min(600px, calc(100vw - 32px)); margin-top: 12vh; margin-bottom: auto; }
.palette { display: flex; flex-direction: column; max-height: min(560px, 72vh); }
.palette #pal-input {
  flex: none; height: 56px; padding: 0 var(--s5); border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
  background: none; font-size: var(--t-md); box-shadow: none;
}
.palette #pal-input:focus-visible { box-shadow: none; border-color: var(--line); }
@media (hover: hover) { .palette #pal-input:hover { box-shadow: none; border-color: var(--line); } }
.pal-list { list-style: none; margin: 0; padding: var(--s2); overflow-y: auto; flex: 1; }
.pal-group {
  padding: var(--s3) var(--s3) var(--s1); font-size: var(--t-xs); font-weight: 600;
  letter-spacing: .04em; text-transform: uppercase; color: var(--muted);
}
.pal-item {
  display: flex; align-items: center; gap: var(--s3); min-height: 40px; padding: var(--s2) var(--s3);
  border-radius: var(--r-md); cursor: pointer;
}
.pal-item .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pal-item .h { flex: none; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--t-sm); color: var(--muted); }
.pal-item[aria-selected="true"] { background: var(--accent-soft); }
.pal-item[aria-selected="true"] .t { color: var(--ink); font-weight: 600; }
.pal-foot { display: flex; gap: var(--s4); padding: var(--s3) var(--s5); border-top: 1px solid var(--line); }
.pal-foot span { display: inline-flex; align-items: center; gap: var(--s1); }
@media (hover: none) { .pal-foot { display: none; } }

/* ---------- Tour and what's new ---------- */
.tour { text-align: center; justify-items: center; }
.tour-art {
  width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.tour-art svg { width: 28px; height: 28px; }
.tour p { max-width: 38ch; color: var(--muted); }
#tour-action:empty { display: none; }
.tour-dots { display: flex; gap: var(--s2); }
.tour-dots span { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); transition: background var(--dur) var(--ease), width var(--dur) var(--ease); }
.tour-dots span.on { width: 24px; border-radius: var(--r-pill); background: var(--accent); }
.tour .dialog-actions { width: 100%; }
.news-list { list-style: none; display: grid; gap: var(--s4); }
.news-list li { display: grid; gap: var(--s1); padding-left: var(--s4); border-left: 2px solid var(--accent-soft); }
.news-list span { color: var(--muted); }

/* Notices on the home page */
.notice-card {
  display: flex; align-items: center; gap: var(--s4); flex-wrap: wrap;
  margin-bottom: var(--s4); padding: var(--s4) var(--s5);
  background: var(--surface); border-radius: var(--r-lg); box-shadow: var(--shadow-1);
}
.notice-icon {
  flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: var(--fill); color: var(--muted);
}
.notice-icon svg { width: 20px; height: 20px; }
.notice-card.warn .notice-icon { background: var(--accent-soft); color: var(--accent); }
.notice-card .grow { flex: 1 1 280px; min-width: 0; }
.notice-card .card-title { font-size: var(--t-md); }
.notice-card .actions { display: flex; align-items: center; gap: var(--s2); }

/* Settings: the card the palette sent you to */
@keyframes flash-card { 0%, 60% { box-shadow: 0 0 0 2px var(--accent); } 100% { box-shadow: var(--shadow-1); } }
.flash-card { animation: flash-card 1.6s var(--ease) both; scroll-margin-top: var(--s9); }
[id^="set-"] { scroll-margin-top: var(--s9); }

/* Favourites */
.fav-btn.is-on { color: var(--accent); }
.fav-btn.is-on svg path { fill: currentColor; }
.fav-btn:not(.is-on) { opacity: 0; }
.bot-card:focus-within .fav-btn { opacity: 1; }
@media (hover: hover) { .bot-card:hover .fav-btn { opacity: 1; } }
@media (hover: none) { .fav-btn:not(.is-on) { opacity: 1; } }

/* Dice */
.dice-chip { gap: var(--s1); color: var(--accent); background: var(--accent-soft); font-variant-numeric: tabular-nums; }
.dice-chip svg { width: 14px; height: 14px; }
#dice-roll svg { width: 16px; height: 16px; margin-right: var(--s2); }
@media (prefers-reduced-motion: reduce) { .flash-card { animation: none; box-shadow: 0 0 0 2px var(--accent); } }
.news-earlier { font-family: var(--font); font-size: var(--t-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }

/* Touch screens have no hover; hover colours would stick after a tap. They
   get a brief press colour instead, and no grey system flash. */
html { -webkit-tap-highlight-color: transparent; }
@media (hover: none) {
  .btn:active, .btn-quiet:active, .icon-btn:active, .icon-toggle:active, button.chip:active,
  .menu-item:active, .chat-list a:active, .main-nav a:active, .title-edit:active, .bond:active { background-color: var(--fill); }
  .segmented label:active span { background-color: var(--fill); }
  .btn-primary:active, .send-btn:active { background-color: var(--primary-hover); }
}

/* Phones zoom in on any field with text under 16px, and the page then slides
   around. Fields on touch screens are never smaller than that. */
@media (pointer: coarse) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea { font-size: max(16px, var(--t-base)) !important; }
  .composer-box textarea { font-size: max(16px, calc(var(--t-md) * var(--chat-scale))) !important; }

  /* Every control is at least 44 by 44 px to a finger (Apple's minimum, and
     WCAG 2.5.5). The drawn size stays the same: a clear border grows the hit
     area, the background stays inside it, and an equal negative margin keeps
     the layout where it was. --hit is (44 - drawn size) / 2. */
  .icon-btn, .icon-toggle, .send-btn, .bond, .brand, .title-edit, .avatar-link {
    --hit: 4px;
    box-sizing: content-box; border: var(--hit) solid transparent; margin: calc(-1 * var(--hit));
    background-clip: padding-box !important;
  }
  .send-btn, .composer-tool, .avatar-link { --hit: 2px; }
  .direct-bar .icon-btn, .suggest-bar .icon-btn, .expr-slot-head .icon-btn { --hit: 6px; }
  /* The pencil matches the 36px persona pill beside it. */
  .composer-foot .persona-edit { width: 36px; height: 36px; --hit: 4px; }
  /* Chips: 32px drawn (series chips 36px), 44px to a finger. */
  button.chip { box-sizing: content-box; border: 6px solid transparent; margin: -6px; background-clip: padding-box !important; }
  .series-filter button.chip { border-width: 4px; margin: -4px; }
  .palette-btn { border-width: 4px 5px; margin: -4px -5px; }
  .brand { --hit: 9px; margin-inline: 0; border-inline-width: 0; }
  /* The chat name sits in a clipped box, so its extra area goes below, over the subtitle. */
  .title-edit { border-width: 0 0 12px; margin: 0 0 -12px calc(-1 * var(--s2)); }
  .swipes > span { pointer-events: none; }
  .site-footer a { display: inline-block; padding-block: 12px; margin-block: -12px; }
  .msg > .avatar-link { --hit: max(0px, calc((44px - var(--pic)) / 2)); }
  /* The subtitle under the chat name lets taps through to the name. */
  .chat-topbar .title small { pointer-events: none; }
  /* Page tabs: the pill stays 36 px tall; the clear border adds the rest. */
  .main-nav a { box-sizing: content-box; border: 4px solid transparent; background-clip: padding-box; }
  /* Small buttons keep their look; an invisible layer takes taps around them. */
  .btn-sm { position: relative; }
  .btn-sm::before { content: ""; position: absolute; inset: -7px -2px; }
  /* Drawn larger: these keep their own border, so they cannot grow unseen. */
  .btn, input[type="text"], input[type="url"], input[type="password"], input[type="search"], input[type="number"], select { height: 44px; }
  /* Drawn 36px; a clear 4px border makes the tap area 44px without moving anything. */
  .composer-foot select { height: 44px; border: 4px solid transparent; margin-block: -4px; background-clip: padding-box; background-position: calc(100% - 12px) center; }
  .btn-sm { height: 32px; }
  /* Segments: a little taller, and each one takes taps over the frame's padding. */
  .segmented span { height: 35px; }
  .segmented label::before, .segmented label::after { content: ""; position: absolute; left: 0; right: 0; height: 3px; }
  .segmented label::before { top: -3px; }
  .segmented label::after { bottom: -3px; }
}

/* Memory dialog: facts, story and chapters, newest chapter first. */
.memory-status { font-size: var(--t-sm); color: var(--ink); padding: var(--s3) var(--s4); background: var(--fill); border-radius: var(--r-md); }
.memory-status:empty { display: none; }
.memory-dialog textarea { min-height: 96px; }
.chapters { display: grid; gap: var(--s2); }
.chapters .chapter { border-top: 1px solid var(--line); padding-top: var(--s2); }
.chapters .chapter:first-child { border-top: 0; padding-top: 0; }
.chapters .chapter summary { display: flex; justify-content: space-between; gap: var(--s3); font-weight: 500; }
.chapters .chapter textarea { margin-top: var(--s2); }

/* A long chat draws its latest messages; older ones load on request. */
.earlier { display: flex; flex-direction: column; align-items: center; gap: var(--s1); padding-bottom: var(--s4); text-align: center; }

/* Import dialog: three ways in, each a short section with one action. */
.import-way { display: grid; gap: var(--s2); padding-top: var(--s4); border-top: 1px solid var(--line); }
.import-way h3 { font-size: var(--t-md); font-weight: 600; }
.import-way > .btn { justify-self: start; }
.drop-zone {
  display: flex; align-items: center; justify-content: center; gap: var(--s3); min-height: 88px; padding: var(--s4);
  border: 1.5px dashed var(--line-strong); border-radius: var(--r-md); background: none;
  color: var(--muted); font: inherit; font-size: var(--t-ui); cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.drop-zone strong { color: var(--ink); font-weight: 600; }
@media (hover: none) { .drop-hint { display: none; } } /* no dragging on a touch screen */
.drop-zone svg { width: 20px; height: 20px; flex: none; }
.drop-zone.is-over { background: var(--fill); border-color: var(--ink); }
@media (hover: hover) { .drop-zone:hover { background: var(--fill); border-color: var(--ink); } }
.inline-form { display: flex; gap: var(--s2); }
.inline-form input { flex: 1; min-width: 0; }
/* Files dragged over the bots page. */
.drop-overlay {
  position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; padding: var(--s4);
  background: color-mix(in srgb, var(--bg) 82%, transparent); pointer-events: none;
}
.drop-overlay > div {
  display: grid; justify-items: center; gap: var(--s3); padding: var(--s6) var(--s7);
  border: 2px dashed var(--ink); border-radius: var(--r-lg); background: var(--surface);
  font-size: var(--t-md); font-weight: 600; color: var(--ink);
}
.drop-overlay svg { width: 28px; height: 28px; }
.import-help { padding-top: var(--s4); border-top: 1px solid var(--line); }
.import-help .steps-list { margin: var(--s3) 0; font-size: var(--t-ui); }
