/* ── Reset & tokens ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #F3F2EF;
  --surface:    #FFFFFF;
  --border:     #E0DFDC;
  --text:       #191919;
  --text-2:     #666666;
  --blue:       #0A66C2;
  --blue-dark:  #004182;
  --blue-light: #EEF3FB;
  --green:      #057642;
  --gold:       #F5A623;
  --gold-dim:   #D4CEBC;
  --path-clr:   #0A66C2;
  --radius:     10px;
  --sh:         0 2px 8px rgba(0,0,0,.10);
  --sh-md:      0 4px 20px rgba(0,0,0,.14);
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); -webkit-tap-highlight-color: transparent; overflow: hidden; }
#app { height: 100%; position: relative; }

/* ── Screens ── */
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; opacity: 0; pointer-events: none; transition: opacity .22s ease, transform .22s ease; transform: translateY(10px); overflow-y: auto; }
.screen.active { opacity: 1; pointer-events: all; transform: none; }

/* ── Shared header ── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; height: 56px; flex-shrink: 0;
  position: sticky; top: 0; z-index: 10;
}
.header-logo { display: flex; align-items: center; gap: 8px; font-size: 18px; font-weight: 700; }
.logo-icon { width: 32px; height: 32px; background: var(--blue); border-radius: 6px; display: flex; align-items: center; justify-content: center; color: #fff; flex-shrink: 0; }
.logo-icon svg { width: 18px; height: 18px; }
.logo-icon.sm { width: 26px; height: 26px; border-radius: 5px; }
.logo-icon.sm svg { width: 14px; height: 14px; }

.btn-icon { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border: none; background: transparent; border-radius: 50%; cursor: pointer; color: var(--text); transition: background .15s; }
.btn-icon:hover { background: var(--bg); }
.btn-icon svg { width: 20px; height: 20px; }

/* ══════════════════════════════════════════
   WELCOME SCREEN
══════════════════════════════════════════ */
#screen-welcome { justify-content: center; align-items: center; background: var(--bg); }

.welcome-wrap {
  width: min(420px, calc(100% - 32px));
  background: var(--surface);
  border-radius: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--sh-md);
  padding: 40px 32px 36px;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
}

.welcome-logo {
  display: flex; align-items: center; gap: 10px;
  font-size: 28px; font-weight: 800; color: var(--text);
}
.welcome-logo .logo-icon { width: 44px; height: 44px; border-radius: 10px; }
.welcome-logo .logo-icon svg { width: 24px; height: 24px; }

.welcome-title { font-size: 22px; font-weight: 700; text-align: center; color: var(--text); }
.welcome-sub { font-size: 14px; color: var(--text-2); text-align: center; line-height: 1.55; }

