:root {
  --cream: #faf3e8;
  --paper: #f4ead9;
  --ink: #3d3428;
  --mute: #8a7968;
  --line: #dcc9b0;
  --rule: rgba(125, 160, 200, 0.22);
  --sky: #7eb8e8;
  --pink: #f4a4b8;
  --yellow: #f5d76e;
  --mint: #9ed4b8;
  --accent: #e8879a;
  --tab-fill: #fff8ef;
  --shadow-soft: 0 4px 18px rgba(61, 52, 40, 0.08);
  --radius-lg: 1.1rem;
  --radius-md: 0.65rem;
  --font-display: "Nunito", "Segoe UI", system-ui, sans-serif;
  --font-ui: "Source Sans 3", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  display: grid;
  grid-template-columns: minmax(240px, 300px) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "nav nav"
    "shelf main";
  font-family: var(--font-ui);
  background-color: var(--cream);
  background-image:
    linear-gradient(var(--rule) 1px, transparent 1px);
  background-size: 100% 1.65rem;
  background-position: 0 0.4rem;
  color: var(--ink);
}

#topnav {
  grid-area: nav;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-bottom: 2px solid var(--line);
  background: linear-gradient(180deg, #fff9f0 0%, var(--cream) 100%);
  font-family: var(--font-ui);
  box-shadow: var(--shadow-soft);
}

.brand {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--ink);
}

.sections {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.section-link {
  color: var(--mute);
  text-decoration: none;
  padding: 0.62rem 1.25rem;
  border: 2px solid transparent;
  border-radius: 999px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1rem;
  transition: background 0.15s, color 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.section-link:hover {
  color: var(--ink);
  background: var(--tab-fill);
  border-color: var(--line);
}

.section-link[aria-current="page"] {
  color: var(--ink);
  background: linear-gradient(135deg, rgba(126, 184, 232, 0.58), rgba(245, 215, 110, 0.72));
  border-color: rgba(126, 184, 232, 0.75);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.85),
    0 3px 10px rgba(126, 184, 232, 0.22);
}

#shelf {
  grid-area: shelf;
  border-right: 2px solid var(--line);
  padding: 1.25rem 1rem 2rem;
  overflow: auto;
  min-height: 0;
  background-color: rgba(255, 248, 239, 0.65);
  background-image:
    repeating-linear-gradient(
      transparent,
      transparent calc(1.65rem - 1px),
      var(--rule) calc(1.65rem - 1px),
      var(--rule) 1.65rem
    );
}

.kicker, #item-kicker {
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.68rem;
  color: var(--sky);
  margin: 0 0 0.35rem;
  font-family: var(--font-ui);
  font-weight: 600;
}

h1 {
  font-family: var(--font-display);
  font-size: 1.55rem;
  margin: 0 0 0.45rem;
  font-weight: 800;
}

.lede {
  color: var(--mute);
  font-size: 0.92rem;
  line-height: 1.45;
  margin: 0 0 1rem;
}

#list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#list button {
  display: grid;
  grid-template-columns: 4.5rem 1fr;
  gap: 0.75rem;
  align-items: center;
  text-align: left;
  background: rgba(255, 252, 247, 0.85);
  border: 2px solid transparent;
  color: inherit;
  padding: 0.5rem 0.6rem;
  cursor: pointer;
  font: inherit;
  border-radius: var(--radius-md);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
  appearance: none;
  -webkit-appearance: none;
}

#list button:hover,
#list button.active {
  border-color: var(--pink);
  background: #fff;
  box-shadow: var(--shadow-soft);
}

#list .shelf-thumb-wrap {
  width: 4.5rem;
  height: 4.5rem;
  flex-shrink: 0;
  border-radius: 0.5rem;
  overflow: hidden;
  background: #fff;
  border: 2px solid var(--line);
  box-shadow:
    0 2px 8px rgba(61, 52, 40, 0.1),
    inset 0 0 0 1px rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
}

