:root {
  --app-height: 100vh;
  --app-width: 100vw;
  --paper: #fffaf0;
  --ink: #253047;
  --muted: #667085;
  --blue: #4d8cf5;
  --green: #28a96b;
  --red: #e35b5b;
  --yellow: #ffd76d;
  --pink: #ff8fb3;
  --line: rgba(77, 140, 245, 0.18);
  --shadow: 0 14px 32px rgba(38, 51, 77, 0.16);
  --radius: 8px;
  font-family: "Trebuchet MS", Arial, sans-serif;
}

@supports (height: 100dvh) {
  :root { --app-height: 100dvh; }
}

* {
  box-sizing: border-box;
  min-width: 0;
  -webkit-tap-highlight-color: transparent;
}
html, body {
  width: 100%;
  height: var(--app-height);
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  color: var(--ink);
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    #f8f1dd;
  background-size: 28px 28px;
}
html {
  position: fixed;
  inset: 0;
  max-width: 100%;
  max-height: 100%;
}
body {
  position: fixed;
  inset: 0;
  max-width: 100%;
  max-height: var(--app-height);
}

button, input, select { font: inherit; }
button {
  border: 0;
  cursor: pointer;
  max-width: 100%;
  min-height: 46px;
  border-radius: var(--radius);
  color: var(--ink);
  background: #fff;
  overflow-wrap: anywhere;
  box-shadow: 0 4px 0 rgba(37, 48, 71, 0.18), 0 8px 16px rgba(37, 48, 71, 0.12);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
}
button:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(37, 48, 71, 0.18); }
button:disabled { opacity: .55; cursor: default; filter: grayscale(.3); }

.game {
  position: fixed;
  left: 0;
  top: 0;
  width: var(--app-width);
  height: var(--app-height);
  max-width: 100vw;
  max-height: var(--app-height);
  min-width: 0;
  overflow: hidden;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(70px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
.screen, .loading-screen, .rotate-screen {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 14px;
}
.screen { align-items: stretch; }
.hidden { display: none !important; }

.app-shell {
  width: min(1180px, 100%);
  height: 100%;
  min-width: 0;
  min-height: 0;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  overflow: hidden;
}
.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: rgba(255, 250, 240, .9);
  border: 3px solid #253047;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
  overflow: hidden;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 900;
  font-size: clamp(18px, 2vw, 26px);
  line-height: 1.12;
}
.brand-badge, .logo-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--yellow);
  border: 3px solid var(--ink);
  transform: rotate(-5deg);
  font-weight: 900;
}
.stats-row { display: flex; gap: 8px; justify-content: end; flex-wrap: wrap; }
.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #fff;
  border: 2px solid rgba(37, 48, 71, .16);
  font-weight: 800;
}
.icon-btn { width: 46px; min-width: 46px; padding: 0; font-weight: 900; }
.muted-note {
  position: relative;
  display: inline-block;
}
.muted-note::after {
  content: "";
  position: absolute;
  left: -0.18em;
  right: -0.18em;
  top: 52%;
  border-top: 3px solid currentColor;
  transform: rotate(-32deg);
  transform-origin: center;
}

.panel {
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  padding: clamp(12px, 2vw, 22px);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background:
    radial-gradient(circle at 12px 12px, rgba(255, 143, 179, .28) 0 5px, transparent 6px),
    rgba(255, 250, 240, .94);
  box-shadow: var(--shadow);
}
.scroll {
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-right: 4px;
}
.menu-grid, .section-grid, .level-grid, .achievement-grid {
  display: grid;
  gap: 12px;
}
.menu-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: 760px; margin: 0 auto; }
.menu-grid .primary { grid-column: 1 / -1; }
.section-grid { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.level-grid { grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); }
.achievement-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

