/* =========================================================
   Caxton — base styles, layout, and reusable components.
   Theme-specific colour tokens live in themes.css.
   ========================================================= */

*,
*::before,
*::after { box-sizing: border-box; }

html {
  /* Font scale lets us bump everything up for younger readers. */
  --font-scale: 1;
  font-size: calc(18px * var(--font-scale));
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html[data-font-size="small"]    { --font-scale: 0.9; }
html[data-font-size="medium"]   { --font-scale: 1; }
html[data-font-size="large"]    { --font-scale: 1.15; }
html[data-font-size="xlarge"]   { --font-scale: 1.3; }
html[data-font-size="xxlarge"]  { --font-scale: 1.5; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-body);
  color: var(--text-primary);
  background: var(--bg-page);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Reset for buttons / form controls so theme tokens flow through. */
button,
select,
input,
textarea {
  font: inherit;
  color: inherit;
}

button { cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: 4px;
}

a { color: var(--accent-strong); }

/* ---------- Accessibility helpers ---------- */

.visually-hidden {
  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: -1000px;
  top: 0;
  background: var(--accent-strong);
  color: var(--bg-page);
  padding: 0.5rem 1rem;
  border-radius: 0 0 8px 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: blur(6px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  background: transparent;
  border: 0;
  padding: 0.35rem 0.5rem;
  border-radius: 8px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--text-primary);
}
.brand:hover { background: var(--bg-panel-soft); }
.brand-mark {
  font-size: 1.4rem;
  color: var(--accent-strong);
  transform: translateY(-1px);
}

.topbar-controls {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.icon-btn {
  min-width: 44px; min-height: 44px;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text-primary);
  border-radius: 8px;
  font-family: var(--font-ui);
  font-size: 0.95rem;
}
.icon-btn:hover { background: var(--bg-panel-soft); }

.theme-picker select {
  min-height: 44px;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text-primary);
  border-radius: 8px;
  font-family: var(--font-ui);
}

/* ---------- Main / view containers ---------- */

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 4rem;
}

.view-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.5rem, 1.2rem + 1vw, 2.25rem);
  margin: 0.5rem 0 1.25rem;
  color: var(--text-primary);
}

.view-intro {
  max-width: 60ch;
  margin: 0 0 2rem;
  color: var(--text-secondary);
  font-size: 1.05rem;
}

/* ---------- Library (story select) grid ---------- */

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1.25rem;
}

.story-card {
  display: flex;
  flex-direction: column;
  text-align: left;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  overflow: hidden;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  font-family: var(--font-body);
  color: var(--text-primary);
  min-height: 220px;
}
.story-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent-strong);
  box-shadow: 0 8px 20px var(--shadow);
}

.story-card .band-strip {
  height: 12px;
  background: var(--band-color, var(--accent));
}

.story-card-body {
  padding: 1rem 1.1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  flex: 1 1 auto;
}

.story-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.2rem;
  margin: 0;
}

.story-blurb {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.95rem;
  flex: 1 1 auto;
}

.story-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-family: var(--font-ui);
}

.band-pill {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--band-color, var(--accent));
  color: var(--band-fg, #1c1c1c);
  font-weight: 600;
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.progress-track {
  height: 8px;
  background: var(--bg-panel-soft);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 0.4rem;
}
.progress-fill {
  height: 100%;
  background: var(--accent-strong);
  border-radius: inherit;
  transition: width 0.4s ease;
}

.completed-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.55rem;
  background: var(--success-soft);
  color: var(--success);
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.8rem;
}

/* ---------- Reader two-panel layout ---------- */

.reader {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr);
  gap: 1.5rem;
  align-items: stretch;
}

@media (max-width: 880px) {
  .reader {
    grid-template-columns: 1fr;
  }
}

.illustration-pane {
  position: relative;
  background: var(--bg-illustration);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
  min-height: 320px;
  display: flex;
  flex-direction: column;
}

@media (max-width: 880px) {
  .illustration-pane { min-height: 220px; }
}

