:root {
  --story-ink: #263b36;
  --story-soft: #5c6f6a;
  --story-panel: #ffffff;
  --story-sky: #e8f7ff;
  --story-cream: #fff7df;
  --story-mint: #e9f8d7;
  --story-blue: #4d9bd4;
  --story-coral: #f08a72;
}

body {
  background:
    linear-gradient(180deg, #eff9ff 0%, #f8fbeb 45%, #fff8e5 100%);
}

.story-builder-app {
  width: min(100% - 24px, 1280px);
  padding: 14px 0 28px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
  padding: 12px 16px;
  border: 1px solid var(--eb-border);
  border-radius: var(--eb-radius-large);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--eb-shadow);
}

.brand-title {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 16px;
}

.game-logo {
  width: min(185px, 31vw);
}

.title-divider {
  width: 1px;
  height: 42px;
  background: var(--eb-border);
}

.brand-title h1,
.finished-screen h2 {
  margin: 0;
  color: var(--eb-green-dark);
  line-height: 1.1;
}

.brand-title h1 {
  font-size: clamp(1.35rem, 2.8vw, 2.15rem);
}

.story-name {
  margin: 3px 0 0;
  color: var(--story-soft);
  font-weight: 800;
}

.story-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--story-soft);
  font-weight: 800;
}

.story-picker select {
  min-height: 44px;
  max-width: 260px;
  padding: 8px 34px 8px 12px;
  border: 2px solid #b9d9e8;
  border-radius: 12px;
  background: white;
  color: var(--story-ink);
  font-weight: 800;
}

.story-library {
  margin: 0 0 18px;
  padding: 18px;
  border: 1px solid var(--eb-border);
  border-radius: var(--eb-radius-large);
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--eb-shadow);
}

.story-library__intro {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.story-library__intro h2 {
  margin: 0;
  color: var(--story-ink);
  font-size: clamp(1.4rem, 3vw, 2.2rem);
  line-height: 1.1;
}

.story-library-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
}

.story-library-nav__link {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 14px;
  border: 2px solid #c9e1cd;
  border-radius: 999px;
  background: #f7fff1;
  color: var(--eb-green-dark);
  font-weight: 900;
  text-decoration: none;
}

.story-library-category {
  scroll-margin-top: 16px;
}

.story-library-category + .story-library-category {
  margin-top: 22px;
}

.story-library-category__title {
  margin: 0 0 10px;
  color: var(--story-soft);
  font-size: 1rem;
  font-weight: 900;
}

.story-cover-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.story-cover-card {
  display: block;
  width: 100%;
  padding: 0;
  border: 3px solid #ffffff;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 8px 18px rgba(41, 69, 61, 0.14);
  cursor: pointer;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease;
}

.story-cover-card:hover,
.story-cover-card:focus-visible {
  border-color: var(--story-coral);
  box-shadow: 0 12px 24px rgba(41, 69, 61, 0.2);
  transform: translateY(-2px);
}

.story-cover-card:focus-visible {
  outline: 3px solid rgba(255, 216, 61, 0.85);
  outline-offset: 3px;
}

.story-cover-card.is-selected {
  border-color: var(--story-blue);
  box-shadow: 0 0 0 4px rgba(77, 155, 212, 0.22), 0 12px 24px rgba(41, 69, 61, 0.18);
}

.story-cover-card img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.screen-reader-text {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.loading-screen {
  text-align: center;
  color: var(--story-soft);
  font-weight: 900;
}

.builder-screen {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 340px);
  gap: 16px;
  align-items: start;
}

.scene-panel,
.support-card,
.finished-screen,
.story-preview-screen {
  border: 1px solid var(--eb-border);
  border-radius: var(--eb-radius-large);
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--eb-shadow);
}

.scene-panel {
  padding: 14px;
}

