:root {
  --bg-deep: #0b101b;
  --bg-panel: #131b2e;
  --bg-card: #1a243d;
  --border: #2a395c;
  --text-main: #e2e8f0;
  --text-muted: #94a3b8;
  --blue: #3b82f6;
  --blue-soft: rgba(59, 130, 246, 0.22);
  --red: #ef4444;
  --red-soft: rgba(239, 68, 68, 0.22);
  --accent: #f59e0b;
  --green: #10b981;
  --purple: #a855f7;
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at top, #162038 0%, var(--bg-deep) 65%);
  color: var(--text-main);
  min-height: 100vh;
  overflow-x: hidden;
}

.hidden {
  display: none !important;
}

.view {
  display: none;
  min-height: 100vh;
}

.view.active {
  display: flex;
  flex-direction: column;
}

/* Buttons */
.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: transform 0.1s ease, filter 0.15s ease, background 0.15s ease;
  color: #fff;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.btn:hover:not(:disabled) {
  filter: brightness(1.1);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

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

.btn-primary {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  border-color: #3b82f6;
}

.btn-accent {
  background: linear-gradient(135deg, #d97706, #b45309);
  border-color: #f59e0b;
}

.btn-secondary {
  background: #243252;
  border-color: var(--border);
  color: var(--text-main);
}

.btn-danger {
  background: #991b1b;
  border-color: #ef4444;
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 0.8rem;
}

.btn-lg {
  padding: 12px 18px;
  font-size: 1rem;
}

.btn small {
  font-weight: 400;
  font-size: 0.74rem;
  opacity: 0.85;
}

/* Toasts */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #1e293b;
  border-left: 4px solid var(--accent);
  padding: 10px 16px;
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  font-size: 0.88rem;
}

.toast.error {
  border-left-color: var(--red);
}

/* ======================== LOBBY ======================== */
.lobby-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 16, 27, 0.8);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-badge {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--blue), var(--red));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
}

.brand h1 {
  font-size: 1.5rem;
  letter-spacing: 1px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.86rem;
}

.connection-pill {
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  background: #1e293b;
  border: 1px solid var(--border);
}

.connection-pill.online {
  border-color: var(--green);
  color: #34d399;
}

.lobby-grid {
  max-width: 1280px;
  width: 100%;
  margin: 24px auto;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 420px 1fr;
  gap: 22px;
}

.lobby-col {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.28);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.card-header h2 {
  font-size: 1.05rem;
}

.tier-badge {
  background: rgba(59, 130, 246, 0.18);
  border: 1px solid rgba(59, 130, 246, 0.45);
  color: #93c5fd;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
}

.profile-identity {
  display: flex;
  gap: 14px;
  align-items: center;
}

.level-circle {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: linear-gradient(145deg, #2563eb, #7c3aed);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 0 18px rgba(59, 130, 246, 0.35);
}

.level-label {
  font-size: 0.65rem;
  opacity: 0.85;
}

#profile-level {
  font-size: 1.45rem;
  font-weight: 800;
  line-height: 1;
}

.profile-meta {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

input[type="text"] {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 10px;
  color: var(--text-main);
  font-size: 0.88rem;
  width: 100%;
}

.xp-bar-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.progress-track {
  height: 8px;
  border-radius: 999px;
  background: #0d1424;
  overflow: hidden;
  border: 1px solid #22304f;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #a855f7);
  transition: width 0.4s ease;
}

.record-row {
  display: flex;
  gap: 14px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.record-row strong {
  color: var(--text-main);
}

.mode-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.status-box {
  margin-top: 12px;
  padding: 12px;
  border-radius: 10px;
  background: #0f1729;
  border: 1px solid #334155;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.84rem;
}

.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid #334155;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
  flex-shrink: 0;
}

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

.divider {
  display: flex;
  align-items: center;
  text-align: center;
  margin: 16px 0 12px;
  color: var(--text-muted);
  font-size: 0.72rem;
  letter-spacing: 1px;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid var(--border);
}

.divider span {
  padding: 0 10px;
}

.private-room-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.join-input-group {
  display: flex;
  gap: 6px;
  flex: 1;
}

.mono-code {
  font-family: monospace;
  font-size: 1rem;
  color: var(--accent);
  letter-spacing: 2px;
}

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

.hint-text {
  color: var(--text-muted);
  font-size: 0.8rem;
}

.lobby-roster-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(195px, 1fr));
  gap: 10px;
  max-height: 540px;
  overflow-y: auto;
  padding-right: 4px;
}