.illustration-art {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  font-family: var(--font-display);
  color: var(--text-on-illustration);
  text-align: center;
  font-size: 1.05rem;
  line-height: 1.35;
}

.illustration-art .scene-icon {
  font-size: clamp(3rem, 8vw, 5rem);
  display: block;
  margin-bottom: 0.5rem;
}

.illustration-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0.85rem;
  background: var(--bg-illustration-footer);
  border-top: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.prompt-toggle {
  background: transparent;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.55rem;
  font-size: 0.8rem;
}

.prompt-block {
  margin: 0;
  padding: 0.85rem 1rem;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  background: var(--bg-panel-soft);
  color: var(--text-secondary);
  white-space: pre-wrap;
  border-top: 1px solid var(--border);
  display: none;
}
.prompt-block.is-open { display: block; }

.text-pane {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.25rem 1.4rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0; /* allow text wrap */
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  flex-wrap: wrap;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.page-header .story-title-small {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1rem;
  color: var(--text-primary);
}

.page-counter {
  font-family: var(--font-ui);
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum';
  color: var(--text-muted);
}

.story-progress {
  height: 6px;
  border-radius: 999px;
  background: var(--bg-panel-soft);
  overflow: hidden;
  position: relative;
  margin-top: -0.25rem;
}
.story-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent-strong) 100%);
  border-radius: inherit;
  transition: width 0.45s cubic-bezier(0.2, 0.7, 0.2, 1);
}

html[data-theme="high-contrast"] .story-progress { border: 1px solid var(--border); }
html[data-theme="high-contrast"] .story-progress-fill { background: var(--accent-strong); }

/* ---------- Punctuation editor ---------- */

.editor-instructions {
  margin: 0;
  font-size: 0.95rem;
  color: var(--text-secondary);
}

.editor {
  font-family: var(--font-body);
  font-size: 1.25rem;
  line-height: 1.95;
  letter-spacing: 0.005em;
  background: var(--bg-editor);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1rem;
  min-height: 6em;
  color: var(--text-primary);
  /* Allow words and slots to wrap naturally. */
}

.editor-word {
  display: inline;
  white-space: nowrap;
}

/* The slot is the tappable space between words. Sized to comfortably exceed
   the WCAG 2.5.8 AA target of 24×24px even at the smallest font scale, and
   to give little fingers a generous landing area. */
.slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.7em;
  height: 1.7em;
  padding: 0 0.22em;
  margin: 0 0.06em;
  border: 0;
  background: transparent;
  border-radius: 6px;
  vertical-align: baseline;
  position: relative;
  color: var(--text-primary);
  font: inherit;
}

.slot::before {
  /* Soft dot that hints "you can tap here" — kept subtle until interacted. */
  content: "";
  width: 0.45em;
  height: 0.45em;
  border-radius: 50%;
  background: var(--slot-dot);
  opacity: 0.55;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.slot.is-filled::before { display: none; }

.slot:hover::before,
.slot:focus-visible::before { opacity: 1; transform: scale(1.25); }

.slot.is-active {
  background: var(--slot-active-bg);
  outline: 2px solid var(--accent-strong);
  outline-offset: 1px;
}

.slot.is-filled {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05em;
  color: var(--punctuation-color);
  padding: 0 0.25em;
  background: var(--slot-filled-bg);
}

.slot.is-correct  { background: var(--success-soft); color: var(--success); }
.slot.is-wrong    { background: var(--error-soft);   color: var(--error); }
.slot.is-missing  { box-shadow: inset 0 0 0 2px var(--error); }

.editor-word.is-capital::first-letter,
.editor-word[data-capital="true"] {
  /* We rewrite the textContent for capitalization, but a CSS hint is cheap. */
}

/* ---------- Punctuation palette ---------- */

.palette {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.palette button {
  min-width: 64px;
  min-height: 64px;
  padding: 0.5rem 0.8rem 0.4rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-panel-soft);
  color: var(--text-primary);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.6rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 0.15rem;
  position: relative;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}

.palette .count-badge {
  position: absolute;
  top: -7px;
  right: -7px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-strong);
  color: var(--accent-on);
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.78rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--bg-panel);
  box-shadow: 0 1px 3px var(--shadow);
  pointer-events: none;
}

