:root {
  --bg-top: #15308a;
  --bg-bottom: #060f2e;
  --panel: #1c2f6e;
  --panel-light: #24408a;
  --track: #0e1c48;
  --text: #f4f7ff;
  --text-dim: #96a9e0;
  --danger: #ee3224;
  --accent: #ffd23f;
  --on-accent: #0a1330;

  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

button { font-family: inherit; }

.screen {
  height: 100vh;
  height: 100dvh;
  padding: calc(16px + var(--safe-top)) 18px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
}
.screen[hidden] { display: none; }

.scroll-screen { overflow-y: auto; }

.brand { font-size: 30px; font-weight: 800; margin: 0; letter-spacing: -0.5px; }
.brand.small { font-size: 20px; }
.accent { color: var(--accent); }

/* ---------- Topbar (shared) ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  flex: 0 0 auto;
}
.topbar-title { flex: 1; min-width: 0; }
.topbar-right { display: flex; align-items: center; gap: 10px; position: relative; }

.icon-btn {
  background: rgba(255,255,255,0.07);
  border: none;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-menu {
  position: absolute;
  right: 0;
  top: 44px;
  background: var(--panel-light);
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  z-index: 30;
  min-width: 220px;
}
.app-menu[hidden] { display: none; }

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  padding: 14px 16px;
  font-size: 14px;
  cursor: pointer;
}
.menu-item:not(:last-child) { border-bottom: 1px solid rgba(255,255,255,0.08); }
.menu-item.danger { color: var(--danger); }

.progress-label { color: var(--text-dim); font-size: 13px; white-space: nowrap; }

/* ---------- Home / library ---------- */

.home-sub { color: var(--text-dim); margin: 0 0 18px; font-size: 14px; flex: 0 0 auto; }

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  overflow-y: auto;
  padding-bottom: 20px;
  flex: 1 1 auto;
}

.new-puzzle-tile {
  aspect-ratio: 3/4;
  border: 1.5px dashed rgba(255,255,255,0.22);
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.new-puzzle-tile .plus { font-size: 30px; color: var(--accent); }

.puzzle-card {
  aspect-ratio: 3/4;
  border-radius: var(--radius-md);
  overflow: hidden;
  position: relative;
  background: var(--panel);
  border: 1px solid rgba(255,255,255,0.08);
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,0.35);
}

.puzzle-card img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

.puzzle-card-info {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 8px 10px;
  background: linear-gradient(180deg, transparent, rgba(0,0,0,0.75));
  font-size: 12px;
  font-weight: 700;
}

.puzzle-card-badge {
  position: absolute;
  top: 8px; right: 8px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 11px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
}

.puzzle-card-delete {
  position: absolute;
  top: 6px; left: 6px;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: none;
  font-size: 14px;
  cursor: pointer;
}

/* ---------- New puzzle screen ---------- */

.field-label {
  display: block;
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 18px 0 8px;
}

.photo-pick-row { display: flex; gap: 10px; }

.photo-pick-btn {
  flex: 1;
  background: var(--panel);
  border: 1.5px dashed rgba(255,255,255,0.2);
  border-radius: var(--radius-md);
  color: var(--text);
  padding: 22px 10px;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.photo-pick-btn .emoji { display: block; font-size: 26px; margin-bottom: 8px; }

.photo-preview-wrap {
  position: relative;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--panel);
}
.photo-preview-wrap img { width: 100%; max-height: 220px; object-fit: contain; display: block; background: #000; }
.photo-change-btn {
  position: absolute; right: 8px; bottom: 8px;
  background: rgba(0,0,0,0.6);
  color: #fff; border: none; border-radius: 999px;
  padding: 6px 12px; font-size: 12px; cursor: pointer;
}

input[type="range"] { width: 100%; accent-color: var(--accent); height: 30px; }

.range-value-row { display: flex; justify-content: space-between; align-items: baseline; }
.range-value { font-size: 22px; font-weight: 800; color: var(--accent); }
.grid-hint { color: var(--text-dim); font-size: 12.5px; }

.segmented {
  display: flex;
  background: var(--track);
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}
.segmented button {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-dim);
  font-weight: 700;
  font-size: 13.5px;
  padding: 10px 6px;
  border-radius: 999px;
  cursor: pointer;
}
.segmented button.selected { background: var(--accent); color: var(--on-accent); }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin-top: 8px;
}
.toggle-row-label { font-size: 14px; font-weight: 600; }
.toggle-row-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.switch {
  position: relative;
  width: 46px; height: 26px;
  border-radius: 999px;
  background: rgba(255,255,255,0.15);
  border: none;
  cursor: pointer;
  flex: 0 0 auto;
}
.switch::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--text);
  transition: transform 0.2s ease;
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); background: var(--on-accent); }

