:root {
  --bg: #0a0c10;
  --card: #13161d;
  --cell: #1a1e28;
  --cell-border: #252a36;
  --x: #ff7a96;
  --o: #5eead4;
  --text: #f0f2f5;
  --dim: #9aa3b5;
  --accent: #818cf8;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 6px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
}

.screen { display: none; flex: 1; min-height: 0; }
.screen.active { display: flex; flex-direction: column; }

.card {
  background: var(--card);
  border-radius: 18px;
  padding: 22px 18px;
  margin: auto;
  width: 100%;
  max-width: 380px;
}

h1 { text-align: center; font-size: 1.9rem; margin-bottom: 18px; font-weight: 700; }

input[type=text] {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid #2c3344;
  background: #1c212c;
  color: var(--text);
  font-size: 16px;
  margin-bottom: 10px;
  outline: none;
}
input:focus { border-color: var(--accent); }

.actions { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }

.btn {
  padding: 13px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn:active { opacity: .85; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.secondary { background: #1c212c; color: var(--text); border: 1px solid #2c3344; }
.btn.ghost { background: transparent; color: var(--dim); border: 1px solid #2c3344; }
.btn.small { padding: 7px 12px; font-size: 13px; border-radius: 9px; }
.btn:disabled { opacity: .4; }

.rooms-section { margin-bottom: 12px; }
.rooms-header { font-size: 11px; color: var(--dim); margin-bottom: 6px; letter-spacing: .4px; text-transform: uppercase; }
.rooms-list { max-height: 140px; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }
.room-item {
  background: #1c212c;
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.room-item .code { font-weight: 700; color: var(--accent); }
.room-item .host { font-size: 12px; color: var(--dim); }

.join-manual { display: flex; gap: 6px; }
.join-manual input { margin: 0; }

.status { text-align: center; color: var(--accent); font-size: 14px; min-height: 18px; margin-top: 6px; }

.room-code-display {
  background: #1c212c;
  border-radius: 14px;
  padding: 16px;
  text-align: center;
  margin-bottom: 12px;
}
.room-code-display strong {
  display: block;
  font-size: 2.2rem;
  letter-spacing: 5px;
  margin: 6px 0;
  color: var(--accent);
}

/* Game layout */
#game.active { gap: 6px; }

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  padding: 2px 0;
}

.players { display: flex; align-items: center; gap: 8px; }
.player {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--card);
  padding: 5px 9px 5px 5px;
  border-radius: 20px;
}
.symbol {
  width: 26px; height: 26px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 14px;
}
.symbol.x { background: var(--x); color: #2a0d14; }
.symbol.o { background: var(--o); color: #0d2420; }
.name { font-size: 13px; font-weight: 600; }
.turn-indicator { width: 6px; height: 6px; border-radius: 50%; }
.turn-indicator.active { background: #4ade80; }
.vs { color: var(--dim); font-size: 11px; }

.room-badge {
  background: #1c212c;
  color: var(--dim);
  font-size: 11px;
  padding: 5px 8px;
  border-radius: 7px;
}

.board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow: hidden;
}

.board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 1px;
  background: #252a36;
  border-radius: 8px;
  padding: 3px;
  width: min(100%, calc(100dvh - 200px));
  height: min(100%, calc(100dvh - 200px));
  aspect-ratio: 1;
  touch-action: none;
}

.cell {
  background: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 11px;
  line-height: 1;
  border-radius: 1px;
  position: relative;
}
.cell.x { color: var(--x); }
.cell.o { color: var(--o); }
.cell.last {
  outline: 1.5px solid #ffffff66;
  outline-offset: -1px;
  z-index: 1;
}

.game-footer { flex-shrink: 0; text-align: center; }
.controls { display: flex; gap: 8px; justify-content: center; margin-top: 4px; }

.chat-panel {
  flex-shrink: 0;
  background: var(--card);
  border-radius: 12px;
  max-height: 90px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 6px 10px;
  font-size: 12px;
  max-height: 50px;
}
.chat-msg .who { font-weight: 600; margin-right: 3px; }
.chat-msg .who.x { color: var(--x); }
.chat-msg .who.o { color: var(--o); }
.chat-input {
  display: flex;
  gap: 5px;
  padding: 5px;
  border-top: 1px solid #252a36;
}
.chat-input input { margin: 0; padding: 7px 10px; font-size: 14px; }

@media (max-height: 640px) {
  .board { width: min(100%, calc(100dvh - 160px)); height: min(100%, calc(100dvh - 160px)); }
  .chat-panel { max-height: 70px; }
}
