:root {
  --emerald: #2e7d52;
  --emerald-dark: #1f5e3c;
  --ruby: #b3373c;
  --ruby-dark: #8e2429;
  --gold: #e0a93f;
  --ink: #2c3e50;
  --cell-bg: #fdfaf0;
  --cell-shadow: rgba(60, 70, 40, 0.18);
}

* { box-sizing: border-box; }

body {
  font-family: 'Montserrat', sans-serif;
  margin: 0;
  padding: 0;
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.sky-bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background: linear-gradient(to bottom, #b8dff0 0%, #d8ecdf 55%, #f2e9c9 100%);
}

.sky-bg::after {
  /* faint rainbow arc over the horizon */
  content: '';
  position: absolute;
  left: 50%;
  bottom: -30vw;
  width: 120vw;
  height: 120vw;
  transform: translateX(-50%);
  border-radius: 50%;
  background: radial-gradient(circle,
    transparent 58%,
    rgba(179, 55, 60, 0.10) 59%, rgba(224, 169, 63, 0.10) 62%,
    rgba(46, 125, 82, 0.10) 65%, rgba(80, 120, 200, 0.10) 68%,
    transparent 70%);
  pointer-events: none;
}

.game-container {
  width: 100%;
  max-width: 920px;
  padding: 1.25rem 1rem 2.5rem;
  text-align: center;
}

h1 {
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 800;
  margin: 0.25rem 0 1rem;
  text-shadow: 2px 2px 0 rgba(255,255,255,0.7);
  letter-spacing: 0.02em;
}
.t-emerald { color: var(--emerald); }
.t-gold    { color: var(--gold); }
.t-ruby    { color: var(--ruby); }

.play-area {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

/* ---------- board ---------- */

.board-wrap {
  position: relative;
  width: min(92vw, 420px);
  flex-shrink: 0;
}

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

.board.shake { animation: board-shake 0.45s; }

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

.cell {
  aspect-ratio: 1;
  background-color: var(--cell-bg);
  border-radius: 12px;
  box-shadow: 0 3px 6px var(--cell-shadow), inset 0 0 0 2px rgba(224, 169, 63, 0.25);
  position: relative;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.3s ease;
}

.board.playable .cell.open { cursor: pointer; }
.board.playable .cell.open:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 10px var(--cell-shadow), inset 0 0 0 2px rgba(224, 169, 63, 0.5);
}

.cell.win { animation: win-pulse 0.8s infinite alternate; }

@keyframes win-pulse {
  from { background-color: var(--cell-bg); }
  to   { background-color: #ffe9a3; box-shadow: 0 3px 14px rgba(224, 169, 63, 0.65); }
}

.cell img.mark {
  position: absolute;
  inset: 8%;
  width: 84%;
  height: 84%;
  z-index: 2;
}
.cell img.mark.pop { animation: pop-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); }
.cell img.mark.poof { animation: poof-out 0.35s ease-in forwards; }

@keyframes pop-in {
  from { transform: scale(0.2); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}
@keyframes poof-out {
  to { transform: scale(0.1) rotate(20deg); opacity: 0; }
}

.cell img.item {
  position: absolute;
  inset: 12%;
  width: 76%;
  height: 76%;
  z-index: 1;
  animation: item-float 2.4s ease-in-out infinite alternate;
}
.cell img.item.drop-in { animation: item-drop 0.5s cubic-bezier(0.34, 1.4, 0.64, 1), item-float 2.4s 0.5s ease-in-out infinite alternate; }
.cell img.item.claimed { animation: claim-out 0.45s ease-in forwards; }

@keyframes item-drop {
  from { transform: translateY(-120%) scale(0.6); opacity: 0; }
  to   { transform: translateY(0) scale(1); opacity: 1; }
}
@keyframes item-float {
  from { transform: translateY(0); }
  to   { transform: translateY(-5%); }
}
@keyframes claim-out {
  40% { transform: scale(1.25); opacity: 1; }
  to  { transform: scale(0); opacity: 0; }
}

/* ---------- overlay sprites (Toto + events) ---------- */

.overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

#toto {
  position: absolute;
  z-index: 12;
  transition: opacity 0.25s ease;
}
#toto.hidden { opacity: 0; }

.zzz {
  position: absolute;
  z-index: 13;
  font-size: clamp(1rem, 4vw, 1.6rem);
  animation: zzz-float 1.6s ease-in-out infinite;
}
@keyframes zzz-float {
  0%   { transform: translateY(0) scale(0.8); opacity: 0; }
  30%  { opacity: 1; }
  100% { transform: translateY(-130%) scale(1.15); opacity: 0; }
}

.fx-sprite {
  position: absolute;
  z-index: 20;
}
.fx-sprite.spin { animation: wobble 0.5s ease-in-out infinite alternate; }

@keyframes wobble {
  from { transform: rotate(-7deg) scale(1); }
  to   { transform: rotate(7deg) scale(1.06); }
}

/* ---------- player panels ---------- */

.panel {
  background: rgba(255, 255, 255, 0.65);
  border-radius: 14px;
  padding: 0.7rem 0.8rem;
  box-shadow: 0 3px 8px rgba(60, 70, 40, 0.12);
  min-width: 132px;
  transition: box-shadow 0.3s ease, outline 0.3s ease;
  outline: 3px solid transparent;
}
.panel.turn { outline: 3px solid var(--gold); }
#panel-X.turn { outline-color: var(--emerald); }
#panel-O.turn { outline-color: var(--ruby); }

.panel-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  text-align: left;
}
.panel-mark { width: 36px; height: 36px; }
.panel-name { font-weight: 800; font-size: 1rem; }
#panel-X .panel-name { color: var(--emerald); }
#panel-O .panel-name { color: var(--ruby); }
.panel-wins {
  display: inline-block;
  font-size: 0.95rem;
  font-weight: 800;
  background: linear-gradient(to bottom, #ffe9a3, #f5cd5e);
  border: 1px solid rgba(160, 120, 20, 0.35);
  border-radius: 12px;
  padding: 1px 9px;
  margin-top: 2px;
  color: #6b4e0e;
  box-shadow: 0 1px 2px rgba(60, 70, 40, 0.2);
}
.panel-wins.bump { animation: wins-bump 0.7s cubic-bezier(0.34, 1.56, 0.64, 1); }

@keyframes wins-bump {
  0%  { transform: scale(1); }
  40% { transform: scale(1.45) rotate(-4deg); }
  100%{ transform: scale(1); }
}

.boon-row {
  display: flex;
  gap: 6px;
  justify-content: center;
  padding: 4px;
  border-radius: 8px;
  margin-top: 4px;
}
.boon-row[data-set="witch"]  { background: rgba(46, 125, 82, 0.08); }
.boon-row[data-set="wizard"] { background: rgba(80, 120, 200, 0.08); }

.boon-slot {
  width: 34px;
  height: 34px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
}
.boon-slot img {
  width: 30px;
  height: 30px;
  filter: grayscale(1) opacity(0.3);
  transition: filter 0.4s ease, transform 0.4s ease;
}
.boon-slot.have img { filter: none; }
.boon-slot.flash img { animation: boon-flash 0.7s ease; }

@keyframes boon-flash {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.6) rotate(-8deg); }
  100% { transform: scale(1); }
}

