:root {
  --bg: #0b0e14;
  --panel: #141a24;
  --panel-alt: #1a2230;
  --border: #2a3447;
  --text: #e6edf7;
  --muted: #8ea0b8;
  --accent: #3b82f6;
  --accent-strong: #2563eb;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --hud-h: 36px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.hidden {
  display: none !important;
}

/* --- Shared Buttons & Pills --- */
.hud-btn,
.btn-primary {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-alt);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  white-space: nowrap;
}

.hud-btn:hover {
  border-color: var(--accent);
}

.hud-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary,
.hud-btn-accent,
.hud-btn.active {
  background: var(--accent-strong);
  border-color: var(--accent);
  color: #fff;
}

.hud-btn-danger {
  border-color: #7f1d1d;
  background: #450a0a;
  color: #fecaca;
}

.hud-btn.muted {
  color: #fca5a5;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
}

.pill.ok {
  background: rgba(34, 197, 94, 0.2);
  color: #86efac;
}

.pill.warn {
  background: rgba(245, 158, 11, 0.2);
  color: #fcd34d;
}

.pill.off,
.pill.neutral {
  background: rgba(148, 163, 184, 0.2);
  color: #cbd5e1;
}

.txt-ok { color: var(--ok); }
.txt-err { color: var(--err); }

