/* public/styles.css
   Design tokens first (colors, type, spacing), then layout, then components.
   Light mode is warm paper; dark mode is ink. Every color comes from a token,
   so changing the look means editing the :root block only. */

:root {
  --paper: #f7f2ea;
  --surface: #fffdf9;
  --surface-2: #f0e8dc;
  --ink: #241c1a;
  --ink-soft: #5d514b;
  --line: #ddd1c2;
  --accent: #a3412f;
  --accent-ink: #ffffff;
  --accent-soft: #f3ddd5;
  --focus: #2d5bd7;
  --danger: #a3212f;
  --ok: #2f6b3d;

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --radius: 12px;
  --radius-pill: 999px;
  --shadow: 0 1px 2px rgb(36 28 26 / 6%), 0 8px 24px rgb(36 28 26 / 6%);
  --gutter: clamp(16px, 4vw, 40px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #161211;
    --surface: #1f1918;
    --surface-2: #2a2220;
    --ink: #efe6dc;
    --ink-soft: #b6a79b;
    --line: #3b312e;
    --accent: #e0846f;
    --accent-ink: #1b1110;
    --accent-soft: #3a2420;
    --focus: #8fb0ff;
    --danger: #ff8a8a;
    --ok: #8bd39b;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
  }
}

/* ---------- base ---------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 1rem/1.55 var(--sans); /* rem: scales with the Text size setting */
}

[hidden] { display: none !important; }

a { color: var(--accent); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}
/* Headings get focus when a view opens (so screen readers announce it); no ring needed there. */
[tabindex="-1"]:focus { outline: none; }

.skip-link {
  position: absolute;
  left: 8px;
  top: -48px;
  padding: 8px 12px;
  background: var(--surface);
  border-radius: 8px;
  z-index: 10;
}
.skip-link:focus { top: 8px; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }

.display {
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(2rem, 5vw, 2.8rem);
  letter-spacing: -0.01em;
}
.display em, .brand em { color: var(--accent); }

.lede { font-size: 1.1rem; margin: 0 0 1.5rem; }
.muted { color: var(--ink-soft); }
.small { font-size: .875rem; }

/* ---------- layout ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px var(--gutter);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-family: var(--serif);
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--ink);
  text-decoration: none;
}
.topbar nav { display: flex; align-items: center; gap: 16px; }
.topbar nav a:not(.button) { color: var(--ink); text-decoration: none; }
.topbar nav a[aria-current="page"]:not(.button) { text-decoration: underline; text-underline-offset: 6px; }

main { outline: none; }

.view {
  max-width: 980px;
  margin: 0 auto;
  padding: 32px var(--gutter) 96px;
}
.view-narrow { max-width: 440px; padding-top: 12vh; }
.view-reading { max-width: 760px; }

.view-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px 16px;
}

/* ---------- forms ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.field { margin: 0 0 14px; border: 0; padding: 0; min-width: 0; }
.field > label, .field > legend, .chip-group > legend {
  display: block;
  font-weight: 600;
  font-size: .9rem;
  margin-bottom: 6px;
}

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  min-height: 44px; /* comfortable touch target */
}
textarea { resize: vertical; line-height: 1.5; }
input:hover, select:hover, textarea:hover { border-color: var(--ink-soft); }

.form-error { color: var(--danger); font-weight: 600; min-height: 1.5em; margin: 8px 0; }
.form-error:empty { min-height: 0; margin: 0; }
.counter { margin: 4px 0 0; font-size: .8rem; color: var(--ink-soft); text-align: right; }
.optional { font-size: .8rem; font-weight: 500; color: var(--ink-soft); font-family: var(--sans); }

/* ---------- buttons ---------- */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 18px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: var(--accent-ink);
  font: 600 1rem/1 var(--sans);
  text-decoration: none;
  cursor: pointer;
}
.button:hover { filter: brightness(1.06); }
.button:disabled { opacity: .55; cursor: not-allowed; filter: none; }
.button-small { min-height: 36px; padding: 6px 14px; font-size: .9rem; }
.button-large { min-height: 52px; padding: 14px 28px; font-size: 1.1rem; }
.button-ghost { background: transparent; color: var(--accent); }
.button-quiet {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--line);
}
.button-danger { background: transparent; color: var(--danger); border-color: var(--danger); }

