/* ── Reset & Variables ──────────────────────────── */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface-hover: #1f3060;
  --text: #e0e0e0;
  --text-dim: #8892b0;
  --text-muted: #5a6785;
  --primary: #e94560;
  --primary-hover: #ff6b81;
  --secondary: #0f3460;
  --secondary-hover: #1a4a7a;
  --danger: #c0392b;
  --danger-hover: #e74c3c;
  --success: #27ae60;
  --gold: #f0c040;
  --radius: 8px;
  --radius-lg: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
}

html { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ── Layout ─────────────────────────────────────── */
#app {
  width: 100%;
  max-width: 900px;
  padding: 1.5rem 1rem 2.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}

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

/* ── Room Panel ──────────────────────────────────── */
.title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-top: 2rem;
  letter-spacing: 4px;
  background: linear-gradient(135deg, var(--primary), var(--gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-dim);
  font-size: 0.95rem;
  margin: 0.5rem 0 2rem;
}

.form-group {
  width: 100%;
  max-width: 320px;
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 0.35rem;
}

.form-group input {
  width: 100%;
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--text-muted);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus { border-color: var(--primary); }
.form-group input::placeholder { color: var(--text-muted); }

/* ── Buttons ─────────────────────────────────────── */
.btn {
  padding: 0.7rem 2rem;
  border-radius: var(--radius);
  border: none;
  font-size: 1rem;
  font-family: var(--font);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  width: 100%;
  max-width: 320px;
  text-align: center;
  user-select: none;
}

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

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { background: #6b3440; color: #999; cursor: not-allowed; }

.btn-secondary { background: var(--secondary); color: var(--text); }
.btn-secondary:hover { background: var(--secondary-hover); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }

.btn-sm {
  padding: 0.5rem 0.8rem;
  font-size: 0.82rem;
  max-width: none;
  width: auto;
  flex: 1;
}

/* ── Divider ─────────────────────────────────────── */
.divider {
  width: 100%; max-width: 320px;
  display: flex; align-items: center;
  margin: 1.2rem 0;
  color: var(--text-muted); font-size: 0.8rem;
}
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--text-muted); opacity: 0.3; }
.divider span { padding: 0 1rem; }

/* ── Room Info ───────────────────────────────────── */
.room-info {
  margin-top: 1.5rem; text-align: center;
  width: 100%; max-width: 320px;
}

.room-code-display {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 1.2rem;
  margin-bottom: 0.8rem;
  cursor: pointer;
  transition: background 0.2s;
}
.room-code-display:hover { background: var(--surface-hover); }

.room-code-display .label {
  display: block; font-size: 0.8rem;
  color: var(--text-dim); margin-bottom: 0.3rem;
}

.room-code-display .code {
  font-size: 2.2rem; font-weight: 700;
  letter-spacing: 6px; color: var(--gold);
}

.waiting-text { color: var(--text-dim); font-size: 0.9rem; margin-bottom: 0.5rem; }

.dot-connecting {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--primary);
  margin: 0 auto 1rem;
  animation: dotPulse 1s infinite;
}

@keyframes dotPulse {
  0%, 100% { opacity: 0.3; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.3); }
}

/* ── Toast ───────────────────────────────────────── */
.toast {
  position: fixed; top: 1rem; left: 50%; transform: translateX(-50%) translateY(-120%);
  padding: 0.7rem 1.5rem;
  border-radius: var(--radius);
  font-size: 0.9rem; font-weight: 600;
  color: #fff; background: var(--surface-hover);
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  z-index: 1000;
  transition: transform 0.3s ease;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ── Game Panel ──────────────────────────────────── */
#game-header {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; max-width: 780px;
  padding: 0.5rem 0; gap: 0.5rem;
}

.player-card {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.5rem 0.8rem; border-radius: var(--radius);
  background: var(--surface);
  transition: box-shadow 0.3s, background 0.3s;
  min-width: 110px;
}
.player-card.active {
  background: var(--surface-hover);
  box-shadow: 0 0 12px rgba(233, 69, 96, 0.4);
}

