:root {
  --felt-light: #1f8a4c;
  --felt-dark: #073e1d;
  --gold: #f5c451;
  --gold-deep: #d49a1f;
  --ink: #0b2316;
  --card-w: clamp(58px, 13.5vw, 96px);
  --shadow: 0 10px 25px rgba(0, 0, 0, 0.35);
  --topbar-chip-h: 42px;
}

* {
  box-sizing: border-box;
  touch-action: manipulation;
}

img {
  user-select: none;
  -webkit-user-drag: none;
}

html,
body {
  margin: 0;
  /* min-height (not height:100%) so the body can grow taller than the viewport
	   when the table doesn't fit on small screens. A fixed height would clip the
	   overflowing bottom controls and make them non-interactive (dead hover). */
  min-height: 100%;
}

body {
  font-family: "Poppins", sans-serif;
  color: #fff;
  background: radial-gradient(
      ellipse 120% 90% at 50% 18%,
      var(--felt-light) 0%,
      #0c5a2b 45%,
      var(--felt-dark) 100%
    )
    fixed;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* Subtle table felt texture / vignette */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(
    ellipse at center,
    transparent 55%,
    rgba(0, 0, 0, 0.35) 100%
  );
}

/* ---------- Top bar ---------- */
.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: 12px;
  padding-left: max(18px, env(safe-area-inset-left));
  padding-right: max(18px, env(safe-area-inset-right));
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  position: relative;
  z-index: 10;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  min-width: 0;
}

.topbar-left {
  grid-column: 1;
  grid-row: 1;
  justify-content: flex-start;
  z-index: 1;
}

.topbar-right {
  grid-column: 3;
  grid-row: 1;
  justify-content: flex-end;
}

.topbar-center {
  grid-column: 1 / -1;
  grid-row: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  column-gap: 10px;
  width: 100%;
}

.topbar-center > .stat-chip:first-child {
  grid-column: 1;
  justify-self: end;
}

.topbar-center > .bankroll-chip {
  grid-column: 2;
  justify-self: center;
}

.topbar-center > #settings-open {
  grid-column: 3;
  justify-self: start;
}

.brand {
  font-family: "Playfair Display", serif;
  font-size: 1.5em;
  letter-spacing: 0.5px;
  color: var(--gold);
}

.bankroll-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
  padding: 4px 14px;
  height: var(--topbar-chip-h);
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--gold-deep);
  border-radius: 999px;
}

.bankroll-label {
  font-size: 0.6em;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.7;
}

#bankroll {
  font-weight: 700;
  color: var(--gold);
  /* Fixed width for the longest possible value ($1,000,000,000) so the
     chip never resizes and the topbar never shifts */
  min-width: 13ch;
  text-align: center;
  display: block;
}

.stat-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
  padding: 4px 14px;
  height: var(--topbar-chip-h);
  min-width: 64px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 999px;
}

.stat-label {
  font-size: 0.6em;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.7;
}

#bust-count {
  font-weight: 700;
  min-width: 3ch;
  text-align: center;
  display: block;
}

#settings-open {
  cursor: pointer;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  font-family: inherit;
  transition:
    background 0.2s,
    border-color 0.2s;
}
#settings-open:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.35);
}
#settings-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-family: inherit;
  text-decoration: none;
  font-size: 1.1em;
  cursor: pointer;
  transition: background 0.2s;
}
.icon-button:hover {
  background: rgba(255, 255, 255, 0.22);
}

/* ---------- Table ---------- */
.table {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* space-between on small/mobile viewports keeps zones pinned and static.
     On larger screens the viewport is tall enough that space-between creates
     huge gaps, so switch to center with a fixed gap instead. */
  justify-content: space-between;
  padding: 18px 12px 24px;
  gap: 0;
  max-width: 560px;
  width: 100%;
  margin-inline: auto;
  position: relative;
  z-index: 1;
}

.dealer-area,
.player-area {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* reserved height so dealing cards never reflows the rest of the table */
  min-height: calc(var(--card-w) * 1.6);
}

.player-area {
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 28px;
  align-items: flex-start;
  min-height: calc(var(--card-w) * 1.95);
}

