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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --accent: #0f3460;
  --highlight: #e94560;
  --text: #eaeaea;
  --text-dim: #888;
  --unit: clamp(16px, min(calc(100vw / 20), calc(100dvh / 40)), 48px);
  --main-gap: calc(0.5 * var(--unit));
  --sidebar-col: calc(4 * var(--unit));
  --cell: var(--unit);
  --mini-cell: clamp(4px, calc(0.25 * var(--unit)), 8px);
}

/* Touch device: unit scales from 16px (small) to 48px (large) */
@media (hover: none) and (pointer: coarse) {
  :root {
    /* Fit complete game footprint incl. opponent rows and controls */
    --unit: clamp(16px, min(calc(100vw / 20), calc(100dvh / 40)), 48px);
    --cell: var(--unit);
    --mini-cell: clamp(4px, calc(0.25 * var(--unit)), 8px);
  }
}

body.is-touch-device {
  --unit: clamp(16px, min(calc(100vw / 20), calc(100dvh / 40)), 48px);
  --cell: var(--unit);
  --mini-cell: clamp(4px, calc(0.25 * var(--unit)), 8px);
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* ── SCREENS ─────────────────────────────────────────────── */
.screen { display: none; width: 100%; }
.screen.active { display: flex; flex-direction: column; align-items: center; justify-content: center; }

/* ── LOBBY ───────────────────────────────────────────────── */
#screen-lobby h1 {
  font-size: 3rem;
  letter-spacing: 4px;
  color: var(--highlight);
  margin-bottom: 2rem;
  text-shadow: 0 0 20px var(--highlight);
}

.lobby-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(380px, 90vw);
}

.lobby-form input {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 2px solid var(--accent);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}
.lobby-form input:focus { border-color: var(--highlight); }

.lobby-buttons { display: flex; flex-direction: column; gap: 0.75rem; }

.join-row { display: flex; gap: 0.5rem; }
.join-row input { flex: 1; }

.open-rooms-panel {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.open-rooms-header {
  font-size: 0.9rem;
  color: var(--text-dim);
}

.open-rooms-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  max-height: 180px;
  overflow-y: auto;
}

.open-room-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0.4rem 0.5rem;
}

.open-room-meta {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.open-room-id {
  font-size: 0.85rem;
  letter-spacing: 1px;
}

.open-room-slots {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.btn-join-room {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
}

.open-rooms-empty {
  font-size: 0.8rem;
  color: var(--text-dim);
  text-align: center;
  padding: 0.35rem 0;
}

button {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  background: var(--highlight);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  user-select: none;
}
button:hover { opacity: 0.85; }
button:active { transform: scale(0.97); }

.btn-secondary {
  background: var(--accent);
  color: var(--text-dim);
  font-weight: 400;
}

.error-text { color: #f66; font-size: 0.875rem; min-height: 1.2em; }

.hidden { display: none !important; }

/* ── WAITING ROOM ────────────────────────────────────────── */
#screen-waiting { gap: 1rem; }
#screen-waiting h2 { font-size: 1.8rem; color: var(--highlight); }
#display-room-id {
  font-size: 1.5rem;
  letter-spacing: 6px;
  color: #fff;
  background: var(--accent);
  padding: 0.3rem 1rem;
  border-radius: 6px;
}
.waiting-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: center;
}
#player-list { list-style: none; display: flex; flex-direction: column; gap: 0.4rem; min-width: 200px; }
#player-list li {
  background: var(--surface);
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border-left: 3px solid var(--highlight);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  }
.player-pos {
  font-size: 0.75rem;
  color: var(--text-dim);
  min-width: 1.2rem;
  flex-shrink: 0;
}

.player-actions {
  margin-left: auto;
  display: flex;
  gap: 0.4rem;
}

.btn-kick {
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  color: #f66;
  border-color: #f66;
  flex-shrink: 0;
}
.btn-swap {
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  flex-shrink: 0;
}

/* Swap-Anfrage-Banner */
.swap-banner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  justify-content: center;
  background: rgba(255, 200, 0, 0.12);
  border: 1px solid #fc0;
  border-radius: 8px;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  color: #fc0;
}

.swap-banner.hidden {
  display: none;
}

.swap-banner-actions {
  display: flex;
  gap: 0.5rem;
}
.hint { color: var(--text-dim); font-size: 0.85rem; }

