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

:root {
  --bg: #0f172a;
  --bg-2: #111827;
  --panel: #1e293b;
  --panel-2: rgba(30, 41, 59, 0.92);
  --line: rgba(59, 130, 246, 0.45);
  --line-soft: #334155;
  --ink: #e5e7eb;
  --muted: #94a3b8;
  --label: #93c5fd;
  --blue: #0066ff;
  --blue-hover: #1a73e8;
  --blue-bright: #3b82f6;
  --gold: #fbbf24;
  --green: #22c55e;
  --red: #ef4444;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  --radius: 16px;
  --touch: 48px;
  --cell: 32px;
  --gap: 4px;
  --glyph: 46px;
}

html,
body {
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  background-color: var(--bg);
  color-scheme: dark;
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: Manrope, "Segoe UI", sans-serif;
  color: var(--ink);
  background-color: var(--bg);
  background-image: linear-gradient(180deg, #0f172a 0%, #111827 100%);
}

body.is-dragging {
  overflow: hidden;
  touch-action: none;
  user-select: none;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: 10px 10px 20px;
}

.topbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 3px solid var(--blue);
}

.brand {
  display: flex;
  justify-content: center;
  max-width: 100%;
}

.logo {
  display: block;
  height: 72px;
  width: auto;
  max-width: min(46vw, 120px);
  object-fit: contain;
  pointer-events: none;
}

.head-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 100%;
}

.intro {
  margin: 14px 0 16px;
  color: #cbd5e1;
  font-size: 0.9rem;
  text-align: center;
}

.game {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.card,
.board-wrap,
.wheel-panel {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--ink);
}

.card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
}

.levels {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.level {
  min-height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line-soft);
  background: rgba(15, 23, 42, 0.55);
  color: #e2e8f0;
  font: 700 0.82rem/1 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}

