:root {
  /* Madeira (do mais escuro para o mais claro) */
  --wood-ebony: #120b06;
  --wood-darkest: #1c120a;
  --wood-dark: #2b1b10;
  --wood-medium: #4a2f1b;
  --wood-warm: #6e4526;
  --wood-light: #d2b48c;

  --square-light: #e6cc9f;
  --square-dark: #a2703f;
  --square-selected: #e8c95a;

  --bg-color: #140d07;
  --panel-bg: #24170d;
  --text-color: #f3e6d0;
  --text-muted: #b99c7c;
  --accent: #e0a96a;
  --gold: #d4a557;
  --success: #6fcf7f;
  --error: #ec7063;

  --font-display: 'Cinzel', 'Trajan Pro', Georgia, serif;
  --font-body: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

  /* Tamanho de uma casa do tabuleiro principal */
  --sq: 92px;

  /* Veios de madeira gerados por ruído SVG (horizontal e fino) */
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.004 0.11' numOctaves='4' seed='7' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.1 0 0 0 -0.38'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)'/%3E%3C/svg%3E");
  --grain-fine: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.008 0.16' numOctaves='2' seed='11' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.28 0 0 0 -0.1'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)'/%3E%3C/svg%3E");

  --bevel: inset 0 1px 0 rgba(255, 225, 180, 0.08), inset 0 -1px 0 rgba(0, 0, 0, 0.5);
}

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

body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(110, 69, 38, 0.35), transparent 60%),
    radial-gradient(ellipse at 50% 120%, rgba(0, 0, 0, 0.7), transparent 60%), var(--grain), var(--bg-color);
  background-attachment: fixed;
  color: var(--text-color);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  align-items: center;
  -webkit-font-smoothing: antialiased;
}

h2,
h3,
.board-label,
.stat-box .label {
  font-family: var(--font-display);
  letter-spacing: 0.06em;
}

/* Espaços para Banners de Anúncios */
.ad-banner {
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.25);
  border: 1px dashed rgba(210, 180, 140, 0.15);
  border-radius: 6px;
  margin: 8px 0;
}

.ad-placeholder {
  color: #7d6450;
  font-size: 0.8rem;
  padding: 10px;
  text-align: center;
}

.ad-header,
.ad-footer {
  width: 100%;
  max-width: 1000px;
  min-height: 70px;
}

.copyright {
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  padding: 4px 16px 16px;
}

.ad-sidebar {
  min-height: 200px;
  width: 100%;
  margin-top: 15px;
}

/* Layout Flex/Grid */
.app-layout {
  display: flex;
  width: 100%;
  max-width: 1100px;
  gap: 20px;
  padding: 10px;
  flex: 1;
}

.sidebar {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* Painéis em madeira escura */
.card,
.game-container,
.modal-card {
  background: linear-gradient(180deg, rgba(255, 220, 170, 0.05), rgba(0, 0, 0, 0.25)), var(--grain), var(--panel-bg);
  border: 1px solid #000;
  box-shadow:
    0 0 0 1px rgba(212, 165, 87, 0.18) inset,
    var(--bevel),
    0 10px 28px rgba(0, 0, 0, 0.6);
}

.card {
  border-radius: 10px;
  padding: 16px 18px;
}

.card h3 {
  color: var(--gold);
  margin-bottom: 12px;
  font-size: 0.95rem;
  text-transform: uppercase;
  border-bottom: 1px solid rgba(212, 165, 87, 0.25);
  padding-bottom: 8px;
}

#user-status {
  margin-bottom: 12px;
  color: var(--text-color);
}

/* Ranking Formatado sem numeração duplicada */
.ranking-card ol {
  list-style: none;
  counter-reset: rank;
}

/* Flex mantém nome e pontos na mesma linha; float jogava os pontos para a linha de baixo */
.ranking-card li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 8px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  counter-increment: rank;
}

.ranking-card li::before {
  content: counter(rank) '.';
  min-width: 1.4em;
  color: var(--gold);
  font-weight: 700;
}

.ranking-card li span:first-child {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#btn-logout {
  margin-top: 8px;
}

/* Regras de pontuação, abaixo do ranking */
.scoring-card ul {
  list-style: none;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.scoring-card li + li {
  margin-top: 8px;
}

.scoring-card strong {
  color: var(--text-color);
}

.ranking-card li span:last-child:not(:only-child) {
  white-space: nowrap;
  color: var(--accent);
  font-weight: 700;
}

/* Seletor de Modo de Jogo: dois botões lado a lado */
.mode-tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 12px;
}

