:root {
  --bg: #08181f;
  --surface: rgba(255, 255, 255, 0.06);
  --surface-strong: rgba(255, 255, 255, 0.1);
  --border: rgba(255, 255, 255, 0.1);
  --text: #eef7fa;
  --text-dim: #8fb2c0;
  --accent-a: #2b6a86;
  --accent-b: #8fd3e8;
  --accent-grad: linear-gradient(135deg, var(--accent-a), var(--accent-b));
  --cream: #ffdf86;
  --danger: #f4636f;
  --success: #4fd18b;
  --warn: #ffdf86;
  --radius: 16px;
  --radius-sm: 10px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ambient background glow blobs */
.bg-glow {
  position: fixed;
  border-radius: 50%;
  filter: blur(120px);
  opacity: 0.35;
  pointer-events: none;
  z-index: 0;
  animation: drift 18s ease-in-out infinite alternate;
}
.bg-glow-a {
  width: 60vmax; height: 60vmax;
  top: -20vmax; left: -15vmax;
  background: var(--accent-a);
}
.bg-glow-b {
  width: 50vmax; height: 50vmax;
  bottom: -20vmax; right: -15vmax;
  background: var(--accent-b);
  animation-delay: -9s;
}
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to   { transform: translate(6vmax, 4vmax) scale(1.1); }
}

.app {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.screen { display: none; width: 100%; }
.screen.active { display: flex; justify-content: center; animation: fadeUp 0.35s ease both; }

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.card {
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(20px) saturate(140%);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  padding: 28px;
}

/* ---------- login ---------- */

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}
.brand-mark {
  width: 52px; height: 52px;
  border-radius: 14px;
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 8px 24px rgba(43, 106, 134, 0.45);
  flex-shrink: 0;
}
.brand-mark img { width: 100%; height: 100%; display: block; object-fit: cover; }
.brand h1 { font-size: 20px; margin: 0; }
.subtitle { color: var(--text-dim); font-size: 13px; margin: 4px 0 0; }

