:root {
  --bg-outer: #061A14;
  --baize-glow: #155240;
  --baize-mid: #0D3B2E;
  --baize-edge: #07221A;
  --vermilion: #DF4B26;
  --vermilion-bright: #E4572E;
  --vermilion-deep: #B83618;
  --cream: #F5EBD6;
  --cream-aged: #E5D6B8;
  --ink: #191C18;
  --ease-tactile: cubic-bezier(0.22, 1, 0.36, 1);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-outer);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.tabletop {
  position: relative;
  width: 100%;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background-color: var(--baize-mid);
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.65);
}

.baize-surface {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(
      ellipse 72% 62% at 50% 48%,
      var(--baize-glow) 0%,
      var(--baize-mid) 54%,
      var(--baize-edge) 100%
    );
  pointer-events: none;
}

.felt-grain {
  position: absolute;
  inset: 0;
  opacity: 0.08;
  pointer-events: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='120' height='120' filter='url(%23n)' opacity='0.9'/%3E%3C/svg%3E");
  background-size: 120px 120px;
  mix-blend-mode: overlay;
}

/* Matchbox double-hairline frame */
.matchbox-frame {
  position: absolute;
  inset:
    calc(10px + env(safe-area-inset-top))
    calc(10px + env(safe-area-inset-right))
    calc(10px + env(safe-area-inset-bottom))
    calc(10px + env(safe-area-inset-left));
  border: 1.25px solid rgba(245, 235, 214, 0.44);
  border-radius: 8px;
  pointer-events: none;
  z-index: 15;
}

.frame-inner {
  position: absolute;
  inset: 5px;
  border: 1px solid rgba(245, 235, 214, 0.24);
  border-radius: 5px;
}

.corner-ornament {
  position: absolute;
  width: 28px;
  height: 28px;
  color: rgba(245, 235, 214, 0.56);
}

.corner-ornament svg {
  width: 100%;
  height: 100%;
  display: block;
}

.corner-tl {
  top: 2px;
  left: 2px;
}

.corner-tr {
  top: 2px;
  right: 2px;
  transform: rotate(90deg);
}

.corner-br {
  bottom: 2px;
  right: 2px;
  transform: rotate(180deg);
}

.corner-bl {
  bottom: 2px;
  left: 2px;
  transform: rotate(270deg);
}

/* Main table layout */
.table-stage {
  position: relative;
  z-index: 10;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding:
    calc(16px + env(safe-area-inset-top))
    calc(18px + env(safe-area-inset-right))
    calc(14px + env(safe-area-inset-bottom))
    calc(18px + env(safe-area-inset-left));
  transition: opacity 240ms var(--ease-tactile);
}

.tabletop:not([data-ui-state="seated"]) .table-stage {
  opacity: 0;
  pointer-events: none;
}