.mode-tab {
  flex: 1 1 0;
  max-width: 240px;
  white-space: nowrap;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  font-family: var(--font-display);
  letter-spacing: 0.04em;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-muted);
  background: linear-gradient(180deg, #3a2516, #24160c);
  border: 1px solid #000;
  box-shadow: 0 0 0 1px rgba(212, 165, 87, 0.25) inset;
  padding: 10px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.mode-icon {
  font-size: 1.3rem;
  line-height: 1;
}

.mode-tab:hover,
.mode-tab:focus-visible {
  outline: none;
  color: var(--text-color);
  box-shadow: 0 0 0 1px var(--gold) inset;
}

.mode-tab.active {
  background: linear-gradient(180deg, #e7b965 0%, #c48a34 55%, #9c6620 100%);
  color: #23140a;
  box-shadow:
    inset 0 1px 0 rgba(255, 245, 210, 0.6),
    0 3px 8px rgba(0, 0, 0, 0.5);
  text-shadow: 0 1px 0 rgba(255, 235, 190, 0.45);
}

/* Níveis do xeque-mate: um botão por nível com o progresso */
.mate-levels {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.mate-levels-label {
  font-family: var(--font-display);
  letter-spacing: 0.06em;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
}

.mate-levels-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.level-pill {
  min-width: 44px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  font-family: var(--font-body);
  color: var(--text-color);
  background: #24160c;
  border: 1px solid #000;
  box-shadow: 0 0 0 1px rgba(212, 165, 87, 0.25) inset;
  padding: 5px 8px;
  border-radius: 8px;
  cursor: pointer;
}

.level-number {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.1;
}

.level-status {
  font-size: 0.68rem;
  color: var(--text-muted);
}

.level-pill.complete .level-status {
  color: var(--gold);
  font-weight: 700;
}

.level-pill.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.level-pill:not(.locked):hover,
.level-pill:focus-visible {
  outline: none;
  box-shadow: 0 0 0 1px var(--gold) inset;
}

.level-pill.active {
  background: linear-gradient(180deg, #e7b965 0%, #c48a34 55%, #9c6620 100%);
  color: #23140a;
  box-shadow: inset 0 1px 0 rgba(255, 245, 210, 0.6);
}

.level-pill.active .level-status {
  color: #3a2410;
}

.seed-form {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: -6px 0 14px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.seed-input {
  width: 6.5em;
  font-family: var(--font-body);
  font-size: 0.9rem;
  background: #24160c;
  color: var(--text-color);
  border: 1px solid #000;
  box-shadow: 0 0 0 1px rgba(212, 165, 87, 0.3) inset;
  padding: 6px 8px;
  border-radius: 6px;
}

.seed-input:focus-visible {
  outline: none;
  box-shadow: 0 0 0 1px var(--gold) inset;
}

.seed-form .btn-icon {
  display: inline-flex;
  padding: 6px 9px;
}

/* Área do Jogo e Tabuleiro */
.game-container {
  flex: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 14px;
  padding: 22px 20px;
}

.game-header {
  text-align: center;
  width: 100%;
  margin-bottom: 15px;
}

#challenge-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: #f6e3c3;
  text-shadow:
    0 2px 0 #000,
    0 0 18px rgba(212, 165, 87, 0.25);
}

#challenge-desc {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 4px;
}

.game-stats {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 14px;
}

.stat-box {
  background: linear-gradient(180deg, #160e07, #22150b);
  border: 1px solid #000;
  box-shadow:
    0 0 0 1px rgba(212, 165, 87, 0.2) inset,
    inset 0 2px 6px rgba(0, 0, 0, 0.6);
  padding: 7px 12px;
  border-radius: 8px;
  /* Largura fixa: valores grandes não alargam a caixa */
  flex: 0 0 116px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-box .label {
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--gold);
}

.stat-box .value {
  font-size: 1.25rem;
  font-weight: 700;
  color: #fff6e6;
  font-variant-numeric: tabular-nums;
  /* Quando o valor não cabe, o script reduz a fonte (fitValue) */
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

/* Disposição dos Tabuleiros (Principal + Miniatura) */
.game-play-area {
  display: flex;
  gap: 20px;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
}

.board-label {
  text-align: center;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--gold);
  margin-bottom: 10px;
}

.mate-progress {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin: 4px 0 12px;
}

.progress-bar {
  display: flex;
  gap: 4px;
  width: min(100%, 260px);
}

.progress-segment {
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--wood-medium);
}

.progress-segment.solved {
  background: var(--gold);
}

.progress-segment.current {
  outline: 2px solid var(--text-muted);
  outline-offset: 1px;
}

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

.board-best {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: -6px 0 10px;
}

/* Moldura do tabuleiro em madeira envernizada */
.board-wrapper {
  background: linear-gradient(180deg, rgba(255, 210, 150, 0.12), rgba(0, 0, 0, 0.3)), var(--grain), #4a2c17;
  padding: 14px 16px 18px;
  border-radius: 12px;
  border: 1px solid #000;
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.18),
    inset 0 -2px 4px rgba(0, 0, 0, 0.5),
    0 12px 24px rgba(0, 0, 0, 0.55);
  position: relative;
}

.board-3x3 {
  display: grid;
  grid-template-columns: repeat(3, var(--sq));
  grid-template-rows: repeat(3, var(--sq));
  gap: 0;
  border: 3px solid #1a0f07;
  border-radius: 3px;
  overflow: hidden;
  box-shadow:
    0 0 0 1px rgba(212, 165, 87, 0.45),
    inset 0 0 12px rgba(0, 0, 0, 0.35);
}

.square {
  width: var(--sq);
  height: var(--sq);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  cursor: pointer;
  transition: background-color 0.15s ease;
  touch-action: none;
  background-image: linear-gradient(135deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.08)), var(--grain-fine);
}

.square.light {
  background-color: var(--square-light);
}

.square.dark {
  background-color: var(--square-dark);
}

.board-3x3:not(.mini-board) .square:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: -3px;
  z-index: 3;
}

