:root {
  --abyss: #04232c;
  --deep: #0a3a45;
  --teal: #1a6a6e;
  --sea: #2a9b96;
  --foam: #d4f0ea;
  --sand: #f4e4c1;
  --cream: #fff8ee;
  --coral: #e85d4c;
  --lobster: #d94a3d;
  --gold: #f0c14b;
  --ink: #14343a;
  --muted: #8eb8b4;
  --card: rgba(255, 248, 238, 0.94);
  --shadow: 0 12px 40px rgba(0, 16, 22, 0.35);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(1200px 700px at 20% -10%, #0e4b55 0%, var(--abyss) 55%, #02161c 100%);
  color: var(--cream);
  font-family: ui-rounded, "Trebuchet MS", "Segoe UI", "Avenir Next", "Avenir", system-ui, sans-serif;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
}

.bg-waves {
  pointer-events: none;
  position: fixed;
  inset: 0;
  background:
    radial-gradient(circle at 80% 120%, rgba(42, 155, 150, 0.18), transparent 45%),
    radial-gradient(circle at 10% 80%, rgba(232, 93, 76, 0.08), transparent 40%);
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 1100px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 16px calc(20px + var(--safe-bot));
}

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

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  border: 0;
  color: inherit;
  cursor: pointer;
  padding: 4px 0;
  font: inherit;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background:
    radial-gradient(circle at 35% 30%, #fff8ee 2px, transparent 3px),
    radial-gradient(circle at 62% 30%, #fff8ee 2px, transparent 3px),
    linear-gradient(160deg, #f07a6a, var(--lobster));
  box-shadow: 0 4px 0 #9b2e26, inset 0 2px 0 rgba(255,255,255,0.25);
  flex-shrink: 0;
}

.brand-text {
  font-weight: 800;
  letter-spacing: 0.02em;
  font-size: 1.1rem;
}

.brand-text em {
  font-style: normal;
  color: var(--gold);
}

.topbar-actions { display: flex; gap: 8px; }

.icon-btn, .act, .cmd {
  font: inherit;
  cursor: pointer;
  border: 0;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

.icon-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  border-radius: 14px;
  background: rgba(255, 248, 238, 0.14);
  color: var(--foam);
  border: 1px solid rgba(212, 240, 234, 0.18);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.icon-btn:hover, .icon-btn:focus-visible { background: rgba(255, 248, 238, 0.24); }
.icon-btn[aria-pressed="true"] { background: rgba(42, 155, 150, 0.45); }

#btn-sound .icon-sound-on { display: none; }
#btn-sound[aria-pressed="true"] .icon-sound-on { display: block; }
#btn-sound[aria-pressed="true"] .icon-sound-off { display: none; }

.level-meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  margin: 4px 0 6px;
}

.level-kicker {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.level-title {
  margin: 2px 0 0;
  font-size: 1.45rem;
  line-height: 1.15;
}

.level-meta-side { text-align: right; }
.par-line, .stars-line { margin: 0; }
.par-line { color: var(--muted); font-size: 0.85rem; }
.stars-line { color: var(--gold); font-size: 1.15rem; letter-spacing: 0.15em; }

.hint {
  margin: 0 0 12px;
  color: var(--foam);
  font-size: 0.95rem;
  opacity: 0.9;
  min-height: 1.3em;
}

.play-layout {
  display: grid;
  gap: 16px;
}

.board-wrap {
  position: relative;
  background: linear-gradient(180deg, rgba(10, 58, 69, 0.65), rgba(4, 35, 44, 0.8));
  border: 1px solid rgba(212, 240, 234, 0.12);
  border-radius: 24px;
  padding: 10px 10px 8px;
  box-shadow: var(--shadow);
}

#board {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 16px;
  touch-action: manipulation;
}

.tray {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 28px;
  padding: 8px 4px 2px;
  color: var(--foam);
  font-size: 0.85rem;
}

.tray .item-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,248,238,0.1);
  border-radius: 999px;
  padding: 4px 10px;
}

.tray .item-chip.got { background: rgba(240, 193, 75, 0.22); color: var(--gold); }

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.queue-panel {
  background: var(--card);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 10px;
  box-shadow: var(--shadow);
}

.queue-head {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--teal);
  padding: 2px 4px 8px;
}

.queue {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 64px;
  padding: 6px;
  background: #efe3c8;
  border-radius: 12px;
  align-content: flex-start;
}

