:root {
  --ink: #1b2340;
  --paper: #f7f5ff;
  --panel: rgba(255, 255, 255, .88);
  --edge: rgba(55, 65, 125, .18);
  --shadow: 0 18px 45px rgba(31, 38, 91, .18);
  --violet: #6c61dd;
  --gold: #ffe789;
}

* { box-sizing: border-box; }

body {
  min-width: 0;
  margin: 0;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 12% 5%, rgba(255, 224, 135, .86), transparent 27%),
    radial-gradient(circle at 88% 3%, rgba(158, 219, 255, .9), transparent 28%),
    radial-gradient(circle at 50% 80%, rgba(212, 166, 255, .36), transparent 34%),
    linear-gradient(155deg, #e9e5ff, #effbff 50%, #fff3d9);
}

button { font: inherit; }

.game-shell {
  width: min(100%, 760px);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(16px, 4vw, 32px);
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.header-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }

.eyebrow {
  margin: 0 0 3px;
  color: #656a9e;
  font-size: .74rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

h1, h2 { margin: 0; }
h1 { font-size: clamp(1.55rem, 5vw, 2.4rem); letter-spacing: -.045em; }

.quiet-button,
#dialog-action {
  min-height: 44px;
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 800;
}

.quiet-button { color: #434879; background: rgba(255,255,255,.72); box-shadow: 0 5px 15px rgba(46,55,104,.11); }
.quiet-button:hover, .quiet-button:focus-visible { background: #fff; outline: 3px solid rgba(99, 102, 241, .32); }
.sound-button[aria-pressed="false"] { color: #737792; background: rgba(233, 235, 246, .86); }

.status-panel {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.status-panel div {
  min-width: 0;
  padding: 10px 8px;
  border: 1px solid var(--edge);
  border-radius: 15px;
  text-align: center;
  background: var(--panel);
  box-shadow: 0 7px 22px rgba(46,55,104,.08);
}

.status-panel span { display: block; color: #696f9b; font-size: .72rem; font-weight: 700; }
.status-panel strong { display: block; overflow: hidden; font-size: clamp(1rem, 4vw, 1.3rem); text-overflow: ellipsis; }
.status-panel strong.is-score-pop { animation: score-pop .32s cubic-bezier(.2, .9, .25, 1.35); }

.instruction { margin: 0 0 14px; text-align: center; color: #545a88; font-size: .95rem; }
.feedback { min-height: 1.35em; margin: -8px 0 10px; color: #6655b8; font-size: .84rem; font-weight: 800; text-align: center; }
.feedback.is-warning { color: #b45170; }
.feedback.is-celebration { color: #397b66; }

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: clamp(3px, 1vw, 7px);
  width: min(100%, 620px);
  margin: 0 auto;
  padding: clamp(7px, 2vw, 12px);
  border: 1px solid rgba(255,255,255,.9);
  border-radius: clamp(18px, 4vw, 27px);
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 10%, rgba(156, 170, 255, .35), transparent 30%),
    linear-gradient(135deg, #414a88, #222955 72%);
  box-shadow: var(--shadow);
  touch-action: manipulation;
  user-select: none;
}

.tile {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  border: 0;
  border-radius: clamp(8px, 2vw, 15px);
  padding: 0;
  cursor: pointer;
  background: rgba(255,255,255,.10);
  box-shadow: inset 0 1px rgba(255,255,255,.45), 0 3px 0 rgba(10,14,48,.22);
  transition: transform .16s ease, filter .16s ease, outline-color .16s ease, opacity .16s ease;
}

.tile::before {
  content: "";
  position: absolute;
  inset: 14%;
  border-radius: 38% 62% 58% 42% / 42% 41% 59% 58%;
  background:
    radial-gradient(ellipse at 31% 24%, rgba(255,255,255,.82) 0 7%, transparent 23%),
    linear-gradient(145deg, rgba(255,255,255,.28), transparent 42%),
    var(--gem);
  box-shadow: inset 0 3px 5px rgba(255,255,255,.58), inset 0 -5px 8px rgba(0,0,0,.18), 0 4px 8px rgba(0,0,0,.22);
  transform: rotate(var(--rotation));
}

.tile[data-type="0"] { --gem: linear-gradient(135deg, #ff9caf, #e52f65 78%); --rotation: 9deg; }
.tile[data-type="1"] { --gem: linear-gradient(135deg, #ffdb78, #ec8c1f 78%); --rotation: 52deg; }
.tile[data-type="2"] { --gem: linear-gradient(135deg, #c8f59b, #42a95e 78%); --rotation: -16deg; }
.tile[data-type="3"] { --gem: linear-gradient(135deg, #a9e8ff, #367bd7 78%); --rotation: 29deg; }
.tile[data-type="4"] { --gem: linear-gradient(135deg, #e0b7ff, #8f53d1 78%); --rotation: 68deg; }
.tile[data-type="5"] { --gem: linear-gradient(135deg, #93f3e8, #17a4aa 78%); --rotation: -48deg; }

.tile:hover { transform: translateY(-2px); filter: brightness(1.08); }
.tile:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }
.tile.is-selected { outline: 3px solid var(--gold); outline-offset: -1px; transform: translateY(-3px) scale(1.04); animation: selected-pulse .7s ease-in-out infinite alternate; }
.tile.is-invalid { animation: wobble .28s linear 2; }
.tile.is-swapping { animation: swap-pop .24s ease-in-out; }
.tile.is-fresh { animation: gem-arrive .26s cubic-bezier(.2, .95, .28, 1.26); }
.tile.is-cleared { pointer-events: none; animation: gem-clear .16s ease forwards; }

.tile.special-line::after,
.tile.special-prism::after {
  position: absolute;
  inset: 31%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 1px 3px rgba(0,0,0,.35);
}
.tile.special-line::after { content: "↔"; font-size: clamp(.85rem, 4vw, 1.7rem); }
.tile.special-prism::after { content: "✦"; font-size: clamp(1rem, 5vw, 2rem); color: #fff6ad; }
.tile.special-prism::before { background: conic-gradient(#ff7a9a, #ffd15d, #76e5a6, #71bfff, #ce90ff, #ff7a9a); }
.tile.special-line, .tile.special-prism { animation: special-glow 1.05s ease-in-out infinite alternate; }

.board.is-resolving::after {
  content: "✦  ·  ✦  ·  ✦";
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 249, 188, .82);
  font-size: clamp(1rem, 5vw, 2.3rem);
  letter-spacing: 1.4em;
  pointer-events: none;
  animation: board-sparkle .32s ease-out forwards;
}
.board.is-reshuffling { animation: board-shuffle .4s ease-in-out; }
.feedback-pop { min-height: 1.4em; margin: 9px 0 0; color: #6855c7; font-weight: 900; text-align: center; text-shadow: 0 1px #fff; }
.feedback-pop.is-visible { animation: message-pop .7s ease both; }

.legend {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 18px auto 0;
  color: #4f5687;
  font-size: .87rem;
}
.legend p { margin: 0; padding: 12px; border-radius: 14px; background: rgba(255,255,255,.62); }

.dialog {
  position: fixed;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(20, 24, 57, .52);
}
.dialog[hidden] {
  display: none;
  pointer-events: none;
}
.dialog-card {
  width: min(100%, 360px);
  padding: 28px;
  border-radius: 22px;
  text-align: center;
  background: #fff;
  box-shadow: 0 25px 70px rgba(7, 10, 34, .35);
}
.dialog-card h2 { margin-bottom: 10px; font-size: 1.55rem; }
.dialog-card p:not(.eyebrow) { color: #5e6386; line-height: 1.55; }
#dialog-action { width: 100%; color: #fff; background: linear-gradient(135deg, #6c61dd, #9b50cf); }
#dialog-action:hover { filter: brightness(1.08); }
#dialog-action:focus-visible { outline: 3px solid #252957; outline-offset: 3px; }
#dialog-action:disabled { cursor: not-allowed; opacity: .55; }

@keyframes wobble { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@keyframes selected-pulse { to { filter: brightness(1.15) saturate(1.14); } }
@keyframes swap-pop { 50% { transform: scale(.88) rotate(-2deg); } }
@keyframes gem-clear { to { opacity: 0; filter: brightness(1.6); transform: scale(.5) rotate(14deg); } }
@keyframes gem-arrive { 0% { opacity: 0; transform: translateY(-14px) scale(.6); } 100% { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes special-glow { to { filter: brightness(1.22) drop-shadow(0 0 7px rgba(255, 234, 131, .85)); } }
@keyframes board-sparkle { from { opacity: 0; transform: scale(.7); } 45% { opacity: 1; } to { opacity: 0; transform: scale(1.22); } }
@keyframes board-shuffle { 25% { transform: translateX(-6px) rotate(-.5deg); } 75% { transform: translateX(6px) rotate(.5deg); } }
@keyframes score-pop { 50% { color: #7d58ce; transform: scale(1.24); } }
@keyframes message-pop { 0% { opacity: 0; transform: translateY(8px) scale(.8); } 25% { opacity: 1; transform: translateY(0) scale(1.08); } 100% { opacity: 0; transform: translateY(-8px) scale(1); } }

@media (max-width: 500px) {
  .game-shell { padding: 14px 10px 22px; }
  .game-header { align-items: flex-start; }
  .header-actions { gap: 5px; }
  .quiet-button { flex: 0 0 auto; font-size: .72rem; min-height: 40px; padding: 8px 9px; }
  .status-panel { gap: 5px; }
  .status-panel div { padding: 8px 3px; border-radius: 11px; }
  .status-panel span { font-size: .62rem; }
  .legend { grid-template-columns: 1fr; margin-top: 14px; }
  .legend p { padding: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