.btn {
  display: block;
  width: 100%;
  border: none;
  border-radius: 999px;
  padding: 15px 20px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  margin-top: 26px;
  box-shadow: 0 0 22px color-mix(in srgb, var(--accent) 45%, transparent);
}
.btn-primary:disabled { opacity: 0.35; box-shadow: none; cursor: not-allowed; }
.btn-secondary { background: transparent; color: var(--text); border: 1.5px solid rgba(255,255,255,0.18); }

/* ---------- Play screen ---------- */

.play-board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  overflow: hidden;
  touch-action: none;
  background: rgba(0,0,0,0.15);
  border-radius: 10px;
}

#board-world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.play-board {
  position: relative;
  background: rgba(0,0,0,0.18);
  border-radius: 10px;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.12), 0 10px 30px rgba(0,0,0,0.4);
}

.play-board::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: var(--ghost-image, none);
  background-size: 100% 100%;
  border-radius: 8px;
  opacity: 0.3;
  pointer-events: none;
}

@keyframes piece-pop {
  0% { transform: scale(1.35); filter: brightness(1.6); }
  100% { transform: scale(1); filter: brightness(1); }
}
.puzzle-piece.just-placed {
  animation: piece-pop 0.35s ease-out;
}

.zoom-controls {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}
.zoom-controls .icon-btn {
  background: rgba(10,10,25,0.7);
  font-weight: 800;
  font-size: 20px;
}
.zoom-controls #zoom-reset { font-size: 15px; }

.play-tray-wrap {
  flex: 0 0 auto;
  background: var(--panel);
  border-radius: var(--radius-md);
  margin-top: 12px;
  max-height: 24vh;
  overflow-y: auto;
  overflow-x: hidden;
  border: 1px solid rgba(255,255,255,0.08);
}

.play-tray {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  padding: 8px 10px;
}

.puzzle-piece {
  touch-action: none;
  cursor: grab;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
}
.puzzle-piece.dragging {
  cursor: grabbing;
  filter: drop-shadow(0 8px 16px rgba(0,0,0,0.6));
  z-index: 500;
}
.puzzle-piece.placed {
  filter: none;
  cursor: default;
}

.tray-empty-hint {
  color: var(--text-dim);
  font-size: 13px;
  padding: 10px;
}

/* ---------- Victory overlay ---------- */

.victory-overlay {
  position: fixed;
  inset: 0;
  background: rgba(3, 3, 8, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.victory-overlay[hidden] { display: none; }
#confetti-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

.victory-card {
  position: relative;
  background: var(--panel-light);
  color: var(--text);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  text-align: center;
  width: min(90vw, 380px);
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  box-shadow: 0 0 60px color-mix(in srgb, var(--accent) 35%, transparent);
  max-height: 88vh;
  overflow-y: auto;
}
.victory-card img { width: 100%; border-radius: var(--radius-md); margin-bottom: 16px; }

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 18px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.15);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.victory-eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  margin: 0 0 6px;
}
.winner-name { font-size: 24px; margin: 0 0 4px; }
.winner-score { color: var(--text-dim); margin: 0 0 20px; }
.victory-actions { display: flex; flex-direction: column; gap: 10px; }

.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