.invite-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}
.invite-panel {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
}
.invite-panel.hidden { display: none; }
.invite-panel input[type="text"] {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
  cursor: text;
  box-sizing: border-box;
}
.invite-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}
.btn-whatsapp {
  background: #25d366;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}
.btn-whatsapp:hover { background: #1ebe5d; }

/* ── GAME SCREEN ─────────────────────────────────────────── */
#screen-game {
  flex-direction: column;
  padding: var(--main-gap);
  gap: var(--main-gap);
  min-height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

#game-container {
  display: grid;
  grid-template-rows: auto auto auto;
  justify-items: center;
  align-content: center;
  gap: var(--main-gap);
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

#game-main {
  display: grid;
  grid-template-columns: var(--sidebar-col) auto var(--sidebar-col);
  align-items: stretch;
  gap: var(--main-gap);
}

#sidebar-left,
#sidebar-right {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: var(--sidebar-col);
  gap: calc(0.3 * var(--unit));
}

#sidebar-left .info-box,
#sidebar-right .info-box {
  width: 100%;
  flex-shrink: 0;
}

/* Sidebar spacer fills remaining height */
.sidebar-spacer {
  flex-grow: 1;
  flex-shrink: 0;
}

/* Rotate buttons at bottom of sidebars, align with touch control row */
.sidebar-rotate {
  height: calc(3 * var(--unit)) !important;
  margin-top: auto;
  flex-shrink: 0;
}

#board-wrapper {
  position: relative;
}

#game-canvas {
  display: block;
  border: 2px solid var(--accent);
  border-radius: 4px;
}

#game-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.65);
  border-radius: 4px;
}
#game-overlay.hidden { display: none; }

#overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

#overlay-text {
  font-size: 2rem;
  font-weight: 700;
  color: var(--highlight);
  letter-spacing: 2px;
}

#overlay-buttons {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  justify-content: center;
}

#overlay-buttons.hidden {
  display: none;
}

/* Sidebar */
.info-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--surface);
  border-radius: 8px;
  padding: calc(0.3 * var(--unit)) calc(0.5 * var(--unit));
  min-width: 0;
  margin-bottom: 0;
}
.info-box .label { font-size: clamp(0.6rem, 0.4 * var(--unit), 0.9rem); color: var(--text-dim); text-transform: uppercase; }
.info-box span:last-child { font-size: clamp(0.9rem, 0.8 * var(--unit), 1.5rem); font-weight: 700; }

#next-canvas { display: block; margin-top: 0.25rem; }

/* Opponents */
.opponents-row {
  display: flex;
  flex-wrap: nowrap;
  gap: var(--main-gap);
  justify-content: center;
  width: calc(2 * var(--sidebar-col) + 11 * var(--cell) + 4px + 2 * var(--main-gap));
  max-width: 100%;
  overflow-x: hidden;
  overflow-y: visible;
}

.opponent-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(0.15 * var(--unit));
  background: var(--surface);
  border-radius: 8px;
  padding: calc(0.2 * var(--unit));
  border: 1px solid var(--accent);
  flex-shrink: 0;
  min-width: 0;
}
.opponent-card span {
  font-size: clamp(0.55rem, calc(0.4 * var(--unit)), 0.85rem);
  color: var(--text-dim);
  max-width: calc(5.2 * var(--unit));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.opponent-card canvas { display: block; }
.opponent-dead { opacity: 0.4; }

/* ── TOUCH CONTROLS ──────────────────────────────────────── */
#touch-controls {
  display: none;
  /* Match game-main columns: sidebar | board split into two | sidebar */
  grid-template-columns:
    var(--sidebar-col)
    minmax(0, calc((11 * var(--cell) + 4px - var(--main-gap)) / 2))
    minmax(0, calc((11 * var(--cell) + 4px - var(--main-gap)) / 2))
    var(--sidebar-col);
  grid-template-rows: calc(3 * var(--unit));
  gap: var(--main-gap);
  width: calc(2 * var(--sidebar-col) + 11 * var(--cell) + 4px + 2 * var(--main-gap));
  flex-shrink: 0;
  max-width: 100%;
  padding: 0;
  justify-self: center;
}