.link-button {
  background: none;
  border: 0;
  padding: 4px;
  font: inherit;
  color: var(--ink-soft);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- builder ---------- */

.step { margin: 36px 0; }
.step > h2 {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--serif);
  font-size: 1.6rem;
}
.step-number {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font: 700 1rem/1 var(--sans);
}

.character {
  margin: 0 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 18px 20px 6px;
  min-width: 0;
}
.character > legend {
  float: left; /* puts the legend inside the box so the header row lays out normally */
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
  margin-bottom: 12px;
  font-family: var(--serif);
  font-size: 1.25rem;
  font-weight: 600;
}
.character > legend + * { clear: both; }
.character > legend .link-button { font: 500 .9rem var(--sans); }

details.section { border-top: 1px solid var(--line); padding: 10px 0 4px; }
details.section > summary {
  cursor: pointer;
  font-weight: 600;
  padding: 6px 0;
  list-style-position: outside;
  margin-left: 18px;
}
details.section[open] > summary { margin-bottom: 10px; }

.chip-group { border: 0; padding: 0; margin: 0 0 14px; min-width: 0; }
.chip-group > legend small { font-weight: 500; color: var(--ink-soft); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip { position: relative; }
.chip input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  min-height: 0;
}
.chip span {
  display: inline-block;
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--surface);
  font-size: .9rem;
  cursor: pointer;
  user-select: none;
}
.chip input:hover + span { border-color: var(--ink-soft); }
.chip input:checked + span {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.chip input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.chip input:disabled + span { opacity: .4; cursor: not-allowed; }

.theme-group h3 { font-size: .95rem; margin: 18px 0 8px; color: var(--ink-soft); }

.heat { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.heat label, .radio-row label {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
}
.heat label:has(input:checked), .radio-row label:has(input:checked) {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.heat input, .radio-row input { width: auto; min-height: 0; margin-top: 3px; accent-color: var(--accent); }
.heat strong { display: block; }
.heat small { color: var(--ink-soft); }
.radio-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; }

.submit-row { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; }

/* ---------- library ---------- */

.story-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.story-item { display: flex; gap: 8px; align-items: stretch; }
.story-item > a, .story-item > .item-body { flex: 1; min-width: 0; }
.story-item a, .story-item .item-body {
  display: grid;
  gap: 4px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--ink);
  text-decoration: none;
}
.story-item a:hover { border-color: var(--accent); }
.story-item .title { font-family: var(--serif); font-size: 1.3rem; font-weight: 600; }
.story-item .meta { font-size: .9rem; color: var(--ink-soft); }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: var(--radius-pill);
  font-size: .78rem;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--ink-soft);
}
.badge-writing { background: var(--accent-soft); color: var(--accent); }
.badge-error { color: var(--danger); }

.empty {
  text-align: center;
  padding: 48px 20px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

/* ---------- reader ---------- */

.story-head { margin-bottom: 24px; }
.story-head h1 { font-size: clamp(2.2rem, 6vw, 3.2rem); }
.story-head .meta { color: var(--ink-soft); }
.title-row { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; }

.status {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  margin: 16px 0;
  border-radius: 10px;
  background: var(--accent-soft);
}
.status-error { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.spinner {
  width: 18px;
  height: 18px;
  flex: none;
  border: 3px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin .9s linear infinite;
  color: var(--accent);
}
@keyframes spin { to { transform: rotate(360deg); } }

.part-title {
  font-family: var(--serif);
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--accent);
  text-align: center;
  margin: 56px 0 8px;
}
.part-subtitle { text-align: center; font-family: var(--serif); font-size: 1.6rem; margin: 0 0 24px; }

.scene { margin: 0 0 48px; }
.scene h3 { font-family: var(--serif); font-size: 1.45rem; margin-top: 32px; }
.prose {
  font-family: var(--serif);
  font-size: 1.15rem;
  line-height: 1.75;
}
.prose p { margin: 0 0 1.05em; }
.scene-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.scene-divider { text-align: center; color: var(--ink-soft); letter-spacing: .6em; margin: 0 0 40px; }

.reframe-form {
  margin-top: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}

.panel {
  margin: 48px 0;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.panel h2 { font-family: var(--serif); font-size: 1.5rem; }

.danger-zone { display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; margin-top: 48px; }

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

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--paper);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.toast.show { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 3s; }
  .toast { transition: none; }
}

