:root {
  --bg: #ffffff;
  --surface: #f3f6fa;
  --text: #344861;
  --muted: #94a3b7;
  --line: #bec6d4;
  --line-strong: #344861;
  --given: #344861;
  --user: #325aaf;
  --accent: #4a7fe0;
  --accent-soft: #e8effc;
  --sel: #bbdefb;
  --area: #e2ebf3;
  --same: #c3d7ea;
  --error: #e55c6c;
  --error-bg: #f7cfd6;
  --note: #6e7c8c;
  --flash: #cfe3ff;
  --shadow: 0 10px 30px rgba(30, 50, 80, .15);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #16191f;
  --surface: #222731;
  --text: #dbe3ee;
  --muted: #7d8899;
  --line: #3a4252;
  --line-strong: #8d9ab0;
  --given: #e3e9f2;
  --user: #7fa8ff;
  --accent: #6b9bff;
  --accent-soft: #24304a;
  --sel: #2f4a78;
  --area: #222b3a;
  --same: #2c3e5e;
  --error: #ff7b8a;
  --error-bg: #4a2630;
  --note: #9aa6b8;
  --flash: #2a4677;
  --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
  overflow: hidden;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

.screen {
  position: fixed; inset: 0;
  display: none; flex-direction: column;
  padding: calc(var(--safe-top) + 8px) 12px calc(var(--safe-bottom) + 12px);
  max-width: 560px; margin: 0 auto;
}
.screen.active { display: flex; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center; color: var(--text);
}
.icon-btn:active { background: var(--surface); }

/* ---------- Inicio ---------- */
.home-top { display: flex; justify-content: space-between; }
.home-center { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.logo {
  display: grid; grid-template-columns: repeat(3, 1fr);
  width: 112px; height: 112px; border-radius: 26px; overflow: hidden;
  background: var(--accent); box-shadow: var(--shadow); margin-bottom: 18px;
  padding: 14px; gap: 3px;
}
.logo span {
  background: rgba(255,255,255,.95); border-radius: 6px;
  display: grid; place-items: center; font-weight: 600; font-size: 22px; color: var(--accent);
}
.logo span:empty { background: rgba(255,255,255,.55); }
h1 { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -.5px; }
.tagline { margin: 0; color: var(--muted); font-size: 15px; text-align: center; }
.home-actions { display: flex; flex-direction: column; gap: 12px; padding: 0 8px 12px; }

.btn {
  height: 56px; border-radius: 28px; font-size: 17px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.15;
  transition: transform .1s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: #fff; }
.btn small { font-size: 12.5px; font-weight: 500; opacity: .85; }
.btn.ghost { background: none; color: var(--accent); height: 48px; }
.sheet .btn, .modal .btn { width: 100%; }
.btn.danger { color: var(--error); }

/* ---------- Juego ---------- */
.game-header { display: flex; align-items: center; justify-content: space-between; }
.game-title { font-weight: 700; font-size: 18px; }
.game-info {
  display: grid; grid-template-columns: repeat(3, 1fr);
  padding: 10px 4px 10px;
}
.info-item { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 15px; font-weight: 600; }
.info-item small { color: var(--muted); font-size: 12px; font-weight: 500; }
.info-item:first-child { align-items: flex-start; }
.info-item:last-child { align-items: flex-end; }
.timer { display: flex; align-items: center; gap: 4px; font-weight: 600; font-variant-numeric: tabular-nums; }
.timer svg { width: 18px; height: 18px; color: var(--muted); }
body.no-timer #info-time { visibility: hidden; }

.board-wrap { position: relative; width: 100%; max-width: min(100%, calc(100dvh - 330px - var(--safe-top) - var(--safe-bottom))); margin: 0 auto; }
.board {
  display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1; width: 100%;
  border: 2px solid var(--line-strong); border-radius: 4px; overflow: hidden;
  background: var(--bg);
}
.cell {
  position: relative; display: grid; place-items: center;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: clamp(20px, 7.4vw, 34px); color: var(--given); font-weight: 400;
  background: var(--bg); transition: background-color .12s;
  cursor: pointer;
}
.cell:nth-child(9n) { border-right: 0; }
.cell.r8 { border-bottom: 0; }
.cell.c2, .cell.c5 { border-right: 2px solid var(--line-strong); }
.cell.r2, .cell.r5 { border-bottom: 2px solid var(--line-strong); }
.cell.area { background: var(--area); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--sel); }
.cell.user { color: var(--user); }
.cell.wrong { color: var(--error); }
.cell.wrong.selected, .cell.wrong.same { background: var(--error-bg); }
.cell.conflict { background: var(--error-bg); }
.cell.flash { animation: flash .6s ease both; }
.cell.pop .v { animation: pop .18s ease; }
@keyframes flash { 0% { background: var(--flash); } 100% { background: inherit; } }
@keyframes pop { 0% { transform: scale(.6); } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }

.notes {
  position: absolute; inset: 1px;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: clamp(9px, 2.6vw, 12px); color: var(--note); font-weight: 500;
}
.notes span { display: grid; place-items: center; }
.notes span.hl { color: var(--user); font-weight: 700; }

.paused-overlay {
  position: absolute; inset: 0; background: var(--surface);
  border-radius: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  color: var(--muted);
}
.play-big { width: 72px; height: 72px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; }
.play-big svg { width: 32px; height: 32px; fill: currentColor; stroke: none; margin-left: 4px; }

.tools { display: grid; grid-template-columns: repeat(4, 1fr); padding: 14px 0 8px; }
.tool { position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--muted); font-size: 12.5px; padding: 6px 0; border-radius: 12px; }
.tool svg { width: 28px; height: 28px; stroke-width: 1.8; }
.tool:active { background: var(--surface); }
.tool .badge {
  position: absolute; top: 0; left: calc(50% + 8px);
  font-style: normal; font-size: 10px; font-weight: 700;
  background: var(--muted); color: var(--bg); padding: 1px 6px; border-radius: 10px;
}
.tool.on { color: var(--accent); }
.tool.on .badge { background: var(--accent); color: #fff; }
.tool:disabled { opacity: .35; }

.numpad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; padding-top: 4px; }
.num {
  position: relative; height: 64px; border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--user); font-size: clamp(26px, 8vw, 36px); font-weight: 400; line-height: 1;
  transition: background-color .12s, color .12s, transform .1s;
}
.num:active { background: var(--surface); }
.num small { font-size: 11px; color: var(--muted); margin-top: 3px; font-weight: 500; }
.num.done { visibility: hidden; }
body:not(.hide-completed) .num.done { visibility: visible; opacity: .3; }
.num.locked { background: var(--accent); color: #fff; }
.num.locked small { color: rgba(255,255,255,.8); }
.num.pressing { transform: scale(.94); }
.num.notes-mode { color: var(--note); }
.num.notes-mode.locked { color: #fff; }
.lock-hint { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; min-height: 16px; }

/* ---------- Hojas ---------- */
.sheet-backdrop, .modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 20, 35, .4); z-index: 10;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s ease;
}
.modal-backdrop { align-items: center; padding: 24px; }
.sheet {
  width: 100%; max-width: 560px; background: var(--bg); border-radius: 22px 22px 0 0;
  padding: 8px 18px calc(var(--safe-bottom) + 10px);
  max-height: 88dvh; overflow-y: auto; animation: slideUp .25s cubic-bezier(.2,.8,.2,1);
}
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { margin: 0 0 14px; font-size: 20px; text-align: center; }
@keyframes fade { from { opacity: 0; } }
@keyframes slideUp { from { transform: translateY(100%); } }

