/* =============================================================
   Rich-text rendering — shared by every place mod-authored HTML is
   rendered (quest dialogue + trivia + quest body, static pages, shop
   descriptions). Also loaded inside the Django admin's CKEditor widgets
   (via CKEDITOR_5_CUSTOM_CSS in settings.py) purely so the `--rt-*`
   tokens below reach the fontColor swatches and editing-area preview
   there; admin markup never carries the `.rich-text`/`.page-body`/etc.
   classes this file otherwise styles, so the rest of the file is inert
   there.

   Two jobs:

   1. **Containment.** CKEditor happily emits content wider than the card
      it lands in — an `<img>` at its natural size, a resized
      `<figure class="image" style="width:900px">`, a wide table. Nothing
      in the page CSS stopped those overflowing, so images spilled out of
      speech bubbles and cards. Everything here is capped at 100% of its
      container, and wide tables scroll inside their own figure instead of
      stretching the page.

   2. **Table styling.** CKEditor's content styles only load inside the
      editor, so a table authored in the builder rendered as bare,
      border-less text on the public side. These rules give it the site's
      parchment styling; an inline border/background from the
      table-properties panel still wins (inline styles beat these).

   Put `.rich-text` on any container whose contents come from a rich-text
   editor. The other selectors are the dialogue/page containers that
   already carry the content (including the ones `cyoa-story.js` builds
   client-side, which have no template to add a class to).

   Kept deliberately narrow — these selectors must not reach general page
   chrome (card thumbnails etc.), whose own `img` rules have the same
   specificity and would lose to a later-loading rule here.
   ============================================================= */

.rich-text,
.page-body,
.dialogue-simple,
.speech-bubble,
.speech-bubble-right {
  /* A pasted URL or a long unbroken word shouldn't widen the container. */
  overflow-wrap: break-word;
}

/* ---- Curated font-color palette (review WO-11) ------------------------
   The CKEditor `fontColor` plugin (config.fontColor.colors in settings.py)
   is restricted to this palette, and each swatch's *value* is one of these
   custom properties rather than a literal hex — CKEditor writes the swatch
   value straight into the authored `style="color: …"`, and an inline style
   always beats theme CSS, so a literal hex picked in one theme could not
   react when the reader is in the other. A `var()` can: the same authored
   markup resolves to a different hex per theme, the same way `--ink`/
   `--accent`/etc. already do in site.css / dark-mode.css.

   Contrast math (WCAG relative-luminance formula), verified against every
   background this palette is actually rendered on:
     - light theme: --bg #f8ecd5 (L=0.848, the darker/harder of the two
       light surfaces — --card #fdf5e6 is L=0.919 and easier) and the
       quest-dialogue bubble's `--dialogue-bg` #F5E0C9 (quests/story.css
       `:root`, L≈0.86)
     - dark theme: --bg / --card #1c1732 (L=0.011) and the dialogue bubble's
       dark `--dialogue-bg` (`--parch-2` #241f3d, L≈0.016 — lighter than
       #1c1732, so it clears the same 4.5:1 floor with more margin, not less)
   Every entry below clears 4.5:1 (WCAG AA, body text) against both its
   light-mode background and #1c1732 in dark mode — margins run 6.5:1 to
   11.6:1, well clear of the 4.5:1 floor:

     name            light hex   vs #f8ecd5   dark hex    vs #1c1732
     Berry Red       #8c1f2b       7.65:1     #ff9aa8       8.57:1
     Sunset Orange   #8a3d0a       6.52:1     #ffb066       9.57:1
     Honey Gold      #6b4d00       6.68:1     #f0d264      11.58:1
     Moss Green      #285c2b       6.76:1     #8fe08f      10.86:1
     Teal            #0d5c54       6.71:1     #7fe0d6      11.11:1
     Sky Blue        #0a4d78       7.65:1     #8fc9ff       9.82:1
     Plum Purple     #5a2a70       8.98:1     #d3a6f0       8.61:1
     Rose Pink       #8a2d55       6.94:1     #f2a6cf       9.14:1
     Slate Gray      #44403a       8.80:1     #cfc9de      10.72:1

   Deliberate omission: a single literal hex cannot pass 4.5:1 against BOTH
   a light theme background (L≈0.85-0.92) and the dark theme background
   (L≈0.011) — the required luminance ranges (≤0.149 vs ≥0.224) don't
   overlap, so no fixed color clears the bar in both themes at once. That's
   exactly the "pure CSS can't fix this" problem WO-11 raised; the var()
   indirection here is what makes option (a) actually deliver AA in both
   themes rather than splitting the difference at ~4:1 in each. */
