:root {
  --bg: #17110c;
  --bg-panel: #241a13;
  --bg-panel-2: #2c2015;
  --border: #3d2e1f;
  --text: #f1e9db;
  --text-muted: #b9a98f;
  --accent: #d8a63d;
  --accent-2: #c9884a;
  --success: #5eab68;
  --danger: #c25a4a;
  --info: #6f9bd8;
  --radius: 14px;
  --font-head: 'Cinzel', 'Georgia', serif;
  --font-body: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* El atributo `hidden` debe ganar siempre a cualquier `display` con más
   especificidad (ids, clases) declarado más abajo en esta hoja de estilos;
   si no, elementos como los modales o el panel de resultado quedarían
   visibles aunque JS les ponga `hidden`. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  min-height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

a { color: var(--accent); }

.card {
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

/* ---------- Header ---------- */
.site-header {
  text-align: center;
  padding: calc(env(safe-area-inset-top, 0) + 14px) 16px 4px;
}
.site-header h1 {
  font-family: var(--font-head);
  font-size: clamp(1.5rem, 6vw, 2.1rem);
  margin: 0 0 2px;
  letter-spacing: 0.02em;
}
.site-header h1 .accent { color: var(--accent); }
.tagline {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.88rem;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 10px 12px 20px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

/* ---------- Cómo jugar ---------- */
.howto h2 { font-family: var(--font-head); font-size: 1.05rem; margin: 0 0 6px; }
.howto summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
  padding: 4px 0;
  touch-action: manipulation;
}
.howto p, .howto li { font-size: 0.88rem; line-height: 1.45; color: var(--text-muted); }
.howto ul { margin: 8px 0 0; padding-left: 1.2em; }
.howto strong { color: var(--text); }

/* ---------- Game area ---------- */
.game-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#board-container {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 62vh;
  border-radius: var(--radius);
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 30%, #2a2015, #120c08 80%);
  border: 1px solid var(--border);
  touch-action: none;
  position: relative;
}
#board-container canvas { display: block; touch-action: none; }

/* ---------- UI panel ---------- */
.ui-panel { display: flex; flex-direction: column; gap: 10px; }

#puzzle-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}
#puzzle-number {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
}
.pill {
  background: #000000aa;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.82rem;
  color: var(--text);
}

/* ---------- Header actions ---------- */
#header-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.icon-btn {
  min-height: 38px;
  min-width: 38px;
  padding: 6px 10px;
  font-size: 1rem;
  line-height: 1;
  border-radius: 999px;
  /* Los emojis del sistema traen su propia paleta multicolor; se atenúa
     su saturación para que la fila de iconos no rompa la paleta cálida
     y restringida del resto de la interfaz. */
  filter: saturate(0.6) contrast(0.92);
  opacity: 0.88;
}
.icon-btn:hover, .icon-btn:focus-visible { filter: saturate(0.85); opacity: 1; }
.icon-btn[aria-pressed="true"] { border-color: var(--accent); background: var(--bg-panel); filter: saturate(0.85); opacity: 1; }

/* ---------- Practice banner ---------- */
#practice-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  background: #00000040;
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
}
#practice-banner button { padding: 8px 10px; min-height: 36px; font-size: 0.82rem; }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
  overflow-y: auto;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal h2 {
  font-family: var(--font-head);
  color: var(--accent);
  margin: 0;
  font-size: 1.1rem;
}
.modal h3 { font-family: var(--font-head); font-size: 0.95rem; margin: 4px 0 0; }
.modal p, .modal li { font-size: 0.88rem; line-height: 1.45; color: var(--text-muted); }
.modal ul { margin: 0; padding-left: 1.2em; }
.modal strong { color: var(--text); }
.modal > button { align-self: flex-start; }
.stats-note { font-size: 0.78rem; font-style: italic; }

.stats-summary {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.stat-tile {
  background: #00000040;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
}
.stat-num {
  display: block;
  font-family: var(--font-head);
  font-size: 1.4rem;
  color: var(--accent);
}
.stat-label { font-size: 0.76rem; color: var(--text-muted); }

.stats-distribution { display: flex; flex-direction: column; gap: 6px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; }
.dist-label { width: 2.4em; flex-shrink: 0; color: var(--text-muted); }
.dist-bar-track {
  flex: 1;
  background: #00000040;
  border-radius: 6px;
  overflow: hidden;
  height: 18px;
}
.dist-bar-fill {
  height: 100%;
  min-width: 2px;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
}
.dist-count { width: 1.6em; text-align: right; flex-shrink: 0; }

#archive-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 48vh;
  overflow-y: auto;
}
.archive-item {
  text-align: left;
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: unset;
  display: block;
}
.archive-item h3 { margin: 0 0 3px; font-family: var(--font-head); font-size: 0.92rem; color: var(--accent); }
.archive-item p { margin: 0; font-size: 0.78rem; color: var(--text-muted); }
.archive-item p.archive-teaser { margin-top: 3px; color: var(--text); }