#list .shelf-thumb-wrap.is-placeholder {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--accent);
  background: linear-gradient(135deg, rgba(245, 215, 110, 0.55), rgba(244, 164, 184, 0.4));
}

#list .shelf-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #fff;
}

body.section-demo #list .shelf-thumb-wrap {
  background: linear-gradient(180deg, #fff 0%, var(--cream) 100%);
  box-shadow:
    0 2px 8px rgba(61, 52, 40, 0.1),
    inset 0 0 0 1px rgba(255, 255, 255, 0.65),
    0 0 0 3px rgba(245, 215, 110, 0.24);
}

body.section-demo #list .shelf-thumb {
  object-fit: contain;
  padding: 0.15rem;
  background: transparent;
}

#list .shelf-text {
  min-width: 0;
}

#list .shelf-title {
  display: block;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.25;
}

#list small {
  display: block;
  color: var(--mute);
  font-size: 0.76rem;
  margin-top: 0.15rem;
  line-height: 1.3;
}

main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background-color: rgba(250, 243, 232, 0.5);
}

main > header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  border-bottom: 2px solid var(--line);
  background: rgba(255, 249, 240, 0.9);
}

h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 800;
}

.tools {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  flex-wrap: wrap;
  font-family: var(--font-ui);
}

.tabs {
  display: flex;
  gap: 0.35rem;
  padding: 0.25rem;
  border: 2px solid var(--line);
  background: var(--tab-fill);
  border-radius: 999px;
}

.tabs-soft button {
  border: 0;
  background: transparent;
  padding: 0.45rem 0.95rem;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--mute);
  border-radius: 999px;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}

.tabs-soft button:hover {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.7);
}

.tabs-soft button[aria-selected="true"] {
  background: linear-gradient(135deg, rgba(158, 212, 184, 0.62), rgba(126, 184, 232, 0.52));
  color: var(--ink);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.85);
}

.tabs-crayon button[aria-selected="true"] {
  background: linear-gradient(135deg, rgba(244, 164, 184, 0.65), rgba(245, 215, 110, 0.62));
}

.pager {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pager button {
  width: 2.35rem;
  height: 2.35rem;
  border: 2px solid var(--line);
  background: #fff8ef;
  cursor: pointer;
  font-size: 1.05rem;
  border-radius: 50%;
  color: var(--ink);
}

.pager button:hover:not(:disabled) {
  border-color: var(--sky);
  background: #fff;
}

.pager button:disabled {
  opacity: 0.35;
  cursor: default;
}

#pos {
  min-width: 4.5rem;
  text-align: center;
  font-size: 0.88rem;
  color: var(--mute);
  font-weight: 500;
}

.hidden { display: none !important; }

.stage {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

#stage-gallery.stage {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

body.section-gallery {
  overflow: hidden;
  height: 100%;
}

body.section-gallery main {
  overflow: hidden;
  min-height: 0;
}

#stage-demo.stage {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0.75rem 1rem 1.25rem;
}

.demo-play {
  flex: 1;
  min-height: 0;
  display: flex;
}

#demo-iframe {
  flex: 1;
  width: 100%;
  min-height: min(72vh, 40rem);
  border: 2px solid var(--line);
  border-radius: var(--radius-lg);
  background: #2a221c;
  box-shadow: var(--shadow-soft);
}

/* Comics: storybook — one big page */
.stage-storybook figure {
  margin: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem 1.25rem 1.5rem;
}

.stage-storybook .mat-frame {
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: min(96%, 52rem);
  width: 100%;
}

.stage-storybook #page {
  max-width: 100%;
  max-height: calc(100vh - 11rem);
  object-fit: contain;
  display: block;
}