:root {
  --rt-red: #8c1f2b;
  --rt-orange: #8a3d0a;
  --rt-gold: #6b4d00;
  --rt-green: #285c2b;
  --rt-teal: #0d5c54;
  --rt-blue: #0a4d78;
  --rt-purple: #5a2a70;
  --rt-pink: #8a2d55;
  --rt-gray: #44403a;
}

/* This file is also loaded inside the Django admin (via CKEDITOR_5_CUSTOM_CSS in
   settings.py — see that setting's comment) so the fontColor swatches and the
   editing-area preview aren't colorless there. The site's own toggle
   (theme-toggle.js) always stamps an explicit `data-theme="light"`/`"dark"` before
   first paint, so the two blocks below never fire for it — but Django admin's
   *own*, independent dark-mode toggle (admin/js/theme.js) defaults to
   `data-theme="auto"` and follows `prefers-color-scheme` in that state, only
   stamping an explicit "light"/"dark" once someone clicks admin's toggle. This
   block covers that default "auto" case so the palette is legible without
   requiring a staff member to find and click admin's separate toggle first. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --rt-red: #ff9aa8;
    --rt-orange: #ffb066;
    --rt-gold: #f0d264;
    --rt-green: #8fe08f;
    --rt-teal: #7fe0d6;
    --rt-blue: #8fc9ff;
    --rt-purple: #d3a6f0;
    --rt-pink: #f2a6cf;
    --rt-gray: #cfc9de;
  }
}

:root[data-theme="dark"] {
  --rt-red: #ff9aa8;
  --rt-orange: #ffb066;
  --rt-gold: #f0d264;
  --rt-green: #8fe08f;
  --rt-teal: #7fe0d6;
  --rt-blue: #8fc9ff;
  --rt-purple: #d3a6f0;
  --rt-pink: #f2a6cf;
  --rt-gray: #cfc9de;
}

/* Quest dialogue bubbles (`.dialogue-simple`, `.speech-bubble`,
   `.speech-bubble-right`) used to be hardcoded to the light parchment
   #F5E0C9 in quests/story.css and quests/cyoa.css regardless of theme, which
   is why the palette above was once pinned back to its light-mode values
   inside them. Both files now read their background from the `--dialogue-*`
   tokens (quests/story.css `:root`), re-skinned to a dark "night parchment"
   palette under `[data-theme="dark"]` in dark-mode.css — so the bubble
   background tracks the theme like everything else, and the ordinary
   `:root[data-theme="dark"]` `--rt-*` palette above (already verified
   against the #1c1732 dark background, which the dark dialogue background
   `--parch-2` #241f3d is lighter than) applies here too without a pin. */

/* ---- Containment ---------------------------------------------------- */

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  :is(img, video, iframe, svg) {
  max-width: 100%;
  height: auto;
}

/* CKEditor's wrapper carries the author's chosen width as an inline style
   (e.g. `width: 900px`), which beats the child img's max-width — so the
   figure itself has to be capped too. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) figure {
  max-width: 100%;
}

/* Preformatted blocks scroll rather than stretch. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) pre {
  max-width: 100%;
  overflow-x: auto;
}

/* ---- Images ---------------------------------------------------------- */

/* CKEditor's own content styles only load inside the editor, so without these an
   image the editor showed centred (in a table cell too) sat flush left on the site.
   Restated from the bundled CKEditor content stylesheet so the editor and the site
   agree: a block image is centred, and the image toolbar's alignment styles float. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.image {
  display: table;
  clear: both;
  margin: 0.9em auto;
  text-align: center;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.image
  img {
  display: block;
  margin: 0 auto;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.image.image_resized {
  display: block;
  box-sizing: border-box;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.image.image_resized
  img {
  width: 100%;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  :is(.image-style-side, .image-style-align-right) {
  float: right;
  margin-left: 1.5em;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  .image-style-side {
  max-width: 50%;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  .image-style-align-left {
  float: left;
  margin-right: 1.5em;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  .image-style-align-center {
  margin-left: auto;
  margin-right: auto;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  .image-style-block-align-left {
  margin-left: 0;
  margin-right: auto;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  .image-style-block-align-right {
  margin-left: auto;
  margin-right: 0;
}

/* ---- Headings --------------------------------------------------------- */