.palette .count-badge[data-state="none"]    { display: none; }
.palette .count-badge[data-state="done"] {
  background: var(--success);
  color: var(--success-soft);
}
.palette .count-badge[data-state="extra"] {
  background: var(--error);
  color: var(--error-soft);
}

.palette button[data-count-state="done"]  { opacity: 0.7; }
.palette button[data-count-state="none"]  { opacity: 0.6; }

.palette button:hover { background: var(--accent-soft); }
.palette button:active { transform: scale(0.96); }
.palette .key-label {
  font-family: var(--font-ui);
  font-weight: 500;
  font-size: 0.65rem;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.palette .erase-btn {
  margin-left: auto;
  min-width: 86px;
  background: var(--bg-panel-soft);
  border-style: dashed;
}
.palette .erase-btn .glyph { font-size: 1.4rem; color: var(--text-secondary); }
.palette .erase-btn .key-label { font-weight: 600; }
.palette .erase-btn:hover { background: var(--error-soft); color: var(--error); }
.palette .erase-btn:hover .glyph { color: var(--error); }

/* Hint that filled slots are themselves "tap to take away". */
.slot.is-filled { cursor: pointer; }
.slot.is-filled:hover {
  outline: 2px dashed var(--error);
  outline-offset: 1px;
}

/* ---------- Action row ---------- */

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
}

.btn {
  min-height: 48px;
  padding: 0.6rem 1.1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 1rem;
  background: var(--bg-panel);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}
.btn:hover { background: var(--bg-panel-soft); }
.btn.primary {
  background: var(--accent-strong);
  color: var(--accent-on);
  border-color: var(--accent-strong);
}
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.feedback {
  font-family: var(--font-ui);
  font-weight: 500;
  margin: 0;
  padding: 0.55rem 0.85rem;
  border-radius: 10px;
  font-size: 0.95rem;
}
.feedback.is-success { background: var(--success-soft); color: var(--success); }
.feedback.is-warn    { background: var(--warn-soft);    color: var(--warn); }
.feedback.is-error   { background: var(--error-soft);   color: var(--error); }
.feedback[hidden]    { display: none; }

/* ---------- Choices ---------- */

.choices {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.6rem;
  margin-top: 0.25rem;
}
@media (min-width: 540px) {
  .choices.has-many { grid-template-columns: 1fr 1fr; }
}
.choice-btn {
  text-align: left;
  padding: 0.85rem 1rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-panel-soft);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 1.05rem;
  line-height: 1.45;
  min-height: 60px;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.choice-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent-strong);
}
.choice-btn .choice-marker {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--accent-strong);
  margin-right: 0.5rem;
}

/* ---------- Story-end celebration ---------- */

.celebration {
  text-align: center;
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.badge-medallion {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: clamp(180px, 40vw, 240px);
  height: clamp(180px, 40vw, 240px);
  border-radius: 50%;
  background: radial-gradient(circle at 30% 25%, var(--accent-soft), var(--accent) 80%);
  border: 6px solid var(--accent-strong);
  color: var(--accent-on);
  box-shadow: 0 12px 32px var(--shadow);
  font-family: var(--font-display);
  font-weight: 700;
}
.badge-medallion .badge-icon { font-size: 3.5rem; line-height: 1; }
.badge-medallion .badge-name {
  font-size: 1.05rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  padding: 0 0.75rem;
}

/* ---------- Misc / utility ---------- */

.spacer { flex: 1 1 auto; }

.crumbs {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  color: var(--text-muted);
}
.crumbs button {
  background: transparent;
  border: 0;
  color: var(--accent-strong);
  font: inherit;
  padding: 0.25rem 0;
  text-decoration: underline;
}

.empty-state {
  padding: 2rem;
  text-align: center;
  color: var(--text-secondary);
  background: var(--bg-panel);
  border: 1px dashed var(--border);
  border-radius: 14px;
}

.dim { color: var(--text-muted); }