.touch-btn {
  width: 100%;
  height: calc(3 * var(--unit));
  font-size: clamp(1rem, 1.5 * var(--unit), 2rem);
  border-radius: 12px;
  background: var(--surface);
  border: 2px solid var(--accent);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.touch-btn:active { background: var(--accent); }
.touch-btn-hard {
  background: rgba(233, 69, 96, 0.15);
  border-color: var(--highlight);
  color: var(--highlight);
}

.touch-btn-hold {
  background: rgba(15, 52, 96, 0.4);
  border-color: #4a9eff;
  color: #4a9eff;
}

/* Hold-Vorschau */
#hold-canvas {
  display: block;
  margin-top: calc(0.15 * var(--unit));
}

.hold-blocked {
  opacity: 0.4;
}
/* Musik-Toggle-Button */
.music-btn {
  width: calc(3 * var(--unit));
  height: calc(3 * var(--unit));
  padding: 0;
  font-size: clamp(1rem, 1.2 * var(--unit), 1.8rem);
  border-radius: 8px;
  background: var(--surface);
  border: 2px solid var(--accent);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.2s, opacity 0.2s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.music-btn:hover {
  border-color: #4a9eff;
}

.music-btn.music-off {
  opacity: 0.45;
}
/* Musik-Stil Auswahl im Steuerungs-Modal */
.music-style-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--bg);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
}

.music-style-label {
  font-size: 0.9rem;
  color: var(--text);
  white-space: nowrap;
}

.music-style-row select {
  flex: 1;
  max-width: 160px;
  padding: 0.3rem 0.5rem;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  outline: none;
}

.music-style-row select:focus {
  border-color: #4a9eff;
}
/* Dreh-Buttons in Sidebars: auf Desktop/Maus versteckt (nach .touch-btn damit display: none gewinnt) */
button.sidebar-rotate { display: none; }

/* Show touch controls on touch devices */
@media (hover: none) and (pointer: coarse) {
  #screen-game {
    overflow: hidden;
  }

  #game-container {
    flex: 0 1 auto;
    overflow: hidden;
    min-height: auto;
  }

  .opponents-row {
    overflow: hidden;
  }

  #touch-controls { display: grid; }
  #touch-controls { margin-top: var(--main-gap); }
  
  
  /* Fixed ordering in sidebars keeps relative positions stable */
  button.sidebar-rotate { display: flex; }
}

/* Fallback for browsers (e.g. Opera Mobile) that misreport pointer media */
body.is-touch-device #touch-controls { display: grid; }
body.is-touch-device #screen-game {
  overflow: hidden;
}
body.is-touch-device #game-container {
  flex: 0 1 auto;
  overflow: hidden;
  min-height: auto;
}
body.is-touch-device .opponents-row { overflow: hidden; }
body.is-touch-device #touch-controls { margin-top: var(--main-gap); }
body.is-touch-device button.sidebar-rotate { display: flex; }

/* Very small viewport protection */
@media (max-height: 500px) {
  #opponents-top, #opponents-bottom { display: none; }
  #game-container { gap: 0.25rem; padding: 0; }
  #screen-game { padding: 0.25rem; gap: 0.25rem; }
  #sidebar-left, #sidebar-right { min-width: auto; }
}

/* Extra small width protection */
@media (max-width: 360px) {
  #opponents-top, #opponents-bottom { overflow: hidden; }
  .opponent-card { padding: 0.2rem; }
}

/* DEV: uncomment to force touch controls visible on desktop */
/* #touch-controls { display: grid !important; } */