.roster-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
}

.roster-card.locked {
  opacity: 0.52;
  filter: grayscale(0.5);
}

.roster-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.roster-name {
  font-weight: 700;
  font-size: 0.9rem;
}

.cost-badge {
  background: rgba(245, 158, 11, 0.2);
  color: #fbbf24;
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.75rem;
  font-weight: 700;
}

.roster-role {
  font-size: 0.74rem;
  color: #93c5fd;
  font-weight: 600;
}

.roster-desc {
  font-size: 0.74rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.roster-stats {
  margin-top: 4px;
  font-size: 0.72rem;
  color: #cbd5e1;
  display: flex;
  gap: 8px;
}

.lock-pill {
  margin-top: 4px;
  font-size: 0.7rem;
  color: #fca5a5;
  font-weight: 600;
}

/* ======================== ARENA VIEW ======================== */
#arena-view {
  height: 100vh;
  overflow: hidden;
  padding: 8px 14px 10px;
  gap: 8px;
}

.arena-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 14px;
}

.team-hud {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.red-hud {
  align-items: flex-end;
}

.team-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 0.92rem;
}

.team-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.team-dot.blue { background: var(--blue); box-shadow: 0 0 8px var(--blue); }
.team-dot.red { background: var(--red); box-shadow: 0 0 8px var(--red); }

.lvl-pill {
  font-size: 0.72rem;
  background: #243252;
  padding: 2px 6px;
  border-radius: 999px;
  color: var(--text-muted);
}

.hp-bar-outer {
  width: 100%;
  height: 20px;
  background: #0b101b;
  border: 1px solid var(--border);
  border-radius: 6px;
  position: relative;
  overflow: hidden;
}

.hp-bar-inner {
  height: 100%;
  transition: width 0.2s linear;
}

