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

:root {
  --blue:    #185FA5;
  --blue-lt: #E6F1FB;
  --gold:    #c9a227;
  --green:   #4caf50;
  --red:     #ef5350;
  --bg:      #0f1117;
  --bg2:     #1e2433;
  --bg3:     #2e3a55;
  --text:    #e8eaf0;
  --text2:   #8899bb;
  --radius:  8px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── Screens ── */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow-y: auto;
}

/* ── Buttons ── */
button {
  cursor: pointer;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 14px;
  transition: opacity .15s, transform .1s;
}
button:active { transform: scale(0.97); }

.btn-primary {
  background: var(--blue);
  color: #fff;
  padding: 12px 32px;
  font-size: 16px;
  font-weight: 600;
}
.btn-primary:hover { opacity: .85; }

.btn-secondary {
  background: var(--bg2);
  color: var(--text2);
  border: 1px solid var(--bg3);
  padding: 8px 18px;
}
.btn-secondary:hover { color: var(--text); border-color: var(--blue); }

/* ── Home Screen ── */
#screen-home .logo {
  font-size: 52px;
  font-weight: 800;
  letter-spacing: -2px;
  color: var(--blue);
  margin-bottom: 4px;
}
#screen-home .tagline {
  color: var(--text2);
  font-size: 15px;
  margin-bottom: 40px;
  text-align: center;
}

/* ── Chapter Select ── */
#screen-chapter { justify-content: flex-start; padding-top: 40px; }

.screen-header {
  width: 100%;
  max-width: 700px;
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 28px;
}
.screen-header h2 { font-size: 20px; flex: 1; }

#chapter-list {
  width: 100%;
  max-width: 700px;
}

.chapter-card {
  background: var(--bg2);
  border: 1px solid var(--bg3);
  border-radius: var(--radius);
  padding: 18px 22px;
  margin-bottom: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 16px;
  transition: border-color .2s, background .2s;
  position: relative;
}
.chapter-card:hover { border-color: var(--blue); background: #1a2540; }
.chapter-card.locked { opacity: .5; cursor: default; }
.chapter-card.locked:hover { border-color: var(--bg3); background: var(--bg2); }

.chapter-title { font-weight: 600; font-size: 15px; flex: 1; }
.chapter-theme  { color: var(--text2); font-size: 13px; flex: 2; }
.chapter-stars  { color: var(--gold); font-size: 13px; white-space: nowrap; }
.chapter-lock   { font-size: 18px; }

.puzzle-list-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 12px;
}

.puzzle-btn {
  background: var(--bg2);
  border: 1px solid var(--bg3);
  border-radius: var(--radius);
  padding: 14px 10px;
  text-align: center;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.puzzle-btn:hover { border-color: var(--blue); }
.puzzle-id   { font-size: 11px; color: var(--text2); }
.puzzle-name { font-size: 13px; font-weight: 600; }
.puzzle-stars { color: var(--gold); font-size: 14px; }

/* ── Game Screen ── */
#screen-game {
  flex-direction: column;
  justify-content: flex-start;
  padding: 0;
}

.game-hud {
  width: 100%;
  background: var(--bg2);
  border-bottom: 1px solid var(--bg3);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  flex-shrink: 0;
}

#puzzle-title { font-weight: 600; font-size: 15px; flex: 1; }
#solved-title  { font-weight: 600; font-size: 15px; }
.solved-badge  { font-size: 13px; font-weight: 700; color: var(--green); border: 1px solid var(--green); border-radius: 99px; padding: 3px 10px; white-space: nowrap; }
#timer        { color: var(--text2); font-size: 14px; font-variant-numeric: tabular-nums; }
#hint-count   { color: var(--text2); font-size: 13px; }

.game-canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 12px;
}

#game-canvas {
  max-width: 100%;
  max-height: calc(100vh - 120px);
  image-rendering: pixelated;
}

/* ── Win Screen ── */
#screen-win .win-card {
  background: var(--bg2);
  border: 1px solid var(--bg3);
  border-radius: 12px;
  padding: 40px 48px;
  text-align: center;
  max-width: 380px;
  width: 100%;
}
#screen-win h2  { font-size: 26px; margin-bottom: 8px; }
#win-stars      { font-size: 36px; color: var(--gold); margin-bottom: 20px; display: block; }
#win-score      { font-size: 22px; font-weight: 700; color: var(--blue); margin-bottom: 4px; }
#win-time, #win-hints, #win-hints-remaining {
  font-size: 14px;
  color: var(--text2);
  margin-bottom: 4px;
}
.win-actions { display: flex; gap: 12px; justify-content: center; margin-top: 28px; flex-wrap: wrap; }

/* ── Legend strip (below HUD) ── */
.legend {
  display: flex;
  gap: 16px;
  padding: 6px 18px;
  background: var(--bg);
  border-bottom: 1px solid var(--bg3);
  font-size: 12px;
  flex-wrap: wrap;
}
.legend-item { display: flex; align-items: center; gap: 5px; color: var(--text2); }
.legend-dot  { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg3);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  z-index: 100;
}
#toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 3px; }

/* ── How to Play Modal ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 16px;
}
.modal-card {
  background: var(--bg2); border: 1px solid var(--bg3);
  border-radius: 12px; padding: 32px;
  max-width: 520px; width: 100%; max-height: 90vh; overflow-y: auto;
  position: relative;
}
.modal-card h2 { font-size: 22px; margin-bottom: 20px; }
.modal-card h3 {
  font-size: 12px; font-weight: 700; color: var(--text2);
  text-transform: uppercase; letter-spacing: .06em; margin: 20px 0 8px;
}
.modal-card p  { font-size: 14px; color: var(--text); line-height: 1.6; margin-bottom: 6px; }
.modal-card ul { font-size: 14px; color: var(--text); line-height: 1.9; padding-left: 20px; }
.modal-close {
  position: absolute; top: 14px; right: 14px;
  background: none; color: var(--text2); font-size: 16px; padding: 4px 8px; border-radius: 4px;
}
.modal-close:hover { color: var(--text); background: var(--bg3); }
.htp-constraints { display: flex; flex-direction: column; gap: 9px; }
.htp-row { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text); }
.htp-badge {
  width: 24px; height: 24px; border: 1.5px solid; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.htp-example { display: block; width: 100%; max-width: 265px; margin: 10px auto; }
.htp-note { color: var(--text2) !important; font-size: 13px !important; margin-top: 4px; }

/* ── Mobile ── */
@media (max-width: 500px) {
  #screen-win .win-card { padding: 28px 24px; }
  .game-hud { padding: 8px 12px; gap: 8px; }
  .btn-primary { padding: 10px 24px; }
}
