/* ===================== Puzzle Maker ===================== */
:root {
  --bg: #17110d;
  --bg-2: #211812;
  --panel: #2a1f17;
  --line: #4a3626;
  --text: #f4e9d8;
  --muted: #b8a58c;
  --gold: #e0b24f;
  --gold-2: #f3d27e;
  --ok: #4fbf72;
  --bad: #e2574c;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(1200px 600px at 20% -10%, #3a2516 0%, transparent 60%), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

button, input { font: inherit; color: inherit; }
button { touch-action: manipulation; }
code { background: var(--bg-2); padding: 1px 6px; border-radius: 4px; color: var(--gold-2); }
.hidden { display: none !important; }
.muted { color: var(--muted); margin: 0; }
.error { color: var(--bad); margin: 8px 0 0; }
.ic { width: 20px; height: 20px; flex: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + var(--safe-t)) calc(20px + var(--safe-r)) 10px calc(20px + var(--safe-l));
  background: rgba(23, 17, 13, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: .5px; white-space: nowrap; }
.brand-mark { color: var(--gold); display: flex; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.tabs { display: flex; gap: 4px; background: var(--bg-2); padding: 4px; border-radius: 999px; border: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 6px 18px; border-radius: 999px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--gold); color: #2a1a07; font-weight: 600; }

main { max-width: 1400px; margin: 0 auto; padding: 24px calc(24px + var(--safe-r)) calc(24px + var(--safe-b)) calc(24px + var(--safe-l)); }
.view { display: none; }
.view.active { display: block; }

.section-head { margin-bottom: 20px; }
.section-head h1 { margin: 0 0 4px; font-size: clamp(22px, 4vw, 28px); }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel);
  transition: transform .1s, background .15s, border-color .15s;
  user-select: none; -webkit-user-select: none; white-space: nowrap;
}
.btn:hover { border-color: var(--gold); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, var(--gold-2), var(--gold)); color: #2a1a07; border-color: var(--gold); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.icon { padding: 8px; width: 40px; }
.btn[aria-pressed="true"] { background: rgba(224, 178, 79, .18); border-color: var(--gold); color: var(--gold-2); }
.btn.danger:hover { border-color: var(--bad); color: var(--bad); }

input[type="number"], input[type="text"], input[type="password"] {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none;
  font-size: 16px; /* stops iOS zoom on focus */
}
input:focus { border-color: var(--gold); }
input[type="number"] { width: 72px; }

.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.row.center { justify-content: center; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.card h3 { margin: 0 0 14px; }

/* ---------- Gallery ---------- */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 18px; }
.puzzle-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; text-align: left; padding: 0;
  transition: transform .15s, border-color .15s, box-shadow .15s;
}
.puzzle-card:hover { transform: translateY(-3px); border-color: var(--gold); box-shadow: var(--shadow); }
.puzzle-card .thumb { aspect-ratio: 1; background-size: cover; background-position: center; position: relative; }
.puzzle-card .thumb::after {
  content: ""; position: absolute; inset: 0;
  background-image:
    linear-gradient(to right, rgba(255,255,255,.35) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,.35) 1px, transparent 1px);
  background-size: var(--cw) var(--ch);
  opacity: 0; transition: opacity .2s;
}
.puzzle-card:hover .thumb::after { opacity: 1; }
.puzzle-card .meta { padding: 10px 14px 14px; }
.puzzle-card .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.puzzle-card .sub { color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 60px 0; }

/* =========================================================
   GAME — always fills the whole screen
   ========================================================= */
body.in-game { overflow: hidden; overscroll-behavior: none; }
body.in-game .topbar, body.in-game main { display: none; }

#view-game.active {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  padding: calc(8px + var(--safe-t)) calc(10px + var(--safe-r)) calc(10px + var(--safe-b)) calc(10px + var(--safe-l));
  gap: 10px;
  background: radial-gradient(900px 500px at 30% -10%, #3a2516 0%, transparent 60%), var(--bg);
}