.diff-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 6px; }
.diff-item {
  display: flex; align-items: center; gap: 14px; padding: 14px 16px; border-radius: 16px;
  background: var(--surface); text-align: left;
}
.diff-item:active { transform: scale(.99); }
.diff-dots { display: flex; gap: 4px; }
.diff-dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.diff-dots i.on { background: var(--accent); }
.diff-item b { font-size: 17px; display: block; }
.diff-item small { color: var(--muted); font-size: 13px; }
.diff-item .grow { flex: 1; }

.settings { display: flex; flex-direction: column; margin-bottom: 6px; }
.setting {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 13px 2px; border-bottom: 1px solid var(--surface); font-size: 16px; cursor: pointer;
}
.setting span { display: flex; flex-direction: column; }
.setting small { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.setting input { display: none; }
.setting i {
  flex: none; width: 50px; height: 30px; border-radius: 15px; background: var(--line); position: relative; transition: background .2s;
}
.setting i::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s;
}
.setting input:checked + i { background: var(--accent); }
.setting input:checked + i::after { transform: translateX(20px); }

.segmented { display: flex; background: var(--surface); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented button { padding: 6px 12px; border-radius: 8px; font-size: 14px; color: var(--muted); font-weight: 500; }
.segmented button.on { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.segmented.wide { margin-bottom: 14px; }
.segmented.wide button { flex: 1; padding: 8px 0; }

.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 8px; }
.stat { background: var(--surface); border-radius: 14px; padding: 14px; }
.stat small { color: var(--muted); font-size: 12.5px; display: block; }
.stat b { font-size: 24px; font-variant-numeric: tabular-nums; }

/* ---------- Modal ---------- */
.modal {
  width: 100%; max-width: 380px; background: var(--bg); border-radius: 22px; padding: 26px 22px 16px;
  text-align: center; box-shadow: var(--shadow); animation: popIn .3s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes popIn { from { transform: scale(.85); opacity: 0; } }
.modal-icon { font-size: 48px; line-height: 1; margin-bottom: 8px; }
.modal h2 { margin: 0 0 6px; font-size: 22px; }
.modal p { margin: 0 0 16px; color: var(--muted); font-size: 15px; }
.modal-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 18px; }
.modal-stats:empty { display: none; }
.modal-stats div { background: var(--surface); border-radius: 12px; padding: 10px 4px; }
.modal-stats small { display: block; color: var(--muted); font-size: 11.5px; }
.modal-stats b { font-size: 18px; font-variant-numeric: tabular-nums; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 24px); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 20px; font-size: 14px; z-index: 20;
  box-shadow: var(--shadow); animation: fade .2s ease; white-space: nowrap;
}

@media (max-height: 700px) {
  .num { height: 54px; }
  .tools { padding: 8px 0 4px; }
  .lock-hint { display: none; }
  .board-wrap { max-width: min(100%, calc(100dvh - 280px - var(--safe-top) - var(--safe-bottom))); }
}
@media (min-width: 600px) {
  .num { height: 72px; }
}
