* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(rgba(10, 30, 12, 0.72), rgba(10, 30, 12, 0.8)), url("assets/stadium.webp");
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  color: #fff;
  overflow-x: hidden;
}

.container {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: clamp(16px, 3vh, 32px) clamp(16px, 4vw, 48px);
  padding-top: max(clamp(16px, 3vh, 32px), env(safe-area-inset-top));
  padding-bottom: max(clamp(16px, 3vh, 32px), env(safe-area-inset-bottom));
  position: relative;
}

h1 {
  margin: 0 0 clamp(8px, 2vh, 20px);
  text-align: center;
  font-size: clamp(1.5rem, 3vw, 2.2rem);
}

.hidden {
  display: none !important;
}

#mute-toggle {
  position: absolute;
  top: max(clamp(12px, 2vh, 20px), env(safe-area-inset-top));
  right: max(clamp(12px, 3vw, 32px), env(safe-area-inset-right));
  min-width: 0;
  padding: 8px 12px;
  font-size: 1.2rem;
  z-index: 5;
}

#stats-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: clamp(0.8rem, 1.4vw, 0.95rem);
  opacity: 0.9;
  margin-bottom: clamp(6px, 1.5vh, 14px);
}

#history-toggle {
  min-width: 0;
  padding: 6px 12px;
  font-size: clamp(0.75rem, 1.2vw, 0.85rem);
}

#history-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(360px, 100vw);
  background: rgba(15, 35, 18, 0.97);
  border-left: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: -12px 0 30px rgba(0, 0, 0, 0.4);
  z-index: 20;
  display: flex;
  flex-direction: column;
  padding: max(16px, env(safe-area-inset-top)) 16px 16px;
  overflow-y: auto;
}

#history-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

#history-close {
  min-width: 0;
  padding: 4px 10px;
  font-size: 1rem;
}

#history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-empty {
  opacity: 0.7;
  text-align: center;
  padding: 20px 0;
}

.history-card {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 10px 14px;
  text-align: left;
}

.history-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.history-date {
  font-size: 0.8rem;
  opacity: 0.75;
}

.history-badge {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}

.history-badge.win {
  background: #2e7d32;
  color: #d9ffd9;
}

.history-badge.loss {
  background: #7d2e2e;
  color: #ffd9d9;
}

.history-badge.tie {
  background: #6b6b2e;
  color: #ffffd9;
}

.history-card-scores {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  opacity: 0.9;
}

.screen {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
}

.screen p {
  font-size: clamp(1rem, 1.6vw, 1.3rem);
  max-width: 700px;
}

.choices {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 20px;
}

button {
  background: #fdd835;
  color: #1b1b1b;
  border: none;
  border-radius: 10px;
  padding: 14px 22px;
  font-size: clamp(0.95rem, 1.3vw, 1.1rem);
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.2s ease;
  min-width: 90px;
}

button:hover {
  background: #ffee58;
  transform: translateY(-2px);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
  transform: none;
}

/* --- Game screen: two-column layout on wide viewports --- */
#game-screen {
  justify-content: flex-start;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.game-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 2vh, 24px);
  width: 100%;
  flex: 1;
}

@media (min-width: 900px) {
  .game-layout {
    flex-direction: row;
    align-items: stretch;
    justify-content: center;
  }

  .game-left,
  .game-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0;
  }
}

.game-left {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 2vh, 18px);
}

.game-right {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.scoreboard {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  padding: 16px 20px;
  width: 100%;
  max-width: 460px;
}

#innings-label {
  font-weight: 700;
  font-size: clamp(1rem, 1.6vw, 1.3rem);
  text-align: center;
  margin-bottom: 10px;
  opacity: 0.85;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 0.85rem;
}

.scorecard-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 3vw, 24px);
}

.scorecard-team {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 8px;
  border-radius: 10px;
  border: 2px solid transparent;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.scorecard-team.batting {
  border-color: #fdd835;
  background: rgba(253, 216, 53, 0.12);
}

.scorecard-avatar {
  font-size: clamp(1.6rem, 3vw, 2.2rem);
}

.scorecard-name {
  font-size: clamp(0.8rem, 1.2vw, 0.95rem);
  opacity: 0.85;
}

.scorecard-runs {
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.scorecard-vs {
  font-size: 0.8rem;
  opacity: 0.6;
  font-weight: 600;
}

#target-line {
  text-align: center;
  margin-top: 8px;
  font-size: clamp(0.85rem, 1.3vw, 1rem);
  opacity: 0.85;
}

.hands {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 100%;
  max-width: 460px;
}

.hand-box .label {
  font-size: 0.9rem;
  opacity: 0.8;
  margin-bottom: 6px;
}

.hand {
  font-size: clamp(3rem, 6vw, 4.5rem);
  transition: transform 0.15s ease;
}

.hand.shake {
  animation: shake 0.4s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-15deg); }
  75% { transform: rotate(15deg); }
}

#round-message {
  min-height: 1.5em;
  font-size: clamp(1rem, 1.6vw, 1.2rem);
  font-weight: 600;
  max-width: 460px;
}

#end-title {
  font-size: clamp(1.6rem, 3vw, 2.4rem);
  margin-bottom: 8px;
}

.final-scorecards {
  display: flex;
  gap: clamp(14px, 4vw, 32px);
  margin: 10px 0 20px;
}

.final-card {
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 16px;
  padding: clamp(16px, 3vw, 26px) clamp(20px, 4vw, 36px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 120px;
  transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.final-card .scorecard-avatar {
  font-size: clamp(2rem, 4vw, 2.6rem);
}

.final-runs {
  font-size: clamp(2.4rem, 6vw, 3.6rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.final-card.winner {
  border-color: #fdd835;
  box-shadow: 0 0 24px rgba(253, 216, 53, 0.5);
  transform: translateY(-4px) scale(1.04);
  background: rgba(253, 216, 53, 0.12);
}

#camera-panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.video-wrap {
  position: relative;
  width: min(640px, 90vw);
  aspect-ratio: 4 / 3;
  max-height: min(55vh, 55dvh);
  margin: 0 auto;
  border-radius: 16px;
  overflow: hidden;
  background: #111;
  transform: scaleX(-1);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

#webcam {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

@media (max-width: 480px) {
  .video-wrap {
    width: 94vw;
    max-height: 42vh;
    max-height: 42dvh;
  }
}

#countdown-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2rem, 5vw, 3.2rem);
  font-weight: 800;
  color: #fff;
  background: rgba(0, 0, 0, 0.35);
  transform: scaleX(-1);
}

#camera-status,
#detected-live {
  margin-top: 10px;
  font-size: clamp(0.9rem, 1.3vw, 1rem);
  opacity: 0.9;
}

#detected-live {
  font-weight: 700;
  font-size: clamp(1.1rem, 1.6vw, 1.3rem);
}

#manual-fallback {
  margin-top: 18px;
  opacity: 0.85;
}

#manual-fallback summary {
  cursor: pointer;
}
