:root {
  color-scheme: dark;
  --cream: #fff5d9;
  --moss: #6f8d4e;
  --deep: #0c1c14;
  --bark: #583b27;
  --gold: #ffd45f;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background: #0a1711;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
}

body {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  color: var(--cream);
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 12%, rgba(130, 173, 105, .22), transparent 34%),
    linear-gradient(160deg, #13291d, #07100c 75%);
}

button { font: inherit; }

.game-shell {
  width: min(100vw, 150vh);
  padding: clamp(0px, 1.2vw, 18px);
  user-select: none;
  -webkit-user-select: none;
}

.game-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: clamp(0px, 2vw, 24px);
  background: #183627;
  box-shadow: 0 24px 80px rgba(0,0,0,.5), inset 0 0 0 1px rgba(255,255,255,.06);
  touch-action: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.hud {
  position: absolute;
  inset: clamp(10px, 2vw, 24px) clamp(10px, 2.2vw, 28px) auto;
  z-index: 5;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: start;
  pointer-events: none;
  opacity: 0;
  transform: translateY(-8px);
  transition: .25s ease;
}

.hud.active { opacity: 1; transform: none; }

.hud-pill {
  justify-self: start;
  min-width: 116px;
  padding: 10px 16px;
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 14px;
  background: rgba(7,18,12,.62);
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
  backdrop-filter: blur(10px);
}