.seat-zone {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.seat-opponent {
  padding-top: 8px;
}

.seat-self {
  padding-bottom: 4px;
}

/* Opponent empty felt imprint ring */
.empty-seat-ring {
  position: absolute;
  width: clamp(114px, 31vw, 140px);
  height: clamp(114px, 31vw, 140px);
  opacity: 0;
  transform: scale(0.92);
  transition:
    opacity 280ms var(--ease-tactile),
    transform 280ms var(--ease-tactile);
  pointer-events: none;
}

.empty-seat-ring svg {
  width: 100%;
  height: 100%;
  display: block;
}

.tabletop[data-opp-occupied="false"] .empty-seat-ring {
  opacity: 1;
  transform: scale(1);
}

/* Cup slots */
.cup-slot {
  position: relative;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cup-slot-opponent {
  width: clamp(118px, 33vw, 146px);
  height: clamp(114px, 32vw, 142px);
  pointer-events: none;
  opacity: 0;
  transform: scale(0.84) translate3d(0, -14px, 0);
  transition:
    transform 380ms var(--ease-tactile),
    opacity 320ms var(--ease-tactile);
}

.cup-slot-self {
  width: clamp(130px, 36vw, 160px);
  height: clamp(126px, 35vw, 156px);
  cursor: pointer;
  touch-action: manipulation;
  outline: none;
  transition:
    transform 260ms var(--ease-tactile),
    opacity 320ms var(--ease-tactile);
}

.cup-slot-self:focus-visible {
  filter: drop-shadow(0 0 0 2px rgba(245, 235, 214, 0.75));
}

.cup-slot svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.cup-motion-arcs {
  opacity: 0;
}

/* Opponent seat states */
.tabletop[data-opp-occupied="true"][data-phase="playing"] .cup-slot-opponent {
  opacity: 1;
  transform: scale(0.9) translate3d(0, 0, 0);
}

.tabletop[data-opp-occupied="true"][data-opp-connected="false"][data-phase="playing"] .cup-slot-opponent {
  animation: oppDisconnectPulse 2.4s ease-in-out infinite;
}

.cup-slot-opponent.is-entering {
  animation: cupSettleIn 360ms var(--ease-tactile) forwards;
}

/* Peek animation (~260ms) */
.tabletop[data-phase="playing"][data-self-peeking="true"] .cup-slot-self {
  transform: translate3d(62px, -84px, 0) rotate(18deg) scale(0.78);
  transition-duration: 260ms;
}

/* Simultaneous reveal (~380ms): both cups lift off cleanly exposing all 10 dice */
.tabletop[data-phase="revealed"] .cup-slot-opponent {
  opacity: 0;
  transform: translate3d(0, -96px, 0) scale(0.74) rotate(-6deg);
  transition-duration: 380ms;
  pointer-events: none;
}

.tabletop[data-phase="revealed"] .cup-slot-self {
  opacity: 0;
  transform: translate3d(0, -96px, 0) scale(0.76) rotate(6deg);
  transition-duration: 380ms;
  pointer-events: none;
}

/* Next round glide down (~300ms) */
.tabletop[data-phase="playing"] .cup-slot-self,
.tabletop[data-phase="playing"] .cup-slot-opponent {
  transition-duration: 300ms;
}

/* Shake rattle (~420ms) */
.cup-slot.is-shaking {
  animation: cupRattle 420ms cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

.cup-slot.is-shaking .cup-motion-arcs {
  animation: motionArcFlash 420ms ease-out both;
}

@keyframes cupRattle {
  0% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
  14% {
    transform: translate3d(-11px, -5px, 0) rotate(-11deg);
  }
  30% {
    transform: translate3d(12px, 3px, 0) rotate(10deg);
  }
  46% {
    transform: translate3d(-10px, -4px, 0) rotate(-9deg);
  }
  62% {
    transform: translate3d(9px, 3px, 0) rotate(8deg);
  }
  78% {
    transform: translate3d(-5px, -2px, 0) rotate(-4deg);
  }
  100% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
}

.cup-slot-opponent.is-shaking {
  animation-name: cupRattleOpp;
}

@keyframes cupRattleOpp {
  0% {
    transform: scale(0.9) translate3d(0, 0, 0) rotate(0deg);
  }
  14% {
    transform: scale(0.9) translate3d(-10px, -4px, 0) rotate(-10deg);
  }
  30% {
    transform: scale(0.9) translate3d(11px, 3px, 0) rotate(9deg);
  }
  46% {
    transform: scale(0.9) translate3d(-9px, -3px, 0) rotate(-8deg);
  }
  62% {
    transform: scale(0.9) translate3d(8px, 2px, 0) rotate(7deg);
  }
  78% {
    transform: scale(0.9) translate3d(-4px, -2px, 0) rotate(-4deg);
  }
  100% {
    transform: scale(0.9) translate3d(0, 0, 0) rotate(0deg);
  }
}

@keyframes motionArcFlash {
  0% {
    opacity: 0;
  }
  18% {
    opacity: 0.95;
  }
  65% {
    opacity: 0.78;
  }
  100% {
    opacity: 0;
  }
}

@keyframes cupSettleIn {
  0% {
    opacity: 0;
    transform: scale(0.72) translate3d(0, -24px, 0);
  }
  65% {
    opacity: 1;
    transform: scale(0.93) translate3d(0, 3px, 0);
  }
  100% {
    opacity: 1;
    transform: scale(0.9) translate3d(0, 0, 0);
  }
}

@keyframes oppDisconnectPulse {
  0%,
  100% {
    opacity: 0.62;
  }
  50% {
    opacity: 0.34;
  }
}

/* 2+3 Vintage Dice Cluster */
.dice-cluster {
  position: absolute;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  pointer-events: none;
  opacity: 0;
  transform: scale(0.92);
  transition:
    opacity 220ms var(--ease-tactile),
    transform 260ms var(--ease-tactile);
}

.dice-cluster-self {
  margin-top: 16px;
}

.dice-cluster-opponent {
  margin-bottom: 4px;
}

.dice-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
}

.die-tile {
  width: clamp(34px, 9.6vw, 44px);
  height: clamp(34px, 9.6vw, 44px);
  transform: rotate(var(--tilt, 0deg));
}

.die-tile svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.tabletop[data-phase="playing"][data-self-peeking="true"][data-self-has-dice="true"] .dice-cluster-self {
  opacity: 1;
  transform: scale(1);
}

.tabletop[data-phase="revealed"] .dice-cluster-self,
.tabletop[data-phase="revealed"] .dice-cluster-opponent {
  opacity: 1;
  transform: scale(1);
  transition-delay: 80ms;
}

/* Center Divider & Medallion */
.center-divider {
  position: relative;
  flex: 0 0 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.divider-line {
  position: absolute;
  left: 8px;
  right: 8px;
  height: 1px;
  background: linear-gradient(
    90deg,
    rgba(245, 235, 214, 0) 0%,
    rgba(245, 235, 214, 0.34) 18%,
    rgba(245, 235, 214, 0.14) 41%,
    rgba(245, 235, 214, 0) 45%,
    rgba(245, 235, 214, 0) 55%,
    rgba(245, 235, 214, 0.14) 59%,
    rgba(245, 235, 214, 0.34) 82%,
    rgba(245, 235, 214, 0) 100%
  );
  pointer-events: none;
}

.readiness-pips {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px;
  border-radius: 999px;
  border: 1px solid rgba(245, 235, 214, 0.24);
  background: rgba(7, 34, 26, 0.65);
}

.readiness-pips[hidden] {
  display: none;
}

.ready-pip {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid rgba(245, 235, 214, 0.42);
  background: transparent;
  transition:
    background-color 200ms var(--ease-tactile),
    border-color 200ms var(--ease-tactile),
    box-shadow 200ms var(--ease-tactile),
    transform 200ms var(--ease-tactile);
}

.tabletop[data-opp-has-dice="true"] .ready-pip-opp {
  background-color: var(--cream);
  border-color: var(--cream);
  box-shadow: 0 0 8px rgba(245, 235, 214, 0.55);
  transform: scale(1.1);
}

.tabletop[data-self-has-dice="true"] .ready-pip-self {
  background-color: var(--vermilion-bright);
  border-color: var(--cream);
  box-shadow: 0 0 10px rgba(228, 87, 46, 0.65);
  transform: scale(1.1);
}

/* Interactive Buttons (>= 48px touch targets) */
button {
  font: inherit;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  outline: none;
}

button[hidden] {
  display: none;
}

button:focus-visible {
  box-shadow: 0 0 0 2.5px var(--cream);
}

button:disabled {
  cursor: default;
  opacity: 0.32;
}

.medallion-btn {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--baize-edge);
  border: 1.5px solid rgba(245, 235, 214, 0.55);
  box-shadow:
    0 6px 18px rgba(0, 0, 0, 0.42),
    inset 0 0 0 3px var(--baize-mid);
  transition:
    transform 180ms var(--ease-tactile),
    box-shadow 180ms var(--ease-tactile);
}

.medallion-btn svg {
  width: 44px;
  height: 44px;
  display: block;
}

.medallion-btn:active:not(:disabled) {
  transform: scale(0.92);
}

.medallion-reveal {
  border-color: var(--vermilion-bright);
  animation: sealInvite 2s ease-in-out infinite;
}

@keyframes sealInvite {
  0%,
  100% {
    box-shadow:
      0 6px 18px rgba(0, 0, 0, 0.42),
      0 0 0 0 rgba(228, 87, 46, 0.42);
  }
  50% {
    box-shadow:
      0 6px 18px rgba(0, 0, 0, 0.42),
      0 0 0 7px rgba(228, 87, 46, 0);
  }
}

.share-btn,
.leave-btn {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top));
  width: 48px;
  height: 48px;
  border-radius: 50%;
  z-index: 30;
  opacity: 0.68;
  transition:
    opacity 160ms ease,
    transform 160ms var(--ease-tactile),
    background-color 160ms ease;
}

