:root {
  /*
    Loom theme variables.
    Authors can customize the book by editing these values.

    --loom-bg: browser/page background
    --loom-paper: main book page
    --loom-text: primary text
    --loom-muted: secondary text
    --loom-line: borders/dividers
    --loom-accent: buttons/headings
    --loom-accent-text: text on accent backgrounds
    --loom-shadow: book shadow
  */
  --loom-bg: #f2efe8;
  --loom-paper: #fffdf8;
  --loom-text: #252525;
  --loom-muted: #67615a;
  --loom-line: #d8d2c8;
  --loom-accent: #355f7d;
  --loom-accent-text: #ffffff;
  --loom-context-highlight: #244f6d;
  --loom-context-bg: #fffdf8;
  --loom-context-border: #c8d4dc;
  --loom-shadow: rgba(35, 35, 35, 0.12);

  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  --sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body[data-theme="dark"] {
  --loom-bg: #18191b;
  --loom-paper: #222427;
  --loom-text: #efede8;
  --loom-muted: #b8b2aa;
  --loom-line: #393d42;
  --loom-accent: #8fc0df;
  --loom-accent-text: #10202b;
  --loom-context-highlight: #a8d2ea;
  --loom-context-bg: #2b2f33;
  --loom-context-border: #4d5c66;
  --loom-shadow: rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light dark;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--loom-bg);
  color: var(--loom-text);
  font-family: var(--serif);
  line-height: 1.6;
  padding: 1rem;
}

.book-shell {
  position: relative;
  width: min(760px, 100%);
  margin: 2rem auto;
  background: var(--loom-paper);
  border: 1px solid var(--loom-line);
  border-radius: 8px;
  box-shadow: 0 16px 48px var(--loom-shadow);
  padding: clamp(1.25rem, 4vw, 2.5rem);
}

.theme-toggle {
  position: absolute;
  top: 1rem;
  right: 1rem;
  font-family: var(--sans);
  font-size: 0.85rem;
  border: 1px solid var(--loom-line);
  background: color-mix(in srgb, var(--loom-paper), white 10%);
  color: var(--loom-muted);
  border-radius: 8px;
  padding: 0.45rem 0.75rem;
  cursor: pointer;
}

.theme-toggle:hover,
.theme-toggle:focus-visible {
  color: var(--loom-accent);
  border-color: var(--loom-accent);
}

.book-title {
  margin: 1rem 0 0.25rem;
  font-size: clamp(2rem, 6vw, 3.25rem);
  line-height: 1;
  letter-spacing: 0;
  text-align: center;
}

.book-meta {
  margin: 0 0 2rem;
  text-align: center;
  color: var(--loom-muted);
  font-family: var(--sans);
  font-size: 0.9rem;
}

.passage-title {
  margin: 0 0 1.35rem;
  text-align: center;
  color: var(--loom-accent);
  font-size: 0.95rem;
  font-family: var(--sans);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.story-text {
  font-size: clamp(1.05rem, 2vw, 1.2rem);
  max-width: 62ch;
  margin: 0 auto 2rem;
}

.story-text h1,
.story-text h2,
.story-text h3 {
  line-height: 1.1;
}

.story-text p {
  margin: 0 0 1em;
}

.story-text strong {
  color: var(--loom-accent);
}

.story-text em {
  color: inherit;
}

.story-text blockquote {
  margin: 1.25rem 0;
  padding: 0.25rem 0 0.25rem 1rem;
  border-left: 3px solid var(--loom-line);
  color: var(--loom-muted);
}

.underline {
  text-decoration: underline;
  text-decoration-thickness: 0.08em;
  text-underline-offset: 0.18em;
}

.text-red {
  color: #a64242;
}

.text-blue {
  color: var(--loom-accent);
}

.text-green {
  color: #4f7a52;
}

.text-gold {
  color: #9a6a24;
}

.text-muted {
  color: var(--loom-muted);
}

.text-gray,
.text-grey {
  color: var(--loom-muted);
}

body[data-theme="dark"] .text-red {
  color: #e38b8b;
}

body[data-theme="dark"] .text-green {
  color: #9cca9f;
}

body[data-theme="dark"] .text-gold {
  color: #e3be72;
}

.dialogue {
  display: grid;
  grid-template-columns: minmax(5rem, max-content) 1fr;
  gap: 0.75rem;
  align-items: start;
}

.dialogue-speaker {
  color: var(--loom-accent);
  font-family: var(--sans);
  font-weight: 700;
}

.dialogue-line {
  display: block;
  padding-left: 0.75rem;
  border-left: 2px solid var(--loom-line);
}

.context-term {
  position: relative;
  display: inline-block;
  color: var(--loom-context-highlight);
  border-bottom: 1px dotted currentColor;
  cursor: help;
  outline: none;
}

.context-term:focus-visible {
  border-bottom-style: solid;
}

.context-box {
  position: absolute;
  left: 0;
  bottom: calc(100% + 0.55rem);
  z-index: 2;
  display: none;
  width: min(18rem, calc(100vw - 2rem));
  padding: 0.75rem;
  border: 1px solid var(--loom-context-border);
  border-radius: 8px;
  background: var(--loom-context-bg);
  color: var(--loom-text);
  box-shadow: 0 10px 28px var(--loom-shadow);
  font-family: var(--sans);
  font-size: 0.9rem;
  line-height: 1.45;
}

.context-box strong,
.context-box span {
  display: block;
}

.context-box strong {
  margin-bottom: 0.25rem;
}

.context-term:hover > .context-box,
.context-term:focus > .context-box,
.context-term:focus-within > .context-box {
  display: block;
}

.choices {
  display: grid;
  gap: 0.85rem;
  margin: 2rem auto;
  max-width: 62ch;
  border-top: 1px solid var(--loom-line);
  padding-top: 1.5rem;
}

.choice-button,
.primary-button,
.secondary-button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--loom-accent);
  background: transparent;
  color: var(--loom-accent);
  padding: 0.85rem 1.1rem;
  transition: transform 120ms ease, background 120ms ease, color 120ms ease, border-color 120ms ease;
}