/* --- Lobby (index.html) --- */
.lobby-body {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.lobby-shell {
  width: 100%;
  max-width: 540px;
}

.lobby-card,
.admin-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

.lobby-head,
.admin-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.lobby-head h1,
.admin-head h1 {
  margin: 0;
  font-size: 20px;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.capacity-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}

.cap-tile {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.cap-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
}

.cap-val {
  display: block;
  margin-top: 4px;
  font-size: 15px;
}

.field-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.form-row {
  display: flex;
  gap: 8px;
}

.form-row input {
  flex: 1;
  min-width: 0;
  background: #0d121a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
}

.status-msg {
  min-height: 20px;
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
}

.status-msg.ok { color: #86efac; }
.status-msg.err { color: #fca5a5; }

.lobby-notes {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.45;
}

@media (max-width: 560px) {
  .capacity-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .form-row {
    flex-direction: column;
  }
}

/* --- Admin (/admin) --- */
.admin-body {
  min-height: 100dvh;
  padding: 18px;
}

.admin-shell {
  max-width: 1260px;
  margin: 0 auto;
}

.admin-head-actions {
  display: flex;
  gap: 8px;
}

.login-card {
  max-width: 460px;
  margin: 40px auto;
}

.admin-dashboard {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.card-title-row h2 {
  margin: 0;
  font-size: 16px;
}

.config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 12px;
  align-items: end;
}

.cfg-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
}

.cfg-field input {
  background: #0d121a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 9px;
  font-size: 13px;
}

.cfg-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.pool-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.direct-link-banner {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(59, 130, 246, 0.16);
  border: 1px solid var(--accent);
  font-size: 13px;
}

.direct-link-banner a {
  color: #93c5fd;
  font-weight: 700;
}

.table-wrap {
  overflow-x: auto;
}

.pool-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.pool-table th,
.pool-table td {
  text-align: left;
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.pool-table th {
  color: var(--muted);
  font-weight: 600;
}

.empty-cell {
  text-align: center !important;
  color: var(--muted);
  padding: 20px !important;
}

.row-actions {
  display: flex;
  gap: 6px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 80;
}

.modal-box {
  width: min(860px, 100%);
  max-height: 85dvh;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.modal-head-btns {
  display: flex;
  gap: 6px;
}

.logs-pre {
  margin: 0;
  padding: 12px;
  overflow: auto;
  font-size: 12px;
  line-height: 1.45;
  color: #dbeafe;
  background: #090c11;
  flex: 1;
}

/* =====================================================================
   PLAY PAGE: 4 Automatic Responsive Layouts + [Auto] Drawer + Chat Bar
   ===================================================================== */

.play-body {
  width: 100dvw;
  height: 100dvh;
  overflow: hidden;
  background: #05070b;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.play-app {
  width: 100dvw;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

/* Top Slim HUD */
.play-hud {
  height: var(--hud-h);
  min-height: var(--hud-h);
  background: #10151f;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 8px;
  z-index: 20;
  overflow-x: auto;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  white-space: nowrap;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.err { background: var(--err); }

.hud-stats {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 5px;
}

.hud-fps {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
}

.hud-fps select {
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 11px;
}

/* Main Workspace (Game Deck + Docked Auto Drawer) */
.play-workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

.play-deck-shell {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

/* 4:3 Game Viewport — default crisp 1x (800x600) on desktop; .stage-fit expands to fill */
.stage-wrap {
  position: relative;
  aspect-ratio: 4 / 3;
  width: min(800px, 100%);
  max-width: 100%;
  max-height: 100%;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.65);
  border: 1px solid #1e293b;
}

.play-app.stage-fit .stage-wrap {
  width: auto;
  height: 100%;
  border: none;
  box-shadow: none;
}

@media (max-aspect-ratio: 4/3) {
  .play-app.stage-fit .stage-wrap {
    width: 100%;
    height: auto;
  }
}

@media (max-width: 900px) {
  .stage-wrap {
    width: auto;
    height: 100%;
    border: none;
    box-shadow: none;
  }
}

.game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: none;
  image-rendering: auto;
  transform-origin: center center;
}

.game-canvas.pixelated {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.hover-ring {
  position: absolute;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid #fde047;
  box-shadow: 0 0 10px rgba(253, 224, 71, 0.7);
  transform: translate(-50%, -50%) scale(0.7);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.hover-ring.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.boot-overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 7, 11, 0.86);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 15;
}

.boot-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px 24px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  font-size: 13px;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Touch Control Primitives (Hidden in Layout 1: Desktop) */
.touch-gutter,
.touch-ds-bottom {
  display: none;
  touch-action: none;
}

.joy-base {
  width: 104px;
  height: 104px;
  border-radius: 50%;
  background: rgba(30, 41, 59, 0.65);
  border: 2px solid rgba(148, 163, 184, 0.45);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.joy-knob {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #93c5fd, #2563eb);
  border: 2px solid rgba(255, 255, 255, 0.7);
  pointer-events: none;
}

.touch-row {
  display: flex;
  gap: 5px;
  justify-content: center;
  width: 100%;
}

.touch-potions,
.touch-grid-4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  width: 100%;
}

.tbtn {
  appearance: none;
  min-width: 34px;
  min-height: 34px;
  padding: 4px 6px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  background: rgba(20, 26, 36, 0.82);
  color: #f8fafc;
  font-size: 12px;
  font-weight: 700;
  touch-action: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tbtn.active,
.tbtn:active {
  background: var(--accent-strong);
  border-color: #93c5fd;
  transform: scale(0.95);
}

.tbtn-pot {
  background: rgba(127, 29, 29, 0.78);
  border-color: rgba(248, 113, 113, 0.55);
}

.tbtn-skill {
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  font-size: 14px;
  letter-spacing: 0.04em;
  background: linear-gradient(180deg, rgba(234, 88, 12, 0.9), rgba(154, 52, 18, 0.92));
  border-color: #fdba74;
}

.tbtn-accent {
  background: rgba(37, 99, 235, 0.82);
  border-color: #93c5fd;
}

.lr-toggle-btn.rmb {
  background: rgba(180, 83, 9, 0.88);
  border-color: #fcd34d;
}

/* =========================================================
   Layout 2: Wide Phone Landscape (3-Column Side Gutters)
   Zero overlap with the center 4:3 game viewport!
   ========================================================= */
@media (pointer: coarse) and (orientation: landscape) and (max-height: 580px),
       (orientation: landscape) and (max-height: 480px) {
  .play-hud {
    height: 30px;
    min-height: 30px;
  }
  .play-deck-shell {
    display: grid;
    grid-template-columns: minmax(116px, 1fr) auto minmax(140px, 1fr);
    align-items: center;
    justify-items: center;
    gap: 6px;
    padding: 4px 6px;
  }
  .stage-wrap {
    height: calc(100dvh - 38px);
    width: auto;
    max-width: calc((100dvh - 38px) * 4 / 3);
  }
  .touch-gutter {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    width: 100%;
    max-width: 185px;
    padding: 4px;
  }
  .joy-base {
    width: 92px;
    height: 92px;
  }
  .joy-knob {
    width: 38px;
    height: 38px;
  }
}

/* =========================================================
   Layout 3: Phone Portrait (DS-Stack: Top 4:3 + Bottom Deck)
   Zero overlap with the top 4:3 game viewport!
   ========================================================= */
@media (max-width: 900px) and (orientation: portrait) {
  .play-deck-shell {
    flex-direction: column;
    justify-content: flex-start;
  }
  .stage-wrap {
    width: 100%;
    height: auto;
    max-height: 55dvh;
    flex-shrink: 0;
  }
  .touch-ds-bottom {
    flex: 1;
    width: 100%;
    min-height: 0;
    display: grid;
    grid-template-columns: 126px 1fr;
    gap: 10px;
    padding: 10px;
    align-items: center;
    background: #0e131c;
    border-top: 1px solid var(--border);
    overflow-y: auto;
  }
  .ds-left {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }
  .ds-right {
    display: flex;
    flex-direction: column;
    gap: 7px;
  }
}

/* =========================================================
   Layout 4: Tablet / iPad 4:3 Floating Corner Overlay
   ========================================================= */
@media (pointer: coarse) and (min-width: 600px) and (min-height: 581px) {
  .touch-gutter {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    position: absolute;
    bottom: 14px;
    z-index: 18;
    width: 168px;
    padding: 10px;
    border-radius: 14px;
    background: rgba(11, 14, 20, 0.42);
    backdrop-filter: blur(4px);
  }
  .touch-gutter-left {
    left: 12px;
  }
  .touch-gutter-right {
    right: 12px;
  }
}

/* Manual Touch Deck Overrides (for testing on Desktop) */
.play-app.touch-force-off .touch-gutter,
.play-app.touch-force-off .touch-ds-bottom {
  display: none !important;
}

.play-app.touch-force-on .touch-gutter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  position: absolute;
  bottom: 14px;
  z-index: 18;
  width: 168px;
  padding: 10px;
  border-radius: 14px;
  background: rgba(11, 14, 20, 0.48);
}
.play-app.touch-force-on .touch-gutter-left { left: 12px; }
.play-app.touch-force-on .touch-gutter-right { right: 12px; }

/* =========================================================
   [Auto] 64-bit Drawer (Docked 360px/480px Desktop/iPad, Full-height sheet Phone)
   Keeps iframe mounted and running even when closed.
   ========================================================= */
.auto-backdrop {
  display: none;
}

.auto-drawer {
  display: flex;
  flex-direction: column;
  background: #141a24;
  border-left: 1px solid var(--border);
  z-index: 60;
}

.auto-drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  background: #10151f;
  gap: 8px;
}

.auto-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  min-width: 0;
}

.auto-head-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.auto-frame-wrap {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.auto-frame {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #0b0e14;
}

@media (min-width: 901px) and (min-height: 581px) {
  .auto-drawer {
    width: 0;
    min-width: 0;
    border-left-width: 0;
    opacity: 0;
    pointer-events: none;
    overflow: hidden;
    transition: width 0.18s ease, min-width 0.18s ease, opacity 0.18s ease;
  }
  .play-app.auto-open .auto-drawer {
    width: 360px;
    min-width: 360px;
    border-left-width: 1px;
    opacity: 1;
    pointer-events: auto;
  }
  .play-app.auto-open.auto-expanded .auto-drawer {
    width: 480px;
    min-width: 480px;
  }
}

@media (max-width: 900px), (max-height: 580px) {
  .auto-drawer {
    position: fixed;
    top: env(safe-area-inset-top);
    right: 0;
    bottom: env(safe-area-inset-bottom);
    width: min(480px, 100dvw);
    max-width: 100dvw;
    height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    transform: translateX(100%);
    transition: transform 0.2s ease;
    box-shadow: -8px 0 28px rgba(0, 0, 0, 0.65);
  }
  .auto-expand-btn {
    display: none;
  }
  .auto-drawer-head .hud-btn {
    min-width: 36px;
    min-height: 36px;
  }
  .play-app.auto-open .auto-drawer {
    transform: translateX(0);
  }
  .play-app.auto-open .auto-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 55;
  }
}

/* =========================================================
   Floating visualViewport-Anchored TCVN3 Chat Bar
   ========================================================= */
.chat-bar {
  position: fixed;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%) translateY(140%);
  width: min(640px, calc(100dvw - 16px));
  background: rgba(16, 21, 31, 0.96);
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 6px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 70;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.65);
}

.chat-bar.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.chat-bar select {
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 4px;
  font-size: 12px;
}

.chat-enter-opt {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
}

.chat-bar input[type="text"] {
  flex: 1;
  min-width: 0;
  background: #090d13;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 9px;
  font-size: 14px;
  user-select: text;
  -webkit-user-select: text;
}