.name-field { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.name-label { font-size: 14px; font-weight: 600; color: var(--text); }
.name-input {
  width: 100%; height: 48px; border: 1.5px solid var(--border);
  border-radius: var(--radius); padding: 0 14px;
  font-size: 16px; color: var(--text); background: var(--bg);
  outline: none; transition: border-color .15s, box-shadow .15s;
  font-family: inherit;
}
.name-input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(10,102,194,.15); background: var(--surface); }
.name-input.error { border-color: #CC1016; box-shadow: 0 0 0 3px rgba(204,16,22,.12); }

.btn-play {
  width: 100%; height: 52px; background: var(--blue); color: #fff;
  border: none; border-radius: 26px; font-size: 17px; font-weight: 700;
  cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px;
  transition: background .15s, transform .1s; letter-spacing: .2px;
}
.btn-play:hover { background: var(--blue-dark); }
.btn-play:active { transform: scale(.97); }
.btn-play svg { width: 18px; height: 18px; }

.btn-ranking-link {
  background: none; border: none; color: var(--blue);
  font-size: 14px; font-weight: 600; cursor: pointer;
  padding: 4px 8px; border-radius: 6px; transition: background .15s;
}
.btn-ranking-link:hover { background: var(--blue-light); }

/* ══════════════════════════════════════════
   GAME SCREEN
══════════════════════════════════════════ */
.game-header {
  background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; height: 56px; flex-shrink: 0;
  position: sticky; top: 0; z-index: 10;
}

.player-name-chip {
  font-size: 15px; font-weight: 700; color: var(--text);
  max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.timer-box {
  display: flex; align-items: center; gap: 5px;
  font-size: 20px; font-weight: 800; color: var(--blue);
  font-variant-numeric: tabular-nums; min-width: 68px; justify-content: flex-end;
}
.timer-box svg { width: 18px; height: 18px; flex-shrink: 0; }
.timer-box.running { color: var(--blue); }

.game-body {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; padding: 20px 16px 28px; gap: 16px;
}

.grid-outer { display: flex; align-items: center; justify-content: center; width: 100%; }

.grid-wrapper {
  position: relative; border-radius: 14px; overflow: hidden;
  box-shadow: var(--sh-md); border: 2px solid var(--border);
  background: var(--surface); touch-action: none; user-select: none;
}

.grid { display: grid; position: relative; z-index: 1; }

.grid-cell {
  background: var(--surface); border: 1px solid #E8E8E8;
  position: relative;
}

.path-svg {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 2;
}

/* overlay antes de iniciar */
.grid-overlay {
  position: absolute; inset: 0; z-index: 10;
  background: rgba(255,255,255,.88);
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  border-radius: 12px;
  transition: opacity .25s;
}
.grid-overlay.hidden { opacity: 0; pointer-events: none; }
.overlay-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.overlay-hint { font-size: 18px; font-weight: 700; color: var(--text-2); }

.btn-start {
  height: 52px; padding: 0 32px;
  background: var(--blue); color: #fff;
  border: none; border-radius: 26px;
  font-size: 17px; font-weight: 700;
  cursor: pointer; display: flex; align-items: center; gap: 8px;
  transition: background .15s, transform .1s;
}
.btn-start:hover { background: var(--blue-dark); }
.btn-start:active { transform: scale(.97); }
.btn-start svg { width: 16px; height: 16px; }

.game-hint {
  font-size: 14px; color: var(--text-2); text-align: center; min-height: 20px;
}
.game-hint strong { color: var(--blue); }

.game-controls { display: flex; }

.btn-outline {
  background: transparent; color: var(--blue);
  border: 1.5px solid var(--blue); border-radius: 24px;
  padding: 10px 22px; font-size: 14px; font-weight: 600;
  cursor: pointer; display: flex; align-items: center; gap: 6px;
  transition: background .15s, transform .1s; font-family: inherit;
}
.btn-outline:hover { background: var(--blue-light); }
.btn-outline:active { transform: scale(.97); }
.btn-outline svg { width: 14px; height: 14px; }

/* ══════════════════════════════════════════
   RANKING SCREEN
══════════════════════════════════════════ */
.result-banner {
  background: var(--green); color: #fff;
  padding: 16px 20px; flex-shrink: 0;
}
.result-banner-inner { display: flex; align-items: center; gap: 14px; max-width: 480px; margin: 0 auto; }
.result-check {
  width: 44px; height: 44px; background: rgba(255,255,255,.22);
  border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.result-check svg { width: 22px; height: 22px; }
.result-info { flex: 1; }
.result-name { font-size: 16px; font-weight: 700; margin-bottom: 3px; }
.result-stats-row { font-size: 13px; opacity: .9; display: flex; align-items: center; gap: 6px; }
.result-sep { opacity: .6; }
.pts-highlight { font-size: 15px; font-weight: 800; }

.ranking-main { flex: 1; overflow-y: auto; padding: 16px; }

.ranking-list { display: flex; flex-direction: column; gap: 8px; max-width: 480px; margin: 0 auto; }

.ranking-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px;
  transition: border-color .15s;
}
.ranking-row.you {
  border-color: var(--blue); background: var(--blue-light);
  box-shadow: 0 0 0 1px var(--blue);
}

.rank-medal { font-size: 22px; width: 28px; text-align: center; flex-shrink: 0; line-height: 1; }
.rank-pos { font-size: 16px; font-weight: 800; color: var(--text-2); width: 28px; text-align: center; flex-shrink: 0; }

.rank-avatar {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: #fff; flex-shrink: 0; letter-spacing: .5px;
}

.rank-info { flex: 1; min-width: 0; }
.rank-name { font-size: 15px; font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.you-tag { font-size: 11px; font-weight: 600; color: var(--blue); background: #fff; border: 1px solid var(--blue); border-radius: 20px; padding: 1px 7px; flex-shrink: 0; }
.rank-meta { font-size: 12px; color: var(--text-2); margin-top: 2px; }

.rank-score-col { text-align: right; flex-shrink: 0; }
.rank-score { font-size: 20px; font-weight: 800; color: var(--blue); line-height: 1; }
.rank-score-lbl { font-size: 11px; color: var(--text-2); margin-top: 2px; }

.ranking-footer {
  padding: 16px; background: var(--surface);
  border-top: 1px solid var(--border); flex-shrink: 0;
}
.btn-play-again {
  width: 100%; max-width: 480px; margin: 0 auto;
  display: flex; height: 52px; background: var(--blue); color: #fff;
  border: none; border-radius: 26px; font-size: 16px; font-weight: 700;
  cursor: pointer; align-items: center; justify-content: center; gap: 8px;
  transition: background .15s, transform .1s;
}
.btn-play-again:hover { background: var(--blue-dark); }
.btn-play-again:active { transform: scale(.97); }
.btn-play-again svg { width: 16px; height: 16px; }

/* ── Win pulse ── */
@keyframes cell-pulse { 0%,100%{opacity:1} 50%{opacity:.45} }
.cell-win { animation: cell-pulse .55s ease 2; }

@keyframes pop { 0%{transform:scale(.5);opacity:0} 70%{transform:scale(1.15)} 100%{transform:scale(1);opacity:1} }
.score-pop { animation: pop .45s cubic-bezier(.34,1.56,.64,1) both; }

@keyframes banner-in { from{transform:translateY(-100%);opacity:0} to{transform:none;opacity:1} }
.banner-anim { animation: banner-in .35s cubic-bezier(.34,1.56,.64,1) both; }

/* ── Misc ── */
@media (max-height: 680px) { .game-body { padding: 12px 14px 16px; gap: 10px; } }

/* ── Error toast ── */
.error-toast {
  position: fixed; bottom: 28px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #CC1016; color: #fff;
  padding: 12px 22px; border-radius: 10px;
  font-size: 14px; font-weight: 600;
  z-index: 200; box-shadow: var(--sh-md);
  opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s;
  white-space: nowrap;
}
.error-toast.show {
  opacity: 1; pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

/* ── Loading skeleton rows ── */
.ranking-skeleton {
  display: flex; flex-direction: column; gap: 8px;
}
.ranking-skeleton .skel-row {
  height: 72px; border-radius: 12px;
  background: linear-gradient(90deg, #eee 25%, #f5f5f5 50%, #eee 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ── Btn-play loading state ── */
.btn-play.loading { opacity: .7; pointer-events: none; }