.hand-label {
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 0.72em;
  opacity: 0.85;
  margin-bottom: 10px;
  /* Center the word itself. The total badge is taken out of flow (below) so it
	   can't pull the label off-center when it appears and disappears. */
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.hand-label .badge {
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 8px;
}

/* ---------- Cards ---------- */
.cards {
  display: flex;
  align-items: flex-start;
  /* Left-anchored, fixed-width track: the left card stays put and new cards
	   extend to the right, so dealing/hitting never shifts the cards already on
	   the table. A two-card hand exactly fills the track, keeping it centered. */
  justify-content: flex-start;
  width: calc(var(--card-w) * 1.5);
  margin-inline: auto;
  min-height: calc(var(--card-w) * 1.4);
}

.card {
  width: var(--card-w);
  flex: none; /* never shrink: extra cards overflow right rather than squeezing */
  border-radius: 8px;
  box-shadow: var(--shadow);
  background: #fff;
  margin-left: calc(var(--card-w) * -0.5);
  transition: transform 0.2s;
}
.card:first-child {
  margin-left: 0;
}
.card:hover {
  transform: translateY(-8px);
  z-index: 5;
}

/* ---------- Player hand ---------- */
.hand {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 14px 8px;
  border-radius: 16px;
  border: 2px solid transparent;
  transition:
    border-color 0.25s,
    background 0.25s;
}
.hand.active {
  border-color: var(--gold);
  background: rgba(245, 196, 81, 0.08);
  box-shadow: 0 0 24px rgba(245, 196, 81, 0.25);
}

.hand-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.hand-bet {
  font-size: 0.8em;
  color: var(--gold);
  font-weight: 600;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  font-weight: 600;
  font-size: 0.82em;
}
.badge.dealer {
  background: rgba(0, 0, 0, 0.55);
}
.badge.bust {
  background: #b3261e;
}
.badge.win {
  background: var(--gold-deep);
  color: var(--ink);
}

.result {
  font-size: 0.72em;
  font-weight: 700;
  letter-spacing: 1px;
  padding: 3px 8px;
  border-radius: 6px;
}
.result.win,
.result.blackjack {
  background: var(--gold);
  color: var(--ink);
}
.result.push {
  background: rgba(255, 255, 255, 0.25);
}
.result.surrender {
  background: rgba(140, 29, 24, 0.6);
}
.result.lose {
  background: #8c1d18;
}

/* ---------- Center message ---------- */
.message {
  /* Reserve enough height for the large Playfair line so showing/hiding the
	   outcome text never nudges the dealer and controls vertically. */
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Playfair Display", serif;
  font-size: clamp(1.1em, 4vw, 1.8em);
  color: var(--gold);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transform: translateY(6px);
  transition:
    opacity 0.35s,
    transform 0.35s;
  text-align: center;
  /* Purely decorative: never intercept hover/clicks on the controls below it.
	   Without this, the reserved (often invisible) message box can overlap the
	   top of the action buttons and swallow their :hover. */
  pointer-events: none;
}
.message.show {
  opacity: 1;
  transform: translateY(0);
}

/* ---------- Controls ---------- */
.controls {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.bet-row {
  display: flex;
  justify-content: center;
}

.bet-circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 3px dashed rgba(255, 255, 255, 0.4);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition:
    border-color 0.25s,
    box-shadow 0.25s;
}
.bet-circle:not(.empty) {
  border-style: solid;
  border-color: var(--gold);
  box-shadow: inset 0 0 18px rgba(245, 196, 81, 0.25);
}
.bet-amount {
  font-size: 0.88em;
  font-weight: 700;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
  max-width: 10ch;
  text-align: center;
}
.bet-placeholder {
  font-size: 0.62em;
  letter-spacing: 2px;
  opacity: 0.55;
}
.bet-circle:not(.empty) .bet-placeholder {
  display: none;
}

.control-slot {
  width: 100%;
  /* Stack all groups on top of each other; the tallest one (action buttons)
     sets the height permanently — nothing outside ever shifts. */
  display: grid;
  grid-template-areas: "slot";
}

.control-group {
  grid-area: slot;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-height: 96px;
  /* Hide/show by fading — never by collapsing — so the slot height is fixed */
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.18s;
  pointer-events: none;
}
.control-group.active {
  visibility: visible;
  opacity: 1;
  pointer-events: auto;
}

.chip-tray {
  display: flex;
  gap: 10px;
  justify-content: center;
  transition:
    opacity 0.25s,
    filter 0.25s;
}
/* Greyed out while a hand is in play (still visible, just inert) */
.chip-tray.disabled {
  opacity: 0.35;
  filter: grayscale(0.7);
  pointer-events: none;
}
.chip {
  display: inline-block;
  padding: 0;
  border: none;
  background: none;
  line-height: 0;
  cursor: pointer;
  transition: transform 0.15s;
}
/* Denomination overlaid on a blank chip (for values without a baked-in image) */

.chip img {
  width: clamp(52px, 14vw, 68px);
  height: clamp(52px, 14vw, 68px);
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.4));
}
.chip:hover {
  transform: translateY(-6px) scale(1.05);
}
.chip:active {
  transform: translateY(-2px) scale(0.98);
}

.button-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
}

