@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700;900&family=Inter:wght@300;400;500;600;700&display=swap');

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg0: #06060f;
  --bg1: #0c0c1e;
  --bg2: #13132a;
  --bg3: #1b1b38;
  --bg4: #242448;
  --surface: rgba(255,255,255,0.04);
  --surface-h: rgba(255,255,255,0.08);
  --border: rgba(255,255,255,0.07);
  --purple: #7c3aed;
  --purple-l: #a78bfa;
  --red: #ef4444;
  --gold: #d9a406;
  --green: #22c55e;
  --text: #e2e8f0;
  --text2: #94a3b8;
  --text3: #4b5563;
  --ff-head: 'Cinzel', serif;
  --ff-ui: 'Inter', sans-serif;
  --r: 12px;
  --r-lg: 18px;
  --shadow: 0 4px 24px rgba(0,0,0,0.6);
  /* tablero */
  --sq-light: #e8e0cf;
  --sq-dark: #8b7bb0;
  --hl-last: rgba(217, 164, 6, 0.42);
  --hl-sel: rgba(124, 58, 237, 0.55);
  --hl-check: rgba(239, 68, 68, 0.75);
}

html, body { height: 100%; }
body {
  background: var(--bg0);
  color: var(--text);
  font-family: var(--ff-ui);
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg1); }
::-webkit-scrollbar-thumb { background: var(--bg4); border-radius: 3px; }

.board-bg {
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(ellipse at 15% 85%, rgba(124,58,237,0.14) 0%, transparent 50%),
    radial-gradient(ellipse at 85% 15%, rgba(217,164,6,0.07) 0%, transparent 55%),
    radial-gradient(ellipse at 50% 50%, rgba(59,130,246,0.05) 0%, transparent 70%);
}
/* trama de damero sutil en el fondo */
.board-bg::after {
  content: ''; position: absolute; inset: 0; opacity: 0.025;
  background:
    repeating-conic-gradient(#fff 0% 25%, transparent 0% 50%);
  background-size: 90px 90px;
  -webkit-mask-image: radial-gradient(ellipse at 50% 30%, #000 0%, transparent 70%);
          mask-image: radial-gradient(ellipse at 50% 30%, #000 0%, transparent 70%);
}

/* ─── Top bar ─── */
.top-bar {
  position: relative; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; gap: 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(6,6,15,0.75); backdrop-filter: blur(8px);
}
.top-brand { color: var(--text2); text-decoration: none; font-weight: 600; font-size: 14px; white-space: nowrap; }
.top-brand:hover { color: var(--text); }
.top-title { font-family: var(--ff-head); font-weight: 700; letter-spacing: 6px; font-size: 16px; color: var(--purple-l); }

#app { position: relative; z-index: 1; max-width: 1120px; margin: 0 auto; padding: 18px 14px 60px; }

.loading { text-align: center; padding: 80px 0; color: var(--text2); }
.spinner {
  width: 44px; height: 44px; margin: 0 auto 14px; border-radius: 10px;
  background: conic-gradient(var(--sq-light) 0% 25%, var(--sq-dark) 0% 50%, var(--sq-light) 0% 75%, var(--sq-dark) 0%);
  background-size: 22px 22px; animation: spin 1.2s ease-in-out infinite;
}
@keyframes spin { 50% { transform: rotate(180deg) scale(0.85); } 100% { transform: rotate(360deg); } }

/* ─── Toast ─── */
#toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--bg3); color: var(--text); border: 1px solid var(--border);
  padding: 10px 22px; border-radius: 30px; font-size: 14px; opacity: 0;
  transition: all .25s; z-index: 100; pointer-events: none; box-shadow: var(--shadow);
  max-width: 90vw;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { border-color: var(--red); color: #fca5a5; }

/* ─── Botones ─── */
button {
  font-family: var(--ff-ui); font-size: 14px; font-weight: 600;
  border: none; border-radius: var(--r); cursor: pointer;
  transition: all .18s; color: var(--text);
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, var(--purple), #5b21b6);
  padding: 13px 26px; width: 100%;
  box-shadow: 0 4px 18px rgba(124,58,237,0.35);
}
.btn-primary:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 6px 24px rgba(124,58,237,0.5); }
.btn-secondary { background: var(--bg3); border: 1px solid var(--border); padding: 11px 20px; }
.btn-secondary:hover:not(:disabled) { background: var(--bg4); }
.btn-danger { background: rgba(239,68,68,0.12); border: 1px solid rgba(239,68,68,0.4); color: #fca5a5; padding: 11px 20px; }
.btn-danger:hover:not(:disabled) { background: rgba(239,68,68,0.22); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text2); padding: 8px 14px; font-size: 13px; }
.btn-ghost:hover:not(:disabled) { color: var(--text); border-color: var(--purple); }

/* ─── Landing ─── */
.landing-hero { text-align: center; padding: 40px 0 26px; }
.landing-hero h1 {
  font-family: var(--ff-head); font-size: clamp(34px, 7vw, 54px); font-weight: 900;
  background: linear-gradient(120deg, #f7f3e8 30%, var(--purple-l) 70%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  letter-spacing: 4px;
}
.landing-hero .tagline { color: var(--text2); font-size: 16px; margin-top: 4px; }
.hero-board {
  width: 132px; height: 132px; margin: 0 auto 18px; border-radius: 14px;
  background: conic-gradient(var(--sq-light) 0% 25%, var(--sq-dark) 0% 50%, var(--sq-light) 0% 75%, var(--sq-dark) 0%);
  background-size: 66px 66px;
  box-shadow: 0 0 60px rgba(124,58,237,0.35), var(--shadow);
  position: relative; overflow: hidden;
}
.hero-board .hero-piece { position: absolute; width: 66px; height: 66px; filter: drop-shadow(0 4px 6px rgba(0,0,0,0.5)); }

.action-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; max-width: 860px; margin: 0 auto; }
.action-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 24px; display: flex; flex-direction: column; gap: 10px;
  transition: border-color .2s, transform .2s;
}
.action-card:hover { border-color: var(--purple); transform: translateY(-2px); }
.action-card h3 { font-family: var(--ff-head); font-size: 17px; letter-spacing: 1px; }
.action-card p { color: var(--text2); font-size: 13.5px; flex: 1; }
.action-card input {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--r);
  color: var(--text); padding: 11px 14px; font-size: 16px; letter-spacing: 4px;
  text-align: center; text-transform: uppercase; font-weight: 700; width: 100%;
}
.action-card input:focus { outline: none; border-color: var(--purple); }
.action-card .btn-primary { padding: 12px 18px; }

/* ─── Lobby (sala de espera) ─── */
.lobby { display: grid; grid-template-columns: 1fr 340px; gap: 18px; align-items: start; }
@media (max-width: 860px) { .lobby { grid-template-columns: 1fr; } }
.panel {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 18px; margin-bottom: 14px;
}
.panel h4 { font-family: var(--ff-head); letter-spacing: 2px; font-size: 13px; color: var(--text2); margin-bottom: 12px; }
.lobby-code {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--bg2); border: 1px dashed rgba(124,58,237,0.5); border-radius: var(--r-lg);
  padding: 16px 20px; margin-bottom: 16px;
}
.lobby-code-label { color: var(--text2); font-size: 13px; }
.lobby-code-value { font-family: var(--ff-head); font-size: 26px; font-weight: 700; letter-spacing: 6px; color: var(--gold); }
.copy-btn { background: var(--bg3); border: 1px solid var(--border); padding: 8px 14px; font-size: 13px; border-radius: 8px; }

