:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --border: #475569;
  --text: #f8fafc;
  --muted: #94a3b8;
  --correct: #10b981;
  --correct-border: #34d399;
  --present: #d97706;
  --present-border: #fbbf24;
  --absent: #1e2433;
  --accent: #38bdf8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  background: radial-gradient(circle at 50% 0%, #1e1b4b 0%, #0f172a 70%);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}

/* Topbar */
.topbar {
  display: flex;
  flex-direction: column;
  background: rgba(15, 23, 42, 0.92);
  border-bottom: 1px solid rgba(148, 163, 184, 0.16);
  backdrop-filter: blur(12px);
  z-index: 10;
  flex-shrink: 0;
}

.topbar-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
}

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

#btn-toggle-termo-drawer[aria-expanded='true'] {
  background: rgba(56, 189, 248, 0.22);
  border: 1px solid rgba(56, 189, 248, 0.5);
  color: #7dd3fc;
}

.termo-drawer {
  border-top: 1px solid rgba(148, 163, 184, 0.14);
  background: rgba(11, 17, 32, 0.96);
  padding: 10px 14px;
  animation: toast-in 0.18s ease;
}

.termo-drawer[hidden] {
  display: none !important;
}

.termo-drawer-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
  max-width: 980px;
  margin: 0 auto;
}

.mode-tabs {
  display: inline-flex;
  background: rgba(30, 41, 59, 0.9);
  padding: 3px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.18);
}

.mode-tab {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 6px 12px;
  border-radius: 7px;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.mode-tab small {
  background: rgba(255, 255, 255, 0.1);
  padding: 1px 5px;
  border-radius: 99px;
  font-size: 0.72rem;
}

.mode-tab:hover {
  color: var(--text);
}

.mode-tab.active {
  background: var(--correct);
  color: #fff;
  box-shadow: 0 2px 10px rgba(16, 185, 129, 0.35);
}

.tier-selector {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

.tier-selector select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(148, 163, 184, 0.28);
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.seed-box {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(30, 41, 59, 0.85);
  padding: 4px 8px;
  border-radius: 9px;
  border: 1px solid rgba(148, 163, 184, 0.2);
}

.seed-label {
  font-size: 0.76rem;
  color: var(--muted);
  font-weight: 600;
}

#seed-input {
  width: 96px;
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(148, 163, 184, 0.25);
  color: #38bdf8;
  font-weight: 800;
  font-size: 0.78rem;
  text-transform: uppercase;
  padding: 4px 7px;
  border-radius: 6px;
  letter-spacing: 0.05em;
}

.btn-subtle,
.btn-primary-sm,
.btn-icon {
  border: none;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 700;
  transition: transform 0.15s ease, background 0.15s ease;
}

.btn-subtle {
  background: var(--surface-2);
  color: var(--text);
  padding: 5px 9px;
  font-size: 0.76rem;
}

.btn-primary-sm {
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  color: #fff;
  padding: 5px 10px;
  font-size: 0.76rem;
}

.btn-icon {
  background: var(--surface);
  color: var(--text);
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  border: 1px solid rgba(148, 163, 184, 0.2);
}

.btn-subtle:hover,
.btn-primary-sm:hover,
.btn-icon:hover {
  transform: translateY(-1px);
}

/* Toasts */
.toast-container {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 90;
  pointer-events: none;
}

.toast {
  background: #f8fafc;
  color: #0f172a;
  padding: 8px 16px;
  border-radius: 99px;
  font-weight: 800;
  font-size: 0.86rem;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.45);
  animation: toast-in 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-10px) scale(0.92);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Main Arena & Boards */
.arena {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 14px;
  overflow-y: auto;
}

.boards-container {
  display: grid;
  gap: 14px;
  width: 100%;
  max-width: 1120px;
  align-items: center;
  justify-items: center;
}

.boards-container.mode-termo {
  grid-template-columns: 1fr;
  max-width: 350px;
}