.choice-button {
  text-align: left;
  border-radius: 8px;
  font-size: 1rem;
  background: rgba(255, 255, 255, 0.12);
}

.choice-button:hover,
.choice-button:focus-visible,
.primary-button:hover,
.primary-button:focus-visible {
  background: var(--loom-accent);
  color: var(--loom-accent-text);
  transform: translateY(-1px);
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.primary-button:disabled:hover,
.secondary-button:disabled:hover {
  background: transparent;
  color: var(--loom-muted);
  transform: none;
}

.primary-button {
  background: var(--loom-accent);
  color: var(--loom-accent-text);
}

.secondary-button {
  border-color: var(--loom-line);
  color: var(--loom-muted);
}

.secondary-button:hover,
.secondary-button:focus-visible {
  border-color: var(--loom-accent);
  color: var(--loom-accent);
}

.status {
  display: grid;
  gap: 1rem;
  margin: 2rem auto 0;
  max-width: 62ch;
  padding-top: 1.5rem;
  border-top: 1px solid var(--loom-line);
  font-family: var(--sans);
  font-size: 0.9rem;
  color: var(--loom-muted);
}

.status h3 {
  margin: 0 0 0.35rem;
  color: var(--loom-text);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
}

.status-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.status-pill {
  border: 1px solid var(--loom-line);
  border-radius: 8px;
  padding: 0.25rem 0.55rem;
  background: rgba(255,255,255,0.10);
}

.toolbar {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 2rem;
}

.front-page {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin: 2rem auto 0;
  max-width: 62ch;
}

.save-note {
  flex-basis: 100%;
  margin: 0;
  color: var(--loom-muted);
  font-family: var(--sans);
  font-size: 0.9rem;
  text-align: center;
}

.status-message {
  margin: 0;
  color: var(--loom-accent);
}

.character-form {
  display: grid;
  gap: 1rem;
  max-width: 28rem;
  margin: 2rem auto;
}

.field {
  display: grid;
  gap: 0.35rem;
  font-family: var(--sans);
}

.field label {
  font-weight: 700;
}

.field input,
.field select,
.file-picker {
  width: 100%;
  border: 1px solid var(--loom-line);
  border-radius: 8px;
  padding: 0.8rem;
  font: inherit;
  background: var(--loom-paper);
  color: var(--loom-text);
}

.field option {
  background: var(--loom-paper);
  color: var(--loom-text);
}

.validator {
  max-width: 62ch;
  margin: 0 auto 2rem;
  padding: 1rem;
  border: 1px dashed var(--loom-accent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--loom-accent), transparent 90%);
  font-family: var(--mono);
  font-size: 0.85rem;
  white-space: pre-wrap;
  overflow-x: auto;
}

.validator strong {
  font-family: var(--sans);
}

.error-screen {
  max-width: 62ch;
  margin: 0 auto;
  font-family: var(--sans);
}

@media (max-width: 520px) {
  body {
    padding: 0;
  }

  .book-shell {
    min-height: 100vh;
    margin: 0;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }

  .theme-toggle {
    position: static;
    display: block;
    margin-left: auto;
    margin-bottom: 1rem;
  }

  .choice-button {
    text-align: center;
  }

  .dialogue {
    grid-template-columns: 1fr;
    gap: 0.25rem;
  }
}