.stone-preview { width: 24px; height: 24px; border-radius: 50%; flex-shrink: 0; }
.stone-preview.black { background: radial-gradient(circle at 35% 35%, #555, #000); }
.stone-preview.white { background: radial-gradient(circle at 35% 35%, #fff, #c0c0c0); }

.player-info { display: flex; flex-direction: column; }
.player-name { font-size: 0.85rem; font-weight: 600; }
.player-tag { font-size: 0.7rem; color: var(--text-dim); }

.game-status { text-align: center; min-width: 80px; }

.turn-indicator {
  width: 10px; height: 10px; border-radius: 50%;
  margin: 0 auto 4px; background: var(--text-muted);
  transition: background 0.3s;
}
.turn-indicator.my-turn { background: var(--success); box-shadow: 0 0 8px var(--success); }
.turn-indicator.not-my-turn { background: var(--text-muted); }

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

/* ── Game Main (board + history side-by-side) ────── */
.game-main {
  display: flex; gap: 1rem;
  width: 100%; max-width: 780px;
  align-items: flex-start;
}

/* ── Board ───────────────────────────────────────── */
.board-container {
  flex-shrink: 0;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
  position: relative;
}

#board-canvas { display: block; cursor: default; }

/* ── Move History Panel ──────────────────────────── */
#move-history-panel {
  flex: 1;
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 0.6rem;
  display: flex; flex-direction: column;
  max-height: 600px;
}

.panel-title {
  font-size: 0.8rem; font-weight: 600;
  color: var(--text-dim);
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--text-muted);
  margin-bottom: 0.4rem;
}

#move-history {
  overflow-y: auto;
  font-size: 0.78rem;
  font-family: "Courier New", monospace;
  flex: 1;
}

#move-history::-webkit-scrollbar { width: 3px; }
#move-history::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 2px; }

.move-row { padding: 0.15rem 0; white-space: nowrap; }
.move-num { color: var(--text-muted); margin-right: 0.3rem; font-size: 0.7rem; }
.move-entry { margin: 0 0.15rem; }
.move-entry.black { color: #fff; }
.move-entry.white { color: #ccc; }
.move-empty { color: var(--text-muted); font-style: italic; }

/* ── Action bar ──────────────────────────────────── */
#action-bar {
  position: fixed; bottom: 5rem; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 0.6rem;
  background: var(--surface);
  border: 1px solid var(--primary);
  border-radius: var(--radius-lg);
  padding: 0.6rem 1.2rem;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
  z-index: 100;
  animation: fadeIn 0.2s ease;
}
#action-bar .action-text {
  font-size: 0.85rem; color: var(--text);
  margin-right: 0.3rem;
}
#action-bar .btn { max-width: none; width: auto; padding: 0.4rem 1rem; }

/* ── Game Controls ───────────────────────────────── */
#game-controls {
  display: flex; gap: 0.5rem;
  margin: 0.8rem 0;
  width: 100%; max-width: 780px;
}

/* ── Chat ────────────────────────────────────────── */
#chat-container {
  width: 100%; max-width: 780px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 0.8rem;
  display: flex; flex-direction: column;
}

#chat-messages {
  height: 120px; overflow-y: auto;
  font-size: 0.85rem; margin-bottom: 0.5rem;
  display: flex; flex-direction: column; gap: 0.3rem;
}
#chat-messages::-webkit-scrollbar { width: 4px; }
#chat-messages::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 2px; }

.chat-msg { padding: 0.25rem 0; line-height: 1.4; }
.chat-msg .from { font-weight: 600; color: var(--primary); margin-right: 0.4rem; }
.chat-msg .text { color: var(--text); }
.chat-msg.system .text { color: var(--text-dim); font-style: italic; }

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

#emoji-panel {
  display: none; flex-wrap: wrap; gap: 0.3rem; padding: 0.5rem;
  background: var(--surface); border-radius: var(--radius);
  border: 1px solid var(--text-muted);
  margin-bottom: 0.5rem; max-height: 140px; overflow-y: auto;
}
.emoji-btn {
  font-size: 1.4rem; cursor: pointer; padding: 0.2rem;
  border-radius: 4px; transition: background 0.15s;
  user-select: none; line-height: 1;
}
.emoji-btn:hover { background: var(--surface-hover); }

#btn-emoji { padding: 0.5rem 0.6rem; font-size: 1.1rem; }

#chat-input {
  flex: 1;
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius);
  border: 1px solid var(--text-muted);
  background: var(--bg); color: var(--text);
  font-size: 0.85rem; font-family: var(--font);
  outline: none;
}
#chat-input:focus { border-color: var(--primary); }
#chat-input::placeholder { color: var(--text-muted); }