.game-bar {
  flex: none;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 6px 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
}
.bar-left { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.game-title { margin: 0; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stats { display: flex; gap: 16px; }
.stats span { display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.stats b { color: var(--gold-2); font-size: 16px; font-variant-numeric: tabular-nums; }
.stats i { font-style: normal; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.game-actions { display: flex; gap: 6px; align-items: center; }

.game-area { flex: 1; min-height: 0; display: flex; gap: 12px; }
.game-area.side { flex-direction: row; }
.game-area.bottom { flex-direction: column; }

.board-wrap { flex: 1; min-width: 0; min-height: 0; display: grid; place-items: center; }
.board, .peek { grid-area: 1 / 1; }
.board {
  position: relative;
  background-color: #0e0a07;
  background-image:
    linear-gradient(to right, rgba(224,178,79,.16) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(224,178,79,.16) 1px, transparent 1px);
  background-size: var(--cw) var(--ch);
  outline: 2px solid var(--gold); outline-offset: 0;
  border-radius: 2px; box-shadow: var(--shadow);
  touch-action: none;
}
.board::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background-image: var(--img); background-size: 100% 100%;
  opacity: 0; filter: grayscale(1); transition: opacity .25s;
}
.board.guide::before { opacity: .2; }
.hl-cell {
  position: absolute; pointer-events: none; z-index: 0;
  background: rgba(224, 178, 79, .28); border-radius: 4px; display: none;
}
.peek { pointer-events: none; background-size: 100% 100%; z-index: 40; box-shadow: var(--shadow); }

.tray-wrap {
  flex: none; min-height: 0; min-width: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  display: flex; flex-direction: column; overflow: hidden;
}
.tray-head { flex: none; padding: 6px 12px; border-bottom: 1px solid var(--line); font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.pill { background: var(--gold); color: #2a1a07; border-radius: 999px; padding: 0 8px; font-size: 12px; }
.tray { flex: 1; min-height: 0; padding: 10px; display: flex; gap: 2px; }
.game-area.side .tray { flex-wrap: wrap; align-content: flex-start; justify-content: center; overflow-y: auto; overflow-x: hidden; }
.game-area.bottom .tray { flex-flow: column wrap; align-content: flex-start; justify-content: center; overflow-x: auto; overflow-y: hidden; }
.tray.over { background: rgba(224, 178, 79, .08); }

/* ---------- Jigsaw pieces (canvas) ---------- */
.piece {
  display: block; flex: none; cursor: grab;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.55));
  transition: filter .2s;
}
/* tray lets the finger scroll along the tray; dragging across it picks the piece up */
.game-area.side .tray .piece { touch-action: pan-y; }
.game-area.bottom .tray .piece { touch-action: pan-x; }
.board .piece { position: absolute; z-index: 1; touch-action: none; filter: drop-shadow(0 1px 1px rgba(0,0,0,.45)); }
.piece.dragging-src { opacity: .2; }
.piece.ghost {
  position: fixed; z-index: 1000; pointer-events: none; cursor: grabbing;
  transform: scale(1.06); transform-origin: var(--ox) var(--oy);
  filter: drop-shadow(0 12px 14px rgba(0,0,0,.6)) drop-shadow(0 0 2px var(--gold));
}
.piece.ok { filter: drop-shadow(0 0 2px var(--ok)) drop-shadow(0 0 3px var(--ok)) !important; z-index: 2; }
.piece.bad { filter: drop-shadow(0 0 2px var(--bad)) drop-shadow(0 0 3px var(--bad)) !important; z-index: 2; animation: shake .35s; }
.board.done .piece { filter: none !important; }
.board.done { animation: glow 1.2s ease-out; }

@keyframes shake { 25% { translate: -3px 0; } 75% { translate: 3px 0; } }
@keyframes glow { from { box-shadow: 0 0 0 0 rgba(243, 210, 126, .9); } to { box-shadow: 0 0 0 30px rgba(243, 210, 126, 0); } }

/* ---------- Admin ---------- */
.lock { max-width: 440px; margin: 40px auto; }
.lock h1 { margin-top: 0; }
.lock form { margin-top: 16px; }

.dropzone {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px; text-align: center;
  display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap;
  transition: border-color .15s, background .15s;
}
.dropzone p { margin: 0; }
.dropzone.over { border-color: var(--gold); background: rgba(224, 178, 79, .08); }
.defaults { margin-top: 14px; color: var(--muted); }
.defaults label { display: inline-flex; gap: 6px; align-items: center; }

.admin-list { display: flex; flex-direction: column; gap: 10px; }
.admin-item {
  display: grid; grid-template-columns: 72px 1fr auto auto; gap: 14px; align-items: center;
  padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-2);
}
.admin-item.off .thumb { opacity: .45; filter: grayscale(.6); }
.helps { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-top: 6px; font-size: 13px; color: var(--muted); }
.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; color: var(--text); user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch i {
  position: relative; width: 34px; height: 20px; border-radius: 999px; flex: none;
  background: var(--line); transition: background .2s;
}
.switch i::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--text); transition: transform .2s;
}
.switch input:checked + i { background: var(--gold); }
.switch input:checked + i::after { transform: translateX(14px); background: #2a1a07; }
.switch input:focus-visible + i { outline: 2px solid var(--gold-2); outline-offset: 2px; }
.st-on { color: var(--ok); }
.st-off { color: var(--muted); }
.admin-item .thumb { width: 72px; height: 72px; border-radius: 6px; background-size: cover; background-position: center; }
.admin-item .title-in { width: 100%; }
.admin-item .kind { font-size: 12px; color: var(--muted); }
.admin-item .cut { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; flex-wrap: wrap; }
.admin-item .cut input { width: 62px; }

.cut-picker { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cut-custom { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.cut-custom input { width: 62px; }
select {
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 32px 8px 10px; min-height: 40px; cursor: pointer; outline: none;
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--gold) 50%), linear-gradient(135deg, var(--gold) 50%, transparent 50%);
  background-position: calc(100% - 16px) 52%, calc(100% - 11px) 52%;
  background-size: 5px 5px; background-repeat: no-repeat;
}
select:focus { border-color: var(--gold); }
select option { background: var(--bg-2); color: var(--text); }
.defaults { margin: 0 0 14px; }
.defaults #def-cut { display: inline-flex; }
.admin-item .acts { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Overlay & toast ---------- */
.overlay { position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center; padding: 20px; background: rgba(0,0,0,.6); backdrop-filter: blur(3px); }
.win-card { background: var(--panel); border: 1px solid var(--gold); border-radius: 16px; padding: 30px clamp(20px, 5vw, 40px); text-align: center; box-shadow: var(--shadow); animation: pop .35s ease-out; max-width: 420px; }
.win-card h2 { margin: 6px 0; }
.win-card p { color: var(--muted); margin: 0 0 20px; }
.win-icon { font-size: 48px; color: var(--gold-2); }
@keyframes pop { from { transform: scale(.8); opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); z-index: 3000; translate: -50% 20px;
  max-width: calc(100vw - 32px); text-align: center;
  background: var(--panel); border: 1px solid var(--gold); padding: 10px 18px; border-radius: 10px;
  box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, translate .2s;
}
.toast.show { opacity: 1; translate: -50% 0; }

/* =========================================================
   Responsive
   ========================================================= */

/* Tablets & small laptops: button labels shrink first */
@media (max-width: 1100px) {
  .game-actions .fs-btn .lbl { display: none; }
  .game-actions .fs-btn { width: 40px; padding: 8px; }
}

/* Tablet portrait / large phones */
@media (max-width: 820px) {
  main { padding: 16px calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l)); }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
  .game-bar { gap: 8px; }
  .game-actions { width: 100%; }
  .game-actions .btn { flex: 1; }
  .game-actions .fs-btn { flex: 0 0 44px; }
  .admin-item { grid-template-columns: 64px 1fr; }
  .admin-item .thumb { width: 64px; height: 64px; }
  .admin-item .cut, .admin-item .acts { grid-column: 1 / -1; }
}

