:root {
  --safe-top: max(4px, env(safe-area-inset-top));
  --safe-bottom: max(4px, env(safe-area-inset-bottom));
  --gold: #ffd978;
  --teal: #3fd6c0;
  --panel: rgba(12, 24, 40, 0.92);
  --font: "Trebuchet MS", "Segoe UI", sans-serif;
  --badge-space: 3.2rem;
}

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

html {
  height: 100%;
  height: 100dvh;
}

body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: #fff8e8;
  background: #060d16 url("../assets/bg.webp") center / cover no-repeat;
  overflow: hidden;
}

/* Fill iframe or fullscreen without clipping HUD */
.stage {
  position: relative;
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: stretch;
  padding:
    calc(var(--safe-top) + var(--badge-space))
    clamp(0.4rem, 1.5vw, 0.9rem)
    calc(var(--safe-bottom) + 0.35rem);
  overflow: hidden;
}

.stage-glow {
  pointer-events: none;
  position: absolute;
  inset: 0;
  background:
    radial-gradient(70% 45% at 50% 8%, rgba(80, 220, 180, 0.16), transparent 60%),
    linear-gradient(180deg, rgba(4, 10, 18, 0.12), rgba(4, 10, 18, 0.5));
}

.machine {
  position: relative;
  z-index: 1;
  width: min(1080px, 100%);
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: clamp(0.25rem, 0.9vh, 0.55rem);
  container-type: size;
  container-name: machine;
}

.jackpot {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  align-items: baseline;
  gap: 0.45rem;
  padding: 0.22rem 0.9rem;
  border-radius: 999px;
  background: rgba(8, 16, 28, 0.78);
  border: 1px solid rgba(255, 217, 120, 0.45);
}

.jackpot-label {
  font-size: clamp(0.6rem, 1.6cqh, 0.78rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--gold);
  text-transform: uppercase;
}

.jackpot strong {
  font-size: clamp(0.9rem, 2.4cqh, 1.25rem);
  color: #fff;
}

.reels-frame {
  flex: 0 1 auto;
  min-height: 0;
  /* Fit both width and remaining height (iframe / fullscreen) */
  width: min(100%, calc((100cqh - 0.5rem) * 5 / 3.1));
  height: min(calc(100cqw * 3.1 / 5), calc(100cqh - 0.5rem));
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 5 / 3.1;
  margin-inline: auto;
  padding: clamp(4px, 1cqh, 10px);
  border-radius: clamp(12px, 2cqh, 20px);
  background: linear-gradient(160deg, rgba(10, 22, 38, 0.82), rgba(6, 14, 24, 0.9));
  border: 2px solid rgba(63, 214, 192, 0.75);
  box-shadow:
    0 0 0 2px rgba(255, 217, 120, 0.3),
    0 12px 28px rgba(0, 0, 0, 0.4),
    inset 0 0 30px rgba(63, 214, 192, 0.08);
  display: flex;
}

.reels {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: clamp(3px, 0.8cqh, 8px);
  width: 100%;
  height: 100%;
  min-height: 0;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-radius: clamp(8px, 1.4cqh, 12px);
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 35%, rgba(80, 140, 190, 0.22), transparent 55%),
    linear-gradient(180deg, #14263a, #0a1524);
  border: 1px solid rgba(255, 217, 120, 0.22);
}

.cell img {
  width: 86%;
  height: 86%;
  object-fit: contain;
  display: block;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.4));
  user-select: none;
  pointer-events: none;
}

.cell .tag {
  position: absolute;
  bottom: 3%;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(0.4rem, 1.3cqh, 0.62rem);
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #fff8e0;
  background: rgba(8, 16, 28, 0.82);
  border: 1px solid rgba(255, 217, 120, 0.45);
  padding: 0.05rem 0.3rem;
  border-radius: 999px;
}

.cell.win {
  box-shadow: 0 0 0 2px var(--gold), 0 0 16px rgba(255, 217, 120, 0.5);
  z-index: 2;
}

.cell.spinning img {
  animation: reelFlick 0.1s linear infinite;
}

@keyframes reelFlick {
  0% { transform: translateY(-8%); opacity: 0.45; }
  50% { transform: translateY(8%); opacity: 1; }
  100% { transform: translateY(-8%); opacity: 0.45; }
}