.boards-container.mode-dueto {
  grid-template-columns: repeat(2, minmax(210px, 310px));
  justify-content: center;
}

.boards-container.mode-quarteto {
  grid-template-columns: repeat(4, minmax(165px, 245px));
  justify-content: center;
}

@media (max-width: 820px) {
  .boards-container.mode-quarteto {
    grid-template-columns: repeat(2, minmax(150px, 230px));
  }
}

@media (max-width: 520px) {
  .boards-container.mode-dueto {
    grid-template-columns: repeat(2, minmax(140px, 1fr));
    gap: 8px;
  }
}

.board-card {
  background: rgba(30, 41, 59, 0.45);
  border: 1px solid rgba(148, 163, 184, 0.14);
  border-radius: 14px;
  padding: 10px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 5px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.board-card.solved {
  border-color: rgba(16, 185, 129, 0.55);
  box-shadow: 0 0 24px rgba(16, 185, 129, 0.14);
}

.board-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
}

.tile {
  aspect-ratio: 1 / 1;
  border-radius: 9px;
  border: 2px solid rgba(148, 163, 184, 0.25);
  background: rgba(15, 23, 42, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: clamp(1rem, 2.2vw, 1.55rem);
  text-transform: uppercase;
  color: var(--text);
  transition: border-color 0.12s ease, transform 0.12s ease;
  perspective: 600px;
}

.mode-quarteto .tile {
  font-size: clamp(0.85rem, 1.5vw, 1.15rem);
  border-radius: 7px;
}

.board-row.active-row .tile {
  background: rgba(30, 41, 59, 0.9);
  border-color: rgba(148, 163, 184, 0.45);
  cursor: pointer;
}

.board-row.active-row .tile.cursor {
  border-color: var(--accent);
  border-bottom-width: 5px;
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.28);
}

.tile.filled {
  border-color: rgba(248, 250, 252, 0.55);
}

/* Tile States */
.tile.correct {
  background: var(--correct);
  border-color: var(--correct-border);
  color: #fff;
}

.tile.present {
  background: var(--present);
  border-color: var(--present-border);
  color: #fff;
}

.tile.absent {
  background: var(--absent);
  border-color: rgba(71, 85, 105, 0.4);
  color: #94a3b8;
}

/* Keyframe Animations */
.tile.pop {
  animation: tile-pop 0.14s ease-out;
}

@keyframes tile-pop {
  0% {
    transform: scale(0.85);
  }
  50% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

.tile.flip {
  animation: tile-flip 0.48s ease-in-out forwards;
}

@keyframes tile-flip {
  0% {
    transform: rotateX(0deg);
  }
  50% {
    transform: rotateX(90deg);
  }
  100% {
    transform: rotateX(0deg);
  }
}

.tile.bounce {
  animation: tile-bounce 0.55s ease;
}

@keyframes tile-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-14px) scale(1.05);
  }
  70% {
    transform: translateY(3px);
  }
}

.board-row.shake {
  animation: row-shake 0.42s ease;
}

@keyframes row-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20%,
  60% {
    transform: translateX(-7px);
  }
  40%,
  80% {
    transform: translateX(7px);
  }
}

/* Virtual Keyboard */
.keyboard-area {
  padding: 6px 10px 12px;
  display: flex;
  justify-content: center;
  background: linear-gradient(to top, rgba(15, 23, 42, 0.95), transparent);
}

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 640px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.key {
  position: relative;
  flex: 1;
  height: 44px;
  max-width: 54px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 800;
  font-size: 0.92rem;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: transform 0.1s ease, filter 0.15s ease;
}

.key.wide {
  flex: 1.65;
  max-width: 88px;
  font-size: 0.78rem;
}

.key:active {
  transform: scale(0.93);
}

.key-label {
  position: relative;
  z-index: 2;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.65);
}

