/* The student page, for Bricks stories, ENK presentations and worksheets alike (Power Bricks'
   page with ENK Studio's Korean caption and progress). Big targets for small fingers, no hover,
   and a page that never scrolls: the stage zooms and pans inside itself. */

:root {
  --bg: #20232a;
  --panel: #2b2f38;
  --text: #ffffff;
  --muted: #b8bfcc;
  --accent: #7b61ff;
  --danger: #d8352a;
  --tool: 56px;
  /* On for a switch (한국어), so it never reads as a fourth mode beside Listen, Write and Erase. */
  --switch-on: #1f8a7a;
  /* The level colours of the printed page's QR badge (QrBadge.levelLook). */
  --level-1: #1e6fd9;
  --level-2: #2e9e4f;
  --level-3: #f5c400;
  --level-4: #f28c1b;
  --level-5: #d8352a;
}

html, body {
  margin: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  position: fixed;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font: 18px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

svg { width: 26px; height: 26px; fill: currentColor; }
svg .line { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* The level list. Sizes shrink a little on short screens (a landscape monitor) so the list fits
   without scrolling; each first value is the fallback for browsers without clamp() (iOS 13.0-13.3). */

#landing {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
  padding: max(20px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  max-width: 520px;
  margin: 0 auto;
  /* It still scrolls when it must, but shows no scroll bar beside the buttons. */
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
#landing::-webkit-scrollbar { display: none; }
#landing { position: relative; z-index: 1; }
#landing h1 { font-size: 24px; font-size: clamp(20px, 3.4vh, 24px); margin: 4px 0 2px; text-shadow: 0 1px 6px rgba(0, 0, 0, 0.7); }
#story-book { color: var(--muted); font-size: 16px; margin: 0 0 18px; margin-bottom: clamp(10px, 2.2vh, 18px); text-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); }

/* A Bricks story's levels: the story's cleaned page whole, in a frame, beside a column of level
   cards when the screen is wide and sideways, above it otherwise. */
/* Upright (a phone or tablet held as students read), one column no wider than a phone's
   sideways width, so the cards don't stretch across a tablet; sideways, room for both. */
#landing.story { max-width: 560px; }
@media (min-width: 760px) and (orientation: landscape) { #landing.story { max-width: 1120px; } }
#story-cover { margin: 8px 0 14px; }
#story-cover img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 46vh;
  object-fit: contain;
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
@media (min-width: 760px) and (orientation: landscape) {
  #landing.story #story-layout {
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(300px, 1fr);
    gap: 32px;
    align-items: center;
    min-height: calc(100% - 40px);
  }
  #landing.story #story-cover { margin: 0; }
  #landing.story #story-cover img { max-height: calc(100vh - 110px); }
}
#landing.story h1 { font-size: 30px; font-size: clamp(24px, 4.2vh, 34px); line-height: 1.15; text-shadow: none; }
#landing.story #story-book { margin-bottom: 4px; text-shadow: none; }
#choose-level { margin: 14px 0 10px; margin: clamp(8px, 2vh, 18px) 0 clamp(6px, 1.4vh, 10px); color: var(--muted); font-size: 16px; font-weight: 600; letter-spacing: 0.02em; }

/* A level: its number in the printed badge's colour, its name and length, and five bricks for
   how hard it is. The whole card is one big target. */
#levels { display: flex; flex-direction: column; gap: 10px; gap: clamp(6px, 1.4vh, 10px); }
.level-card {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 72px;
  min-height: clamp(58px, 9.5vh, 76px);
  padding: 8px 16px 8px 10px;
  border-radius: 16px;
  border-left: 6px solid var(--level-colour);
  background: var(--panel);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.3);
  text-align: left;
}
.level-card:active { filter: brightness(1.15); transform: scale(0.99); }
.level-card.level-1 { --level-colour: var(--level-1); --level-ink: #fff; }
.level-card.level-2 { --level-colour: var(--level-2); --level-ink: #fff; }
.level-card.level-3 { --level-colour: var(--level-3); --level-ink: #101012; }
.level-card.level-4 { --level-colour: var(--level-4); --level-ink: #101012; }
.level-card.level-5 { --level-colour: var(--level-5); --level-ink: #fff; }
.level-number {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  width: clamp(38px, 6.6vh, 48px);
  height: clamp(38px, 6.6vh, 48px);
  border-radius: 12px;
  background: var(--level-colour);
  color: var(--level-ink);
  font-size: 24px;
  font-weight: 800;
}
.level-words { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.level-name { font-size: 20px; font-size: clamp(18px, 2.7vh, 21px); font-weight: 700; line-height: 1.2; }
/* Empty until the level's JSON arrives; it keeps its line so the card doesn't jump. */
.level-length { min-height: 1.3em; color: var(--muted); font-size: 15px; line-height: 1.3; }
.level-bricks { flex: none; display: flex; gap: 3px; }
.level-bricks i { width: 10px; height: 16px; border-radius: 3px; background: rgba(255, 255, 255, 0.14); }
.level-bricks i.on { background: var(--level-colour); }

.message { color: var(--muted); padding: 24px 16px; text-align: center; font-size: 18px; }

/* The list of every story: a heading per book, then its stories. */
.book-heading {
  font-size: 17px;
  margin: 14px 0 2px;
  margin-top: clamp(8px, 2vh, 14px);
  padding-bottom: 4px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.25);
  color: var(--text);
}
.book-heading:first-child { margin-top: 0; }
.story-button {
  min-height: 46px;
  min-height: clamp(40px, 6vh, 46px);
  border-radius: 12px;
  padding: 6px 16px;
  text-align: left;
  font-size: 17px;
  font-weight: 600;
  background: var(--panel);
}
.story-button:active { filter: brightness(1.15); }

/* A list with pictures (the tongue twisters): a grid of tiles, each its picture with its name
   under it, the whole tile one big target. Wider than the button column, so a tablet shows more
   at once. The square is the padding trick, since aspect-ratio needs iOS 14.5. */
#landing.wide { max-width: 960px; }
#levels.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
}
.tile {
  display: flex;
  flex-direction: column;
  padding: 8px;
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.35);
  text-align: center;
}
.tile:active { filter: brightness(1.15); transform: scale(0.98); }
.tile-picture {
  position: relative;
  display: block;
  width: 100%;
  padding-top: 100%;
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
}
.tile-picture img { position: absolute; inset: 0; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; }
/* The Bricks stories: each its cover alone (the title is printed on it), as wide as the book's
   spread. Big enough to read, but at least two fit across a phone held sideways; held upright,
   a phone shows one at a time and the next peeking in below. */
#levels.wide-tiles { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
#levels.wide-tiles .tile { padding: 6px; }
#levels.wide-tiles .tile-picture { padding-top: 70.7%; background: var(--bg); }
#levels.tiles .book-heading { grid-column: 1 / -1; margin-top: 10px; }
#levels.tiles .book-heading:first-child { margin-top: 0; }

/* The presentation list's order. */
.sort { display: flex; align-items: center; gap: 8px; margin: 0 0 14px; }
.sort-label { color: var(--muted); font-size: 15px; }
.sort button {
  min-height: 38px;
  padding: 4px 16px;
  border-radius: 19px;
  font-size: 15px;
  font-weight: 600;
  background: var(--panel);
  color: var(--muted);
}
.sort button[aria-pressed="true"] { background: var(--accent); color: #fff; }

/* More of the list below the screen: a round arrow at the bottom, which scrolls on a tap. */
#more {
  position: fixed;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 2;
  width: 48px;
  height: 48px;
  margin-left: -24px;
  border-radius: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.5);
  animation: more-bob 1.6s ease-in-out infinite;
}
#more[hidden] { display: none; }
@keyframes more-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(5px); } }
@media (prefers-reduced-motion: reduce) { #more { animation: none; } }
/* The QR button, top right over the list, and the code it shows: as big as the screen allows,
   black on white with its border, so a phone across the classroom can read it. */
#qr-button {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px 0 10px;
  border-radius: 22px;
  background: var(--panel);
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.45);
}
#qr-button:active { filter: brightness(1.15); }
/* Where the button could sit over the title, the title stops short of it. */
@media (max-width: 760px) { #landing h1 { padding-right: 84px; } }
#qr-button svg { width: 22px; height: 22px; }
#qr-view {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  box-sizing: border-box;
  background: var(--bg);
}
#qr-code { width: min(86vw, calc(100vh - 150px)); height: min(86vw, calc(100vh - 150px)); }
#qr-code svg { display: block; width: 100%; height: 100%; border-radius: 12px; }
#qr-url { margin: 0; max-width: 100%; color: var(--muted); font-size: 16px; text-align: center; overflow-wrap: anywhere; }
#qr-close {
  min-height: 48px;
  min-width: 140px;
  border-radius: 14px;
  font-size: 18px;
  font-weight: 700;
  background: var(--accent);
  color: #fff;
}
.tile-name {
  display: block;
  padding: 8px 2px 2px;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.25;
}
.text-link {
  background: none;
  color: var(--muted);
  padding: 8px 0;
  min-height: 40px;
  font-size: 15px;
}
#fatal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; margin: 0; }