.share-btn {
  left: calc(16px + env(safe-area-inset-left));
}

.leave-btn {
  right: calc(16px + env(safe-area-inset-right));
}

.share-btn svg,
.leave-btn svg {
  width: 24px;
  height: 24px;
  display: block;
}

.share-btn .share-done-glyph {
  display: none;
}

.share-btn[data-copied="true"] {
  opacity: 1;
  background-color: rgba(223, 75, 38, 0.22);
}

.share-btn[data-copied="true"] .share-glyph {
  display: none;
}

.share-btn[data-copied="true"] .share-done-glyph {
  display: block;
}

.share-btn:hover:not(:disabled),
.share-btn:focus-visible,
.leave-btn:hover:not(:disabled),
.leave-btn:focus-visible {
  opacity: 1;
  background-color: rgba(245, 235, 214, 0.08);
}

.share-btn:active,
.leave-btn:active {
  transform: scale(0.92);
}

.tabletop:not([data-ui-state="seated"]) .share-btn,
.tabletop:not([data-ui-state="seated"]) .leave-btn {
  display: none;
}

/* Bottom Stamp Controls */
.bottom-controls {
  flex: 0 0 auto;
  height: 76px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 36px;
  padding-bottom: 6px;
  z-index: 25;
}

.stamp-btn {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #154A3A 0%, #09261D 100%);
  border: 1.5px solid rgba(245, 235, 214, 0.52);
  box-shadow:
    0 6px 16px rgba(0, 0, 0, 0.42),
    inset 0 0 0 2.5px rgba(25, 28, 24, 0.55);
  transition:
    transform 160ms var(--ease-tactile),
    border-color 180ms ease,
    background 180ms ease,
    opacity 180ms ease;
}