#attempts-row { display: flex; gap: 6px; }
.attempt-dot {
  width: 20px; height: 20px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: #00000055;
  flex-shrink: 0;
}
.attempt-dot.used-wrong { background: var(--danger); border-color: var(--danger); }
.attempt-dot.used-correct { background: var(--success); border-color: var(--success); }
.attempt-dot.current { border-color: var(--accent); }

#status-message {
  min-height: 1.2em;
  font-size: 0.88rem;
  color: var(--text);
}
#status-message.error { color: var(--danger); font-weight: 600; }
#status-message.ok { color: var(--success); font-weight: 600; }

#hint-panel { font-size: 0.85rem; color: var(--text-muted); display: flex; flex-direction: column; gap: 4px; }
#hint-panel .hint-item { background: #00000040; border-left: 3px solid var(--accent); padding: 6px 10px; border-radius: 6px; }

#move-controls { display: flex; gap: 10px; }

button {
  font-family: var(--font-body);
  font-size: 0.92rem;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 12px 14px;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  background: var(--bg-panel-2);
  color: var(--text);
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.4; cursor: default; }

#btn-confirm {
  flex: 1;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: #241a13;
  border-color: var(--accent-2);
}
#btn-undo { background: var(--bg-panel-2); }

#promotion-overlay {
  background: #000000cc;
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
}
#promotion-overlay p { margin: 0 0 8px; font-size: 0.85rem; }
.promo-choices { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.promo-choices button { flex: 1; min-width: 68px; }

/* ---------- Result panel ---------- */
#result-panel { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--border); padding-top: 10px; }
#result-title { font-family: var(--font-head); font-size: 1.15rem; margin: 0; color: var(--accent); }
#result-players { margin: 0; color: var(--text-muted); font-size: 0.88rem; }
#result-context p { font-size: 0.9rem; line-height: 1.5; margin: 0 0 8px; }
#result-continuation { font-size: 0.85rem; color: var(--text-muted); font-style: italic; }
#share-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
#share-feedback { font-size: 0.82rem; color: var(--success); }
#next-puzzle-countdown { font-size: 0.85rem; color: var(--text-muted); }

/* ---------- Footer ---------- */
.site-footer {
  text-align: center;
  padding: 10px 16px calc(env(safe-area-inset-bottom, 0) + 16px);
  color: var(--text-muted);
  font-size: 0.76rem;
  max-width: 720px;
  margin: 0 auto;
}
.site-footer p { margin: 4px 0; }

/* ---------- Responsive tweaks ---------- */
@media (min-width: 640px) {
  .game-area { flex-direction: row; align-items: flex-start; }
  #board-container { max-width: 60%; max-height: none; }
  .ui-panel { flex: 1; }
  /* En pantallas anchas el contenido (tarjeta + tablero) es más bajo que
     el viewport; centrarlo evita un hueco vacío grande antes del pie de
     página y da al layout una composición deliberada en vez de un bloque
     pegado arriba con espacio muerto debajo. */
  main { justify-content: center; }
}

@media (min-width: 960px) {
  main { max-width: 880px; gap: 16px; }
  #board-container { max-width: 56%; }
}

@media (max-width: 340px) {
  .site-header h1 { font-size: 1.3rem; }
  button { font-size: 0.85rem; padding: 10px 8px; }
}

/* ---------- Tema claro ---------- */
:root[data-theme="light"] {
  --bg: #f5efe1;
  --bg-panel: #fffaf1;
  --bg-panel-2: #f1e7d4;
  --border: #d8c6a3;
  --text: #2b2013;
  --text-muted: #6b5a3f;
  --accent: #9c6a1f;
  --accent-2: #7e551a;
  --success: #2f8f47;
  --danger: #b3392a;
  --info: #2f6fb2;
  color-scheme: light;
}
:root[data-theme="light"] #board-container {
  background: radial-gradient(ellipse at 50% 30%, #efe4c8, #cbb98f 80%);
}
:root[data-theme="light"] .pill,
:root[data-theme="light"] .attempt-dot,
:root[data-theme="light"] #hint-panel .hint-item,
:root[data-theme="light"] .stat-tile,
:root[data-theme="light"] .dist-bar-track,
:root[data-theme="light"] #practice-banner {
  background: #00000012;
}
:root[data-theme="light"] .attempt-dot.used-wrong { background: var(--danger); }
:root[data-theme="light"] .attempt-dot.used-correct { background: var(--success); }
:root[data-theme="light"] #promotion-overlay {
  background: #fffaf1ee;
  border-color: var(--accent);
}
:root[data-theme="light"] #btn-confirm { color: #fffaf1; }
:root[data-theme="light"] .modal-backdrop { background: rgba(40, 30, 12, 0.5); }