.field {
  display: block;
  margin-bottom: 14px;
}
.field span {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.field input, .field select {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus, .field select:focus {
  border-color: var(--accent-b);
  box-shadow: 0 0 0 3px rgba(143, 211, 232, 0.15);
}

.advanced { margin: 4px 0 16px; }
.advanced summary {
  cursor: pointer;
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 10px;
  user-select: none;
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.15s ease, opacity 0.15s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-block { width: 100%; }
.btn-primary {
  background: var(--accent-grad);
  color: white;
  box-shadow: 0 8px 20px rgba(43, 106, 134, 0.35);
}
.btn-primary:hover:not(:disabled) { box-shadow: 0 10px 26px rgba(43, 106, 134, 0.5); }
.btn-ghost {
  background: var(--surface-strong);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.16); }
.btn-danger {
  background: rgba(244, 63, 94, 0.15);
  color: #ff8ba0;
  border: 1px solid rgba(244, 63, 94, 0.4);
}
.btn-danger:hover { background: rgba(244, 63, 94, 0.28); }

.link-btn {
  background: none; border: none; color: var(--accent-b);
  font: inherit; font-size: 12px; cursor: pointer; padding: 0; text-decoration: underline;
}

.fingerprint {
  margin: 20px 0 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.03em;
}

/* ---------- lobby ---------- */

.lobby-card { max-width: 480px; }
.lobby-header { margin-bottom: 18px; }
.lobby-header h1 { margin: 0 0 4px; font-size: 20px; }

.tabs {
  display: flex;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 9px 12px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab.active {
  background: var(--surface-strong);
  color: var(--text);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeUp 0.25s ease both; }

.browse-toolbar { display: flex; justify-content: flex-end; margin-bottom: 10px; }

.room-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 360px;
  overflow-y: auto;
  padding-right: 2px;
}
.room-card {
  text-align: left;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.room-card:hover:not(:disabled) {
  border-color: var(--accent-b);
  background: rgba(143, 211, 232, 0.08);
  transform: translateY(-1px);
}
.room-card:disabled { opacity: 0.4; cursor: not-allowed; }
.room-card-name { font-weight: 700; font-size: 14px; }
.room-card-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.room-list-loading, .empty-state {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  padding: 24px 0;
}

/* ---------- games hub ---------- */

.mode-switch {
  display: flex;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
}
.mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-btn.active { background: var(--accent-grad); color: white; }

.game-type-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.game-type-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 20px 12px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.game-type-card:hover {
  border-color: var(--accent-b);
  background: rgba(143, 211, 232, 0.08);
  transform: translateY(-2px);
}
.game-type-icon { font-size: 32px; }
.game-type-name { font-weight: 700; font-size: 13px; }
.game-type-card.selected {
  border-color: var(--accent-b);
  background: rgba(143, 211, 232, 0.16);
  box-shadow: 0 0 0 2px rgba(143, 211, 232, 0.4);
}

.field-check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 13px;
  cursor: pointer;
}
.field-check input { width: 16px; height: 16px; accent-color: var(--accent-b); }
.ai-difficulty-field input[type="range"] { width: 100%; accent-color: var(--accent-b); }

/* ---------- game room ---------- */

.game-room-card {
  max-width: 900px;
  width: 100%;
  padding: 0;
  display: flex;
  flex-direction: column;
  height: min(760px, 90vh);
  overflow: hidden;
}
.game-room-body {
  flex: 1;
  display: flex;
  min-height: 0;
}
.game-board-container {
  flex: 1;
  min-width: 0;
  overflow: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
}
.game-lobby-chat {
  width: 260px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.lobby-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.lobby-chat-input-row {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
}
.lobby-chat-input-row input {
  flex: 1;
  padding: 9px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.lobby-chat-input-row input:focus { border-color: var(--accent-b); }
.lobby-chat-input-row .btn-send { width: 38px; height: 38px; font-size: 13px; }

.game-status-line {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  text-align: center;
}
.game-status-line.win { color: var(--success); }
.game-status-line.lose { color: var(--danger); }

@media (max-width: 760px) {
  .game-room-body { flex-direction: column; }
  .game-lobby-chat { width: auto; border-left: none; border-top: 1px solid var(--border); max-height: 220px; }
}

/* ---------- connect 4 ---------- */

.c4-board-wrap { display: inline-flex; flex-direction: column; gap: 6px; }
.c4-drop-row, .c4-board {
  display: grid;
  grid-template-columns: repeat(7, minmax(32px, 44px));
  gap: 6px;
}
.c4-drop-btn {
  background: var(--surface-strong);
  border: 1px solid var(--border);
  color: var(--accent-b);
  border-radius: 8px;
  height: 28px;
  cursor: pointer;
  font-size: 14px;
}
.c4-drop-btn:hover { background: rgba(143, 211, 232, 0.15); }
.c4-board {
  background: var(--accent-a);
  padding: 8px;
  border-radius: var(--radius-sm);
}
.c4-cell {
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.35);
}
.c4-cell.filled.red { background: #f4636f; }
.c4-cell.filled.yellow { background: var(--cream); }

/* ---------- battleship ---------- */

.bs-placing, .bs-battle { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.bs-placing .subtitle { max-width: 360px; text-align: center; }
.bs-placing-controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.bs-boards { display: flex; gap: 24px; flex-wrap: wrap; justify-content: center; }
.bs-board-col { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.bs-board-col h3 { margin: 0; font-size: 13px; color: var(--text-dim); }

.bs-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(20px, 30px));
  grid-template-rows: repeat(10, minmax(20px, 30px));
  gap: 3px;
  background: var(--accent-a);
  padding: 6px;
  border-radius: var(--radius-sm);
}
.bs-cell {
  background: rgba(143, 211, 232, 0.15);
  border-radius: 3px;
  cursor: default;
}
.bs-place-grid .bs-cell { cursor: pointer; }
.bs-place-grid .bs-cell:hover { background: rgba(143, 211, 232, 0.3); }
.bs-cell.ship { background: var(--text-dim); }
.bs-enemy-grid .bs-cell { cursor: pointer; }
.bs-cell.miss { background: rgba(0, 0, 0, 0.4); }
.bs-cell.miss::after { content: '·'; }
.bs-cell.hit { background: var(--warn); }
.bs-cell.sunk { background: var(--danger); }

/* ---------- tetris / puyo (shared canvas game layout) ---------- */

.tet-layout, .puyo-layout {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  justify-content: center;
}
.tet-main, .puyo-main {
  background: #0b1f26;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.tet-side-box, .puyo-side-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.tet-label, .puyo-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.tet-hold, .tet-next, .puyo-next {
  background: #0b1f26;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.tet-score, .puyo-score { font-weight: 800; font-size: 18px; }
.tet-opp, .puyo-opp { background: #0b1f26; border-radius: 6px; border: 1px solid var(--border); }
.tet-controls-hint, .puyo-controls-hint { text-align: center; max-width: 420px; }

/* ---------- chat ---------- */

.chat-card {
  max-width: 620px;
  height: min(720px, 88vh);
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.chat-header-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.chat-header-title h1 {
  font-size: 16px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}
.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-header-actions .btn { padding: 8px 12px; font-size: 13px; }

.player-count {
  font-size: 12px;
  color: var(--text-dim);
  background: rgba(0, 0, 0, 0.25);
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge-host { background: rgba(255, 223, 134, 0.18); color: #ffdf86; border: 1px solid rgba(255, 223, 134, 0.4); }
.badge-enc { background: rgba(79, 209, 139, 0.15); color: #6be3a8; border: 1px solid rgba(79, 209, 139, 0.4); }

.conn-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--text-dim);
  box-shadow: 0 0 0 3px transparent;
  transition: background 0.2s, box-shadow 0.2s;
}
.conn-connected { background: var(--success); box-shadow: 0 0 0 3px rgba(79, 209, 139, 0.2); }
.conn-connecting { background: var(--warn); box-shadow: 0 0 0 3px rgba(255, 223, 134, 0.2); animation: pulse 1s infinite; }
.conn-disconnected { background: var(--danger); box-shadow: 0 0 0 3px rgba(244, 63, 94, 0.2); }
@keyframes pulse { 50% { opacity: 0.4; } }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.msg-row {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: msgIn 0.2s ease both;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-row.mine {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 800;
  color: rgba(0, 0, 0, 0.6);
  flex-shrink: 0;
}

.bubble-wrap { min-width: 0; }
.msg-row.mine .bubble-wrap { text-align: right; }

.bubble-meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 3px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.msg-row.mine .bubble-meta { justify-content: flex-end; }
.msg-time { opacity: 0.7; }
.lock { font-size: 10px; }

.bubble {
  background: var(--surface-strong);
  border: 1px solid var(--border);
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.4;
  word-break: break-word;
  white-space: pre-wrap;
  display: inline-block;
  text-align: left;
}
.msg-row.mine .bubble {
  background: var(--accent-grad);
  color: white;
  border: none;
}

.system-line {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
  background: rgba(0, 0, 0, 0.2);
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.chat-input-row {
  display: flex;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.chat-input-row input {
  flex: 1;
  padding: 12px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.chat-input-row input:focus {
  border-color: var(--accent-b);
  box-shadow: 0 0 0 3px rgba(143, 211, 232, 0.15);
}
.btn-send {
  width: 46px; height: 46px;
  border-radius: 50%;
  padding: 0;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}
.toast {
  background: rgba(20, 20, 35, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  backdrop-filter: blur(10px);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { border-color: rgba(79, 209, 139, 0.5); }
.toast-error { border-color: rgba(244, 63, 94, 0.5); }
.toast-warn { border-color: rgba(255, 223, 134, 0.5); }

/* ---------- install button ---------- */

.install-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 90;
  background: var(--accent-grad);
  color: white;
  border: none;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(43, 106, 134, 0.45);
}

/* ---------- drawing panel ---------- */

.drawing-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  height: 100dvh;
  width: min(480px, 92vw);
  background: rgba(11, 25, 31, 0.98);
  border-left: 1px solid var(--border);
  backdrop-filter: blur(16px);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  box-shadow: -20px 0 50px rgba(0, 0, 0, 0.4);
}
.drawing-panel.open { transform: translateX(0); }

.drawing-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawing-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}
.drawing-swatches { display: flex; gap: 6px; }
.drawing-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  padding: 0;
}
.drawing-swatch:hover { border-color: var(--accent-b); }
.drawing-color-custom {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
}
.drawing-size-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}
.drawing-eraser.active { background: var(--accent-grad); color: white; }
.drawing-resolution select {
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 13px;
}
.drawing-canvas-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  background: rgba(0, 0, 0, 0.25);
  border-radius: var(--radius-sm);
  padding: 8px;
}
.drawing-canvas {
  max-width: 100%;
  max-height: 100%;
  background: white;
  border-radius: 4px;
  touch-action: none;
  cursor: crosshair;
}
.chat-drawing-img {
  max-width: 220px;
  max-height: 220px;
  border-radius: 8px;
  border: 1px solid var(--border);
  display: block;
}

/* ---------- scrollbars ---------- */

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.15); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.28); }

/* ---------- responsive ---------- */

@media (max-width: 520px) {
  .app { padding: 0; }
  .card { max-width: none; border-radius: 0; min-height: 100vh; min-height: 100dvh; border: none; }
  .chat-card { height: 100vh; height: 100dvh; }
}
