:root {
  --bg-primary: #0f1117;
  --bg-secondary: #1a1d27;
  --bg-tertiary: #252833;
  --text-primary: #e4e6ed;
  --text-secondary: #9499a8;
  --border-color: #2e3140;
  --accent: #6c63ff;
  --accent-hover: #5a52d9;

  --color-red: #e74c3c;
  --color-green: #2ecc71;
  --color-blue: #3498db;
  --color-yellow: #f1c40f;

  --color-red-bg: #3d1a1a;
  --color-green-bg: #1a3d24;
  --color-blue-bg: #1a2a3d;
  --color-yellow-bg: #3d3a1a;

  --cell-size: min(2.4vw, 38px);
}

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
}

.view { display: none; }
.view.active { display: flex; }
.hidden { display: none !important; }

/* --- Buttons --- */

button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  transition: background 0.2s, transform 0.1s;
}

button:hover { background: var(--accent-hover); transform: translateY(-1px); }
button:active { transform: translateY(0); }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

button.secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

button.secondary:hover { background: var(--border-color); }
button.small { padding: 6px 12px; font-size: 12px; }

.icon-btn {
  background: none;
  color: var(--text-secondary);
  font-size: 18px;
  padding: 4px 8px;
}

.icon-btn:hover { color: var(--text-primary); background: none; }

input[type="text"] {
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

input[type="text"]:focus { border-color: var(--accent); }

/* ========================================
   1. HOME VIEW
   ======================================== */

#home-view.active {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.home-container {
  width: 100%;
  max-width: 500px;
  padding: 60px 20px;
}

.home-container h1 {
  text-align: center;
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 40px;
  letter-spacing: -0.5px;
  color: var(--accent);
}

.create-section {
  display: flex;
  gap: 10px;
  margin-bottom: 32px;
}

.create-section input { flex: 1; }

.room-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.room-list-header h2 {
  font-size: 14px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#room-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 60px;
}

.empty-rooms {
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
  padding: 20px;
}

.room-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  transition: border-color 0.2s;
}

.room-item:hover { border-color: var(--accent); }
.room-item-info { display: flex; gap: 16px; align-items: center; }
.room-id { font-weight: 700; font-family: monospace; font-size: 16px; }
.room-status { font-size: 12px; color: var(--text-secondary); }

/* --- Modal --- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 30px;
  min-width: 320px;
  text-align: center;
}

.modal-content h3 { margin-bottom: 8px; font-size: 18px; }
.modal-content p { color: var(--text-secondary); margin-bottom: 16px; font-family: monospace; }
.modal-content input { width: 100%; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: center; }

/* ========================================
   2. ROOM VIEW (waiting lobby)
   ======================================== */

#room-view.active {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.room-container {
  width: 100%;
  max-width: 420px;
  padding: 40px 24px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  text-align: center;
}

.room-header {
  margin-bottom: 28px;
}

.room-code-label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  margin-bottom: 4px;
}

#room-code {
  font-weight: 800;
  font-family: monospace;
  font-size: 32px;
  letter-spacing: 4px;
  color: var(--accent);
}

.room-player-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 24px;
}

.room-player-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--bg-tertiary);
  text-align: left;
}

.room-player-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.room-player-name {
  flex: 1;
  font-weight: 500;
  font-size: 15px;
}

.room-player-badge {
  font-size: 10px;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.room-player-slot {
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px dashed var(--border-color);
  color: var(--text-secondary);
  font-size: 13px;
}

.room-status-text {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
  min-height: 1.4em;
}

.room-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.room-actions button { min-width: 120px; }

/* ========================================
   3. GAME VIEW
   ======================================== */

#game-view {
  height: 100vh;
  align-items: stretch;
}

.game-layout {
  display: flex;
  width: 100%;
  height: 100%;
}

.game-sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.player-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.player-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--bg-tertiary);
  font-size: 13px;
  transition: outline 0.2s;
}

.player-item.current-turn { outline: 2px solid var(--accent); }