.square.selected {
  background-color: var(--square-selected) !important;
  box-shadow: inset 0 0 0 3px rgba(120, 80, 10, 0.45);
}

.square.highlight::after {
  content: '';
  width: 26%;
  height: 26%;
  background-color: rgba(30, 20, 10, 0.35);
  border-radius: 50%;
  position: absolute;
  pointer-events: none;
}

.square.highlight-capture::after {
  content: '';
  inset: 4%;
  border: 5px solid rgba(160, 30, 20, 0.6);
  border-radius: 50%;
  position: absolute;
  pointer-events: none;
}

/* Miniatura do Tabuleiro Objetivo */
.mini-wrapper {
  padding: 10px 12px 12px;
}

.mini-wrapper.hidden {
  display: none !important;
}

.mini-board {
  grid-template-columns: repeat(3, 46px) !important;
  grid-template-rows: repeat(3, 46px) !important;
  border-width: 2px;
}

.mini-board .square {
  width: 46px !important;
  height: 46px !important;
  cursor: default;
}

.mini-board .piece {
  width: 40px;
  height: 40px;
  cursor: default;
}

/* Peças de Xadrez (SVG em pieces.js) */
.piece {
  width: calc(var(--sq) * 0.88);
  height: calc(var(--sq) * 0.88);
  cursor: grab;
  user-select: none;
  z-index: 2;
  transition: transform 0.12s ease;
  touch-action: none;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.45));
}