.stamp-btn svg {
  width: 38px;
  height: 38px;
  display: block;
}

.stamp-btn:active:not(:disabled) {
  transform: scale(0.91);
}

.stamp-shake:not(:disabled) {
  border-color: rgba(228, 87, 46, 0.78);
}

.stamp-peek[aria-pressed="true"]:not(:disabled) {
  border-color: var(--vermilion-bright);
  background: radial-gradient(circle at 35% 30%, #DF4B26 0%, #A93115 100%);
  box-shadow:
    0 0 0 3px rgba(228, 87, 46, 0.28),
    0 6px 16px rgba(0, 0, 0, 0.42);
}

/* State Overlays (booting / locked / full / left) */
.state-overlay {
  position: absolute;
  inset: 0;
  z-index: 28;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 24px;
}

.state-overlay[hidden] {
  display: none;
}

.pin-form { display: grid; gap: 10px; width: min(100%, 15rem); text-align: center; color: #f5ebd6; }
.pin-form input, .pin-form button { min-width: 0; min-height: 44px; font: inherit; font-size: 1rem; border: 1px solid #f5ebd6; border-radius: 4px; text-align: center; }
.pin-form input { background: #173d31; color: #f5ebd6; letter-spacing: 0.3em; }
.pin-form button { background: #e46a46; color: #142d24; font-weight: 700; }
#pin-message { min-height: 1.4em; margin: 0; font-size: 0.875rem; }

.tabletop:not([data-ui-state="booting"]) .overlay-booting {
  display: none;
}

.boot-seal {
  width: 72px;
  height: 72px;
  opacity: 0.72;
  animation: bootPulse 1.6s ease-in-out infinite;
}

.boot-seal svg,
.state-emblem svg {
  width: 100%;
  height: 100%;
  display: block;
}

.state-emblem {
  width: 120px;
  height: 120px;
}

.stamp-retry {
  width: 64px;
  height: 64px;
  border-color: rgba(245, 235, 214, 0.65);
}

.stamp-retry svg {
  width: 36px;
  height: 36px;
}

@keyframes bootPulse {
  0%,
  100% {
    opacity: 0.35;
    transform: scale(0.96);
  }
  50% {
    opacity: 0.78;
    transform: scale(1);
  }
}

@media (max-height: 620px) {
  .center-divider {
    flex: 0 0 46px;
    height: 46px;
  }

  .medallion-btn {
    width: 50px;
    height: 50px;
  }

  .medallion-btn svg {
    width: 38px;
    height: 38px;
  }

  .bottom-controls {
    height: 64px;
    gap: 30px;
  }

  .stamp-btn {
    width: 54px;
    height: 54px;
  }

  .stamp-btn svg {
    width: 34px;
    height: 34px;
  }

  .tabletop[data-phase="playing"][data-self-peeking="true"] .cup-slot-self {
    transform: translate3d(58px, -72px, 0) rotate(18deg) scale(0.75);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0ms !important;
  }
}