.player-color-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.player-name { flex: 1; font-weight: 500; }

.player-admin-badge {
  font-size: 10px;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
}

.game-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#dice-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.dice {
  width: 70px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  font-size: 32px;
  font-weight: 800;
  font-family: monospace;
  color: var(--text-primary);
  min-height: 70px;
}

.dice.rolling {
  animation: dice-shake 0.3s ease-in-out;
}

@keyframes dice-shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-10deg) scale(1.05); }
  75% { transform: rotate(10deg) scale(1.05); }
}

#turn-info {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  min-height: 1.4em;
}

/* --- Board --- */

.board-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(15, var(--cell-size));
  grid-template-rows: repeat(15, var(--cell-size));
  gap: 1px;
  background: var(--bg-primary);
  border: 2px solid var(--border-color);
  border-radius: 4px;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-radius: 2px;
}

.cell.empty { background: transparent; }
.cell.path { background: #3a3e4d; border: 1px solid #3a3d4a; }
.cell.home-base { background: transparent; }
.cell.home-base-slot { border-radius: 50%; }
.cell.home-column { border: 1px solid #3a3d4a; }
.cell.center { background: #1e2030; border: 1px solid #3a3d4a; }
.cell.center-finish { background: var(--bg-tertiary); border: 1px solid var(--border-color); }

.cell.color-red { background: var(--color-red-bg); }
.cell.color-green { background: var(--color-green-bg); }
.cell.color-blue { background: var(--color-blue-bg); }
.cell.color-yellow { background: var(--color-yellow-bg); }

.cell.start-cell-red { background: var(--color-red-bg); border: 2px solid var(--color-red); }
.cell.start-cell-green { background: var(--color-green-bg); border: 2px solid var(--color-green); }
.cell.start-cell-blue { background: var(--color-blue-bg); border: 2px solid var(--color-blue); }
.cell.start-cell-yellow { background: var(--color-yellow-bg); border: 2px solid var(--color-yellow); }

/* --- Pieces --- */

.piece {
  width: calc(var(--cell-size) * 0.7);
  height: calc(var(--cell-size) * 0.7);
  border-radius: 50%;
  position: absolute;
  z-index: 10;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  transition: transform 0.15s;
}

.piece.red { background: var(--color-red); }
.piece.green { background: var(--color-green); }
.piece.blue { background: var(--color-blue); }
.piece.yellow { background: var(--color-yellow); }

.piece.valid-move {
  cursor: pointer;
  animation: piece-glow 1s ease-in-out infinite alternate;
}

.piece.valid-move:hover { transform: scale(1.2); }

@keyframes piece-glow {
  from { box-shadow: 0 0 4px rgba(108, 99, 255, 0.4), 0 2px 4px rgba(0, 0, 0, 0.4); }
  to { box-shadow: 0 0 12px rgba(108, 99, 255, 0.8), 0 2px 4px rgba(0, 0, 0, 0.4); }
}

.piece.stack-0 { transform: translate(-25%, -25%); }
.piece.stack-1 { transform: translate(25%, -25%); }
.piece.stack-2 { transform: translate(-25%, 25%); }
.piece.stack-3 { transform: translate(25%, 25%); }
.piece.valid-move.stack-0:hover { transform: translate(-25%, -25%) scale(1.2); }
.piece.valid-move.stack-1:hover { transform: translate(25%, -25%) scale(1.2); }
.piece.valid-move.stack-2:hover { transform: translate(-25%, 25%) scale(1.2); }
.piece.valid-move.stack-3:hover { transform: translate(25%, 25%) scale(1.2); }

/* --- Responsive --- */

@media (max-width: 768px) {
  .game-layout { flex-direction: column; }
  .game-sidebar {
    width: 100%;
    min-width: unset;
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    padding: 10px;
    gap: 8px;
  }
  .player-list { flex-direction: row; flex: unset; }
  :root { --cell-size: min(5.8vw, 32px); }
}