body.mode-notebook .stage-storybook .mat-frame {
  padding: 0.65rem 0.75rem 0.85rem;
  background-color: #fffef9;
  background-image:
    repeating-linear-gradient(
      transparent,
      transparent calc(1.55rem - 1px),
      var(--rule) calc(1.55rem - 1px),
      var(--rule) 1.55rem
    );
  border: 2px solid var(--line);
  border-radius: 0.35rem;
  box-shadow:
    2px 3px 0 rgba(61, 52, 40, 0.06),
    var(--shadow-soft);
}

body.mode-notebook .stage-storybook #page {
  background: transparent;
  border: 0;
  box-shadow: none;
}

body.mode-comic .stage-storybook .mat-frame {
  padding: 0.5rem;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow:
    0 8px 28px rgba(61, 52, 40, 0.12),
    0 0 0 3px rgba(245, 215, 110, 0.35);
}

body.mode-comic .stage-storybook #page {
  border-radius: calc(var(--radius-lg) - 0.35rem);
  background: #fff;
  border: 0;
}

figcaption {
  margin-top: 0.65rem;
  color: var(--mute);
  font-size: 0.88rem;
  text-align: center;
  font-weight: 500;
}

#stage-song {
  display: grid;
  grid-template-columns: minmax(240px, 42%) 1fr;
  gap: 1.2rem;
  padding: 1.2rem;
  align-items: start;
}

#song-picture {
  max-width: 100%;
  max-height: calc(100vh - 11rem);
  width: 100%;
  object-fit: contain;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-soft);
}

#song-lyrics {
  margin: 0;
  white-space: pre-wrap;
  font-family: var(--font-ui);
  font-size: 1.05rem;
  line-height: 1.55;
  background: rgba(255, 252, 247, 0.8);
  padding: 1rem 1.1rem;
  border-radius: var(--radius-md);
  border: 1px dashed var(--line);
}

#stage-novel {
  padding: 1.4rem 1.8rem 3rem;
  max-width: 42rem;
  line-height: 1.55;
}

#stage-novel h1 {
  font-family: var(--font-display);
  font-size: 1.8rem;
  margin: 0 0 0.6rem;
}

#stage-novel h2 {
  font-size: 1.2rem;
  margin: 1.6rem 0 0.6rem;
}

#stage-novel p { margin: 0 0 0.9rem; }
#stage-novel em { color: var(--mute); }

#stage-novel img {
  display: block;
  width: 100%;
  max-width: 22rem;
  margin: 0.4rem auto 1.2rem;
  border: 2px solid var(--line);
  background: #fff;
  border-radius: var(--radius-md);
}

/* Gallery: top preview stage (non-scrolling) + thumb list below (only scroller) */
.stage-shelf {
  padding: 0;
  min-height: 0;
  flex: 1;
  overflow: hidden;
}

.gallery-stage {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-columns: 1fr;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.gallery-preview-dock,
#gallery-focus-panel {
  grid-row: 1;
  width: 100%;
  flex-shrink: 0;
  display: none;
  padding: 0.65rem 1rem 0.75rem;
  border-bottom: 2px dashed var(--line);
  background: rgba(255, 249, 240, 0.96);
  position: relative;
  z-index: 1;
}

.gallery-preview-dock.is-open,
#gallery-focus-panel.is-open {
  display: block;
}

.gallery-scroll {
  grid-row: 2;
  min-width: 0;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem 1rem 1.25rem;
}

.gallery-shelf-lede {
  margin: 0;
  padding: 0 0.25rem 0.65rem;
  font-size: 0.92rem;
  color: var(--mute);
  font-weight: 500;
}

#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(7.25rem, 1fr));
  gap: 0.65rem;
  align-content: start;
  padding-bottom: 0.5rem;
  width: 100%;
}

#gallery-grid button {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin: 0;
  padding: 0.55rem;
  border: 2px solid var(--line);
  background: linear-gradient(180deg, #fff 0%, #fff8ef 100%);
  color: inherit;
  cursor: pointer;
  font: inherit;
  border-radius: var(--radius-md);
  text-align: left;
  box-shadow: 0 2px 8px rgba(61, 52, 40, 0.06);
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
}