/* ---------- win strip (end-of-round) ---------- */

.win-strip {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin: 1rem auto 0;
}
.win-strip[hidden] { display: none; }

.win-tile {
  width: clamp(54px, 16vw, 68px);
  height: clamp(54px, 16vw, 68px);
  border-radius: 12px;
  background: #ffe9a3;
  border: 2px solid var(--gold);
  box-shadow: 0 0 14px rgba(224, 169, 63, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: win-tile-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
.win-tile img { width: 80%; height: 80%; }

@keyframes win-tile-pop {
  from { transform: scale(0) rotate(-12deg); opacity: 0; }
  to   { transform: scale(1) rotate(0); opacity: 1; }
}

/* ---------- status & controls ---------- */

#status {
  font-size: clamp(1rem, 3.5vw, 1.2rem);
  font-weight: 600;
  min-height: 2.2em;
  margin: 1rem auto 0.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 480px;
}

.controls { margin-top: 0.25rem; }

button {
  font-family: 'Montserrat', sans-serif;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.6rem 1.2rem;
  background-color: var(--emerald);
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.25s ease;
  box-shadow: 0 3px 4px rgba(0,0,0,0.12);
  margin: 0.35rem;
}
button:hover {
  background-color: var(--emerald-dark);
  transform: translateY(-2px);
  box-shadow: 0 4px 7px rgba(0,0,0,0.18);
}

button:disabled,
button:disabled:hover {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 3px 4px rgba(0,0,0,0.12);
}

#next-round { background-color: var(--ruby); }
#next-round:hover:not(:disabled) { background-color: var(--ruby-dark); }
#next-round:disabled { background-color: var(--ruby); }
#next-round.attention { animation: btn-pulse 1s infinite alternate; }

#reset-game {
  background-color: rgba(255, 255, 255, 0.7);
  color: var(--ink);
}
#reset-game:hover { background-color: white; }

