/* ================================
   Game of Life: Twists & Turns
   Mobile-First CSS
   Breakpoints: base < 768px < desktop
   ================================ */

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #0f3460;
  --bg-dark: #0a0a1a;
  --text-primary: #f0f0f0;
  --text-secondary: #b0b0c0;
  --accent: #e94560;
  --accent-hover: #ff5a7a;
  --success: #4ade80;
  --warning: #fbbf24;
  --info: #60a5fa;
  --earn-it: #ff6b35;
  --learn-it: #4a90d9;
  --live-it: #50c878;
  --love-it: #e91e63;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.6);
  --tap-min: 44px; /* minimum tap target size */
  --font-base: clamp(14px, 3.5vw, 16px);
  --font-sm: clamp(12px, 3vw, 14px);
  --font-md: clamp(16px, 4vw, 18px);
  --font-lg: clamp(20px, 5vw, 24px);
  --font-xl: clamp(28px, 6vw, 36px);
  --space-unit: clamp(4px, 1.5vw, 8px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  font-size: var(--font-base);
  -webkit-text-size-adjust: 100%; /* prevent auto-zoom on iOS */
  touch-action: manipulation; /* double-tap zoom suppression */
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.4;
  overflow-x: hidden;
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(80px + var(--safe-bottom)); /* room for sticky spin bar */
}

button {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

input, textarea, select {
  font-size: var(--font-md); /* prevent iOS zoom on focus */
}

/* ==================== SCREEN SYSTEM ==================== */
.screen-area {
  display: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.screen-area.active {
  display: flex;
  flex-direction: column;
  opacity: 1;
}

#lobby-area, #waiting-area, #game-screen, #game-over-overlay {
  width: 100%;
}

/* ==================== LOBBY ==================== */
.lobby-container {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-unit) calc(var(--space-unit) * 2);
  gap: var(--space-unit);
}

.title-main {
  font-size: var(--font-xl);
  font-weight: 800;
  text-align: center;
  background: linear-gradient(135deg, var(--accent), var(--warning));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: var(--space-unit);
  line-height: 1.2;
}

.title-sub {
  font-size: var(--font-md);
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: var(--space-unit);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-unit);
  width: 100%;
  max-width: 340px;
}

label {
  font-size: var(--font-sm);
  color: var(--text-secondary);
  font-weight: 600;
}

input[type="text"], input[type="number"] {
  background: var(--bg-secondary);
  border: 2px solid var(--bg-card);
  border-radius: var(--radius-md);
  padding: 12px var(--space-unit);
  color: var(--text-primary);
  font-size: var(--font-md);
  outline: none;
  transition: border-color 0.2s;
}

input:focus {
  border-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius-md);
  font-size: var(--font-md);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, filter 0.15s, opacity 0.15s;
  user-select: none;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.96);
}

.btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  filter: brightness(1.1);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 2px solid var(--bg-secondary);
}

.btn-accent {
  background: var(--earn-it);
  color: white;
}

.btn-success {
  background: var(--success);
  color: var(--bg-dark);
}

.btn-full {
  width: 100%;
}

.btn-copy {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 2px solid var(--bg-card);
  gap: 6px;
}

.indicator {
  text-align: center;
  padding: 2rem;
  color: var(--text-secondary);
  font-size: var(--font-md);
}

.spinning-loader {
  width: 40px;
  height: 40px;
  border: 4px solid var(--bg-card);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ==================== WAITING ROOM ==================== */
.waiting-container {
  padding: var(--space-unit) calc(var(--space-unit) * 1.5);
  gap: var(--space-unit);
  max-width: 500px;
  margin: 0 auto;
  width: 100%;
}

.room-header {
  text-align: center;
  padding: var(--space-unit);
}

.room-code {
  font-size: var(--font-xl);
  font-weight: 800;
  letter-spacing: 2px;
  color: var(--accent);
  font-family: 'Courier New', monospace;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  display: inline-block;
  margin: 8px 0;
}

.invite-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.invite-link-input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-dark);
  border: 1px solid var(--bg-card);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--font-sm);
  font-family: monospace;
  word-break: break-all;
}

.players-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 auto;
  width: 100%;
  max-width: 300px;
}

.waiting-player {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  font-size: var(--font-md);
}

.start-btn-wrapper {
  text-align: center;
  padding: var(--space-unit);
}

/* ==================== GAME SCREEN ==================== */
.game-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
}

.hud-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-bottom: 2px solid var(--bg-card);
  flex-shrink: 0;
  gap: 8px;
  z-index: 10;
}

.year-display {
  font-size: var(--font-sm);
  color: var(--text-secondary);
  white-space: nowrap;
}

.current-player-name {
  font-size: var(--font-md);
  font-weight: 700;
  color: var(--accent);
  text-align: center;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Turn advancement notification */
.turn-notification {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(1.2);
  background: var(--bg-dark);
  border: 3px solid var(--accent);
  border-radius: var(--radius-lg);
  padding: 24px 48px;
  font-size: var(--font-lg);
  font-weight: 800;
  text-align: center;
  z-index: 100;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s;
}

.turn-notification.show {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
}

.board-scroll-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  scroll-behavior: smooth;
}