.hero-title { text-align: center; margin: 6px 0 16px; font-size: clamp(28px, 5vw, 54px); line-height: 1.05; }
.subtitle { text-align: center; color: var(--muted); font-weight: 700; margin-top: -6px; }
.big-btn, .menu-btn, .answer-btn {
  padding: 14px 16px;
  font-weight: 900;
  font-size: clamp(16px, 2.4vw, 22px);
}
.primary { background: var(--yellow); }
.accent { background: #b8f1d3; }
.danger { background: #ffd0d0; }
.blue { background: #cfe0ff; }
.pink { background: #ffd4e2; }

.card, .section-card, .achievement-card {
  background: #fff;
  border: 2px solid rgba(37, 48, 71, .2);
  border-radius: var(--radius);
  padding: 14px;
}
.section-card { text-align: left; display: grid; gap: 8px; min-height: 128px; }
.section-card strong { font-size: 18px; }
.sticker { font-size: 30px; }
.level-btn { min-height: 76px; display: grid; gap: 2px; place-items: center; padding: 8px; font-weight: 900; }
.level-btn.locked { background: #eceff4; color: #8a94a6; }
.stars { color: #d99600; letter-spacing: 1px; min-height: 20px; }

.quiz-layout {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 12px;
}
.quiz-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.progress-track { height: 14px; border-radius: 999px; background: #e7ecf6; overflow: hidden; border: 2px solid rgba(37,48,71,.14); }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--green), var(--blue)); }
.question-card {
  display: grid;
  place-items: center;
  min-height: 120px;
  padding: 18px;
  text-align: center;
  border-radius: var(--radius);
  border: 3px dashed rgba(37,48,71,.28);
  background: #fff;
  font-size: clamp(22px, 4.5vw, 42px);
  font-weight: 900;
}
.answers { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-content: center; }
.answer-btn { min-height: 78px; background: #fff; }
.answer-btn.correct {
  background: #7ee2a8;
  border: 3px solid #168a50;
  box-shadow: 0 4px 0 #168a50, 0 8px 16px rgba(22, 138, 80, .22);
  color: #123826;
}
.answer-btn.wrong {
  background: #ff8f8f;
  border: 3px solid #c93535;
  box-shadow: 0 4px 0 #c93535, 0 8px 16px rgba(201, 53, 53, .22);
  color: #4a1717;
}
.answer-btn.correct:disabled,
.answer-btn.wrong:disabled {
  opacity: 1;
  filter: none;
}
.answer-btn.hidden-answer { visibility: hidden; }
.explain { min-height: 48px; padding: 10px; color: var(--muted); font-weight: 800; text-align: center; }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.loader-card, .rotate-card {
  width: min(420px, 92vw);
  padding: 26px;
  text-align: center;
  border-radius: var(--radius);
  border: 3px solid var(--ink);
  background: var(--paper);
  box-shadow: var(--shadow);
}
.loader-card .logo-mark { margin: 0 auto 20px; width: 74px; height: 74px; }
.loader-line { height: 16px; border: 2px solid var(--ink); border-radius: 999px; overflow: hidden; background: #fff; }
.loader-line span { display: block; height: 100%; width: 70%; background: var(--green); animation: load 1s ease-in-out infinite alternate; }
@keyframes load { from { transform: translateX(-45%); } to { transform: translateX(55%); } }
.rotate-icon { font-size: 64px; font-weight: 900; color: var(--blue); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 28px));
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  box-shadow: var(--shadow);
  z-index: 5;
}

.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 2px dashed rgba(37,48,71,.12); }
.result-score { font-size: clamp(34px, 7vw, 70px); text-align: center; font-weight: 900; }
.chalkboard { background: #264237; color: #f8fff4; border-color: #1a2b25; }
.chalkboard .subtitle, .chalkboard p { color: #dceee4; }
.leaderboard-card { max-width: 620px; margin: 0 auto; }
.leaderboard-list {
  display: grid;
  gap: 8px;
  max-width: 720px;
  margin: 0 auto;
}
.leaderboard-row {
  display: grid;
  grid-template-columns: 70px minmax(0, 1fr) 100px;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 2px solid rgba(37, 48, 71, .18);
  border-radius: var(--radius);
  background: #fff;
  font-weight: 800;
}
.leaderboard-row strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leaderboard-row span:last-child {
  text-align: right;
}
.leaderboard-head {
  background: var(--yellow);
  border-color: rgba(37, 48, 71, .35);
}
.leaderboard-me {
  background: #cfe0ff;
  border-color: var(--blue);
}

@media (max-width: 640px) {
  .game { padding: 0; }
  .screen, .loading-screen, .rotate-screen {
    padding:
      max(6px, env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      calc(128px + env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }
  .app-shell {
    gap: 6px;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .topbar {
    grid-template-columns: 38px minmax(0, 1fr) 38px;
    grid-template-areas: "back brand sound";
    gap: 6px;
    padding: 6px;
    border-width: 2px;
  }
  .topbar > button:first-child { grid-area: back; }
  .topbar > button[data-action="toggle-sound"] { grid-area: sound; }
  .icon-btn {
    width: 38px;
    min-width: 38px;
    min-height: 38px;
  }
  .brand {
    grid-area: brand;
    justify-content: center;
    font-size: clamp(15px, 4.7vw, 20px);
    line-height: 1.05;
    text-align: center;
  }
  .brand-badge {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-width: 2px;
    font-size: 15px;
  }
  .brand { min-width: 0; }
  .stats-row { display: none; }
  .pill {
    min-height: 27px;
    padding: 3px 7px;
    font-size: 13px;
    line-height: 1;
  }
  .menu-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }
  .menu-grid .primary { grid-column: 1 / -1; }
  .section-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }
  .level-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
  }
  .achievement-grid {
    grid-template-columns: 1fr;
    gap: 7px;
  }
  .panel {
    padding: 8px;
    border-width: 2px;
  }
  .panel > h2 {
    margin: 2px 0 8px;
    font-size: 22px;
    line-height: 1.1;
  }
  .card, .section-card, .achievement-card {
    padding: 8px;
    border-width: 2px;
    overflow-wrap: anywhere;
  }
  .section-card {
    min-height: 86px;
    gap: 4px;
  }
  .section-card strong {
    font-size: 15px;
    line-height: 1.08;
  }
  .section-card span:last-child {
    font-size: 12px;
    line-height: 1.12;
  }
  .sticker { font-size: 23px; }
  .level-btn {
    min-height: 54px;
    padding: 5px;
    font-size: 14px;
  }
  .stars {
    min-height: 14px;
    font-size: 12px;
  }
  .card h3 {
    margin: 0 0 6px;
    font-size: 15px;
    line-height: 1.1;
  }
  .card .actions {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 5px;
  }
  .card .actions button {
    min-height: 38px;
    padding: 5px 2px;
    font-size: 13px;
  }
  .hero-title {
    margin: 0 0 8px;
    font-size: clamp(26px, 8vw, 34px);
  }
  .subtitle {
    margin: 0 0 10px;
    font-size: 16px;
  }
  .menu-btn {
    min-height: 54px;
    padding: 9px 10px;
    font-size: clamp(15px, 4.1vw, 18px);
    line-height: 1.08;
    overflow-wrap: anywhere;
  }
  .quiz-layout {
    height: auto;
    min-height: 100%;
    grid-template-rows: auto auto auto auto;
    gap: 7px;
  }
  .quiz-head {
    gap: 5px;
    justify-content: center;
  }
  .progress-track {
    height: 9px;
    border-width: 1px;
  }
  .question-card {
    min-height: 82px;
    max-height: 130px;
    padding: 10px;
    overflow: auto;
    border-width: 2px;
    font-size: clamp(18px, 5.5vw, 24px);
    line-height: 1.14;
  }
  .answers {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }
  .answer-btn {
    min-height: 52px;
    padding: 7px 8px;
    font-size: clamp(17px, 5vw, 22px);
    line-height: 1.05;
  }
  .explain {
    min-height: 32px;
    max-height: 58px;
    overflow: auto;
    padding: 5px;
    font-size: 13px;
    line-height: 1.15;
  }
  .actions {
    gap: 6px;
  }
  .quiz-layout > div:last-child > .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .quiz-layout > div:last-child > .actions button:only-child {
    grid-column: 1 / -1;
  }
  .actions button,
  .big-btn {
    min-height: 42px;
    padding: 7px 9px;
    font-size: 14px;
    line-height: 1.1;
    overflow-wrap: anywhere;
  }
  .result-score {
    font-size: clamp(34px, 15vw, 52px);
    line-height: 1;
    margin: 6px 0;
  }
  .settings-row {
    padding: 8px 0;
  }
  .settings-row button {
    min-width: 92px;
  }
  .achievement-card {
    font-size: 14px;
    line-height: 1.12;
  }
  .app-progress .panel {
    padding: 6px;
  }
  .app-progress .panel > h2 {
    margin: 0 0 5px;
    font-size: 18px;
  }
  .app-progress .section-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }
  .app-progress .card {
    padding: 6px 4px;
    text-align: center;
  }
  .app-progress .card strong {
    display: block;
    font-size: 11px;
    line-height: 1.05;
  }
  .app-progress .result-score {
    font-size: clamp(20px, 7vw, 28px);
    line-height: 1;
    margin: 3px 0 0;
  }
  .app-progress .achievement-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
  }
  .app-progress .achievement-card {
    min-height: 0;
    padding: 5px;
    font-size: 11px;
    line-height: 1.05;
  }
  .app-progress .achievement-card strong {
    display: -webkit-box;
    overflow: hidden;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    font-size: 11px;
    line-height: 1.05;
  }
  .app-progress .achievement-card span {
    font-size: 10px;
  }
  .leaderboard-list {
    gap: 6px;
  }
  .leaderboard-row {
    grid-template-columns: 44px minmax(0, 1fr) 62px;
    gap: 6px;
    padding: 8px;
    font-size: 13px;
    line-height: 1.08;
  }
  .leaderboard-head {
    font-size: 12px;
  }
}

@media (max-width: 360px) {
  .screen, .loading-screen, .rotate-screen {
    padding:
      max(5px, env(safe-area-inset-top))
      max(5px, env(safe-area-inset-right))
      calc(124px + env(safe-area-inset-bottom))
      max(5px, env(safe-area-inset-left));
  }
  .topbar { grid-template-columns: 34px minmax(0, 1fr) 34px; gap: 5px; padding: 5px; }
  .icon-btn { width: 34px; min-width: 34px; min-height: 34px; }
  .brand { font-size: 14px; }
  .brand-badge { width: 32px; height: 32px; min-width: 32px; font-size: 13px; }
  .pill { font-size: 12px; padding: 3px 6px; }
  .menu-btn { min-height: 48px; font-size: 14px; }
  .section-grid { gap: 6px; }
  .section-card { min-height: 78px; }
  .level-grid { gap: 6px; }
  .level-btn { min-height: 50px; }
  .app-progress .panel > h2 { font-size: 16px; margin-bottom: 4px; }
  .app-progress .section-grid { gap: 4px; }
  .app-progress .card { padding: 5px 3px; }
  .app-progress .card strong { font-size: 10px; }
  .app-progress .result-score { font-size: clamp(18px, 7vw, 24px); }
  .app-progress .achievement-grid { gap: 4px; }
  .app-progress .achievement-card { padding: 4px; font-size: 10px; }
  .app-progress .achievement-card strong { font-size: 10px; }
  .question-card { min-height: 76px; max-height: 112px; }
  .answer-btn { min-height: 48px; font-size: 17px; }
}

@media (orientation: landscape) and (max-width: 820px) and (pointer: coarse) {
  #screen, #loading { display: none !important; }
  #rotate { display: grid !important; }
}