.chip {
  appearance: none;
  border: 0;
  font: inherit;
  cursor: pointer;
  background: #fff8ee;
  color: var(--ink);
  border-radius: 12px;
  padding: 8px 10px;
  min-height: 44px;
  box-shadow: 0 2px 0 #d2c3a0;
  font-weight: 700;
  font-size: 0.85rem;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.chip:hover { filter: brightness(0.98); }
.chip.active { outline: 2px solid var(--sea); background: #e6f7f4; }
.chip.F { background: #e8f6f4; }
.chip.L, .chip.R { background: #f8ebd4; }
.chip.G { background: #fde4df; }

.repeat-block {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  background: #d9efe8;
  border-radius: 14px;
  padding: 6px;
  border: 2px dashed #2a9b96;
  min-height: 56px;
}

.repeat-block.open {
  border-style: solid;
  box-shadow: 0 0 0 3px rgba(42, 155, 150, 0.25);
}

.repeat-count {
  font: inherit;
  font-weight: 800;
  background: var(--sea);
  color: white;
  border: 0;
  border-radius: 999px;
  min-width: 44px;
  min-height: 36px;
  cursor: pointer;
}

.pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.cmd {
  min-height: 64px;
  border-radius: 16px;
  background: var(--cream);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 4px 0 #c9b48a;
  font-weight: 700;
  font-size: 0.78rem;
}

.cmd .cmd-icon { font-size: 1.25rem; line-height: 1; }
.cmd:active { transform: translateY(2px); box-shadow: 0 2px 0 #c9b48a; }
.cmd:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.cmd-repeat { background: #cceee8; }
.cmd-done { background: var(--gold); grid-column: span 1; }
.cmd.hidden, .hidden { display: none !important; }

.actions {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 8px;
}

.act {
  min-height: 48px;
  border-radius: 14px;
  background: rgba(255, 248, 238, 0.16);
  color: var(--cream);
  border: 1px solid rgba(212, 240, 234, 0.18);
  font-weight: 800;
}

.act.primary {
  background: linear-gradient(180deg, #f07a6a, var(--coral));
  border: 0;
  color: white;
  box-shadow: 0 4px 0 #9b2e26;
}

.act.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 #9b2e26; }
.act:disabled { opacity: 0.45; cursor: not-allowed; }
.act.big { min-height: 52px; padding: 0 22px; font-size: 1rem; }

.status {
  margin: 0;
  min-height: 1.4em;
  color: var(--foam);
  font-size: 0.92rem;
}

.status.fail { color: #ffb3a8; }
.status.win { color: var(--gold); }

.keys-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(2, 14, 18, 0.62);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.overlay[hidden] { display: none !important; }

.card {
  background: var(--card);
  color: var(--ink);
  border-radius: 24px;
  padding: 22px 22px 20px;
  width: min(520px, 100%);
  box-shadow: var(--shadow);
  text-align: center;
}

.splash canvas { display: block; margin: 0 auto -6px; }
.splash h1, #win-heading, #levels-heading, #game-name {
  margin: 0 0 8px;
  font-size: 2rem;
  letter-spacing: 0.01em;
}
.splash h1 em, #game-name em { color: var(--coral); font-style: normal; }

.tagline { margin: 0 0 18px; color: #3d5c61; line-height: 1.4; }
.splash-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.tiny-note { margin: 16px 0 0; font-size: 0.72rem; color: #6a8084; }

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.card-head h2 { margin: 0; font-size: 1.3rem; }
.card-head .icon-btn { color: var(--ink); background: #efe3c8; }

.levels-card { width: min(640px, 100%); text-align: left; }
.level-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.level-card {
  font: inherit;
  text-align: left;
  border: 0;
  border-radius: 16px;
  padding: 12px;
  background: #fff8ee;
  color: var(--ink);
  box-shadow: 0 3px 0 #d2c3a0;
  cursor: pointer;
  min-height: 76px;
}

.level-card:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  background: #e8dfcc;
}

.level-card .n { font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--teal); margin: 0; }
.level-card .nme { font-weight: 800; margin: 2px 0 4px; }
.level-card .st { color: var(--coral); letter-spacing: 0.12em; margin: 0; }
.level-card.current { outline: 2px solid var(--sea); }

.win-kicker { margin: 0; letter-spacing: 0.14em; text-transform: uppercase; color: var(--teal); font-size: 0.75rem; }
.win-stars { font-size: 2rem; color: var(--coral); margin: 4px 0 8px; letter-spacing: 0.15em; }
.win-stats { margin: 0 0 16px; color: #3d5c61; }

@media (min-width: 900px) {
  .play-layout {
    grid-template-columns: 1.15fr 0.85fr;
    align-items: start;
  }
  .pad { grid-template-columns: repeat(3, 1fr); }
  .actions { grid-template-columns: 1fr 1fr; }
  #btn-run { grid-column: 1 / -1; }
  .level-grid { grid-template-columns: repeat(5, 1fr); }
}

@media (max-width: 420px) {
  .brand-text { font-size: 0.95rem; }
  .level-title { font-size: 1.2rem; }
  .pad { grid-template-columns: repeat(2, 1fr); }
  .keys-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