/* CKEditor's own heading styles only load inside the editor, and site.css's global
   h1,h2,h3 rule stops at h3 — but the "quest" toolbar's one heading option emits h4,
   and h5/h6 are reachable via "page"/admin source editing. Without this, picking
   "Heading" renders as plain bold text at body size: indistinguishable from a
   paragraph everywhere the content is actually read (reported as "Paragraph and
   Heading look the same" in the quest body preview). Give the full range an explicit,
   descending scale here rather than relying on each page happening to already style
   headings for unrelated reasons. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  :is(h2, h3, h4, h5, h6) {
  font-family: "Fredoka", system-ui, sans-serif;
  font-weight: 600;
  line-height: 1.25;
  color: var(--ink, #6e5946);
  margin: 0.8em 0 0.4em;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) h2 {
  font-size: 1.5rem;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) h3 {
  font-size: 1.3rem;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) h4 {
  font-size: 1.15rem;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) h5 {
  font-size: 1.05rem;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) h6 {
  font-size: 0.95rem;
}

/* ---- Tables --------------------------------------------------------- */

/* A table is centred, as the editor shows it (CKEditor: `.table { margin: .9em auto }`),
   and shrinks to its own width. One with many columns can't shrink below its content,
   so the figure scrolls horizontally instead of pushing the card wider. That scroll is
   why the figure stays a block (`fit-content`) rather than CKEditor's `display: table`. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table {
  width: fit-content;
  max-width: 100%;
  margin: 0.9em auto;
  overflow-x: auto;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) table {
  border-collapse: collapse;
  max-width: 100%;
  margin: 0;
  background: var(--card, #fdf5e6);
}

/* A table the author resized in the editor carries its width on the figure. */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table[style*="width"]
  > table {
  width: 100%;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  table.ck-table-resized {
  table-layout: fixed;
}

/* The table toolbar's alignment buttons (Table properties > Alignment). */
:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table.table-style-align-left {
  float: left;
  margin-right: 1.5em;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table.table-style-align-right {
  float: right;
  margin-left: 1.5em;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table.table-style-block-align-left {
  margin-left: 0;
  margin-right: auto;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table.table-style-block-align-right {
  margin-left: auto;
  margin-right: 0;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  :is(td, th) {
  border: 1px solid var(--line, #e3cda6);
  padding: 0.4rem 0.6rem;
  /* Middle, as the editor shows a cell (the browser's own default there). */
  vertical-align: middle;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right) th {
  background: var(--bg, #f8ecd5);
  font-weight: 700;
  text-align: left;
}

:is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
  figure.table > figcaption {
  font-size: 0.9em;
  color: var(--ink-soft, #a08a73);
  text-align: center;
  padding: 0.3rem 0;
}

/* ---- Phones ----------------------------------------------------------- */

/* A phone column is too narrow to wrap text around anything, so floated tables and
   images stack, centred. And a table holding pictures shares the width out evenly
   (`table-layout: fixed`) so the pictures shrink to fit instead of pushing the
   table into a sideways scroll; a text-only table keeps its scroll. */
@media (max-width: 640px) {
  :is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
    :is(
      figure.table.table-style-align-left,
      figure.table.table-style-align-right,
      .image-style-side,
      .image-style-align-left,
      .image-style-align-right
    ) {
    float: none;
    margin-left: auto;
    margin-right: auto;
  }

  :is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
    figure.table:has(img)
    > table {
    width: 100% !important;
    table-layout: fixed;
  }

  :is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
    figure.table:has(img)
    :is(td, th) {
    padding: 0.35rem;
  }

  :is(.rich-text, .page-body, .dialogue-simple, .speech-bubble, .speech-bubble-right)
    figure.table:has(img)
    :is(img, figure.image) {
    max-width: 100% !important;
    height: auto !important;
  }
}

/* ---- The editor's own box ---------------------------------------------

   This file is also loaded beside every CKEditor widget (CKEDITOR_5_CUSTOM_CSS). In the
   Django admin the editor sits in a flex row after its label, and its wrapper's default
   `min-width: auto` let the toolbar's full width push it past the right edge of the
   page, cutting off the toolbar's last buttons (table, image, source). Letting it shrink
   makes the toolbar fold its overflow into the "⋮" menu instead. */
.ck-editor-container {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
}