.key-quadrants {
  position: absolute;
  inset: 0;
  display: grid;
  z-index: 1;
}

.key-quadrants.q-1 {
  grid-template-columns: 1fr;
}

.key-quadrants.q-2 {
  grid-template-columns: 1fr 1fr;
}

.key-quadrants.q-4 {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.quad.correct {
  background: var(--correct);
}
.quad.present {
  background: var(--present);
}
.quad.absent {
  background: #0f1420;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 80;
  animation: fade-in 0.2s ease;
}

.modal-backdrop[hidden] {
  display: none;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-card {
  background: #1e293b;
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 18px;
  padding: 24px;
  width: 100%;
  max-width: 430px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-badge {
  align-self: center;
  background: rgba(56, 189, 248, 0.15);
  color: #38bdf8;
  padding: 4px 12px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.1em;
}

.modal-words {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.word-pill {
  background: rgba(16, 185, 129, 0.18);
  border: 1px solid rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
  padding: 6px 14px;
  border-radius: 10px;
  font-weight: 900;
  font-size: 1rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: rgba(15, 23, 42, 0.6);
  padding: 12px;
  border-radius: 12px;
}

.stat-box strong {
  display: block;
  font-size: 1.45rem;
  color: #f8fafc;
}

.stat-box span {
  font-size: 0.75rem;
  color: var(--muted);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-share,
.btn-next {
  border: none;
  padding: 12px 16px;
  border-radius: 10px;
  font-weight: 800;
  font-size: 0.92rem;
  cursor: pointer;
  transition: transform 0.15s ease;
}

.btn-share {
  background: var(--correct);
  color: #fff;
}

.btn-next {
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  color: #fff;
}

.btn-close-text {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 6px;
  font-size: 0.84rem;
  cursor: pointer;
}

.help-text {
  font-size: 0.88rem;
  color: #cbd5e1;
  line-height: 1.45;
}

.help-examples {
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: left;
  background: rgba(15, 23, 42, 0.55);
  padding: 12px;
  border-radius: 12px;
}

.help-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: #cbd5e1;
}

.mini-tile {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--surface-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  flex-shrink: 0;
}

.mini-tile.correct {
  background: var(--correct);
}
.mini-tile.present {
  background: var(--present);
}
.mini-tile.absent {
  background: var(--absent);
}

/* ==================== MOBILE OPTIMIZATIONS ==================== */
@media (max-width: 640px) {
  .topbar-main {
    padding: 6px 8px;
    gap: 6px;
  }

  .mode-tab {
    padding: 5px 8px;
    font-size: 0.76rem;
  }

  .mode-tab small {
    display: none;
  }

  #btn-toggle-termo-drawer {
    font-size: 0.72rem;
    padding: 5px 8px;
  }

  .tier-selector,
  .seed-box {
    width: 100%;
    justify-content: space-between;
  }

  .tier-selector select,
  #seed-input {
    flex: 1;
    min-width: 0;
    font-size: 16px;
  }

  .arena {
    padding: 6px 8px;
  }

  .boards-container.mode-termo {
    max-width: 280px;
  }

  .boards-container.mode-dueto,
  .boards-container.mode-quarteto {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .board-card {
    padding: 6px;
    border-radius: 10px;
    gap: 4px;
  }

  .board-row {
    gap: 4px;
  }

  .tile {
    border-radius: 6px;
    font-size: 1rem;
  }

  .mode-quarteto .tile {
    font-size: 0.78rem;
    border-radius: 5px;
    border-width: 1.5px;
  }

  .keyboard-area {
    padding: 4px 4px calc(8px + env(safe-area-inset-bottom));
  }

  .keyboard {
    gap: 5px;
  }

  .kb-row {
    gap: 3px;
  }

  .key {
    height: 46px;
    border-radius: 6px;
    font-size: 0.85rem;
  }

  .key.wide {
    font-size: 0.72rem;
  }
}