#gallery-grid button:hover {
  transform: translateY(-2px);
  border-color: var(--sky);
  box-shadow: var(--shadow-soft);
}

#gallery-grid button.active {
  border-color: var(--pink);
  background: linear-gradient(180deg, #fff5f8 0%, #fff 100%);
  box-shadow:
    0 0 0 3px rgba(244, 164, 184, 0.35),
    var(--shadow-soft);
}

#gallery-grid img {
  width: 100%;
  height: 4.75rem;
  max-height: 4.75rem;
  object-fit: contain;
  background: #fff;
  border-radius: 0.45rem;
  border: 1px solid rgba(220, 201, 176, 0.6);
}

#gallery-grid button.active .grid-thumb-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 4.75rem;
  border-radius: 0.45rem;
  border: 2px dashed var(--pink);
  background: linear-gradient(135deg, rgba(244, 164, 184, 0.2), rgba(245, 215, 110, 0.18));
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
  line-height: 1.25;
  padding: 0.35rem;
}

#gallery-grid button.active .grid-thumb-slot img {
  display: none;
}

#gallery-grid .chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

#gallery-grid .type-chip {
  display: inline-block;
  font-size: 0.62rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  font-family: var(--font-display);
  border: 2px solid rgba(61, 52, 40, 0.12);
  box-shadow: 0 2px 0 rgba(61, 52, 40, 0.07);
}

#gallery-grid .type-chip.characters {
  background: linear-gradient(180deg, #b8dcfa 0%, #7eb8e8 100%);
  color: #1e4468;
  border-color: rgba(42, 80, 112, 0.25);
}
#gallery-grid .type-chip.elements {
  background: linear-gradient(180deg, #fff0a8 0%, #f5d76e 100%);
  color: #5c4818;
  border-color: rgba(107, 85, 32, 0.28);
}
#gallery-grid .type-chip.backgrounds {
  background: linear-gradient(180deg, #c8efd8 0%, #9ed4b8 100%);
  color: #1f5038;
  border-color: rgba(42, 85, 64, 0.25);
}
#gallery-grid .type-chip.styles {
  background: linear-gradient(180deg, #ffc8d8 0%, #f4a4b8 100%);
  color: #6b2840;
  border-color: rgba(107, 48, 64, 0.25);
}

#gallery-grid .art-label {
  font-size: 0.8rem;
  line-height: 1.25;
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 700;
}

.gallery-focus {
  margin: 0 auto;
  width: 100%;
  max-width: 42rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 0.5rem 0.65rem 0.65rem;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow:
    0 8px 28px rgba(61, 52, 40, 0.1),
    0 0 0 3px rgba(245, 215, 110, 0.28);
}

.gallery-focus.hidden {
  display: none !important;
}

#gallery-page {
  display: block;
  width: 100%;
  max-width: 100%;
  max-height: min(48vh, 26rem);
  height: auto;
  margin: 0 auto;
  object-fit: contain;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: calc(var(--radius-lg) - 0.25rem);
}

#gallery-caption {
  margin-top: 0.55rem;
  color: var(--mute);
  font-size: 0.9rem;
  text-align: center;
  font-weight: 500;
}

body.section-gallery #pager {
  /* Pager still selects stickers */
}