.section-tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.section-tab {
  flex: 1;
  min-width: 0;
  padding: 8px 4px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: var(--font-sm);
  font-weight: 700;
  color: var(--text-secondary);
  background: var(--bg-dark);
  cursor: pointer;
  transition: all 0.15s;
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
}

.section-tab.active {
  color: white;
}

.section-tab[data-section="earn_it"].active { background: var(--earn-it); }
.section-tab[data-section="learn_it"].active { background: var(--learn-it); }
.section-tab[data-section="live_it"].active { background: var(--live-it); }
.section-tab[data-section="love_it"].active { background: var(--love-it); }

.space-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 6px;
  margin-bottom: 16px;
}

.board-space {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border: 2px solid transparent;
  transition: border-color 0.15s;
}

.board-space:hover {
  border-color: var(--text-secondary);
}

.space-strip {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

.space-label {
  font-size: 9px;
  font-weight: 600;
  text-align: center;
  line-height: 1.2;
  margin-top: 4px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
  padding: 0 2px;
}

.space-icon {
  font-size: 16px;
  line-height: 1;
}

/* Player tokens */
.player-token {
  position: absolute;
  bottom: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  font-size: 10px;
  font-weight: 800;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  box-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

/* Score cards area */
.score-cards {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 0;
}

.score-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  font-size: var(--font-sm);
  border-left: 3px solid transparent;
}

.score-card.active {
  background: var(--bg-card);
  border-left-color: var(--accent);
}

.score-card .player-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.score-card .player-name {
  font-weight: 700;
  min-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score-card .player-stats {
  display: flex;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 11px;
}

/* Toast notifications */
.toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  width: calc(100% - 32px);
  max-width: 400px;
  pointer-events: none;
}

.toast {
  background: var(--bg-dark);
  color: var(--text-primary);
  padding: 12px 20px;
  border-radius: var(--radius-lg);
  font-size: var(--font-sm);
  font-weight: 600;
  text-align: center;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.2s, transform 0.2s;
  border: 2px solid var(--bg-card);
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

.toast.toast-success { border-color: var(--success); }
.toast.toast-error { border-color: var(--accent); }
.toast.toast-info { border-color: var(--info); }
.toast.toast-warning { border-color: var(--warning); }

/* Action panel/modal */
.action-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-dark);
  border-top: 3px solid var(--accent);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 16px;
  z-index: 150;
  max-height: 60vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

.action-panel.show {
  transform: translateY(0);
}

.action-panel.answering {
  pointer-events: none;
}

.action-header {
  font-size: var(--font-md);
  font-weight: 700;
  margin-bottom: 8px;
  text-align: center;
}

.action-sub-info {
  font-size: var(--font-sm);
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: 12px;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-btn {
  min-height: var(--tap-min);
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius-md);
  font-size: var(--font-md);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, filter 0.15s;
}

.action-btn:active { transform: scale(0.96); }

.action-primary {
  background: var(--success);
  color: var(--bg-dark);
}

.action-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 2px solid var(--bg-card);
}

.action-accent {
  background: var(--earn-it);
  color: white;
}

/* Career modal */
.career-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 160;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.career-modal-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.career-modal {
  background: var(--bg-dark);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  width: 100%;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.career-modal-header {
  padding: 16px;
  border-bottom: 1px solid var(--bg-card);
  flex-shrink: 0;
}

.career-modal-header h3 {
  font-size: var(--font-md);
  margin-bottom: 8px;
  text-align: center;
}

.career-search {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-secondary);
  border: 2px solid var(--bg-card);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--font-md);
  outline: none;
}

.career-search:focus {
  border-color: var(--accent);
}

.career-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.career-option {
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 0.15s;
  border: 2px solid transparent;
  min-height: var(--tap-min);
}

.career-option:active {
  background: var(--bg-secondary);
  border-color: var(--accent);
}

.career-name {
  font-weight: 700;
  font-size: var(--font-md);
}

.career-detail {
  font-size: var(--font-sm);
  color: var(--text-secondary);
}

.career-empty {
  text-align: center;
  padding: 32px;
  color: var(--text-secondary);
}

/* Spin button — sticky bottom action bar */
.spin-action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px calc(var(--space-unit) * 1.5) calc(12px + var(--safe-bottom));
  background: linear-gradient(to top, var(--bg-dark) 70%, transparent);
  display: flex;
  justify-content: center;
  z-index: 50;
  pointer-events: none;
}

#spin-button {
  pointer-events: auto;
  width: 100%;
  max-width: 320px;
  font-size: var(--font-lg);
  font-weight: 900;
  letter-spacing: 3px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}

#spin-button.my-turn {
  background: linear-gradient(135deg, var(--accent), var(--warning)) !important;
  color: white !important;
  animation: pulse-glow 2s infinite;
}