@media (max-width: 560px) {
  .topbar nav { gap: 10px; }
  .topbar nav a[data-nav="library"] { display: none; } /* the brand link already goes to the library */
  .prose { font-size: 1.08rem; }
}

/* ---------- character roles ---------- */

.add-row { display: flex; flex-wrap: wrap; gap: 10px; }
.character.is-supporting { border-style: dashed; }
.supporting-fields { margin-bottom: 8px; }
.check {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 6px 0 14px;
  cursor: pointer;
}
.check input { width: auto; min-height: 0; margin-top: 4px; accent-color: var(--accent); }

details.theme-group { border-top: 1px solid var(--line); padding: 8px 0; }
details.theme-group > summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
details.theme-group .chips { padding: 6px 0 4px; }

/* ---------- ratings and favorites ---------- */

.scene-feedback { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 8px; }
.flames { display: inline-flex; gap: 2px; }
.flame {
  border: 0;
  background: none;
  padding: 6px 4px;
  min-width: 36px;
  min-height: 40px;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  filter: grayscale(1);
  opacity: .35;
  transition: transform .12s, opacity .12s, filter .12s;
}
.flame.lit { filter: none; opacity: 1; }
.flame:hover { transform: scale(1.15); opacity: .8; }
.save-button.saved { color: var(--accent); border-color: var(--accent); }
.story-item .excerpt { font-family: var(--serif); color: var(--ink-soft); font-size: .95rem; }

/* ---------- loading fruit ---------- */

.fruit { width: 72px; height: 86px; flex: none; overflow: visible; }
.fruit-shadow { fill: currentColor; opacity: .12; }
.fruit-caption { color: var(--accent); }

/* Banana: three flaps of peel open, hold, close; the whole thing bobs. */
.banana-body { animation: bob 3s ease-in-out infinite; transform-box: fill-box; transform-origin: 50% 100%; }
.flap { transform-box: fill-box; }
.flap-left { transform-origin: 85% 100%; animation: peel-left 3s ease-in-out infinite; }
.flap-right { transform-origin: 15% 100%; animation: peel-right 3s ease-in-out .12s infinite; }
.flap-front { transform-origin: 50% 100%; animation: peel-front 3s ease-in-out .25s infinite; }
@keyframes peel-left { 0%, 12% { transform: rotate(0); } 42%, 74% { transform: rotate(-64deg); } 96%, 100% { transform: rotate(0); } }
@keyframes peel-right { 0%, 12% { transform: rotate(0); } 42%, 74% { transform: rotate(64deg); } 96%, 100% { transform: rotate(0); } }
@keyframes peel-front { 0%, 18% { transform: scaleY(1); } 48%, 74% { transform: scaleY(-.5); } 96%, 100% { transform: scaleY(1); } }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }

/* Peach: a soft squeeze, a light sweep across the skin, a drip and some sparkle. */
.peach-body { transform-box: fill-box; transform-origin: 50% 100%; animation: squeeze 2.8s ease-in-out infinite; }
.shine-band { transform-box: fill-box; transform-origin: center; animation: glisten 2.8s ease-in-out infinite; }
.highlight { opacity: .5; animation: gleam 2.8s ease-in-out infinite; }
.droplet { transform-box: fill-box; animation: drip 2.8s ease-in infinite; }
.sparkle { transform-box: fill-box; transform-origin: center; animation: twinkle 2.8s ease-in-out infinite; }
.sparkle.s2 { animation-delay: .9s; }
.sparkle.s3 { animation-delay: 1.7s; }
@keyframes squeeze { 0%, 100% { transform: scale(1, 1); } 45% { transform: scale(1.04, .97); } 60% { transform: scale(.98, 1.02); } }
@keyframes glisten { 0%, 20% { transform: translateX(0) rotate(20deg); } 70%, 100% { transform: translateX(115px) rotate(20deg); } }
@keyframes gleam { 0%, 100% { opacity: .35; } 50% { opacity: .75; } }
@keyframes drip { 0%, 30% { transform: translateY(0); opacity: 0; } 45% { opacity: .9; } 90% { transform: translateY(20px); opacity: .8; } 100% { transform: translateY(24px); opacity: 0; } }
@keyframes twinkle { 0%, 100% { transform: scale(0); opacity: 0; } 15% { transform: scale(1.1) rotate(20deg); opacity: 1; } 35% { transform: scale(0); opacity: 0; } }

/* People who ask for less motion get a still picture: banana half peeled, peach shining. */
@media (prefers-reduced-motion: reduce) {
  .fruit * { animation: none !important; }
  .flap-left { transform: rotate(-40deg); }
  .flap-right { transform: rotate(40deg); }
  .sparkle.s1 { transform: scale(1); }
}

/* ---------- lists with delete buttons ---------- */

.item-delete {
  flex: none;
  min-width: 72px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--danger);
  font: 600 .85rem var(--sans);
  cursor: pointer;
}
.item-delete:hover { border-color: var(--danger); }
.item-delete:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- saved characters ---------- */

.character > legend { flex-wrap: wrap; gap: 6px; }
.card-tools { display: flex; flex-wrap: wrap; gap: 14px; }
.saved-picker {
  margin-top: 14px;
  padding: 14px 16px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.saved-picker > label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 8px; }
.saved-picker select { flex: 1 1 220px; max-width: 420px; }

/* ---------- account and text size ---------- */

.view-narrow-wide { max-width: 640px; }
.text-stepper { display: inline-flex; gap: 6px; margin-left: auto; }
.text-stepper .link-button {
  font: 600 .95rem var(--sans);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 10px;
  color: var(--ink-soft);
}

/* The top bar wraps onto two rows on small screens instead of overflowing. */
.topbar, .topbar nav { flex-wrap: wrap; }
@media (max-width: 560px) {
  .topbar nav { gap: 6px 12px; font-size: .95rem; }
  #logout { display: none; } /* on phones, Log out lives on the Account page */
}

/* ---------- required fields and best guesses ---------- */

.field.req > label::after,
.chip-group.req > legend::after,
.req-star { content: " *"; color: var(--accent); font-weight: 700; }
.req-star { margin-right: 2px; }
.hint {
  font-size: .9rem;
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  background: var(--surface);
  border-radius: 0 8px 8px 0;
}
.notice:empty { display: none; }
.notice {
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--accent-soft);
  font-weight: 600;
}
.field.guessed select, .field.guessed input { border: 2px dashed var(--accent); }
.field.guessed > label::before, .chip-group.guessed > legend::before {
  content: "guess · ";
  color: var(--accent);
  font-weight: 600;
  font-size: .8rem;
}
.chip-group.guessed .chips { outline: 2px dashed var(--accent); outline-offset: 6px; border-radius: 6px; }

/* ---------- the cast panel on a story ---------- */

.cast > summary { cursor: pointer; font-family: var(--serif); font-size: 1.2rem; font-weight: 600; }
.cast-member { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.cast-member h3 { font-family: var(--serif); font-size: 1.15rem; margin: 0 0 8px; }
.cast-member dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 0; font-size: .92rem; }
.cast-member dt { color: var(--ink-soft); }
.cast-member dd { margin: 0; }
.guess-tag {
  display: inline-block;
  margin-left: 8px;
  padding: 0 8px;
  border-radius: var(--radius-pill);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: .75rem;
  font-weight: 700;
}