@media (max-width: 900px) {
  html, body {
    height: 100%;
    height: 100dvh;
  }

  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "nav"
      "shelf"
      "main";
  }

  #topnav {
    flex-wrap: wrap;
    padding: 0.65rem 1rem;
  }

  #shelf {
    border-right: 0;
    border-bottom: 2px solid var(--line);
    padding: 0.75rem 1rem 0.9rem;
    overflow: hidden;
  }

  #shelf .lede {
    display: none;
  }

  h1 {
    font-size: 1.25rem;
    margin-bottom: 0.55rem;
  }

  #list {
    flex-direction: row;
    gap: 0.45rem;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 0.2rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  #list button {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    gap: 0.4rem;
    min-width: 5.4rem;
    max-width: 5.6rem;
    flex: 0 0 auto;
    align-items: start;
    justify-items: center;
    text-align: center;
    padding: 0.4rem 0.35rem 0.5rem;
  }

  #list .shelf-thumb-wrap {
    width: 4.1rem;
    height: 4.1rem;
  }

  #list .shelf-title {
    font-size: 0.78rem;
    line-height: 1.2;
  }

  #list small {
    display: none;
  }

  main > header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
    padding: 0.7rem 1rem;
  }

  .tools {
    gap: 0.55rem;
    justify-content: space-between;
  }

  .tabs-soft button {
    padding: 0.4rem 0.75rem;
    font-size: 0.82rem;
  }

  .stage-storybook figure {
    padding: 0.75rem 0.85rem 1rem;
  }

  .stage-storybook #page {
    max-height: calc(100dvh - 17.5rem);
  }

  #stage-song {
    grid-template-columns: 1fr;
    padding: 1rem;
  }

  #song-picture {
    max-height: min(42vh, 18rem);
  }

  #stage-novel {
    padding: 1.1rem 1.15rem 2.5rem;
  }

  #stage-demo.stage {
    padding: 0.55rem 0.75rem 0.9rem;
  }

  #demo-iframe {
    min-height: min(58dvh, 28rem);
  }

  .gallery-preview-dock,
  #gallery-focus-panel {
    padding: 0.55rem 0.75rem 0.65rem;
    position: relative;
    inset: auto;
  }

  .gallery-scroll {
    padding: 0.65rem 0.75rem 1rem;
  }

  #gallery-page {
    max-height: min(36vh, 14rem);
  }

  #gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  }

  #gallery-grid img,
  #gallery-grid button.active .grid-thumb-slot {
    height: 4.25rem;
    max-height: 4.25rem;
  }
}

@media (max-width: 560px) {
  #topnav {
    flex-direction: column;
    align-items: stretch;
    gap: 0.55rem;
    padding: 0.6rem 0.75rem 0.7rem;
  }

  .brand {
    font-size: 1.2rem;
  }

  .sections {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.28rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 0.05rem;
  }

  .sections::-webkit-scrollbar {
    display: none;
  }

  .section-link {
    flex: 1 0 auto;
    text-align: center;
    padding: 0.48rem 0.7rem;
    font-size: 0.86rem;
  }

  #shelf {
    padding: 0.65rem 0.75rem 0.75rem;
  }

  .kicker,
  #item-kicker {
    font-size: 0.64rem;
  }

  h1 {
    font-size: 1.15rem;
    margin-bottom: 0.45rem;
  }

  h2 {
    font-size: 1.1rem;
  }

  #list button {
    min-width: 4.9rem;
    max-width: 5.1rem;
  }

  #list .shelf-thumb-wrap {
    width: 3.7rem;
    height: 3.7rem;
  }

  .tools {
    width: 100%;
  }

  .tabs {
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .tabs::-webkit-scrollbar {
    display: none;
  }

  .tabs-soft button {
    flex: 0 0 auto;
    padding: 0.38rem 0.62rem;
    font-size: 0.78rem;
    white-space: nowrap;
  }

  .pager button {
    width: 2.5rem;
    height: 2.5rem;
  }

  .stage-storybook #page {
    max-height: calc(100dvh - 18.5rem);
  }

  #song-lyrics {
    font-size: 0.98rem;
    padding: 0.85rem 0.9rem;
  }

  #gallery-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.55rem;
  }

  #gallery-page {
    max-height: min(32vh, 12rem);
  }

  #demo-iframe {
    min-height: min(52dvh, 22rem);
    border-radius: var(--radius-md);
  }
}