.scene-topline {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.scene-count,
.card-label,
.story-loop {
  margin: 0;
  color: var(--story-soft);
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.scene-topline h2 {
  margin: 1px 0 0;
  color: var(--story-ink);
  font-size: clamp(1.2rem, 2.2vw, 1.8rem);
  line-height: 1.15;
}

.story-loop {
  padding: 8px 10px;
  border-radius: 999px;
  background: var(--story-mint);
  color: var(--eb-green-dark);
  white-space: nowrap;
}

.scene-frame {
  display: grid;
  place-items: center;
  width: 100%;
  margin: 0;
  overflow: hidden;
  border: 3px solid #ffffff;
  border-radius: 18px;
  background: var(--story-sky);
  box-shadow: inset 0 0 0 1px rgba(49, 87, 103, 0.08);
  aspect-ratio: 16 / 9;
}

.scene-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scene-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
}

.teacher-panel {
  display: grid;
  gap: 12px;
}

.support-card {
  padding: 14px;
}

.teacher-prompt {
  min-height: 3.3em;
  margin: 7px 0 0;
  color: var(--story-ink);
  font-size: 1.1rem;
  font-weight: 900;
  line-height: 1.35;
}

.help-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 9px;
}

.help-button,
.text-button {
  min-height: 44px;
  border: 2px solid #c8deec;
  border-radius: 12px;
  background: #ffffff;
  color: #2f6f93;
  font-weight: 900;
  cursor: pointer;
}

.help-button.is-active {
  border-color: var(--story-blue);
  background: #eaf7ff;
}

.help-panel {
  margin-top: 10px;
  padding: 10px;
  border-radius: 12px;
  background: var(--story-cream);
}

.word-list,
.sentence-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.word-list li,
.sentence-list li {
  padding: 7px 10px;
  border: 1px solid rgba(99, 148, 65, 0.25);
  border-radius: 999px;
  background: #ffffff;
  color: var(--story-ink);
  font-weight: 900;
}

.sentence-list li {
  border-radius: 12px;
}

.input-card {
  display: grid;
  gap: 8px;
}

.input-card textarea {
  width: 100%;
  min-height: 140px;
  resize: vertical;
  padding: 12px;
  border: 2px solid #d6e7ee;
  border-radius: 14px;
  color: var(--story-ink);
  background: #fbfdff;
  font-weight: 700;
  line-height: 1.45;
}

.input-card textarea:focus,
.story-picker select:focus,
.help-button:focus,
.text-button:focus {
  outline: 3px solid rgba(255, 216, 61, 0.65);
  outline-offset: 2px;
}

.finish-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.finish-button {
  min-height: 48px;
}

.text-button {
  padding: 8px 12px;
  color: #8b4b43;
  border-color: #efc0b5;
}

.finished-screen {
  padding: 22px;
}

.finished-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 2px solid #edf2e3;
  padding-bottom: 14px;
}

.finished-mascot {
  width: min(120px, 24vw);
}

.story-output {
  min-height: 180px;
  margin: 18px 0;
  padding: 18px;
  border-radius: 18px;
  background: #fffdf6;
  color: var(--story-ink);
  font-size: clamp(1.05rem, 2vw, 1.25rem);
  font-weight: 700;
  line-height: 1.65;
}

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

.story-output p:last-child {
  margin-bottom: 0;
}

.empty-story,
.copy-message {
  color: var(--story-soft);
}

.finished-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.copy-message {
  min-height: 1.5em;
  margin: 12px 0 0;
  font-weight: 900;
}

.story-preview-screen {
  padding: 22px;
}

.story-preview-toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  margin-bottom: 18px;
}

.story-print-area {
  width: min(100%, 880px);
  margin: 0 auto;
  color: var(--story-ink);
}

.story-preview-header {
  margin-bottom: 24px;
  padding-bottom: 18px;
  border-bottom: 2px solid #edf2e3;
  text-align: center;
}

.story-preview-header h2 {
  margin: 4px 0 14px;
  color: var(--eb-green-dark);
  font-size: clamp(2rem, 5vw, 3.4rem);
  line-height: 1.1;
}

.created-by-field {
  display: inline-flex;
  align-items: center;
  max-width: min(100%, 420px);
  gap: 10px;
  color: var(--story-soft);
  font-weight: 900;
}

.created-by-field input {
  min-width: 0;
  min-height: 42px;
  padding: 8px 12px;
  border: 2px solid #d6e7ee;
  border-radius: 12px;
  color: var(--story-ink);
  background: #fbfdff;
  font: inherit;
}

.created-by-field input:focus {
  outline: 3px solid rgba(255, 216, 61, 0.65);
  outline-offset: 2px;
}

