/* =============================================================================
   Did They Read? — Reading Journal Maker — UI
   Mobile-first, modern design. "Desert Dusk" golden-hour sunset theme: warm
   cream + charcoal slate with rust-orange, teal, and gold accents. Switches and
   segmented controls drawn from Apple HIG; tonal buttons/elevation from
   Material 3. Light + dark mode via prefers-color-scheme.
   ============================================================================= */

:root {
  /* Brand / accent — "Desert Dusk" golden-hour sunset */
  --accent: #e76f51;          /* rust-orange (primary CTA) */
  --accent-press: #d65a3c;
  --accent-soft: rgba(231, 111, 81, 0.12);
  --accent-2: #2a9d8f;        /* teal — links, success, switches */
  --gold: #e9c46a;            /* warm gold — badges, highlights */
  --danger: #e63946;          /* sunset red — errors, destructive */

  /* Neutrals (light) — warm cream + charcoal slate */
  --bg: #faf3e0;              /* warm cream */
  --surface: #ffffff;
  --surface-2: #fbefe2;       /* warm tint */
  --text: #2a3840;            /* charcoal slate */
  --text-2: #5c6b73;
  --text-3: #93a1a8;
  --border: rgba(42, 56, 64, 0.12);
  --border-strong: rgba(42, 56, 64, 0.22);
  --shadow: 0 1px 2px rgba(64,40,20,.05), 0 8px 24px rgba(64,40,20,.08);
  --shadow-lg: 0 4px 12px rgba(64,40,20,.10), 0 16px 40px rgba(64,40,20,.12);

  --radius: 16px;
  --radius-sm: 11px;
  --radius-pill: 999px;

  --appbar-h: 56px;
  --tabs-h: 52px;
  --maxw: 1080px;

  --ease: cubic-bezier(.4, 0, .2, 1);

  font-synthesis: none;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #f2856a;        /* brighter rust for dark backgrounds */
    --accent-press: #e76f51;
    --accent-soft: rgba(242, 133, 106, 0.22);
    --accent-2: #4cc0b0;
    --gold: #edd08a;
    --danger: #ff6b6b;
    --bg: #1b2a30;            /* deep slate */
    --surface: #233a44;
    --surface-2: #2c4854;
    --text: #ffe3d3;          /* cream */
    --text-2: #cdbbae;
    --text-3: #93a1a8;
    --border: rgba(255, 227, 211, 0.12);
    --border-strong: rgba(255, 227, 211, 0.22);
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.5);
    --shadow-lg: 0 4px 12px rgba(0,0,0,.5), 0 16px 40px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
               Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------------------------------------------------------------- App bar -- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.appbar-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  height: var(--appbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 11px;
  color: #fff;
  background: var(--accent);
  box-shadow: 0 4px 12px rgba(231,111,81,.35);
}
.brand-text h1 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.brand-text p { margin: 0; font-size: 12px; color: var(--text-2); }

/* ------------------------------------------------------------------ Hero -- */
/* Golden-hour value-prop band above the tool. */
.hero {
  position: relative;
  overflow: hidden;
  color: #fff;
  background: var(--accent);
}
.hero-inner {
  position: relative;
  z-index: 1;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 26px 16px 30px;
}
.hero-badge {
  display: inline-block;
  font-size: 11.5px; font-weight: 700; letter-spacing: .05em;
  text-transform: uppercase;
  background: rgba(255,255,255,.22);
  padding: 5px 11px; border-radius: var(--radius-pill);
  margin-bottom: 12px;
}
.hero-title {
  margin: 0 0 8px;
  font-size: 27px; font-weight: 800; letter-spacing: -.02em;
  line-height: 1.12;
  text-shadow: 0 1px 2px rgba(120,50,20,.18);
}
.hero-title em { font-style: italic; }
.hero-sub {
  margin: 0; max-width: 54ch;
  font-size: 15px; line-height: 1.5;
  color: rgba(255,255,255,.94);
}
@media (min-width: 860px) {
  .hero-inner { padding: 40px 16px 46px; }
  .hero-title { font-size: 36px; }
  .hero-sub { font-size: 16px; }
}