.btn-group-secondary,
.btn-group-primary {
  display: flex;
  gap: 10px;
}

/* ---------- Buttons ---------- */
.btn {
  font-family: inherit;
  font-size: 1em;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 12px 26px;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  color: #fff;
  transition:
    transform 0.12s,
    filter 0.2s,
    opacity 0.2s;
}
.btn:active {
  transform: translateY(2px);
}
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: linear-gradient(180deg, var(--gold) 0%, var(--gold-deep) 100%);
  color: var(--ink);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.35);
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.07);
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
}
.btn-ghost:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
}

.btn-action {
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.25);
  min-width: 92px;
}
.btn-action:hover:not(:disabled) {
  background: rgba(0, 0, 0, 0.6);
  border-color: var(--gold);
}

.insurance-text {
  margin: 0;
  font-size: 0.95em;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: max(28px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  padding: 12px 22px;
  border-radius: 999px;
  font-size: 0.9em;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.25s,
    transform 0.25s;
  z-index: 50;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Rules modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(3px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal-overlay.show {
  display: flex;
}

.modal {
  background: #10261a;
  border: 1px solid var(--gold-deep);
  border-radius: 18px;
  padding: 26px;
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  text-align: left;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal h2 {
  font-family: "Playfair Display", serif;
  color: var(--gold);
  margin-top: 0;
}
.modal p {
  font-size: 0.95em;
  line-height: 1.6;
}
.modal strong {
  color: var(--gold);
}
.modal .btn {
  display: block;
  margin: 18px auto 0;
}

/* ---------- Button alignment ---------- */
.controls.align-left .bet-row,
.controls.align-left .chip-tray,
.controls.align-left .button-row {
  justify-content: flex-start;
}
.controls.align-left .bet-circle {
  margin-inline: 0;
}

.controls.align-right .bet-row,
.controls.align-right .chip-tray,
.controls.align-right .button-row {
  justify-content: flex-end;
}
.controls.align-right .bet-circle {
  margin-inline: 0;
}

/* ---------- Settings modal ---------- */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.settings-row:last-child {
  border-bottom: none;
}

.settings-label {
  font-size: 0.95em;
  opacity: 0.9;
}

.align-toggle {
  display: flex;
  gap: 6px;
}

.align-toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  transition:
    background 0.15s,
    border-color 0.15s;
}
.align-toggle-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}
.align-toggle-btn.active {
  background: var(--gold-deep);
  border-color: var(--gold);
  color: var(--ink);
}

/* ---------- Standalone PWA (added to home screen) ---------- */
@media (display-mode: standalone) {
  .brand {
    display: none;
  }
}

/* ---------- Responsive ---------- */
@media (max-width: 600px) {
  .player-area {
    gap: 14px;
  }
  .btn {
    padding: 11px 18px;
  }
  .btn-action {
    min-width: 72px;
    padding: 11px 12px;
  }
  .brand {
    display: none;
  }
}

/* Desktop with short screen (< 1700px tall): compact layout to avoid scrolling */
@media (min-width: 601px) and (max-height: 1699px) {
  :root {
    --card-w: clamp(44px, 7.5vh, 72px);
    --topbar-chip-h: 36px;
  }
  .topbar {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: 8px;
  }
  .table {
    justify-content: space-between;
    padding-top: 10px;
    padding-bottom: 12px;
    gap: 0;
  }
  .dealer-area,
  .player-area {
    min-height: calc(var(--card-w) * 1.5);
  }
  .player-area {
    gap: 14px;
    min-height: calc(var(--card-w) * 1.75);
  }
  .hand-label {
    margin-bottom: 6px;
  }
  .hand {
    padding: 6px 10px 4px;
  }
  .message {
    min-height: 30px;
  }
  .controls {
    gap: 8px;
  }
  .bet-circle {
    width: 78px;
    height: 78px;
  }
  .bet-amount {
    font-size: 0.68em;
  }
  .control-group {
    min-height: 74px;
    gap: 8px;
  }
  .chip-tray {
    gap: 8px;
  }
  .chip img {
    width: clamp(42px, 6.5vh, 54px);
    height: clamp(42px, 6.5vh, 54px);
  }
  .btn {
    font-size: 0.92em;
    padding: 11px 18px;
  }
  .btn-action {
    min-width: 72px;
    padding: 11px 12px;
  }
  .brand {
    display: none;
  }
}

/* Large desktop / fullscreen (tall window): scale up and group near center */
@media (min-width: 1000px) and (min-height: 700px) {
  :root {
    --card-w: clamp(112px, 6.4vh, 138px);
    --topbar-chip-h: 52px;
  }
  .topbar {
    padding-top: max(18px, env(safe-area-inset-top));
    padding-bottom: 18px;
  }
  .brand {
    font-size: 1.9em;
  }
  .bankroll-chip,
  .stat-chip {
    padding-inline: 18px;
  }
  .stat-chip {
    min-width: 78px;
  }
  .table {
    max-width: 820px;
    justify-content: center;
    gap: 18px;
    padding-top: 28px;
    padding-bottom: 34px;
  }
  .dealer-area,
  .player-area {
    min-height: calc(var(--card-w) * 1.45);
  }
  .player-area {
    gap: 22px;
    min-height: calc(var(--card-w) * 1.7);
  }
  .hand-label {
    margin-bottom: 8px;
  }
  .message {
    min-height: 40px;
  }
  .controls {
    max-width: 720px;
    gap: 14px;
  }
  .bet-circle {
    width: 108px;
    height: 108px;
  }
  .bet-amount {
    font-size: 0.96em;
  }
  .chip-tray,
  .button-row,
  .btn-group-secondary,
  .btn-group-primary {
    gap: 14px;
  }
  .chip img {
    width: clamp(72px, 3.8vh, 86px);
    height: clamp(72px, 3.8vh, 86px);
  }
  .btn {
    font-size: 1.05em;
    padding: 14px 30px;
  }
  .btn-action {
    min-width: 108px;
  }
}

/* Wide but short window (e.g. 1/4 screen on 4K): compact layout, no scrolling */
@media (min-width: 1000px) and (max-height: 699px) {
  :root {
    --card-w: clamp(40px, 7vh, 62px);
    --topbar-chip-h: 34px;
  }
  .topbar {
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: 6px;
  }
  .brand {
    display: none;
  }
  .table {
    max-width: 820px;
    justify-content: space-between;
    padding-top: 8px;
    padding-bottom: 10px;
    gap: 0;
  }
  .dealer-area,
  .player-area {
    min-height: calc(var(--card-w) * 1.5);
  }
  .player-area {
    gap: 12px;
    min-height: calc(var(--card-w) * 1.75);
  }
  .hand-label {
    margin-bottom: 4px;
  }
  .hand {
    padding: 4px 8px 2px;
  }
  .message {
    min-height: 26px;
  }
  .controls {
    max-width: 720px;
    gap: 6px;
  }
  .bet-circle {
    width: 68px;
    height: 68px;
  }
  .bet-amount {
    font-size: 0.62em;
  }
  .control-group {
    min-height: 64px;
    gap: 6px;
  }
  .chip-tray {
    gap: 8px;
  }
  .chip img {
    width: clamp(36px, 5.5vh, 48px);
    height: clamp(36px, 5.5vh, 48px);
  }
  .btn {
    font-size: 0.88em;
    padding: 9px 16px;
  }
  .btn-action {
    min-width: 68px;
    padding: 9px 10px;
  }
}

/* Runtime fallback: if the rendered game overflows vertically, force compact mode. */
body.compact-viewport {
  --card-w: clamp(40px, 7vh, 62px);
  --topbar-chip-h: 34px;
}

body.compact-viewport .topbar {
  padding-top: max(6px, env(safe-area-inset-top));
  padding-bottom: 6px;
}

body.compact-viewport .brand {
  display: none;
}

body.compact-viewport .table {
  max-width: 820px;
  justify-content: space-between;
  padding-top: 8px;
  padding-bottom: 10px;
  gap: 0;
}

body.compact-viewport .dealer-area,
body.compact-viewport .player-area {
  min-height: calc(var(--card-w) * 1.5);
}

body.compact-viewport .player-area {
  gap: 12px;
  min-height: calc(var(--card-w) * 1.75);
}

body.compact-viewport .hand-label {
  margin-bottom: 4px;
}

body.compact-viewport .hand {
  padding: 4px 8px 2px;
}

body.compact-viewport .message {
  min-height: 26px;
}

body.compact-viewport .controls {
  max-width: 720px;
  gap: 6px;
}

body.compact-viewport .bet-circle {
  width: 68px;
  height: 68px;
}

body.compact-viewport .bet-amount {
  font-size: 0.62em;
}

body.compact-viewport .control-group {
  min-height: 64px;
  gap: 6px;
}

body.compact-viewport .chip-tray,
body.compact-viewport .button-row,
body.compact-viewport .btn-group-secondary,
body.compact-viewport .btn-group-primary {
  gap: 8px;
}

body.compact-viewport .chip img {
  width: clamp(36px, 5.5vh, 48px);
  height: clamp(36px, 5.5vh, 48px);
}

body.compact-viewport .btn {
  font-size: 0.88em;
  padding: 9px 16px;
}

body.compact-viewport .btn-action {
  min-width: 68px;
  padding: 9px 10px;
}