.hp-bar-inner.blue { background: linear-gradient(90deg, #1d4ed8, #3b82f6); }
.hp-bar-inner.red { background: linear-gradient(90deg, #ef4444, #b91c1c); float: right; }

.hp-bar-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.74rem;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}

.center-hud {
  text-align: center;
  min-width: 220px;
}

.phase-pill {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.18);
  border: 1px solid rgba(245, 158, 11, 0.45);
  color: #fbbf24;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.8px;
}

.timer-display {
  font-size: 1.35rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.turn-subtext {
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* Canvas Stage */
.canvas-stage {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #090e17;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

#battle-canvas {
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1200 / 640;
  background: #101829;
  cursor: crosshair;
  touch-action: none;
}

.stage-overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 12, 22, 0.82);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.dice-modal,
.resolution-modal {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 26px 32px;
  text-align: center;
  max-width: 460px;
  width: 92%;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}

.dice-duel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  margin: 22px 0;
}

.dice-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.team-label {
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 1px;
}

.team-label.blue { color: var(--blue); }
.team-label.red { color: var(--red); }

.dice-face {
  width: 78px;
  height: 78px;
  border-radius: 14px;
  background: #1e293b;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.3rem;
  font-weight: 900;
}

.dice-face.rolling {
  animation: diceShake 0.25s infinite alternate;
}

@keyframes diceShake {
  from { transform: rotate(-6deg) scale(0.96); }
  to { transform: rotate(6deg) scale(1.04); }
}

.dice-vs {
  font-weight: 800;
  color: var(--text-muted);
}

.dice-winner-banner {
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(16, 185, 129, 0.18);
  border: 1px solid var(--green);
  color: #6ee7b7;
  font-weight: 700;
}

.dice-winner-banner.tie {
  background: rgba(245, 158, 11, 0.18);
  border-color: var(--accent);
  color: #fcd34d;
}

/* Resolution */
.res-badge {
  display: inline-block;
  padding: 4px 14px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 0.85rem;
  margin-bottom: 10px;
}

.res-badge.win { background: rgba(16, 185, 129, 0.2); color: #34d399; border: 1px solid #10b981; }
.res-badge.loss { background: rgba(239, 68, 68, 0.2); color: #f87171; border: 1px solid #ef4444; }
.res-badge.draw { background: rgba(245, 158, 11, 0.2); color: #fbbf24; border: 1px solid #f59e0b; }

.res-stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 18px 0 12px;
}

.res-stat {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.res-stat span { font-size: 0.75rem; color: var(--text-muted); }
.res-stat strong { font-size: 1.2rem; }
.xp-highlight { color: #a78bfa; }

.res-xp-breakdown {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 18px;
}

/* Toolbars */
.arena-toolbar {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 12px;
}

.toolbar-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.toolbar-label {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 600;
}

.tool-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.84rem;
  font-weight: 600;
}

.tool-btn.active {
  background: rgba(59, 130, 246, 0.25);
  border-color: var(--blue);
  color: #fff;
}

.brush-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}

/* Placement bar */
#toolbar-placement {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.economy-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.econ-pill {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}

.econ-pill span { color: var(--text-muted); }
.econ-pill strong { color: #fbbf24; }

.placement-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.placement-unit-bar {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.unit-btn {
  flex: 0 0 auto;
  min-width: 108px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  color: var(--text-main);
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.unit-btn.selected {
  border-color: var(--blue);
  background: rgba(59, 130, 246, 0.24);
  box-shadow: 0 0 0 1px var(--blue);
}

.unit-btn.locked,
.unit-btn.unaffordable {
  opacity: 0.42;
  cursor: not-allowed;
}

.unit-btn-head {
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  font-size: 0.78rem;
}

.unit-btn-role {
  font-size: 0.68rem;
  color: var(--text-muted);
}

/* Battle toolbar */
.battle-bar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.battle-info {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.88rem;
}

.live-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ef4444;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* Level-Up Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 16, 0.85);
  z-index: 1500;
  display: flex;
  align-items: center;
  justify-content: center;
}

.levelup-card {
  background: linear-gradient(160deg, #1b2745, #12182b);
  border: 2px solid var(--accent);
  border-radius: 18px;
  padding: 28px;
  max-width: 500px;
  width: 92%;
  text-align: center;
  box-shadow: 0 0 50px rgba(245, 158, 11, 0.28);
}

.levelup-burst {
  color: var(--accent);
  font-weight: 900;
  letter-spacing: 1.5px;
  margin-bottom: 6px;
}

.levelup-unlocks-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  margin: 16px 0 22px;
  text-align: left;
}

@media (max-width: 960px) {
  .lobby-grid {
    grid-template-columns: 1fr;
  }
}

/* ======================== AUTH VIEW & PERSISTENT STATS ======================== */
.auth-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  gap: 20px;
}

.auth-brand {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 440px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.auth-tab {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px;
  color: var(--text-muted);
  font-weight: 700;
  cursor: pointer;
}

.auth-tab.active {
  background: rgba(59, 130, 246, 0.2);
  border-color: var(--blue);
  color: #fff;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.form-field input {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 9px 11px;
  color: var(--text-main);
  font-size: 0.92rem;
  width: 100%;
}

.auth-error {
  background: rgba(239, 68, 68, 0.16);
  border: 1px solid var(--red);
  color: #fca5a5;
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 0.83rem;
}

.auth-guest-btn {
  width: 100%;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.account-pill {
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  background: rgba(168, 85, 247, 0.16);
  border: 1px solid rgba(168, 85, 247, 0.45);
  color: #d8b4fe;
}

.stats-mini-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-top: 4px;
}

.mini-stat {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.mini-stat span {
  font-size: 0.68rem;
  color: var(--text-muted);
}

.mini-stat strong {
  font-size: 0.9rem;
  color: var(--text-main);
}

.match-history-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-height: 220px;
  overflow-y: auto;
}

.history-empty {
  color: var(--text-muted);
  font-size: 0.84rem;
  text-align: center;
  padding: 14px 0;
}

.history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.82rem;
}

.history-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.history-badge {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
}

.history-badge.win { background: rgba(16, 185, 129, 0.2); color: #34d399; }
.history-badge.loss { background: rgba(239, 68, 68, 0.2); color: #f87171; }
.history-badge.draw { background: rgba(245, 158, 11, 0.2); color: #fbbf24; }

.history-meta {
  color: var(--text-muted);
  font-size: 0.76rem;
}

.history-xp {
  font-weight: 700;
  color: #c4b5fd;
}

.theme-picker {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 600;
}

.theme-select {
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text-main);
  padding: 5px 9px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.theme-select.compact {
  padding: 4px 7px;
  font-size: 0.75rem;
}

.btn.iso-active {
  background: linear-gradient(135deg, #0284c7, #2563eb);
  border-color: #38bdf8;
  color: #fff;
}

.camera-controls {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #0d1424;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px 4px;
}

.zoom-label {
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--text-main);
  min-width: 38px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.btn.auto-zoom-active {
  background: rgba(16, 185, 129, 0.2);
  border-color: #10b981;
  color: #6ee7b7;
}

.theme-select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