/* ------------------------------------------------------------------ Tabs -- */
/* Visible only on mobile; desktop shows both panels side-by-side. */
.tabs {
  position: sticky;
  top: calc(var(--appbar-h) + env(safe-area-inset-top));
  z-index: 20;
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.tab {
  flex: 1;
  appearance: none;
  border: none;
  background: var(--surface-2);
  color: var(--text-2);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 9px 12px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background .2s var(--ease), color .2s var(--ease);
}
.tab.is-active { background: var(--accent); color: #fff; }

/* ---------------------------------------------------------------- Layout -- */
.layout {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 12px 16px calc(40px + env(safe-area-inset-bottom));
  display: block;
}

.panel { display: block; }
.panel-create { display: flex; flex-direction: column; gap: 24px; }

/* On mobile only one panel shows at a time. */
@media (max-width: 859px) {
  .panel[data-panel]:not(.is-active) { display: none; }
}

/* ----------------------------------------------------------------- Cards -- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-2);
}
/* Numbered step chip in the create flow. */
.step-num {
  flex: none;
  width: 22px; height: 22px;
  display: inline-grid; place-items: center;
  font-size: 12px; font-weight: 800;
  letter-spacing: 0;
  color: #fff;
  background: var(--accent);
  border-radius: 50%;
  box-shadow: 0 2px 6px rgba(231,111,81,.3);
}

/* ---------------------------------------------------------------- Fields -- */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field label,
.toggle-label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.muted { color: var(--text-3); font-weight: 400; }

input[type="text"],
input[type="number"],
input[type="date"],
textarea {
  width: 100%;
  font: inherit;
  font-size: 16px;            /* >=16px prevents iOS zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
  -webkit-appearance: none;
  appearance: none;
}
textarea { resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--text-3); }

input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
  background: var(--surface);
}
input[type="date"] { min-height: 46px; }

/* Stepper (chapter count) */
.stepper {
  display: flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.stepper input {
  border: none;
  background: transparent;
  text-align: center;
  box-shadow: none;
  border-radius: 0;
  font-weight: 600;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper-btn {
  flex: 0 0 52px;
  align-self: stretch;
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s var(--ease);
}
.stepper-btn:active { background: var(--accent-soft); }

/* --------------------------------------------------------- Segmented ctl -- */
.segmented {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 3px;
  isolation: isolate;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  position: relative;
  z-index: 2;
  margin: 0;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-2);
  padding: 8px 10px;
  border-radius: calc(var(--radius-sm) - 3px);
  cursor: pointer;
  transition: color .2s var(--ease);
}
.segmented-thumb {
  position: absolute;
  z-index: 1;
  top: 3px; bottom: 3px; left: 3px;
  width: calc(50% - 3px);
  background: var(--surface);
  border-radius: calc(var(--radius-sm) - 3px);
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
  transition: transform .25s var(--ease);
}
#ps-a5:checked ~ .segmented-thumb { transform: translateX(100%); }
#ps-letter:checked ~ label[for="ps-letter"],
#ps-a5:checked ~ label[for="ps-a5"] { color: var(--text); }

.hint { margin: 10px 2px 0; font-size: 13px; color: var(--text-2); }

/* ----------------------------------------------------- Toggle (iOS switch) */
.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.toggle:first-of-type { padding-top: 0; }
.toggle:last-of-type { padding-bottom: 0; border-bottom: none; }
.toggle-text { display: flex; flex-direction: column; }
.toggle-label { margin: 0; }
.toggle-sub { font-size: 12.5px; color: var(--text-2); margin-top: 2px; }

.switch { position: relative; flex: 0 0 auto; width: 51px; height: 31px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track {
  position: absolute; inset: 0;
  background: var(--border-strong);
  border-radius: var(--radius-pill);
  transition: background .25s var(--ease);
}
.switch-track::before {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 27px; height: 27px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0,0,0,.25);
  transition: transform .25s var(--ease);
}
.switch input:checked + .switch-track { background: var(--accent-2); }   /* teal */
.switch input:checked + .switch-track::before { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 4px var(--accent-soft); }

/* --------------------------------------------------------------- Buttons -- */
.actions { display: flex; flex-direction: column; gap: 10px; }
.actions-split { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  appearance: none;
  border: none;
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  min-height: 48px;                 /* comfortable touch target */
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .08s var(--ease), background .15s var(--ease), opacity .15s var(--ease);
  user-select: none;
}
.btn:active { transform: scale(.975); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 4px 14px rgba(231,111,81,.32); }
.btn-primary:active { background: var(--accent-press); }
/* The headline output: clearly the file to print. */
.btn-download {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1.5px solid var(--accent);
  font-weight: 700;
}
.btn-download:active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.btn-tonal { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn[disabled] { opacity: .5; cursor: progress; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: "";
  position: absolute;
  width: 18px; height: 18px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  color: #fff;
  animation: spin .7s linear infinite;
}
.btn-tonal.is-busy::after,
.btn-download.is-busy::after { color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }

.actions-hint { margin-top: 12px; line-height: 1.5; }
.actions-hint strong { color: var(--text); }

/* ---------------------------------------------------- Question editor list -- */
.qlist-hint { margin-top: 0; margin-bottom: 12px; line-height: 1.5; }

/* AI entry point (nudge) at the top of the Questions step. */
.ai-callout {
  display: flex;
  gap: 12px;
  padding: 14px;
  margin-bottom: 16px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
}
.ai-callout-icon { font-size: 22px; line-height: 1.25; flex: none; }
.ai-callout-body { flex: 1; min-width: 0; }
.ai-callout-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; color: var(--text); }
.ai-callout-text { margin: 0 0 12px; font-size: 13px; line-height: 1.5; color: var(--text-2); }
.ai-callout-btn { width: 100%; }
.qlist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.qrow {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.qrow-input {
  width: 100%;
  min-width: 0;
  resize: none;
  overflow: hidden;            /* height is auto-grown by JS */
  line-height: 1.4;
  background: var(--surface);   /* stand out against the row's tinted background */
}
/* Equal-width buttons spanning the full width of the text box, styled to match it. */
.qrow-ctrls {
  display: flex;
  gap: 8px;
}
.qrow-btn {
  flex: 1 1 0;
  height: 40px;
  padding: 0 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 13px; font-weight: 600; line-height: 1;
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);   /* same rounded corners as the text box */
  cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease), border-color .15s var(--ease), opacity .15s var(--ease);
}
.qrow-btn:hover { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.qrow-btn:disabled { opacity: .35; cursor: default; }
.qrow-btn:disabled:hover { background: var(--surface); color: var(--text-2); border-color: var(--border); }
.qrow-del:hover { background: rgba(230,57,70,.12); color: var(--danger); border-color: var(--danger); }

.qlist-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
}
.qlist-toolbar .btn { flex: 1 1 auto; min-height: 42px; font-size: 14px; }
.btn-link {
  appearance: none; border: none; background: none;
  font: inherit; font-size: 13px; font-weight: 600;
  color: var(--accent); cursor: pointer; padding: 8px 4px;
  flex: 0 0 auto;
}
.btn-link:active { opacity: .6; }
.qlist-empty { margin-top: 10px; font-style: italic; }

.status { margin: 12px 2px 0; font-size: 13.5px; color: var(--text-2); min-height: 18px; }
.status.error { color: var(--danger); }
.status.ok { color: var(--accent-2); }

/* ------------------------------------------------------------------ Help -- */
.help summary {
  cursor: pointer; font-weight: 600; font-size: 14px; color: var(--accent);
  list-style: none;
}
.help summary::-webkit-details-marker { display: none; }
.help summary::before { content: "ⓘ "; }
.help[open] summary { margin-bottom: 8px; }
.help ol { margin: 0; padding-left: 20px; line-height: 1.7; font-size: 14px; color: var(--text); }

/* --------------------------------------------------------------- Preview -- */
.preview-card { padding: 14px; }
.preview-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.preview-head .card-title { margin: 0; }
.chip {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--accent); font: inherit; font-weight: 600; font-size: 13px;
  padding: 6px 14px; border-radius: var(--radius-pill); cursor: pointer;
}
.chip:active { background: var(--accent-soft); }

.preview-stage {
  position: relative;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  overflow: hidden;
  min-height: 60vh;
}
.preview-empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; color: var(--text-3); text-align: center; padding: 24px;
}
.preview-empty p { margin: 0; font-size: 14px; }
#preview-frame { display: block; width: 100%; height: 100%; min-height: 60vh; border: none; background: #e9e9ec; }

.hidden { display: none !important; }
/* The [hidden] attribute must win over component display rules (e.g. .btn). */
[hidden] { display: none !important; }

/* =========================================================== Desktop ≥860 */
@media (min-width: 860px) {
  .tabs { display: none; }
  .layout {
    display: grid;
    grid-template-columns: minmax(360px, 420px) 1fr;
    gap: 20px;
    padding-top: 20px;
    align-items: start;
  }
  /* Both panels always visible on desktop. */
  .panel[data-panel] { display: block; }
  /* More breathing room *between* the stacked cards (card padding stays normal).
     Note: needs equal/greater specificity than '.panel[data-panel]' above, which
     otherwise forces display:block and kills the flex gap. */
  .panel-create[data-panel] { display: flex; flex-direction: column; gap: 24px; }
  .panel-preview { position: sticky; top: calc(var(--appbar-h) + 20px); }
  .preview-stage, #preview-frame { min-height: calc(100vh - var(--appbar-h) - 120px); }
}

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

.help-note { margin: 10px 0 0; font-size: 13px; color: var(--text-2); line-height: 1.5; }

/* ====================================================== AI "Generate" modal */
.ai-modal {
  width: min(540px, calc(100vw - 24px));
  max-height: min(88vh, 760px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.ai-modal::backdrop { background: rgba(20, 14, 8, .5); backdrop-filter: blur(2px); }
.ai-modal[open] { animation: ai-pop .18s var(--ease); }
@keyframes ai-pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.ai-modal-inner {
  display: flex;
  flex-direction: column;
  max-height: inherit;
  overflow-y: auto;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}
.ai-modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ai-modal-head h2 { margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -.01em; }
.ai-close {
  appearance: none; border: none; background: var(--surface-2); color: var(--text-2);
  width: 32px; height: 32px; border-radius: 50%; font-size: 15px; cursor: pointer;
  flex: none; display: grid; place-items: center;
}
.ai-close:hover { background: var(--accent-soft); color: var(--accent); }
.ai-intro { margin-top: 8px; line-height: 1.5; }

/* Search + results */
.ai-search { position: relative; }
.ai-results {
  list-style: none; margin: 6px 0 0; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow); max-height: 260px; overflow-y: auto;
}
.ai-result {
  display: flex; align-items: center; gap: 10px; padding: 8px;
  border-radius: var(--radius-sm); cursor: pointer;
}
.ai-result:hover { background: var(--accent-soft); }
.ai-result-cover, .ai-selected-cover {
  width: 36px; height: 52px; object-fit: cover; border-radius: 4px; flex: none;
  background: var(--surface-2); box-shadow: 0 1px 3px rgba(0,0,0,.18);
}
.ai-result-cover--blank { display: inline-block; }
.ai-result-text { display: flex; flex-direction: column; min-width: 0; }
.ai-result-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-result-author { font-size: 12.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Selected book chip */
.ai-selected {
  display: flex; align-items: center; gap: 12px; margin-bottom: 14px;
  padding: 10px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.ai-selected-text { flex: 1; min-width: 0; }
.ai-selected-title { font-size: 14.5px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-selected-author { font-size: 12.5px; color: var(--text-2); }
.ai-selected-clear {
  appearance: none; border: none; background: transparent; color: var(--text-2);
  font-size: 15px; cursor: pointer; flex: none; padding: 4px;
}
.ai-selected-clear:hover { color: var(--danger); }

/* Select styled like the text inputs */
.ai-select {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 14px; cursor: pointer;
  -webkit-appearance: none; appearance: none;
}
.ai-select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }

/* AI segmented: thumb driven by a JS class instead of paper-size ids */
.ai-segmented input:checked + label { color: var(--text); }
.ai-segmented.is-second .segmented-thumb { transform: translateX(100%); }

.ai-toggle { padding-top: 0; border-bottom: none; }
.ai-chapter-input { margin-top: 12px; margin-bottom: 0; }

/* Question-type chips */
.ai-types { border: none; padding: 0; margin: 0 0 4px; }
.ai-types legend { font-size: 13px; font-weight: 600; margin-bottom: 6px; padding: 0; }
.ai-types-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
@media (min-width: 460px) { .ai-types-grid { grid-template-columns: repeat(3, 1fr); } }
.ai-chip { position: relative; margin: 0; }
.ai-chip input { position: absolute; opacity: 0; pointer-events: none; }
.ai-chip span {
  display: block; text-align: center; font-size: 13px; font-weight: 600;
  color: var(--text-2); padding: 9px 8px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border); cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease), border-color .15s var(--ease);
}
.ai-chip input:checked + span { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.ai-chip input:focus-visible + span { box-shadow: 0 0 0 4px var(--accent-soft); }

.ai-modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }

/* ============================== Question list cap + "show all" popover */
/* Inline preview is capped to the first 5 rows (keep in sync with INLINE_CAP
   in app.js); the rest live in the popover. */
.qlist.is-capped > .qrow:nth-child(n + 6) { display: none; }

.qlist-showall {
  appearance: none;
  width: 100%;
  margin-top: 12px;
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
  color: var(--accent);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s var(--ease), border-color .15s var(--ease);
}
.qlist-showall:hover { background: var(--accent-soft); border-color: var(--accent); }

/* Popover chrome mirrors the AI modal, but the body hosts the moved #qlist and
   scrolls so a long list never grows the dialog past the viewport. */
.q-modal {
  width: min(560px, calc(100vw - 24px));
  max-height: min(88vh, 800px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.q-modal::backdrop { background: rgba(20, 14, 8, .5); backdrop-filter: blur(2px); }
.q-modal[open] { animation: ai-pop .18s var(--ease); }
.q-modal-inner {
  display: flex;
  flex-direction: column;
  max-height: inherit;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
}
.q-modal-body {
  flex: 1;
  min-height: 0;            /* allow the flex child to shrink and scroll */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin-top: 10px;
}
.q-modal-foot {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
