:root {
  --bg: #15161a;
  --panel: #1d1f25;
  --text: #ece8df;
  --muted: #8d8f99;
  --accent: #e9c46a;
  --check: #ff6b5b;
  --clock-on-bg: #f4f1e8;
  --clock-on-fg: #15161a;
  /* Espaço mínimo reservado aos painéis — o tabuleiro fica com o resto. */
  --panel-h: 52px;
  --panel-w: 120px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* "hidden" sempre esconde, mesmo em elementos com display próprio (labels, botões…). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* O tabuleiro pega o máximo possível; os painéis dividem a sobra.
   --board-ratio (largura/altura do tabuleiro na tela) é calculado pelo app. */
#board-wrap {
  position: relative;
  flex: 0 0 auto;
  width: 100%;
  height: min(calc(100vw / var(--board-ratio, 1.1)), calc(100vh - 2 * var(--panel-h)));
  height: min(calc(100vw / var(--board-ratio, 1.1)), calc(100dvh - 2 * var(--panel-h)));
  min-width: 0;
  min-height: 0;
}

#board-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---------- Painéis dos jogadores ---------- */
.panel {
  flex: 1 1 var(--panel-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 14px;
  background: var(--panel);
  transition: background .25s;
}
.panel.flip { transform: rotate(180deg); }
.panel.active { background: #2a2c33; }

.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.piece-icon { flex: 0 0 auto; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #8d8f99; }
.panel[data-color="w"] .piece-icon { background: #f1e6d0; }
.panel[data-color="b"] .piece-icon { background: #111; }
.name {
  font-size: clamp(16px, 2.4vmin, 24px);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status { color: var(--check); font-weight: 700; font-size: clamp(16px, 2.2vmin, 24px); }

.clock {
  font-variant-numeric: tabular-nums;
  font-size: clamp(22px, 3.8vmin, 38px);
  font-weight: 700;
  padding: 2px 12px;
  border-radius: 10px;
  background: #0f1013;
  color: var(--muted);
  min-width: 4.2em;
  text-align: center;
  transition: background .25s, color .25s;
}
.panel.active .clock { background: var(--clock-on-bg); color: var(--clock-on-fg); }
.panel.active .clock.low { background: var(--check); color: #fff; }
.clock.off { visibility: hidden; }

/* Paisagem: painéis viram colunas laterais */
@media (orientation: landscape) {
  body { flex-direction: row; }
  #board-wrap {
    height: 100%;
    width: min(calc(100vh * var(--board-ratio, 1.1)), calc(100vw - 2 * var(--panel-w)));
    width: min(calc(100dvh * var(--board-ratio, 1.1)), calc(100vw - 2 * var(--panel-w)));
  }
  .panel {
    flex: 1 1 var(--panel-w);
    flex-direction: column;
    justify-content: center;
    padding: 12px 8px;
  }
  .clock { min-width: 0; width: 100%; max-width: 4.2em; }
  .menu-btn { margin: 0; }
  .who { flex-direction: column; text-align: center; max-width: 100%; }
  .name { max-width: 100%; }
}

/* ---------- Botão de menu ---------- */
.menu-btn {
  flex: 0 0 auto;
  margin-left: auto;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: #33363f;
  color: #fff;
  font-size: 24px;
  line-height: 1;
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 11, 14, .72);
  backdrop-filter: blur(4px);
}
.overlay[hidden] { display: none; }
#setup { background: var(--bg); }

.card {
  width: min(460px, 100%);
  max-height: 100%;
  overflow: auto;
  background: var(--panel);
  border-radius: 18px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.card.flip { transform: rotate(180deg); }
.card h1 { margin: 0 0 6px; font-size: 32px; }
.card h2 { margin: 0; font-size: 26px; }
.card p { margin: 0; color: var(--muted); }

label { display: flex; flex-direction: column; gap: 6px; text-align: left; font-size: 14px; color: var(--muted); flex: 1; }
.row { display: flex; gap: 12px; }

input, select {
  font: inherit;
  font-size: 18px;
  color: var(--text);
  background: #0f1013;
  border: 1px solid #33363f;
  border-radius: 10px;
  padding: 12px;
}

fieldset { border: 1px solid #33363f; border-radius: 10px; padding: 8px 12px 12px; text-align: left; }
legend { color: var(--muted); font-size: 14px; padding: 0 6px; }
label.radio { flex-direction: row; align-items: center; gap: 10px; color: var(--text); font-size: 16px; padding: 6px 0; }
label.radio input { width: 20px; height: 20px; }

button {
  font: inherit;
  font-size: 18px;
  border: 0;
  border-radius: 12px;
  padding: 14px;
  background: #2c2f37;
  color: var(--text);
  cursor: pointer;
}
button:active { transform: scale(.98); }
button.primary { background: var(--accent); color: #1a1a1a; font-weight: 700; }
button.secondary { background: transparent; border: 1px solid #3a3d46; }
button.danger { color: var(--check); }

.menu button { width: 100%; }

/* Seletor 3D / 2D */
.seg { display: flex; gap: 8px; }
.seg label { flex: 1; flex-direction: row; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-radius: 10px;
  border: 2px solid #33363f;
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
}
.seg input:checked + span { border-color: var(--accent); background: #353843; }
.seg span { font-size: 16px; }
#bot-options { margin-top: 10px; }
#bot-options[hidden], #label-black[hidden] { display: none; }

/* ---------- Tela inicial: abas ---------- */
.card.home { width: min(520px, 100%); }
#setup-form { display: flex; flex-direction: column; gap: 14px; }
#setup [data-panel][hidden] { display: none; }
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tabs button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px; font-size: 22px; border: 2px solid transparent;
}
.tabs button span { font-size: 13px; color: var(--muted); }
.tabs button.on { border-color: var(--accent); background: #353843; }
.tabs button.on span { color: var(--text); }
section[data-panel] { display: flex; flex-direction: column; gap: 12px; }

.info-box, #stats { display: flex; flex-direction: column; gap: 6px; text-align: left; }
#stats h3 { margin-top: 10px; }
.stat {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 10px 12px; border-radius: 10px; background: #15161a;
}
.stat span { color: var(--muted); }
.stat.link { cursor: pointer; }

.lessons { display: flex; flex-direction: column; gap: 8px; }
.lesson { display: flex; align-items: center; gap: 12px; text-align: left; padding: 10px 12px; }
.lesson.done { opacity: .75; }
.lesson-icon { font-size: 28px; width: 36px; text-align: center; }
.lesson-body { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.lesson-body small { color: var(--muted); font-size: 13px; }
.bar { height: 6px; border-radius: 3px; background: #15161a; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); }

/* ---------- Sorteio ---------- */
.draw-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.draw-side {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 16px 8px; border-radius: 12px; background: #15161a; transition: transform .1s, box-shadow .3s;
}
.draw-side b { font-size: 20px; }
.draw-side small { color: var(--muted); font-size: 12px; }
.draw-side .piece-icon { width: 34px; height: 34px; }
.draw-side.spin { transform: scale(.94); }
.draw-side.winner { box-shadow: 0 0 0 3px var(--accent); }
.piece-icon.w { background: #f1e6d0; }
.piece-icon.b { background: #111; }

/* ---------- Avisos ---------- */
#sheet .card { text-align: center; }
#sheet-text { font-size: 17px; color: var(--text); line-height: 1.4; }
.sheet-buttons { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.sheet-buttons button { flex: 1 1 160px; }
#result-extra { color: var(--muted); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--panel-h) + 24px); transform: translateX(-50%);
  z-index: 30; max-width: 90vw; padding: 10px 18px; border-radius: 999px;
  background: rgba(20, 21, 26, .92); color: #fff; font-size: 16px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  pointer-events: none;
}
#toast[hidden] { display: none; }
#btn-hint + #btn-menu { margin-left: 0; }

/* ---------- Rodapé: créditos e doação ---------- */
.credits {
  margin-top: 6px; padding-top: 14px; border-top: 1px solid #33363f;
  display: flex; flex-direction: column; gap: 10px; font-size: 14px; color: var(--muted);
}
.credits p { margin: 0; }
.credits a { color: var(--accent); text-decoration: none; }
.credits a:hover { text-decoration: underline; }
.donate { display: flex; flex-direction: column; gap: 8px; }
.pix {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px; background: #15161a; border: 1px dashed #4a4d57;
}
.pix span { font-size: 16px; font-weight: 700; color: var(--text); }
.pix small { color: var(--muted); }

/* ---------- Online ---------- */
#online-options[hidden] { display: none; }
#online-options { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.hint-text { margin: 0; font-size: 14px; color: var(--muted); text-align: left; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: .3em; font-weight: 700; text-align: center; }
.room-code {
  font-size: 48px; font-weight: 800; letter-spacing: .25em; padding: 10px;
  border-radius: 14px; background: #15161a; color: var(--accent);
}
.waiting-dots { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- Revisão da partida ---------- */
body.reviewing { --panel-h: 124px; --panel-w: 240px; }
body:not(.reviewing) .review-info,
body:not(.reviewing) .review-bar { display: none; }
body.reviewing .panel > :not(.review-info):not(.review-bar) { display: none; }
body.reviewing .panel { justify-content: center; }
.review-info { width: 100%; text-align: center; font-size: clamp(15px, 2.2vmin, 20px); line-height: 1.35; }
.review-info .tag { font-weight: 700; }
.review-bar { width: 100%; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.review-moves {
  display: flex; gap: 4px; overflow-x: auto; padding-bottom: 2px;
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.review-moves::-webkit-scrollbar { display: none; }
.review-moves .no { color: var(--muted); font-size: 13px; align-self: center; padding-left: 4px; }
.review-moves button {
  flex: 0 0 auto; padding: 6px 9px; font-size: 15px; border-radius: 8px;
  background: #15161a; border: 2px solid transparent;
}
.review-moves button.on { border-color: var(--accent); background: #353843; }
.review-controls { display: flex; gap: 6px; }
.review-controls button { flex: 1; padding: 10px 4px; font-size: 18px; }
.review-controls .rv-analyze, .review-controls .rv-exit { flex: 1.6; font-size: 15px; }
/* Professor falando */
.coach { display: flex; align-items: flex-start; gap: 10px; text-align: left; max-width: 640px; margin: 0 auto; }
.coach-face { font-size: 34px; line-height: 1; }
.bubble {
  position: relative; flex: 1; background: #f4f1e8; color: #1b1b1b;
  border-radius: 14px; padding: 8px 12px; font-size: clamp(14px, 2vmin, 17px);
}
.bubble::before {
  content: ''; position: absolute; left: -8px; top: 12px;
  border: 8px solid transparent; border-right-color: #f4f1e8; border-left: 0;
}
.bubble small { color: #555; }
.bubble .tag { font-weight: 800; white-space: nowrap; }
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin-top: 4px; font-size: 13px; }
.q-icon {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 3px; margin: 0 2px 0 4px;
  border-radius: 9px; color: #fff; font-size: 11px; font-weight: 800; vertical-align: middle;
}
body.reviewing { --panel-h: 136px; }
@media (orientation: landscape) {
  .coach { flex-direction: column; align-items: center; }
  .bubble::before { display: none; }
  .summary { grid-template-columns: 1fr; }
}

/* Barra de avaliação */
#eval-bar {
  position: absolute; left: 4px; top: 6%; bottom: 6%; width: 14px; z-index: 2;
  border-radius: 4px; overflow: hidden; background: #3a3a3a; box-shadow: 0 0 0 1px rgba(0,0,0,.4);
  display: flex; flex-direction: column; justify-content: flex-end;
}
#eval-bar[hidden] { display: none; }
#eval-bar .fill { background: #f4f1e8; transition: height .4s ease; }
#eval-bar.flip { justify-content: flex-start; }
#eval-bar .val {
  position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%);
  font-size: 9px; font-weight: 800; text-align: center; color: #888; writing-mode: vertical-rl;
}

/* Classificação dos lances */
.q-best { color: #5ad1ff; }
.q-good { color: #7ccf7c; }
.q-inacc { color: #f4d35e; }
.q-mistake { color: #f29e4c; }
.q-blunder { color: #ff5c5c; }
@media (orientation: landscape) {
  .review-bar { height: 100%; justify-content: center; }
  .review-moves { flex-wrap: wrap; overflow: auto; max-height: 60vh; }
  .review-controls { flex-wrap: wrap; }
  .review-controls button { flex: 1 1 40%; }
}

/* ---------- Temas ---------- */
.overlay.sheet { align-items: flex-end; background: rgba(10, 11, 14, .25); backdrop-filter: none; z-index: 20; }
.card.wide { width: min(760px, 100%); max-height: 62vh; text-align: left; }
.card h3 { margin: 4px 0 0; font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  font-size: 15px;
  text-align: left;
  border: 2px solid transparent;
}
.chip.on { border-color: var(--accent); background: #353843; }
.swatch {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  border: 4px solid;
  background-size: 50% 50%;
}
.swatch.round { border-radius: 50%; border: 1px solid #555; }
.preview { width: 38px; height: 38px; flex: 0 0 auto; }

.promo-row { display: flex; gap: 10px; justify-content: center; }
.promo-row button { font-size: 52px; width: 84px; height: 84px; padding: 0; line-height: 1; }