.level.is-active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.label {
  display: block;
  margin-bottom: 8px;
  color: var(--label);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.stat {
  min-width: 0;
  padding: 12px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
}

.stat .label {
  margin-bottom: 4px;
  min-height: 2.2em;
  line-height: 1.2;
}

.stat strong {
  display: block;
  font-size: 1.35rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.help {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.help-rules p {
  margin: 0;
}

.stage {
  position: relative;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  background: linear-gradient(180deg, #10233f 0%, #0b1220 42%);
  border: 2px solid var(--blue);
  box-shadow: 0 12px 28px rgba(0, 102, 255, 0.16);
  overflow: hidden;
  transition: border-color 0.35s ease, box-shadow 0.35s ease;
}

.board-wrap.is-won {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.35), 0 16px 36px rgba(34, 197, 94, 0.22);
}

.board {
  --cols: 3;
  --rows: 3;
  --cell: 32px;
  --gap: 4px;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  gap: var(--gap);
  width: max-content;
  max-width: 100%;
  margin: 0 auto;
  user-select: none;
  -webkit-user-select: none;
}

.tile {
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #f8fafc;
  color: transparent;
  font: 800 calc(var(--cell) * 0.52) / 1 Manrope, sans-serif;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.85),
    0 2px 0 rgba(0, 0, 0, 0.28);
  user-select: none;
}

.tile.is-gap {
  background: transparent;
  box-shadow: none;
}

.tile.is-filled {
  color: #0f172a;
}

.tile.is-fresh {
  animation: tileIn 0.38s ease both;
}

.wheel-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 10px 12px;
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
}

.preview {
  flex: 0 0 auto;
  width: 100%;
  min-height: 28px;
  margin: 0;
  color: #f8fafc;
  font: 800 1.05rem/28px Manrope, sans-serif;
  letter-spacing: 0.16em;
  text-align: center;
  text-transform: uppercase;
  user-select: none;
}

.preview.is-ok {
  color: #86efac;
}

.preview.is-bad {
  color: #fca5a5;
  letter-spacing: 0.06em;
}

.wheel-slot {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.wheel {
  position: relative;
  width: min(100%, 280px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 42%, #3b82f6 0%, #1d4ed8 56%, #1e3a8a 100%);
  box-shadow:
    inset 0 0 0 4px #fbbf24,
    inset 0 0 0 8px #1e3a8a,
    0 12px 24px rgba(0, 0, 0, 0.35);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.wheel.is-live {
  cursor: pointer;
}

body.is-dragging .wheel {
  cursor: grabbing;
}

.wheel.is-bad {
  animation: shake 0.42s ease;
}

.wheel.is-ok {
  animation: ring 0.4s ease;
}

.wheel-svg {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.wheel-svg polyline {
  fill: none;
  stroke: rgba(248, 250, 252, 0.95);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35));
}

.wheel-svg polyline.is-ok {
  stroke: #4ade80;
}

.wheel-svg polyline.is-bad {
  stroke: #fca5a5;
}

.hub {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 50%;
  width: 16%;
  height: 16%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 35% 30%, #fde68a, #f59e0b 46%, #d97706);
  box-shadow:
    inset 0 -3px 0 rgba(0, 0, 0, 0.18),
    0 4px 10px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.glyphs {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.glyph {
  position: absolute;
  width: var(--glyph);
  height: var(--glyph);
  display: grid;
  place-items: center;
  border-radius: 22%;
  background: linear-gradient(180deg, #ffffff 0%, #e7eef8 100%);
  box-shadow:
    inset 0 1px 0 #fff,
    0 3px 0 rgba(15, 23, 42, 0.35);
  color: #0f172a;
  font: 800 calc(var(--glyph) * 0.5) / 1 Manrope, sans-serif;
  transform: translate(-50%, -50%);
  transition: left 0.35s ease, top 0.35s ease, transform 0.12s ease;
  pointer-events: none;
  user-select: none;
}

.glyph.is-on {
  transform: translate(-50%, -50%) scale(1.08);
  box-shadow:
    inset 0 1px 0 #fff,
    0 0 0 3px #fbbf24,
    0 6px 14px rgba(0, 0, 0, 0.28);
  z-index: 3;
}

.celebrate {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: hidden;
  pointer-events: none;
}

.bit {
  position: absolute;
  top: 8%;
  width: 8px;
  height: 12px;
  border-radius: 2px;
  animation: fall 1.15s ease-in forwards;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  border-radius: var(--radius);
  background: rgba(8, 14, 28, 0.9);
  color: #f4f7ff;
  text-align: center;
  animation: fadeIn 0.25s ease;
}

.overlay.hidden {
  display: none;
}

.overlay.is-result {
  background: linear-gradient(180deg, transparent 0%, transparent 46%, rgba(8, 14, 28, 0.94) 100%);
  align-items: flex-end;
  pointer-events: none;
}

.overlay.is-result .overlay-inner {
  pointer-events: auto;
  padding: 12px;
  border-radius: 14px;
  background: rgba(8, 14, 28, 0.94);
  border: 1px solid rgba(59, 130, 246, 0.45);
}

.overlay-inner {
  width: min(100%, 320px);
  overflow-wrap: anywhere;
}

.eyebrow {
  margin-bottom: 8px;
  color: #7eb6ff;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.overlay h2 {
  margin-bottom: 8px;
  font-size: 1.7rem;
}

.overlay p {
  margin-bottom: 16px;
  color: #c5d4ea;
  font-size: 0.9rem;
}

.overlay.is-result h2 {
  font-size: 1.35rem;
}

.overlay.is-result p {
  margin-bottom: 12px;
}

.btn {
  min-height: var(--touch);
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  font: 700 0.85rem/1 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-primary {
  background: var(--blue);
  color: #fff;
}

.btn-primary:hover {
  background: var(--blue-hover);
}

.btn-primary:disabled:hover {
  background: var(--blue);
}

.btn-ghost {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
}

.btn-restart {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--blue);
  color: #fff;
  box-shadow: 0 8px 18px rgba(0, 102, 255, 0.28);
  letter-spacing: 0.02em;
}

.shuffle-ico {
  width: 1.15em;
  height: 1.15em;
  flex: 0 0 auto;
}

.foot,
.live {
  margin-top: 14px;
  text-align: center;
  color: #94a3b8;
  font-size: 0.75rem;
}

.live {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.btn:focus-visible,
.level:focus-visible {
  outline: 3px solid #7eb6ff;
  outline-offset: 2px;
}

.help-rules {
  display: none;
}

@media (min-width: 769px) {
  body {
    background-image: linear-gradient(180deg, #0f172a 0%, #111827 55%, #0b1220 100%);
  }

  .app {
    padding: 16px 14px 24px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
  }

  .brand {
    grid-column: 2;
    justify-self: center;
  }

  .head-actions {
    grid-column: 3;
    justify-self: end;
    justify-content: flex-end;
  }

  .logo {
    height: 84px;
    max-width: 132px;
  }

  .intro {
    text-align: left;
  }

  .game {
    display: grid;
    grid-template-columns: minmax(200px, 240px) minmax(300px, 460px) minmax(210px, 250px);
    gap: 16px;
    justify-content: center;
    align-items: stretch;
  }

  .stats {
    grid-template-columns: 1fr;
  }

  .stat .label {
    min-height: 0;
  }

  .stat strong {
    font-size: 1.6rem;
  }

  .stage,
  .side-left,
  .side-right {
    width: auto;
  }

  .stage {
    gap: 12px;
  }

  .board-wrap {
    height: min(340px, 40dvh);
    min-height: 180px;
    padding: 16px;
  }

  .wheel-panel {
    height: min(340px, 40dvh);
    min-height: 220px;
  }

  .side-right {
    justify-content: space-between;
  }

  .help-rules {
    display: block;
  }

  .btn-restart {
    margin-top: auto;
  }
}

@media (max-width: 768px) {
  html,
  body {
    height: 100dvh;
    overflow: hidden;
    background-color: #0f172a;
    color: #e5e7eb;
  }

  .app {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding:
      max(4px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .intro,
  .foot,
  .help-rules {
    display: none;
  }

  .topbar {
    flex-shrink: 0;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding-bottom: 4px;
    border-bottom-width: 2px;
  }

  .logo {
    height: 46px;
    max-width: 72px;
  }

  .head-actions {
    flex-wrap: nowrap;
    justify-content: flex-end;
  }

  .head-actions .btn {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  .game {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    gap: 6px;
    overflow: hidden;
    justify-content: flex-start;
  }

  .side-left,
  .card,
  .stat,
  .btn-ghost,
  .level {
    background-color: rgba(30, 41, 59, 0.92);
    color: #e5e7eb;
    border-color: rgba(59, 130, 246, 0.55);
  }

  .level.is-active {
    background: #0066ff;
    border-color: #0066ff;
    color: #fff;
  }

  .label,
  .stat strong,
  .stat .label {
    color: #e2e8f0;
  }

  .side-left {
    flex: 0 0 auto;
    gap: 6px;
    padding: 6px;
  }

  .levels {
    gap: 4px;
  }

  .level {
    min-height: 32px;
    padding: 0 4px;
    font-size: 0.72rem;
  }

  .stats {
    gap: 4px;
  }

  .stat {
    padding: 4px 6px;
  }

  .stat .label {
    min-height: 0;
    margin-bottom: 2px;
    font-size: 0.56rem;
    letter-spacing: 0.04em;
  }

  .stat strong {
    font-size: 0.95rem;
  }

  .stage {
    flex: 1 1 auto;
    min-height: 0;
    gap: 6px;
    overflow: hidden;
  }

  .board-wrap {
    flex: 1 1 42%;
    min-height: 0;
    width: 100%;
    margin: 0;
    padding: 8px;
    background-color: #0b1220;
    border-color: #3b82f6;
  }

  .wheel-panel {
    flex: 1 1 58%;
    min-height: 0;
    width: 100%;
    margin: 0;
    padding: 2px 8px 6px;
    overflow: hidden;
    background-color: #1e293b;
    border-color: rgba(59, 130, 246, 0.55);
  }

  .preview {
    min-height: 22px;
    font-size: 0.92rem;
    line-height: 22px;
  }

  .overlay {
    padding: 10px;
  }

  .overlay h2 {
    font-size: 1.25rem;
    margin-bottom: 6px;
  }

  .overlay p {
    margin-bottom: 12px;
    font-size: 0.82rem;
  }

  .overlay.is-result h2 {
    font-size: 1.15rem;
  }

  .side-right {
    align-self: stretch;
    width: 100%;
    gap: 0;
    margin: 0;
    padding: 0;
    flex: 0 0 auto;
    background: transparent;
    border: 0;
    box-shadow: none;
  }

  #shuffleBtn,
  #startBtn {
    width: 100%;
    min-height: 48px;
    margin: 0;
    padding: 12px 16px;
    font-size: 1rem;
  }

  #shuffleBtn {
    background: #0066ff;
    color: #fff;
    border-color: #0066ff;
  }
}

@media (max-width: 380px) {
  .head-actions .btn {
    padding: 6px 7px;
    font-size: 0.68rem;
  }

  .stat .label {
    font-size: 0.5rem;
  }
}

@media (max-width: 768px) and (max-height: 680px) {
  .logo {
    height: 40px;
  }

  .stat strong {
    font-size: 0.78rem;
  }

  .level,
  .head-actions .btn {
    min-height: 30px;
  }

  .preview {
    min-height: 18px;
    font-size: 0.82rem;
    line-height: 18px;
  }

  .overlay h2 {
    font-size: 1.1rem;
  }

  #shuffleBtn,
  #startBtn {
    min-height: 42px;
    padding: 8px 12px;
  }
}

@media (max-height: 520px) {
  html,
  body {
    height: 100dvh;
    overflow: hidden;
  }

  .app {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding: 6px;
  }

  .intro,
  .foot,
  .help-rules {
    display: none;
  }

  .topbar {
    flex-direction: row;
    justify-content: space-between;
    padding-bottom: 4px;
  }

  .logo {
    height: 40px;
  }

  .game {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(132px, 168px) minmax(0, 1fr) minmax(132px, 168px);
    gap: 6px;
    align-items: stretch;
    overflow: hidden;
  }

  .side-left,
  .side-right,
  .stage {
    min-height: 0;
    height: 100%;
  }

  .side-left,
  .side-right {
    padding: 8px;
    gap: 8px;
    overflow: hidden;
  }

  .stats {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .stat {
    padding: 6px;
  }

  .stat .label {
    min-height: 0;
    margin-bottom: 2px;
  }

  .stat strong {
    font-size: 1rem;
  }

  .stage {
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
    gap: 6px;
  }

  .board-wrap,
  .wheel-panel {
    height: 100%;
    min-height: 0;
  }

  .btn-restart {
    margin-top: auto;
  }
}

@keyframes tileIn {
  0% { transform: scale(0.4); filter: brightness(1.7); }
  62% { transform: scale(1.12); }
  100% { transform: scale(1); filter: brightness(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

@keyframes ring {
  0% { filter: brightness(1.35); }
  100% { filter: brightness(1); }
}

@keyframes fall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(42vh) rotate(240deg); opacity: 0; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile.is-fresh,
  .glyph,
  .wheel,
  .wheel.is-bad,
  .wheel.is-ok,
  .board-wrap,
  .overlay,
  .bit {
    animation: none;
    transition: none;
  }
}