/* ── CONTROLS MODAL ─────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 14px;
  padding: 1.5rem;
  width: min(400px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.modal h3 {
  font-size: 1.3rem;
  color: var(--highlight);
  text-align: center;
}
.modal-hint { font-size: 0.8rem; color: var(--text-dim); text-align: center; }

.gamepad-status {
  font-size: 0.78rem;
  padding: 0.3rem 0.75rem;
  border-radius: 6px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.gp-connected    { background: rgba(0,200,100,0.15); color: #0c8; }
.gp-disconnected { background: rgba(255,255,255,0.05); color: var(--text-dim); }

.game-setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--accent);
}

.game-setting-row input[type='checkbox'] {
  width: 18px;
  height: 18px;
}

.bindings-header {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.5rem;
  padding: 0 0.25rem;
}
.bindings-col-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 1px;
}

#bindings-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.binding-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg);
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid transparent;
}
.binding-row:hover { border-color: var(--accent); }

.binding-label {
  font-size: 0.9rem;
  color: var(--text);
}

.binding-key {
  padding: 0.3rem 0.4rem;
  font-size: 0.8rem;
  font-family: monospace;
  background: var(--accent);
  color: var(--text);
  border: 2px solid var(--accent);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.binding-key:hover { border-color: var(--highlight); }
.binding-key.listening {
  border-color: var(--highlight);
  background: rgba(233,69,96,0.15);
  color: var(--highlight);
  animation: pulse 0.8s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.modal-footer {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  padding-top: 0.5rem;
  border-top: 1px solid var(--accent);
}

@media (pointer: coarse) {
  .no-touch-only {
    display: none;
  }
}
/* ── GAME OVER ───────────────────────────────────────────── */
#screen-gameover { gap: 1.2rem; text-align: center; }
.gameover-actions { display: flex; gap: 0.75rem; flex-wrap: wrap; justify-content: center; }
#gameover-title { font-size: 2.5rem; color: var(--highlight); }
#gameover-message { color: var(--text-dim); }
#final-scores {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-width: 250px;
}
.score-row {
  display: flex;
  justify-content: space-between;
  background: var(--surface);
  padding: 0.5rem 1rem;
  border-radius: 6px;
}
.score-row.winner { border-left: 3px solid gold; }

/* ── ACCOUNT / AUTH ──────────────────────────────────────── */
#auth-section {
  margin-top: 0.75rem;
  border-top: 1px solid var(--accent);
  padding-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
#auth-guest { display: flex; flex-direction: column; align-items: center; gap: 0.4rem; }

.btn-sm {
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
}

.auth-tabs {
  display: flex;
  gap: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--accent);
}
.auth-tab {
  flex: 1;
  border-radius: 0;
  background: var(--accent);
  color: var(--text-dim);
  font-weight: 400;
  padding: 0.5rem;
  font-size: 0.9rem;
}
.auth-tab.active {
  background: var(--highlight);
  color: #fff;
  font-weight: 600;
}
#auth-form-panel {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.auth-btn-row { display: flex; gap: 0.5rem; }
.auth-btn-row button { flex: 1; }
.auth-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.auth-actions { display: flex; gap: 0.4rem; }

/* ── STATS MODAL ─────────────────────────────────────────── */
.modal-wide { width: min(600px, 95vw); max-height: 90dvh; overflow-y: auto; }

.modal-section-title {
  font-size: 1rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.5rem;
  margin-top: 0.25rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.stat-card {
  background: var(--bg);
  border-radius: 8px;
  padding: 0.6rem 0.5rem;
  text-align: center;
  border: 1px solid var(--accent);
}
.stat-card .stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--highlight);
}
.stat-card .stat-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  margin-top: 0.2rem;
}

.table-wrapper { overflow-x: auto; }
#leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
#leaderboard-table th {
  text-align: left;
  padding: 0.4rem 0.6rem;
  color: var(--text-dim);
  font-size: 0.75rem;
  text-transform: uppercase;
  border-bottom: 1px solid var(--accent);
}
#leaderboard-table td {
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}
#leaderboard-table tr.lb-me td { background: rgba(233,69,96,0.08); color: #fff; }
#leaderboard-table tr:hover td { background: rgba(255,255,255,0.03); }
.lb-rank { color: var(--text-dim); }
.lb-gold   { color: gold; }
.lb-silver { color: silver; }
.lb-bronze { color: #cd7f32; }

.modal-footer { margin-top: 1rem; display: flex; justify-content: flex-end; }

/* ── RULES MODAL ─────────────────────────────────────────── */
.rules-text {
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.5;
}
.rules-list {
  padding-left: 1.2rem;
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.8;
}
.rules-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.rules-table th {
  color: var(--highlight);
  text-align: left;
  border-bottom: 1px solid var(--accent);
  padding: 0.3rem 0.5rem;
}
.rules-table td {
  padding: 0.3rem 0.5rem;
  color: var(--text-dim);
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.rules-table td:first-child { color: var(--text); }

/* ── RESPONSIVE ──────────────────────────────────────────── */
@media (max-width: 500px) {
  #screen-lobby h1 { font-size: 2.2rem; }
}