.hud-pill strong { display: block; font-size: clamp(22px, 3vw, 38px); line-height: .95; }
.hud-label { display: block; margin-bottom: 4px; color: rgba(255,245,217,.68); font-size: 11px; letter-spacing: .14em; text-transform: uppercase; }
.hud-right { justify-self: end; text-align: right; }
.hud-center { min-width: 210px; padding-bottom: 9px; text-align: center; color: var(--gold); font-weight: 800; transition: border-color .2s, box-shadow .2s; }
.hud-center .hud-dot { display: inline-block; width: 4px; height: 4px; margin: 0 9px 3px; border-radius: 50%; background: rgba(255,255,255,.4); }
.harmony-meter { display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: center; margin-top: 7px; color: rgba(255,245,217,.68); font-size: 9px; font-weight: 900; letter-spacing: .09em; text-transform: uppercase; }
.harmony-meter b { display: block; height: 5px; overflow: hidden; border-radius: 9px; background: rgba(255,255,255,.12); }
.harmony-meter em { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #f0c857, #ff9fb2, #f9f2a6); box-shadow: 0 0 9px #ffe477; transition: width .16s ease; }
.hud-center.blooming { border-color: rgba(255,224,113,.7); box-shadow: 0 0 28px rgba(255,202,88,.28), inset 0 0 15px rgba(255,225,135,.1); }
.hud-center.blooming .harmony-meter { color: #fff2ab; }
.hud-center.blooming .harmony-meter em { width: 100% !important; animation: harmony-glow .42s ease-in-out infinite alternate; }
@keyframes harmony-glow { to { filter: brightness(1.35); box-shadow: 0 0 14px #fff2a6; } }

.screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  opacity: 0;
  visibility: hidden;
  transform: scale(1.015);
  transition: opacity .35s ease, transform .35s ease, visibility .35s;
  background: radial-gradient(circle at center, rgba(14,35,23,.38), rgba(3,12,8,.78));
}

.screen.visible { opacity: 1; visibility: visible; transform: none; }

.screen-card {
  width: min(540px, 92%);
  padding: clamp(25px, 4.2vw, 54px);
  text-align: center;
  border: 1px solid rgba(255,247,220,.22);
  border-radius: 28px;
  background: linear-gradient(145deg, rgba(22,52,34,.92), rgba(8,23,15,.94));
  box-shadow: 0 30px 90px rgba(0,0,0,.42), inset 0 1px rgba(255,255,255,.08);
  backdrop-filter: blur(12px);
}

.screen-card.with-leaderboard { width: min(680px, 94%); padding: clamp(20px, 3vw, 38px); }

.leaderboard {
  margin-top: 18px;
  padding: 12px 14px 10px;
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 16px;
  background: rgba(4,16,10,.35);
  text-align: left;
}

.leaderboard.compact { margin-top: 16px; }
.leaderboard-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
.leaderboard-heading h3 { margin: 0; color: #ffe28a; font-size: 12px; letter-spacing: .16em; text-transform: uppercase; }
.leaderboard-heading span { color: rgba(255,245,217,.46); font-size: 10px; }
.leaderboard-list { display: block; margin: 0; padding: 0; list-style: none; counter-reset: rank; }
.leaderboard-list li { counter-increment: rank; display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; gap: 7px; min-width: 0; color: rgba(255,245,217,.76); font-size: 12px; }
.leaderboard-list li::before { content: counter(rank) "."; color: rgba(255,226,138,.55); font-weight: 900; }
.leaderboard-list .board-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-list .board-score { color: white; font-weight: 900; font-variant-numeric: tabular-nums; }
.leaderboard-list .empty-board { grid-column: 1 / -1; display: block; text-align: center; color: rgba(255,245,217,.45); }
.leaderboard-list .empty-board::before { content: none; }

.top-ten-form { margin: 0 auto 18px; padding: 13px; border: 1px solid rgba(255,215,91,.42); border-radius: 16px; background: rgba(108,75,22,.25); }
.top-ten-form[hidden] { display: none; }
.top-ten-form strong, .top-ten-form span { display: block; }
.top-ten-form strong { color: #ffe279; }
.top-ten-form span { margin: 3px 0 10px; color: rgba(255,245,217,.72); font-size: 12px; }
.top-ten-form div { display: flex; gap: 7px; }
.top-ten-form input { min-width: 0; flex: 1; padding: 10px 12px; border: 1px solid rgba(255,255,255,.2); border-radius: 10px; color: white; background: rgba(0,0,0,.25); outline: none; }
.top-ten-form input:focus { border-color: #ffe279; box-shadow: 0 0 0 3px rgba(255,226,121,.18); }
.top-ten-form button { padding: 9px 14px; border: 0; border-radius: 10px; color: #17301d; background: #ffd665; font-weight: 900; cursor: pointer; }
.top-ten-form button:disabled { opacity: .5; cursor: default; filter: grayscale(.35); }
.top-ten-form small { display: block; min-height: 13px; margin-top: 6px; color: #d7e7aa; }
.privacy-note { margin: 7px 0 0; color: rgba(255,245,217,.64); font-size: 10px; font-weight: 500; line-height: 1.45; }
.privacy-note a, .legal-links a { color: #ffe28a; text-underline-offset: 3px; }
.privacy-note a:hover, .privacy-note a:focus-visible, .legal-links a:hover, .legal-links a:focus-visible { color: #fff8df; }
.legal-links { display: flex; justify-content: center; gap: 8px; margin-top: 10px; color: rgba(255,245,217,.42); font-size: 10px; }

.eyebrow {
  margin: 0 0 8px;
  color: #c6dc9a;
  font-size: clamp(10px, 1.3vw, 14px);
  font-weight: 800;
  letter-spacing: .2em;
  text-transform: uppercase;
}

h1, h2 {
  margin: 0;
  color: #fff8df;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(44px, 7.2vw, 76px);
  font-weight: 600;
  line-height: .9;
  letter-spacing: -.055em;
  text-shadow: 0 4px 0 rgba(54,35,21,.4), 0 15px 40px rgba(0,0,0,.35);
}

h2 { font-size: clamp(38px, 6vw, 70px); }
.subtitle { margin: 9px 0 24px; color: #e8c982; font: italic clamp(18px, 2.8vw, 28px) Georgia, serif; }

.instructions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 18px;
  margin: 0 auto 26px;
  color: rgba(255,245,217,.76);
  font-size: clamp(12px, 1.7vw, 16px);
}

.instructions b {
  display: inline-block;
  margin-right: 5px;
  color: white;
  letter-spacing: .12em;
}

.primary-button {
  min-width: 210px;
  padding: 15px 27px;
  border: 0;
  border-radius: 999px;
  color: #17301d;
  background: linear-gradient(#ffe88f, #efbd43);
  box-shadow: 0 7px 0 #916424, 0 15px 40px rgba(0,0,0,.35);
  cursor: pointer;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: .02em;
  transition: transform .12s ease, filter .12s ease, box-shadow .12s ease;
}

.primary-button:hover { filter: brightness(1.08); transform: translateY(-2px); }
.primary-button:active { transform: translateY(5px); box-shadow: 0 2px 0 #916424, 0 8px 18px rgba(0,0,0,.3); }
.primary-button:disabled { opacity: .62; cursor: wait; filter: saturate(.65); transform: none; }
.primary-button:focus-visible { outline: 4px solid white; outline-offset: 4px; }
.sound-note { margin: 22px 0 0; color: rgba(255,245,217,.48); font-size: 12px; }

.final-score { margin: 14px auto 10px; }
.final-score span { display: block; color: rgba(255,245,217,.65); font-size: 12px; letter-spacing: .18em; text-transform: uppercase; }
.final-score strong { display: block; margin: 5px 0; color: var(--gold); font: 700 clamp(58px, 10vw, 112px)/.9 Georgia, serif; }
.record-line { margin: 14px 0 22px; color: #d2dfa7; font-weight: 800; }

.touch-controls {
  position: absolute;
  z-index: 7;
  inset: auto max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: clamp(14px, 8vw, 80px);
  pointer-events: none;
  opacity: 0;
  transform: translateY(12px);
  transition: .25s ease;
}

.touch-controls.active { opacity: 1; transform: none; }
.touch-controls button {
  height: clamp(58px, 13vw, 86px);
  border: 1px solid rgba(255,255,255,.24);
  border-radius: 19px;
  color: white;
  background: rgba(9,25,16,.55);
  box-shadow: 0 8px 25px rgba(0,0,0,.23), inset 0 1px rgba(255,255,255,.1);
  backdrop-filter: blur(7px);
  pointer-events: auto;
  touch-action: manipulation;
}
.touch-controls button:active, .touch-controls button.pressed { background: rgba(244,198,76,.58); transform: translateY(2px); }
.touch-controls span { display: block; font-size: 32px; line-height: 1; }
.touch-controls.scrambled button {
  border-color: rgba(218,196,237,.72);
  background: linear-gradient(145deg, rgba(83,70,98,.82), rgba(41,38,51,.9));
  box-shadow: 0 10px 26px rgba(17,12,24,.34), inset 0 1px rgba(255,255,255,.16);
}
.touch-controls.scrambled span { color: #f2e8ff; }

.desktop-hint { margin: 10px 0 0; text-align: center; color: rgba(255,245,217,.38); font-size: 12px; }

.legal-page { display: block; min-height: 100vh; min-height: 100svh; padding: clamp(16px, 4vw, 48px); overflow-x: hidden; overflow-y: auto; color: var(--cream); user-select: text; -webkit-user-select: text; }
.legal-shell { width: min(850px, 100%); margin: 0 auto; border: 1px solid rgba(255,247,220,.22); border-radius: 24px; background: linear-gradient(145deg, rgba(22,52,34,.97), rgba(8,23,15,.98)); box-shadow: 0 30px 90px rgba(0,0,0,.45), inset 0 1px rgba(255,255,255,.08); }
.legal-header { padding: clamp(26px, 6vw, 58px); border-bottom: 1px solid rgba(255,247,220,.16); background: radial-gradient(circle at 18% 0, rgba(198,220,154,.18), transparent 45%); }
.legal-header .eyebrow { margin-bottom: 12px; }
.legal-header h1 { font-size: clamp(3.2rem, 10vw, 6.4rem); }
.legal-header p:last-child { max-width: 650px; margin: 20px 0 0; color: rgba(255,245,217,.7); line-height: 1.6; }
.legal-content { padding: clamp(24px, 5vw, 48px); }
.legal-content section + section { margin-top: 28px; padding-top: 25px; border-top: 1px solid rgba(255,247,220,.14); }
.legal-content h2 { margin: 0 0 10px; color: #ffe28a; font: 600 1.45rem/1.1 Georgia, "Times New Roman", serif; }
.legal-content p { color: rgba(255,245,217,.82); font-weight: 400; line-height: 1.7; }
.legal-content a, .legal-footer a { color: #d7e7aa; text-underline-offset: 3px; }
.legal-content code { padding: .12rem .38rem; border: 1px solid rgba(255,226,138,.3); border-radius: 4px; background: rgba(0,0,0,.25); color: #ffe28a; }
.legal-footer { padding: 19px clamp(24px, 5vw, 48px); display: flex; flex-wrap: wrap; gap: 10px 22px; border-top: 1px solid rgba(255,247,220,.16); color: rgba(255,245,217,.55); font-size: 12px; }

@media (pointer: coarse), (max-width: 820px) {
  .touch-controls { display: grid; }
  .desktop-hint { display: none; }
  .game-shell { width: 100vw; padding: 0; }
  .game-frame { border: 0; border-radius: 0; }
  .hud { bottom: auto; }
}

@media (max-aspect-ratio: 4/5) {
  body { align-items: center; }
  .game-shell { width: 100vw; }
  .game-frame { aspect-ratio: 4 / 5; }
  canvas { width: auto; height: 100%; margin-left: 50%; transform: translateX(-50%); }
  .screen-card { width: min(92%, 480px); padding: 28px 21px; }
  .screen { align-items: flex-start; overflow-y: auto; padding: 8px 0; overscroll-behavior: contain; }
  .screen-card.with-leaderboard { max-height: calc(100% - 16px); padding: 18px 16px; overflow-y: auto; scrollbar-width: thin; scrollbar-color: rgba(255,226,121,.5) transparent; }
  .screen-card.with-leaderboard::-webkit-scrollbar { width: 5px; }
  .screen-card.with-leaderboard::-webkit-scrollbar-thumb { border-radius: 8px; background: rgba(255,226,121,.5); }
  .leaderboard { margin-top: 12px; padding: 10px 9px 9px; }
  .leaderboard-list li { grid-template-columns: 18px minmax(0, 1fr) auto; gap: 5px; font-size: 11px; }
  .game-over-card .subtitle { margin-bottom: 10px; }
  .game-over-card .final-score strong { font-size: 52px; }
  .game-over-card .record-line { margin: 8px 0 12px; }
  .instructions { flex-direction: column; gap: 6px; }
  .hud-center { min-width: 142px; }
  .harmony-meter { grid-template-columns: 1fr; gap: 3px; }
  .harmony-meter b { height: 4px; }
  .hud-pill { min-width: 90px; padding: 8px 11px; }
}

@media (max-height: 520px) {
  .screen-card { padding: 20px 30px; }
  .subtitle { margin-bottom: 13px; }
  .instructions { margin-bottom: 15px; }
  .sound-note { display: none; }
  .touch-controls button { height: 55px; }
  .leaderboard { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