/* One level's page. */

#reader {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

#stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  touch-action: none;
  background: #3a3f4a;
}
#stage canvas { position: absolute; left: 0; top: 0; touch-action: none; }

.floating {
  position: absolute;
  z-index: 2;
}
/* Messages over the page never get in the way of a finger. */
#status, #turn-hint, #caption { pointer-events: none; }

#back {
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 48px;
  padding: 0 16px 0 8px;
  border-radius: 24px;
  background: rgba(32, 35, 42, 0.85);
  font-weight: 600;
}
#back svg path { fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }

#status, #turn-hint {
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 10px 18px;
  border-radius: 20px;
  background: rgba(32, 35, 42, 0.9);
  text-align: center;
  max-width: calc(100% - 32px);
  box-sizing: border-box;
}
#status { top: max(68px, calc(env(safe-area-inset-top) + 58px)); min-width: 200px; }
.status-bar {
  display: block;
  height: 6px;
  margin-top: 8px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.18);
  overflow: hidden;
}
.status-bar > span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--accent);
  transition: width 0.2s linear;
}
/* Size not known: a stripe sliding along instead of a filling bar. */
.status-bar.unknown > span { width: 30%; animation: status-slide 1.2s ease-in-out infinite; }
@keyframes status-slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
#turn-hint { bottom: 12px; color: var(--muted); font-size: 16px; }