/* ── Connection Bar ──────────────────────────────── */
#connection-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: center;
  gap: 0.4rem; padding: 0.4rem;
  font-size: 0.75rem; color: var(--text-muted);
  background: rgba(26, 26, 46, 0.9);
  backdrop-filter: blur(8px);
}

.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); }
.dot.connected { background: var(--success); }
.dot.disconnected { background: var(--danger); }

/* ── Utility ─────────────────────────────────────── */
.hidden { display: none !important; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 0; transform: scale(1); }
}
.panel:not(.hidden) { animation: fadeIn 0.3s ease; }

/* ── Responsive ──────────────────────────────────── */
@media (max-width: 650px) {
  .title { font-size: 2rem; margin-top: 1rem; }
  #game-header { flex-wrap: wrap; justify-content: center; }
  .game-status { order: -1; width: 100%; margin-bottom: 0.3rem; }

  .game-main { flex-direction: column; align-items: center; }

  #move-history-panel {
    width: 100%; max-height: 100px;
  }

  #chat-messages { height: 80px; }
  #game-controls { flex-wrap: wrap; }
}

/* ── Auth section ───────────────────────────────────── */
.auth-section {
  width: 100%; max-width: 320px; margin-bottom: 1rem;
}

.auth-logged-in {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.6rem 0.8rem; background: var(--surface);
  border-radius: var(--radius);
  flex-wrap: wrap; justify-content: center;
}

.auth-username {
  font-weight: 600; color: var(--text);
}

.auth-stats {
  font-size: 0.75rem; color: var(--text-dim);
}

.auth-form {
  display: flex; flex-direction: column; gap: 0.5rem;
  padding: 0.6rem 0;
}

.auth-form input {
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius);
  border: 1px solid var(--text-muted);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem; font-family: var(--font);
  outline: none;
}

.auth-form input:focus { border-color: var(--primary); }

.auth-buttons {
  display: flex; gap: 0.5rem;
}

.auth-hint {
  font-size: 0.7rem; color: var(--text-muted);
  text-align: center; margin: 0;
}

/* ── History panel ──────────────────────────────────── */
.panel-header {
  display: flex; align-items: center; gap: 1rem;
  width: 100%; max-width: 780px; margin-bottom: 1rem;
}

.panel-header h2 {
  font-size: 1.1rem; color: var(--text);
}

.history-list {
  width: 100%; max-width: 780px;
  display: flex; flex-direction: column; gap: 0.5rem;
}

.history-item {
  display: flex; align-items: center; gap: 0.8rem;
  padding: 0.7rem 0.9rem;
  background: var(--surface);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

.history-item:hover { background: var(--surface-hover); }

.history-result { font-size: 1.2rem; flex-shrink: 0; }

.history-info { flex: 1; }
.history-info div:first-child { font-size: 0.9rem; color: var(--text); }
.history-meta { font-size: 0.7rem; color: var(--text-dim); margin-top: 0.15rem; }

.history-arrow { color: var(--text-dim); font-size: 0.8rem; }

.history-empty {
  text-align: center; color: var(--text-dim);
  font-size: 0.9rem; margin-top: 2rem;
}

/* ── History groups ── */
.history-group {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 0.8rem;
  margin-bottom: 0.8rem;
}

.history-group-header {
  display: flex; flex-direction: column; gap: 0.15rem;
  padding-bottom: 0.5rem; margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--text-muted);
}

.history-opponent {
  font-size: 0.95rem; font-weight: 600;
  color: var(--text);
}

.history-summary {
  font-size: 0.72rem; color: var(--text-dim);
}

.history-sub-item {
  padding: 0.4rem 0.5rem; margin: 0.15rem 0;
  border-radius: 6px; background: rgba(255,255,255,0.03);
}

.history-sub-item + .history-sub-item { border-top: none; }

/* ── Replay panel ───────────────────────────────────── */
.replay-board-container {
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
}

.replay-controls {
  display: flex; align-items: center; justify-content: center;
  gap: 0.5rem; margin-top: 0.8rem;
}

.replay-controls .btn { max-width: none; width: auto; }
.replay-info { font-size: 0.85rem; color: var(--text-dim); min-width: 80px; text-align: center; }
.replay-controls select {
  padding: 0.3rem 0.5rem; border-radius: var(--radius);
  border: 1px solid var(--text-muted);
  background: var(--surface); color: var(--text);
  font-size: 0.8rem; font-family: var(--font);
}