@keyframes btn-pulse {
  from { transform: scale(1); }
  to   { transform: scale(1.08); box-shadow: 0 4px 12px rgba(179, 55, 60, 0.5); }
}

#mute-button,
#snapshot-button {
  background-color: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  font-size: 1.1rem;
  padding: 0.5rem 0.9rem;
}
#mute-button:hover,
#snapshot-button:hover { background-color: white; }

/* ---------- snapshot modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(20, 30, 25, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 1.5rem 1rem;
  overflow-y: auto;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  position: relative;
  background: var(--background-color, #f9f9f9);
  background: linear-gradient(to bottom, #eaf4ef, #f7f1dd);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
  padding: 1.5rem;
  width: 100%;
  max-width: 720px;
  text-align: center;
}

.modal h2 {
  margin: 0 0 1rem;
  color: var(--ink);
  font-size: 1.4rem;
}

.modal-close {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  background: transparent;
  color: var(--ink);
  font-size: 1.6rem;
  line-height: 1;
  padding: 0.2rem 0.55rem;
  margin: 0;
  box-shadow: none;
}
.modal-close:hover { background: rgba(0,0,0,0.08); transform: none; }

.snapshot-preview {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 1rem;
  min-height: 120px;
}
#snapshot-canvas {
  max-width: 100%;
  max-height: 56vh;
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.2);
  background: #cfe7f2;
}

.snapshot-controls {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  align-items: center;
}

.ratio-group {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0,0,0,0.15);
}
.ratio-btn {
  margin: 0;
  border-radius: 0;
  background: rgba(255,255,255,0.8);
  color: var(--ink);
  box-shadow: none;
  padding: 0.5rem 1rem;
}
.ratio-btn:hover { background: white; transform: none; box-shadow: none; }
.ratio-btn.active { background: var(--emerald); color: white; }
.ratio-btn.active:hover { background: var(--emerald-dark); }

.snapshot-toggle {
  font-size: 0.85rem;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
}

#snapshot-download.primary { background: var(--ruby); }
#snapshot-download.primary:hover { background: var(--ruby-dark); }

.alt-block {
  width: 100%;
  max-width: 520px;
  text-align: left;
}
.alt-block label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
  color: var(--ink);
}
.alt-hint { font-weight: 400; opacity: 0.7; }
#snapshot-alt {
  width: 100%;
  font-family: 'Montserrat', sans-serif;
  font-size: 0.8rem;
  border-radius: 8px;
  border: 1px solid rgba(60,70,40,0.25);
  padding: 0.5rem;
  resize: vertical;
  background: rgba(255,255,255,0.8);
  color: var(--ink);
}
#snapshot-copy {
  margin: 0.5rem 0 0;
  background: var(--emerald);
  font-size: 0.8rem;
  padding: 0.45rem 1rem;
}
#snapshot-copy.copied { background: var(--gold); color: var(--ink); }

/* ---------- help ---------- */

.help {
  max-width: 560px;
  margin: 1.5rem auto 0;
  text-align: left;
  font-size: 0.85rem;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 12px;
  padding: 0.75rem 1rem;
}
.help summary {
  cursor: pointer;
  font-weight: 700;
  text-align: center;
}
.help p { line-height: 1.5; }

/* ---------- small screens ---------- */

@media (max-width: 720px) {
  .play-area { flex-direction: column; }
  .panel {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
  }
  .panel-header { margin-bottom: 0; }
  .boon-row { margin-top: 0; }
  /* show panels below the board on phones */
  .board-wrap { order: -1; }
}