#spin-button.blocked {
  background: var(--bg-card) !important;
  color: var(--text-secondary) !important;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 10px rgba(233, 69, 96, 0.4); }
  50% { box-shadow: 0 0 25px rgba(233, 69, 96, 0.7); }
}

/* ==================== GAME OVER OVERLAY ==================== */
.game-over-container {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-unit) calc(var(--space-unit) * 2);
  gap: var(--space-unit);
  overflow-y: auto;
}

.over-title {
  font-size: var(--font-xl);
  font-weight: 900;
  text-align: center;
  color: var(--warning);
}

.standings-container {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.standing-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
}

.rank {
  font-size: var(--font-lg);
  min-width: 36px;
  text-align: center;
}

.name-score {
  flex: 1;
  min-width: 0;
}

.standing-name {
  font-size: var(--font-md);
  font-weight: 700;
}

.standing-detail {
  font-size: var(--font-sm);
  color: var(--text-secondary);
}

.total-score {
  font-size: var(--font-lg);
  font-weight: 800;
  color: var(--warning);
}

.play-again-wrapper {
  padding: var(--space-unit);
}

/* ==================== MEDIA QUERIES (desktop enhancement) ==================== */

/* Tablet and above */
@media (min-width: 768px) {
  body {
    padding-bottom: 0;
  }

  .spin-action-bar {
    padding-bottom: 16px;
  }

  #spin-button {
    width: 200px;
    font-size: var(--font-md);
    letter-spacing: 2px;
  }

  .board-scroll-area {
    padding: 16px;
  }

  .space-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 8px;
  }

  .space-label {
    font-size: 11px;
  }

  .score-cards {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .score-card {
    flex: 1;
    min-width: 160px;
  }

  .toast-container {
    top: 80px;
  }

  .action-panel {
    bottom: auto;
    top: 50%;
    left: 50%;
    right: auto;
    transform: translate(-50%, -50%) translateY(100%);
    max-width: 480px;
    width: 90vw;
    border-top: none;
    border: 3px solid var(--accent);
    border-radius: var(--radius-lg);
  }

  .action-panel.show {
    transform: translate(-50%, -50%) translateY(0);
  }

  .career-modal-overlay {
    align-items: center;
  }

  .career-modal {
    border-radius: var(--radius-lg);
    max-width: 500px;
    max-height: 60vh;
    border: 2px solid var(--bg-card);
  }
}

/* Desktop */
@media (min-width: 1024px) {
  :root {
    --tap-min: 48px;
  }

  .title-main {
    font-size: var(--font-xl);
  }

  .space-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
  }

  .board-space {
    border-radius: var(--radius-md);
  }

  .action-buttons {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .action-btn {
    flex: 1;
    min-width: 120px;
  }

  .turn-notification {
    padding: 32px 64px;
    font-size: var(--font-xl);
  }
}

/* ==================== CONNECTION BANNER ==================== */
.conn-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #5c1a1a;
  border-bottom: 2px solid #ff4444;
  z-index: 500;
  padding: 10px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.conn-banner.show {
  opacity: 1;
  pointer-events: auto;
}

/* Lobby progress note (creating / joining a room) */
.form-indicator {
  text-align: center;
  color: var(--text-dim, #a0a0b0);
  font-size: 0.95rem;
  padding: 14px 0;
  animation: pulse-soft 1.4s ease-in-out infinite;
}

@keyframes pulse-soft {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 1; }
}

.conn-banner-inner {
  display: flex;
  align-items: center;
  gap: 10px;
}

.conn-icon {
  font-size: 18px;
}

.conn-text {
  flex: 1;
  font-size: var(--font-sm);
  font-weight: 600;
  color: #ffcdd2;
}

.conn-retry-btn {
  min-height: 32px;
  padding: 6px 16px;
  font-size: var(--font-sm);
}

/* Connection indicator (top-right dot during play) */
.conn-indicator {
  position: fixed;
  top: 8px;
  right: 8px;
  font-size: 14px;
  color: var(--success);
  z-index: 100;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: block;
  opacity: 0;
  transition: opacity 0.3s;
}

.conn-indicator.connected {
  opacity: 1;
  background: var(--success);
}

.conn-indicator.disconnected {
  opacity: 1;
  background: var(--accent);
}

.btn-small {
  padding: 6px 12px;
  font-size: var(--font-sm);
}

.or-divider {
  text-align: center;
  color: var(--text-secondary);
  font-size: var(--font-sm);
  margin: 8px 0;
  position: relative;
}

.or-divider::before,
.or-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background: var(--bg-card);
}

.or-divider::before { left: 0; }
.or-divider::after { right: 0; }

.path-picker {
  display: flex;
  gap: 6px;
}

.path-picker .section-tab {
  flex: 1;
  min-height: var(--tap-min);
}

.wait-msg {
  text-align: center;
  color: var(--text-secondary);
  font-size: var(--font-sm);
  padding: 8px 0;
}