/* Phones */
@media (max-width: 520px) {
  .topbar { padding-left: calc(12px + var(--safe-l)); padding-right: calc(12px + var(--safe-r)); }
  .brand { font-size: 16px; }
  .brand > span:last-child { display: none; }
  .tab { padding: 6px 14px; }
  .gallery { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .puzzle-card .meta { padding: 8px 10px 10px; }
  .card { padding: 14px; }
  #view-game.active { padding-left: calc(6px + var(--safe-l)); padding-right: calc(6px + var(--safe-r)); gap: 8px; }
  .game-title { font-size: 15px; }
  .stats { gap: 10px; }
  .stats b { font-size: 14px; }
  .game-actions .lbl { display: none; }
  .game-actions .btn { padding: 8px 6px; }
  .dropzone { padding: 18px; }
}

/* Short landscape screens (phones turned sideways): squeeze the bar into one line */
@media (max-height: 500px) and (orientation: landscape) {
  #view-game.active { padding-top: calc(4px + var(--safe-t)); padding-bottom: calc(4px + var(--safe-b)); gap: 6px; }
  .game-bar { flex-wrap: nowrap; padding: 3px 6px; }
  .game-actions { width: auto; }
  .game-actions .lbl { display: none; }
  .game-actions .btn { flex: none; width: 38px; min-height: 36px; padding: 6px; }
  .btn.icon { min-height: 36px; width: 36px; }
  .stats i { display: none; }
  .tray-head { display: none; }
}
