:root {
  color-scheme: dark; /* la página ya es oscura: evita que el navegador la "oscurezca" e invierta las piezas */
  --bg: #1f2126;
  --panel: #2a2d34;
  --text: #e8e6e3;
  --muted: #9aa0a6;
  --light: #eed9b6;
  --dark: #b08662;
  --accent: #6fbf73;
  --size: min(92vw, 78vh, 600px);
  --gutter: 16px;
}
@supports (height: 100dvh) {
  :root { --size: min(92vw, 78dvh, 600px); }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
           env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  -webkit-tap-highlight-color: transparent;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app {
  display: flex;
  gap: 24px;
  padding: 16px;
  align-items: flex-start;
  flex-wrap: wrap;
  justify-content: center;
}

/* ---------- Tablero ---------- */

.board {
  width: var(--size);
  height: var(--size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none; /* el dedo arrastra piezas en vez de mover la página */
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.last.light { background: #f3e37a; }
.sq.last.dark { background: #d4b84a; }
.sq.selected { box-shadow: inset 0 0 0 4px rgba(40, 120, 220, .8); }
.sq.check { background: radial-gradient(circle, #ff4d4d 0%, #e02020 40%, transparent 80%) !important; }

.sq.move::after {
  content: "";
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(20, 20, 20, .28);
  position: absolute;
  pointer-events: none;
}
.sq.capture::after {
  content: "";
  position: absolute;
  inset: 4%;
  border-radius: 50%;
  border: 5px solid rgba(20, 20, 20, .28);
  pointer-events: none;
}

.coord {
  position: absolute;
  font-size: calc(var(--size) / 44);
  font-weight: 700;
  pointer-events: none;
}
.coord.rank { top: 3px; left: 4px; }
.coord.file { bottom: 2px; right: 5px; }
.sq.light .coord { color: var(--dark); }
.sq.dark .coord { color: var(--light); }

.piece {
  font-size: calc(var(--size) / 9.5);
  line-height: 1;
  cursor: grab;
  transition: transform .1s;
  z-index: 1;
}
.piece:active { cursor: grabbing; }
.piece.lifted { opacity: .25; }
.piece.ghost {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  font-size: calc(var(--size) / 7.5);
  transform: translate(-50%, -60%);
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, .45));
}
.piece.shake { animation: shake .35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}
.piece {
  forced-color-adjust: none;
}
.piece.w {
  color: #fff;
  text-shadow: 0 0 1px #000, 0 0 1px #000, 0 0 2px #000, 0 2px 3px rgba(0, 0, 0, .4);
}
.piece.b {
  color: #1a1a1a;
  text-shadow: 0 0 1px #fff8, 0 2px 3px rgba(0, 0, 0, .4);
}

/* ---------- Jugadores ---------- */

.player {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 36px;
  padding: 0 4px;
  font-size: 15px;
}
.player .name { font-weight: 600; }
.player .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: transparent;
}
.player.active .dot { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.player .caps { font-size: 20px; letter-spacing: -3px; color: var(--muted); }
.player .adv { color: var(--muted); font-size: 13px; }

/* ---------- Panel ---------- */

.panel {
  width: 340px;
  background: var(--panel);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(var(--size) + 72px);
  overflow-y: auto;
}
.section { display: flex; flex-direction: column; gap: 12px; }
.section[hidden] { display: none; }
.panel h1 { margin: 0; font-size: 24px; }
.panel h2 { margin: 6px 0 0; font-size: 15px; color: var(--muted); font-weight: 600; }

.status {
  background: #1b1d22;
  border-radius: 6px;
  padding: 10px 12px;
  font-weight: 600;
  min-height: 42px;
}
.status.over { color: #ffd166; }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
select, button {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: #3a3e47;
  border: 1px solid #4a4f5a;
  border-radius: 6px;
  padding: 8px 10px;
}
button { cursor: pointer; touch-action: manipulation; }
@media (hover: hover) {
  button:hover, select:hover { background: #454a55; }
  }

.buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.buttons button { flex: 1 1 auto; }

.moves {
  margin: 0;
  padding: 0 0 0 34px;
  overflow-y: auto;
  min-height: 60px;
  max-height: 160px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 14px;
}
.moves li { padding: 2px 0; color: var(--muted); }
.moves span { display: inline-block; width: 80px; color: var(--text); }
.moves span.current { color: var(--accent); font-weight: 700; }

/* ---------- Profe ---------- */

.coach {
  background: linear-gradient(180deg, #34394a, #2d3140);
  border: 2px solid #4b5372;
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.coach-head { display: flex; align-items: center; gap: 10px; }
.avatar {
  font-size: 40px;
  line-height: 1;
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  background: #eed9b6;
  border-radius: 50%;
}
.avatar.talking { animation: bob .5s ease-in-out 3; }
@keyframes bob { 50% { transform: translateY(-5px) rotate(-6deg); } }
.coach-name { font-weight: 800; font-size: 17px; flex: 1; }
.coach-tools { display: flex; gap: 4px; }
.icon-btn { padding: 4px 7px; font-size: 16px; background: transparent; border-color: transparent; }
.icon-btn.off { opacity: .35; }
.bubble {
  background: #fff;
  color: #222;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 18px;
  line-height: 1.35;
  font-weight: 600;
  min-height: 56px;
  position: relative;
}
.bubble::before {
  content: "";
  position: absolute;
  top: -8px;
  left: 20px;
  border: 8px solid transparent;
  border-top: 0;
  border-bottom-color: #fff;
}
.coach-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.coach-actions:empty { display: none; }
.coach-actions button, .hint-btn {
  flex: 1 1 auto;
  font-size: 16px;
  font-weight: 700;
  padding: 10px 12px;
  border-radius: 10px;
}
.coach-actions button { background: #4c7ef3; border-color: #4c7ef3; color: #fff; }
.coach-actions button.secondary { background: #3a3e47; border-color: #4a4f5a; }
.hint-btn { background: #f5b82e; border-color: #f5b82e; color: #3a2a00; }
.hint-btn:disabled { opacity: .4; cursor: default; }

.sq.coach { box-shadow: inset 0 0 0 5px #f5b82e, inset 0 0 22px rgba(245, 184, 46, .9); }
.sq.coach .piece { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.12); } }

.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }

/* ---------- Pestañas y lecciones ---------- */

.tabs { display: flex; gap: 6px; }
.tab { flex: 1; font-size: 16px; font-weight: 700; padding: 10px; }
.tab.active { background: #4c7ef3; border-color: #4c7ef3; color: #fff; }

.lessons { display: flex; flex-direction: column; gap: 8px; }
.lesson {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 12px;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 700;
}
.lesson .icon { font-size: 30px; }
.lesson .info { flex: 1; }
.stars { color: #f5b82e; font-size: 15px; letter-spacing: 1px; }
.stars .empty { color: #5a606c; }
.lesson.done { border-color: #f5b82e; }

.puzzle-head { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 800; }
.puzzle-head .icon { font-size: 32px; }
.dots { display: flex; gap: 6px; }
.dots span { width: 16px; height: 16px; border-radius: 50%; background: #454a55; }
.dots span.solved { background: #f5b82e; }
.dots span.current { outline: 3px solid #4c7ef3; }

/* ---------- Festejo ---------- */

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 20; }
.confetti span {
  position: absolute;
  bottom: -60px;
  animation: rise 2.2s ease-out forwards;
}
@keyframes rise {
  to { transform: translateY(-115vh) rotate(540deg); opacity: .2; }
}

/* ---------- Partidas guardadas ---------- */

.saves {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 160px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.saves li {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #1b1d22;
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
}
.saves .info { flex: 1; min-width: 0; }
.saves .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saves .meta { color: var(--muted); font-size: 11px; }
.saves button { padding: 4px 8px; font-size: 12px; }
.saves .empty { color: var(--muted); background: none; padding: 0; }

/* ---------- Coronación ---------- */

.promo {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.promo.hidden { display: none; }
.promo-box {
  background: var(--panel);
  padding: 18px 22px;
  border-radius: 10px;
  text-align: center;
}
.promo-box p { margin: 0 0 12px; }
.promo-choices { display: flex; gap: 8px; }
.promo-choices button {
  width: 70px;
  height: 70px;
  font-size: 48px;
  line-height: 1;
  padding: 0;
  background: var(--light);
}

/* ---------- Celular vertical y tablets angostas ---------- */

@media (max-width: 760px) {
  :root {
    --gutter: 10px;
    --size: min(calc(100vw - 2 * var(--gutter)), 600px);
  }
  body { align-items: flex-start; }
  .app {
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: center;
    gap: 10px;
    padding: 8px var(--gutter) 24px;
    width: 100%;
  }
  .panel {
    width: var(--size);
    max-height: none;
    overflow: visible;
    padding: 12px;
    gap: 10px;
  }
  .player { height: 30px; font-size: 14px; gap: 8px; }
  .player .caps { font-size: 16px; }
  .player .name { white-space: nowrap; }
  .player .caps { overflow: hidden; white-space: nowrap; min-width: 0; }
  .coach { padding: 10px; gap: 8px; }
  .avatar { font-size: 32px; width: 44px; height: 44px; }
  .coach-name { font-size: 15px; }
  .bubble { font-size: 16px; padding: 10px 12px; min-height: 0; }
  .coach-actions button, .hint-btn { font-size: 15px; padding: 10px; min-height: 44px; }
  .tab { font-size: 15px; min-height: 44px; }
  select, .buttons button { min-height: 44px; font-size: 16px; } /* 16px evita el zoom automático de iOS */
  .moves { max-height: 140px; }
  .lesson { padding: 10px; font-size: 16px; }
  .lesson .icon { font-size: 26px; }
  .promo-choices button { width: 60px; height: 60px; font-size: 42px; }
}

/* Celular muy angosto (iPhone SE, Android chicos) */
@media (max-width: 360px) {
  .coach-tools .icon-btn { padding: 4px; font-size: 14px; }
  .bubble { font-size: 15px; }
}

/* ---------- Celular horizontal: tablero y panel lado a lado ---------- */

@media (max-height: 520px) and (orientation: landscape) {
  :root { --size: min(calc(100vh - 76px), 55vw); }
  @supports (height: 100dvh) {
    :root { --size: min(calc(100dvh - 76px), 55vw); }
  }
  body { align-items: flex-start; }
  .app {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: flex-start;
    gap: 12px;
    padding: 6px 12px;
    width: 100%;
    justify-content: center;
  }
  .player { height: 30px; font-size: 13px; }
  .player .caps { font-size: 15px; }
  .panel {
    flex: 1;
    min-width: 0;
    max-width: 420px;
    max-height: calc(100vh - 12px);
    max-height: calc(100dvh - 12px);
    padding: 10px;
    gap: 8px;
  }
  .avatar { font-size: 28px; width: 38px; height: 38px; }
  .bubble { font-size: 15px; padding: 8px 10px; min-height: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .avatar.talking, .sq.coach .piece, .piece.shake, .confetti span { animation: none; }
}