.seat-row { display: flex; flex-direction: column; gap: 10px; }
.seat {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r);
  padding: 12px 14px;
}
.seat.empty { border-style: dashed; color: var(--text3); justify-content: center; min-height: 62px; }
.seat.is-me { border-color: rgba(124,58,237,0.6); }
.seat .player-avatar {
  width: 38px; height: 38px; border-radius: 50%; object-fit: cover; flex-shrink: 0;
  background: var(--bg4); display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.seat .p-name { font-weight: 600; }
.seat .p-meta { font-size: 12px; color: var(--text2); }
.seat .host-tag, .seat .bot-tag {
  margin-left: auto; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 20px;
}
.seat .host-tag { color: var(--gold); background: rgba(217,164,6,0.12); }
.seat .bot-tag { color: var(--purple-l); background: rgba(124,58,237,0.15); }

.opt-group { margin-bottom: 14px; }
.opt-group .opt-label { font-size: 12.5px; color: var(--text2); margin-bottom: 7px; font-weight: 600; }
.opt-pills { display: flex; flex-wrap: wrap; gap: 7px; }
.opt-pill {
  background: var(--bg3); border: 1px solid var(--border); border-radius: 20px;
  padding: 7px 13px; font-size: 13px; color: var(--text2);
}
.opt-pill.sel { background: rgba(124,58,237,0.25); border-color: var(--purple); color: #fff; }

/* ─── Layout de partida ─── */
.game { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 18px; align-items: start; }
@media (max-width: 920px) { .game { grid-template-columns: 1fr; } }
.board-col { min-width: 0; max-width: 660px; margin: 0 auto; width: 100%; }

.player-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 4px;
}
.player-bar .player-avatar {
  width: 34px; height: 34px; border-radius: 9px; object-fit: cover; flex-shrink: 0;
  background: var(--bg4); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 14px;
}
.player-bar .pb-name { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-bar .pb-sub { font-size: 11.5px; color: var(--text2); }
.player-bar .pb-caps { display: flex; align-items: center; margin-left: 4px; min-width: 0; flex: 1; }
.player-bar .pb-caps .cap-svg { width: 19px; height: 19px; margin-left: -6px; filter: drop-shadow(0 1px 1px rgba(0,0,0,0.5)); }
.player-bar .pb-caps .mat-diff { font-size: 12px; color: var(--green); font-weight: 700; margin-left: 6px; }

.clock {
  margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; font-size: 21px;
  background: var(--bg2); border: 1px solid var(--border); border-radius: 10px;
  padding: 5px 14px; letter-spacing: 1px; color: var(--text2);
}
.clock.active { color: #fff; border-color: var(--purple); box-shadow: 0 0 14px rgba(124,58,237,0.35); }
.clock.low { color: #fecaca; border-color: var(--red); animation: clock-pulse 1s infinite; }
@keyframes clock-pulse { 50% { box-shadow: 0 0 16px rgba(239,68,68,0.6); } }

/* ─── Tablero ─── */
.board-wrap {
  position: relative; width: 100%; aspect-ratio: 1;
  border-radius: 10px; overflow: hidden;
  box-shadow: 0 0 0 3px var(--bg3), 0 0 50px rgba(124,58,237,0.22), var(--shadow);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.board-grid { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); }
.sq { position: relative; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq .coord {
  position: absolute; font-size: clamp(8px, 1.6vw, 11px); font-weight: 700; pointer-events: none; opacity: 0.7;
}
.sq .coord.file { right: 3px; bottom: 1px; }
.sq .coord.rank { left: 3px; top: 2px; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.sq .hl { position: absolute; inset: 0; pointer-events: none; }
.sq.hl-last .hl { background: var(--hl-last); }
.sq.hl-sel .hl { background: var(--hl-sel); }
.sq.hl-check .hl { background: radial-gradient(circle at 50% 50%, var(--hl-check) 15%, rgba(239,68,68,0.25) 60%, transparent 75%); }
.sq.hl-hint::after {
  content: ''; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  width: 30%; height: 30%; border-radius: 50%; background: rgba(20, 12, 40, 0.28);
}
.sq.hl-hint-cap::after {
  content: ''; position: absolute; inset: 6%;
  border-radius: 50%; border: 4px solid rgba(20, 12, 40, 0.30); background: transparent;
}
.sq.drop-target .hl { box-shadow: inset 0 0 0 4px rgba(124,58,237,0.75); }

.pieces-layer { position: absolute; inset: 0; pointer-events: none; }
.piece {
  position: absolute; width: 12.5%; height: 12.5%;
  transition: transform .16s ease-out;
  pointer-events: auto; cursor: default; z-index: 2;
  display: flex; align-items: center; justify-content: center;
}
.piece.mine { cursor: grab; }
.piece.dragging { transition: none; z-index: 10; cursor: grabbing; pointer-events: none; }
.piece .piece-svg { width: 92%; height: 92%; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.35)); }
.piece .piece-img, .piece-img { object-fit: contain; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.piece.dragging .piece-svg { transform: scale(1.15); filter: drop-shadow(0 8px 10px rgba(0,0,0,0.5)); }

/* revisión de jugadas pasadas */
.review-banner {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: rgba(217,164,6,0.12); border: 1px solid rgba(217,164,6,0.4);
  color: var(--gold); border-radius: var(--r); padding: 8px 12px; margin-top: 10px; font-size: 13px;
}

/* ─── Sidebar ─── */
.side-panel { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.moves-panel { max-height: 300px; display: flex; flex-direction: column; }
.moves-list { overflow-y: auto; font-size: 14px; flex: 1; font-variant-numeric: tabular-nums; }
.moves-list .mv-row { display: grid; grid-template-columns: 34px 1fr 1fr; padding: 2px 4px; border-radius: 6px; }
.moves-list .mv-row:nth-child(odd) { background: rgba(255,255,255,0.025); }
.moves-list .mv-no { color: var(--text3); }
.moves-list .mv {
  cursor: pointer; padding: 1px 8px; border-radius: 6px; font-weight: 500;
}
.moves-list .mv:hover { background: var(--bg3); }
.moves-list .mv.current { background: rgba(124,58,237,0.3); color: #fff; }
.moves-nav { display: flex; gap: 6px; margin-top: 10px; }
.moves-nav button { flex: 1; padding: 7px 0; font-size: 15px; }

.game-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.game-actions button { flex: 1 1 45%; }

.draw-offer-box {
  background: rgba(217,164,6,0.1); border: 1px solid rgba(217,164,6,0.45);
  border-radius: var(--r); padding: 12px; text-align: center; font-size: 13.5px;
}
.draw-offer-box .row { display: flex; gap: 8px; margin-top: 10px; }
.draw-offer-box .row button { flex: 1; }

/* Chat */
.chat-panel { display: flex; flex-direction: column; max-height: 260px; }
.chat-messages { flex: 1; overflow-y: auto; font-size: 13.5px; margin-bottom: 10px; min-height: 60px; }
.chat-msg { padding: 2px 0; word-wrap: break-word; }
.chat-msg .from { font-weight: 700; color: var(--purple-l); }
.chat-input { display: flex; gap: 8px; }
.chat-input input {
  flex: 1; background: var(--bg1); border: 1px solid var(--border); border-radius: var(--r);
  color: var(--text); padding: 9px 12px; font-size: 13.5px; min-width: 0;
}
.chat-input input:focus { outline: none; border-color: var(--purple); }
.chat-input button { padding: 9px 14px; background: var(--bg3); border: 1px solid var(--border); font-size: 13px; }

/* toggles de preferencias */
.pref-row { display: flex; align-items: center; justify-content: space-between; padding: 5px 0; font-size: 13.5px; color: var(--text2); }
.toggle { position: relative; width: 40px; height: 22px; background: var(--bg4); border-radius: 20px; cursor: pointer; transition: background .2s; flex-shrink: 0; }
.toggle::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--text2); transition: all .2s; }
.toggle.on { background: var(--purple); }
.toggle.on::after { left: 21px; background: #fff; }

/* ─── Modal de promoción ─── */
#promo-modal {
  position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center;
  background: rgba(6,6,15,0.75); backdrop-filter: blur(3px);
}
/* el display:flex de arriba pisa el atributo hidden del HTML (que solo es
   display:none de user-agent) — sin esto el velo queda SIEMPRE encima */
#promo-modal[hidden] { display: none !important; }
#promo-modal .promo-box {
  background: var(--bg2); border: 1px solid var(--purple); border-radius: var(--r-lg);
  padding: 20px; text-align: center; box-shadow: var(--shadow);
}
#promo-modal .promo-title { font-family: var(--ff-head); letter-spacing: 2px; font-size: 14px; margin-bottom: 14px; color: var(--text2); }
#promo-modal .promo-opts { display: flex; gap: 10px; }
#promo-modal .promo-opts button {
  width: 64px; height: 64px; background: var(--bg3); border: 1px solid var(--border); border-radius: var(--r);
  display: flex; align-items: center; justify-content: center; padding: 6px;
}
#promo-modal .promo-opts button:hover { border-color: var(--purple); background: var(--bg4); }
#promo-modal .promo-opts .piece-svg { width: 100%; height: 100%; }

/* ─── Fin de partida ─── */
.end-overlay {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(6,6,15,0.68); backdrop-filter: blur(2px);
  animation: fade-in .4s;
}
@keyframes fade-in { from { opacity: 0; } }
.end-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 26px 34px; text-align: center; box-shadow: var(--shadow); max-width: 92%;
  animation: pop-in .35s cubic-bezier(.2, 1.6, .4, 1);
}
@keyframes pop-in { from { transform: scale(0.7); opacity: 0; } }
.end-card.win { border-color: rgba(34,197,94,0.55); box-shadow: 0 0 50px rgba(34,197,94,0.18), var(--shadow); }
.end-card.loss { border-color: rgba(239,68,68,0.45); }
.end-card.draw { border-color: rgba(217,164,6,0.5); }
.end-card h2 { font-family: var(--ff-head); font-size: 24px; letter-spacing: 2px; margin-bottom: 4px; }
.end-card .end-reason { color: var(--text2); font-size: 14px; margin-bottom: 16px; }
.end-card .end-actions { display: flex; flex-direction: column; gap: 9px; }

@media (max-width: 560px) {
  #app { padding: 12px 8px 40px; }
  .clock { font-size: 18px; padding: 4px 10px; }
  .top-title { letter-spacing: 3px; font-size: 14px; }
}