.story-preview-byline {
  margin: 8px 0 0;
  color: var(--story-soft);
  font-size: 1.1rem;
  font-weight: 900;
}

.story-preview-scenes {
  display: grid;
  gap: 22px;
}

.story-preview-scene {
  break-inside: avoid;
  page-break-inside: avoid;
  padding: 18px;
  border: 1px solid #edf2e3;
  border-radius: 8px;
  background: #fffdf6;
}

.story-preview-scene h3 {
  margin: 0 0 12px;
  color: var(--story-soft);
  font-size: 0.98rem;
  font-weight: 900;
}

.story-preview-figure {
  margin: 0;
  overflow: hidden;
  border: 2px solid #ffffff;
  border-radius: 8px;
  background: var(--story-sky);
  aspect-ratio: 16 / 9;
}

.story-preview-figure img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.story-preview-text {
  min-height: 2.6em;
  margin: 14px 0 0;
  white-space: pre-wrap;
  color: var(--story-ink);
  font-size: clamp(1.15rem, 2vw, 1.45rem);
  font-weight: 800;
  line-height: 1.55;
}

@media (max-width: 940px) {
  .story-cover-grid {
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  }

  .builder-screen {
    grid-template-columns: 1fr;
  }

  .teacher-panel {
    grid-template-columns: 1fr 1fr;
  }

  .input-card,
  .finish-card {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .story-builder-app {
    width: 100%;
    padding: 10px;
  }

  .game-header,
  .brand-title,
  .scene-topline,
  .finished-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .game-logo {
    width: 142px;
  }

  .title-divider,
  .story-loop {
    display: none;
  }

  .story-picker,
  .story-picker select {
    width: 100%;
  }

  .story-library {
    padding: 14px 10px;
    border-radius: 18px;
  }

  .story-library__intro {
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }

  .story-library-nav {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 6px;
  }

  .story-library-nav__link {
    min-height: 40px;
    padding: 7px 8px;
    font-size: 0.82rem;
  }

  .story-cover-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .teacher-panel,
  .help-actions,
  .finish-card,
  .scene-controls {
    grid-template-columns: 1fr;
  }

  .scene-panel,
  .support-card,
  .finished-screen,
  .story-preview-screen {
    border-radius: 18px;
  }

  .story-preview-screen {
    padding: 14px 10px;
  }

  .story-preview-toolbar,
  .created-by-field {
    align-items: stretch;
    flex-direction: column;
  }

  .story-preview-toolbar .eb-button,
  .created-by-field input {
    width: 100%;
  }

  .story-preview-scene {
    padding: 12px;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 12mm;
  }

  body {
    background: #ffffff;
  }

  .game-header,
  .story-library,
  .builder-screen,
  .finished-actions,
  .copy-message,
  .story-preview-toolbar,
  .created-by-field {
    display: none !important;
  }

  .story-builder-app {
    width: 100%;
    padding: 0;
  }

  .finished-screen:not([hidden]),
  .story-preview-screen:not([hidden]) {
    display: block !important;
    border: 0;
    box-shadow: none;
    padding: 0;
  }

  .finished-screen[hidden],
  .story-preview-screen[hidden] {
    display: none !important;
  }

  .finished-header,
  .story-preview-header {
    border-bottom-color: #dddddd;
  }

  .finished-mascot {
    width: 90px;
  }

  .story-print-area {
    width: 100%;
  }

  .story-preview-header {
    margin-bottom: 14px;
    padding-bottom: 10px;
  }

  .story-preview-header h2 {
    font-size: 28pt;
  }

  .story-preview-byline {
    color: #333333;
    font-size: 12pt;
  }

  .story-preview-scenes {
    gap: 12mm;
  }

  .story-preview-scene {
    break-inside: avoid;
    page-break-inside: avoid;
    padding: 0 0 8mm;
    border: 0;
    border-radius: 0;
    background: #ffffff;
  }

  .story-preview-scene h3 {
    margin-bottom: 4mm;
    color: #555555;
    font-size: 11pt;
  }

  .story-preview-figure {
    border: 1px solid #dddddd;
    border-radius: 4px;
  }

  .story-preview-text {
    min-height: 0;
    margin-top: 4mm;
    color: #111111;
    font-size: 14pt;
    line-height: 1.5;
  }
}