.piece svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.piece.white {
  --piece-fill: url(#pc-ivory);
  --piece-stroke: #24170d;
  --piece-detail: #5e3f22;
}

.piece.black {
  --piece-fill: url(#pc-ebony);
  --piece-stroke: #050302;
  --piece-detail: #c9a063;
}

.pc-body {
  fill: var(--piece-fill);
  stroke: var(--piece-stroke);
  stroke-width: 2.4;
  stroke-linejoin: round;
}

.pc-line {
  fill: none;
  stroke: var(--piece-detail);
  stroke-width: 2.2;
  stroke-linecap: round;
}

.pc-dot {
  fill: var(--piece-stroke);
}

.piece.black .pc-dot {
  fill: var(--piece-detail);
}

.square.selected .piece {
  transform: translateY(-2px) scale(1.04);
}

.piece.dragging {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%) scale(1.1);
  cursor: grabbing;
  opacity: 0.95;
  filter: drop-shadow(0 10px 8px rgba(0, 0, 0, 0.5));
}

/* Mensagens de Feedback */
.feedback-msg {
  margin-top: 12px;
  font-weight: 600;
  font-size: 1.05rem;
  min-height: 24px;
  text-align: center;
}

.feedback-msg.success {
  color: var(--success);
}

.feedback-msg.error {
  color: var(--error);
}

/* Botões e Ações */
.game-controls {
  display: flex;
  gap: 12px;
  margin-top: 15px;
}

button {
  font-family: var(--font-body);
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 11px 22px;
  border: 1px solid #000;
  border-radius: 7px;
  font-weight: 700;
  cursor: pointer;
  transition:
    filter 0.15s,
    transform 0.05s;
}

button:active {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* Latão */
.btn-primary {
  background: linear-gradient(180deg, #e7b965 0%, #c48a34 55%, #9c6620 100%);
  color: #23140a;
  box-shadow:
    inset 0 1px 0 rgba(255, 245, 210, 0.6),
    0 3px 8px rgba(0, 0, 0, 0.5);
  text-shadow: 0 1px 0 rgba(255, 235, 190, 0.45);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

/* Madeira */
.btn-secondary {
  background: linear-gradient(180deg, rgba(255, 220, 170, 0.14), rgba(0, 0, 0, 0.2)), var(--grain), var(--wood-warm);
  color: var(--text-color);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.2),
    0 3px 8px rgba(0, 0, 0, 0.5);
}

.btn-secondary:hover {
  filter: brightness(1.15);
}

/* Responsividade Mobile */
@media (max-width: 850px) {
  :root {
    --sq: 78px;
  }

  .app-layout {
    flex-direction: column;
  }

  /* No celular o jogo vem primeiro; perfil e ranking ficam no fim */
  .sidebar {
    display: contents;
  }

  .game-container {
    order: 1;
    padding: 18px 12px;
  }

  #challenge-title {
    font-size: 1.4rem;
  }

  /* Botões de modo menores e com quebra de linha para caber lado a lado */
  .mode-tab {
    min-width: 0;
    white-space: normal;
    font-size: 0.82rem;
    gap: 6px;
    padding: 8px 10px;
  }

  .mate-levels {
    flex-direction: column;
    gap: 6px;
  }

  .mate-levels-list {
    gap: 4px;
  }

  .level-pill {
    min-width: 36px;
    padding: 4px 5px;
  }

  .profile-card {
    order: 2;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 15px;
  }

  .profile-card h3 {
    display: none;
  }

  .profile-card p {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #user-status {
    margin-bottom: 0;
  }

  .profile-card button,
  #btn-logout {
    margin-top: 0;
    padding: 7px 14px;
  }

  .ranking-card {
    order: 3;
  }

  .scoring-card {
    order: 4;
  }

  .ad-sidebar {
    order: 5;
    margin-top: 0;
  }

  .game-play-area {
    flex-direction: column;
  }

  .stat-box {
    flex: 1 1 0;
    padding: 6px 8px;
  }
}

/* Modais de Autenticação */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(5, 3, 2, 0.78);
  backdrop-filter: blur(3px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.modal-backdrop.hidden {
  display: none !important;
}

.modal-card {
  border-radius: 12px;
  padding: 26px;
  width: 90%;
  max-width: 380px;
  text-align: center;
}

.modal-card h3 {
  color: var(--gold);
  font-size: 1.15rem;
  margin-bottom: 18px;
}

.input-group {
  margin-bottom: 15px;
  text-align: left;
}

.input-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 5px;
  color: var(--text-muted);
}

.input-group input {
  width: 100%;
  padding: 9px 12px;
  font-family: var(--font-body);
  background-color: #0f0904;
  border: 1px solid #000;
  box-shadow: 0 0 0 1px rgba(212, 165, 87, 0.25) inset;
  border-radius: 6px;
  color: #fff;
  font-size: 1rem;
  outline: none;
  user-select: text;
}

.input-group input:focus {
  box-shadow: 0 0 0 1px var(--gold) inset;
}

.modal-buttons {
  display: flex;
  gap: 10px;
  margin-top: 15px;
}

.modal-buttons button {
  flex: 1;
}

.modal-error {
  color: var(--error);
  font-size: 0.85rem;
  margin-top: 8px;
  min-height: 18px;
}

.btn-link {
  background: none;
  border: none;
  box-shadow: none;
  color: var(--text-muted);
  margin-top: 10px;
  font-size: 0.8rem;
  text-transform: none;
  letter-spacing: 0;
  text-decoration: underline;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

/* Visível só para leitores de tela */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