/* The Korean caption: one line's meaning, under the page's words rather than over them. */
#caption {
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  box-sizing: border-box;
  padding: 12px 20px;
  border-radius: 16px;
  background: rgba(32, 35, 42, 0.92);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  text-align: center;
  font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  font-size: 22px;
  font-size: clamp(18px, 3.2vmin, 26px);
  line-height: 1.45;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

#toolbar {
  display: flex;
  gap: 8px;
  padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: var(--panel);
  overflow-x: auto;
  justify-content: center;
}
.tool {
  flex: 1 1 0;
  min-width: 52px;
  max-width: 120px;
  min-height: var(--tool);
  border-radius: 14px;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 4px;
}
.tool:active { background: rgba(255, 255, 255, 0.12); }
.tool[aria-pressed="true"] { background: var(--accent); }
.tool:disabled { opacity: 0.4; }
#play-all .stop { display: none; }
#play-all.playing .play { display: none; }
#play-all.playing .stop { display: inline; }

/* The pen: its colour shown on the Write button, and a panel to change colour and size. */
.icon-wrap { position: relative; display: inline-flex; }
.pen-dot {
  position: absolute;
  right: -5px;
  bottom: -3px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: #ff0000;
}
#pen-panel {
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  background: var(--panel);
  border-radius: 18px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.45);
  max-width: calc(100% - 24px);
  box-sizing: border-box;
}
.pen-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.pen-colour, .pen-width {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #3a3f4a;
  border: 3px solid transparent;
  padding: 0;
}
.pen-colour[aria-pressed="true"], .pen-width[aria-pressed="true"] { border-color: #fff; }
.pen-width span { display: block; border-radius: 50%; background: #fff; }

/* The Clear confirmation: big buttons, no browser dialog (iOS draws those tiny). */
#confirm {
  position: absolute;
  inset: 0;
  z-index: 3;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.dialog {
  background: var(--panel);
  border-radius: 18px;
  padding: 24px;
  max-width: 420px;
  width: 100%;
  box-sizing: border-box;
  text-align: center;
}
.dialog p { font-size: 22px; margin: 0 0 20px; }
.dialog-buttons { display: flex; gap: 12px; }
.dialog-buttons button {
  flex: 1;
  min-height: 60px;
  border-radius: 14px;
  font-size: 20px;
  font-weight: 700;
  background: #454b57;
}
.dialog-buttons .danger { background: var(--danger); }

/* The 한국어 switch. */
.tool.toggle[aria-pressed="true"] { background: var(--switch-on); }
#korean .glyph { font-size: 22px; line-height: 26px; height: 26px; font-weight: 700; }
#korean span { font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif; }
/* Seven tools on a 375-pixel phone: closer together, so none scrolls off. */
@media (max-width: 440px) {
  #toolbar { gap: 4px; }
  .tool { min-width: 46px; }
}