.status {
  flex: 0 0 auto;
  margin: 0;
  min-height: 1em;
  text-align: center;
  font-size: clamp(0.68rem, 1.8cqh, 0.85rem);
  color: rgba(255, 248, 232, 0.9);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.hud {
  flex: 0 0 auto;
  width: 100%;
  max-width: 980px;
  display: grid;
  grid-template-columns: 1fr 1.1fr 1.5fr 1.1fr 1fr;
  gap: clamp(0.25rem, 0.9vw, 0.55rem);
  align-items: stretch;
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.1rem;
  min-height: clamp(2.6rem, 7cqh, 3.8rem);
  padding: 0.28rem 0.35rem;
  border-radius: 999px;
  background: var(--panel);
  border: 1.5px solid rgba(255, 217, 120, 0.55);
}

.panel-label {
  font-size: clamp(0.5rem, 1.4cqh, 0.68rem);
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--gold);
}

.panel-value {
  font-size: clamp(0.85rem, 2.3cqh, 1.15rem);
  font-weight: 800;
  color: #fff;
}

.bet-controls {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.circle-btn {
  width: clamp(1.4rem, 3.5cqh, 1.7rem);
  height: clamp(1.4rem, 3.5cqh, 1.7rem);
  border-radius: 50%;
  border: 1.5px solid var(--gold);
  background: #152536;
  color: var(--gold);
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.actions {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.3rem;
  align-items: center;
}

.pill-btn {
  min-height: clamp(2rem, 5.5cqh, 2.5rem);
  border-radius: 999px;
  border: 1.5px solid rgba(255, 217, 120, 0.55);
  background: #152536;
  color: var(--gold);
  font-family: inherit;
  font-size: clamp(0.5rem, 1.4cqh, 0.68rem);
  font-weight: 800;
  letter-spacing: 0.03em;
  cursor: pointer;
  padding: 0.25rem 0.35rem;
}

.pill-btn[aria-pressed="true"] {
  background: rgba(63, 214, 192, 0.22);
  border-color: var(--teal);
  color: #fff;
}

.spin-btn {
  width: clamp(3.4rem, 9cqh, 5rem);
  height: clamp(3.4rem, 9cqh, 5rem);
  border-radius: 50%;
  border: 3px solid var(--gold);
  background: radial-gradient(circle at 35% 28%, #5a3a78, #2a1840 55%, #140c22);
  color: var(--gold);
  font-family: inherit;
  font-size: clamp(0.8rem, 2.2cqh, 1.05rem);
  font-weight: 900;
  letter-spacing: 0.05em;
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.35);
}

.spin-btn:hover { filter: brightness(1.08); }

.spin-btn:disabled,
.pill-btn:disabled,
.circle-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Short viewports / iframes */
@media (max-height: 720px) {
  :root { --badge-space: 2.6rem; }
  .status { display: none; }
}

@media (max-height: 560px) {
  :root { --badge-space: 2.1rem; }
  .jackpot { padding: 0.12rem 0.7rem; }
  .panel { min-height: 2.3rem; }
  .spin-btn {
    width: 3.1rem;
    height: 3.1rem;
    font-size: 0.75rem;
  }
}

@media (max-width: 880px) {
  .hud {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "actions actions"
      "bet balance"
      "lines win";
  }
  .actions { grid-area: actions; }
  .panel-bet { grid-area: bet; }
  .panel:nth-child(1) { grid-area: lines; }
  .panel:nth-child(4) { grid-area: balance; }
  .panel:nth-child(5) { grid-area: win; }
}

@media (max-width: 560px) {
  :root { --badge-space: 3.6rem; }
  .actions {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  .spin-btn { order: -1; }
  .pill-btn { width: min(100%, 200px); }
}

/* Wide short (iframe landscape) — keep HUD in one row, shrink reels */
@media (min-width: 700px) and (max-height: 640px) {
  .hud {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-template-areas: none;
  }
  .actions,
  .panel-bet,
  .panel:nth-child(1),
  .panel:nth-child(4),
  .panel:nth-child(5) {
    grid-area: auto;
  }
  .reels-frame {
    width: min(100%, 72cqh * 1.65);
  }
}

@supports not (height: 1cqh) {
  .reels-frame {
    width: min(100%, 920px, 70vh);
    height: auto;
    max-height: calc(100dvh - 11rem);
    aspect-ratio: 5 / 3.1;
  }
}
