:root {
  --gold: #ffcf4d;
  --text: #f4efff;
  --dim: #a99cc9;
  --panel: rgba(255, 255, 255, 0.055);
  --panel-brd: rgba(255, 255, 255, 0.1);
  --danger: #ff6b6b;
}
body[data-theme="arcade"] { --bg1: #17102e; --bg2: #2a1a58; --accent: #ffcf4d; --glow: rgba(143, 123, 255, 0.45); }
body[data-theme="pirate"] { --bg1: #08131f; --bg2: #10304a; --accent: #ffd76a; --glow: rgba(64, 196, 255, 0.35); }
body[data-theme="space"]  { --bg1: #04050d; --bg2: #141833; --accent: #7fe3ff; --glow: rgba(127, 227, 255, 0.35); }

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: radial-gradient(1200px 700px at 50% -10%, var(--bg2), var(--bg1) 60%, #06040c);
  color: var(--text);
  min-height: 100vh;
  overscroll-behavior: none;
}

/* ---------- header ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  flex-wrap: wrap;
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(180deg, rgba(0,0,0,0.5), rgba(0,0,0,0.25));
  backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(255,255,255,0.07);
}
.logo { font-weight: 800; font-size: 20px; letter-spacing: 0.5px; display: flex; align-items: center; gap: 8px; }
.logo span { background: linear-gradient(180deg, #ffe9a3, var(--gold)); -webkit-background-clip: text; background-clip: text; color: transparent; text-transform: lowercase; }
.pill {
  display: flex; align-items: center; gap: 7px;
  background: var(--panel); border: 1px solid var(--panel-brd);
  border-radius: 999px; padding: 5px 12px; font-size: 15px;
}
.coin-ico { width: 20px; height: 20px; }
.pill b { font-size: 16px; min-width: 26px; text-align: center; }
.pill .lv { color: var(--dim); font-size: 11px; font-weight: 700; }
.bar { position: relative; width: 64px; height: 8px; border-radius: 4px; background: rgba(0,0,0,0.45); overflow: hidden; border: 1px solid rgba(255,255,255,0.08); }
.bar i { position: absolute; inset: 0; width: 0%; background: linear-gradient(90deg, #67e06b, #b6f26b); border-radius: 4px; transition: width 0.3s; }
.bar.xp i { background: linear-gradient(90deg, #8f7bff, #c96bff); }
#badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge-pill {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--panel); border: 1px solid var(--panel-brd);
  padding: 4px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 700;
}
.badge-pill img { width: 18px; height: 18px; }
.hdrbtns { margin-left: auto; display: flex; gap: 8px; }
.iconbtn {
  position: relative; width: 42px; height: 42px; border-radius: 12px;
  border: 1px solid var(--panel-brd); background: var(--panel);
  color: var(--text); font-size: 19px; cursor: pointer;
  transition: transform 0.12s, background 0.2s;
}
.iconbtn:hover { transform: translateY(-1px); background: rgba(255,255,255,0.12); }
.giftbtn { overflow: hidden; background-size: cover; background-position: center; background-image: url("sprites/s26.png"); }
.giftbtn::after { content: ""; position: absolute; inset: 0; background: rgba(0,0,0,0.15); }
.dot {
  position: absolute; top: 5px; right: 5px; width: 11px; height: 11px;
  background: #ff5a5a; border-radius: 50%; border: 2px solid #1b1230;
  animation: pulse 1.2s infinite;
}
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.35); } }

/* ---------- layout ---------- */
main {
  display: flex; gap: 16px; align-items: flex-start;
  justify-content: center;
  padding: 16px;
  max-width: 1120px; margin: 0 auto;
}
.canvas-wrap {
  position: relative; flex: 0 1 660px; min-width: 320px;
}
#game {
  display: block; width: 100%; height: auto;
  border-radius: 18px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.08), 0 0 60px var(--glow);
  touch-action: none;
  cursor: crosshair;
  background: var(--bg1);
}
#toasts {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 6px; align-items: center;
  pointer-events: none; z-index: 15; width: max-content; max-width: 90%;
}
.toast {
  background: rgba(12, 8, 28, 0.92); border: 1px solid var(--panel-brd);
  padding: 7px 14px; border-radius: 999px; font-size: 13.5px; font-weight: 600;
  animation: toastIn 0.25s ease-out; box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  white-space: nowrap;
}
.toast.warn { border-color: rgba(255,107,107,0.5); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
#connBanner {
  position: absolute; inset: auto 0 14px 0; margin: 0 auto; width: max-content;
  background: rgba(12,8,28,0.9); border: 1px solid var(--panel-brd);
  padding: 6px 16px; border-radius: 999px; font-size: 13px; z-index: 15;
}
#connBanner span { animation: blink 1.4s infinite; }
@keyframes blink { 50% { opacity: 0.4; } }

/* ---------- sidebar ---------- */
#side { display: flex; flex-direction: column; gap: 12px; width: 300px; flex-shrink: 0; }
.panel {
  background: var(--panel); border: 1px solid var(--panel-brd);
  border-radius: 14px; padding: 12px 14px;
}
.panel h3 {
  font-size: 12px; text-transform: uppercase; letter-spacing: 1.2px;
  color: var(--dim); margin-bottom: 10px; display: flex; align-items: center; gap: 8px;
}
.badge {
  font-size: 10px; letter-spacing: 0.5px; color: #0d2b12;
  background: linear-gradient(180deg, #9df07f, #4ecb52);
  padding: 2px 8px; border-radius: 999px; font-weight: 800;
}
.slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.slot {
  aspect-ratio: 1; border-radius: 12px; position: relative;
  background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.08);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  transition: all 0.25s;
}
.slot img { width: 62%; height: 62%; object-fit: contain; filter: grayscale(1) brightness(0.6); }
.slot .q { font-size: 22px; font-weight: 800; color: rgba(255,255,255,0.25); }
.slot .nm { font-size: 8.5px; color: var(--dim); text-transform: uppercase; letter-spacing: 0.4px; }
.slot.owned { border-color: rgba(255, 207, 77, 0.55); box-shadow: 0 0 14px rgba(255, 207, 77, 0.18) inset, 0 0 10px rgba(255, 207, 77, 0.12); }
.slot.owned img { filter: none; }
.slot.owned .nm { color: #e8d9a0; }

.puzzle { display: grid; grid-template-columns: repeat(3, 44px); gap: 5px; }
.pz {
  aspect-ratio: 1; border-radius: 8px; background: rgba(0,0,0,0.35);
  border: 1px solid rgba(255,255,255,0.08); position: relative;
}
.pz.on { background: rgba(255, 207, 77, 0.12); border-color: rgba(255,207,77,0.4); }
.pz.on::after {
  content: "🧩"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}
.puzzle-count { font-size: 11px; color: var(--dim); text-transform: none; letter-spacing: 0; }

.pu-row { display: flex; gap: 8px; }
.pbtn {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 7px;
  padding: 10px 8px; border-radius: 12px; cursor: pointer;
  background: linear-gradient(180deg, rgba(255,255,255,0.12), rgba(255,255,255,0.04));
  border: 1px solid var(--panel-brd); color: var(--text);
  font-size: 13.5px; font-weight: 700; transition: all 0.15s;
}
.pbtn:hover:not(:disabled) { transform: translateY(-1px); background: rgba(255,255,255,0.16); }
.pbtn:disabled { opacity: 0.45; cursor: not-allowed; }
.pbtn .pu-ico { width: 26px; height: 26px; }
.pbtn small { color: var(--dim); font-weight: 600; }
.pu-time {
  position: absolute; bottom: -8px; right: 8px;
  background: #2f7d3a; font-size: 10px; padding: 1px 7px; border-radius: 999px;
}
.pbtn { position: relative; }
.pbtn.active { border-color: rgba(120, 255, 140, 0.6); box-shadow: 0 0 12px rgba(120,255,140,0.25); }

.themes { display: flex; flex-direction: column; gap: 7px; }
.tbtn {
  display: flex; align-items: center; gap: 9px; padding: 9px 11px;
  border-radius: 11px; border: 1px solid var(--panel-brd);
  background: rgba(0,0,0,0.25); color: var(--text); cursor: pointer;
  font-size: 13.5px; font-weight: 600; transition: all 0.15s; text-align: left;
}
.tbtn .sw { width: 26px; height: 16px; border-radius: 5px; flex-shrink: 0; border: 1px solid rgba(255,255,255,0.2); }
.tbtn:hover:not(:disabled) { background: rgba(255,255,255,0.09); }
.tbtn.current { border-color: rgba(255,207,77,0.6); box-shadow: 0 0 10px rgba(255,207,77,0.15); }
.tbtn:disabled { opacity: 0.5; cursor: not-allowed; }
.tbtn .lock { margin-left: auto; font-size: 11px; color: var(--dim); }

.statline { display: flex; justify-content: space-between; font-size: 13px; padding: 4px 0; color: var(--dim); }
.statline b { color: var(--text); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5, 3, 12, 0.82); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.start-card, .modal {
  background: linear-gradient(180deg, rgba(38, 26, 74, 0.95), rgba(20, 13, 42, 0.97));
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 22px; padding: 30px 34px; max-width: 460px; width: 100%;
  text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,0.6);
  animation: pop 0.3s ease-out;
}
@keyframes pop { from { transform: scale(0.92); opacity: 0; } to { transform: none; opacity: 1; } }
.start-coin { margin-bottom: 10px; }
.start-coin img { width: 84px; animation: bob 2.2s ease-in-out infinite; filter: drop-shadow(0 10px 18px rgba(255,207,77,0.35)); }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-10px) rotate(4deg); } }
.start-card h1 {
  font-size: 42px; letter-spacing: 1px; margin-bottom: 6px;
  background: linear-gradient(180deg, #ffe9a3, var(--gold));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tag { color: var(--dim); font-size: 14.5px; margin-bottom: 16px; line-height: 1.5; }
.how { list-style: none; text-align: left; margin: 0 auto 20px; max-width: 380px; display: flex; flex-direction: column; gap: 8px; }
.how li { font-size: 13.5px; color: #d9d0ef; background: rgba(255,255,255,0.05); border-radius: 10px; padding: 8px 12px; }
.bigbtn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  border: none; cursor: pointer; color: #fff;
  background: linear-gradient(180deg, #ffdf7e, #f5a91f);
  font-size: 19px; font-weight: 800; letter-spacing: 1px;
  padding: 13px 34px; border-radius: 999px;
  box-shadow: 0 8px 0 #9a6206, 0 14px 26px rgba(0,0,0,0.4);
  transition: transform 0.1s, box-shadow 0.1s;
}
.bigbtn:hover { transform: translateY(-2px); }
.bigbtn:active { transform: translateY(3px); box-shadow: 0 4px 0 #9a6206, 0 8px 16px rgba(0,0,0,0.4); }
.play-ico { width: 44px; height: 30px; object-fit: cover; }
.modal h2 { font-size: 26px; margin-bottom: 8px; }
.modal-emoji { font-size: 56px; margin-bottom: 8px; animation: pop 0.4s; }
.modal p { color: #d9d0ef; font-size: 15px; margin-bottom: 18px; line-height: 1.5; }
.help { max-width: 520px; text-align: left; }
.help h2 { text-align: center; margin-bottom: 14px; }
.help-body { max-height: 52vh; overflow-y: auto; display: flex; flex-direction: column; gap: 9px; margin-bottom: 18px; }
.help-body p { font-size: 13.5px; color: #cfc6e6; line-height: 1.45; }
.help .bigbtn { display: block; margin: 0 auto; }

/* ---------- responsive ---------- */
@media (max-width: 940px) {
  main { flex-direction: column; align-items: center; }
  .canvas-wrap { flex: none; width: 100%; max-width: 640px; }
  #side { width: 100%; max-width: 640px; display: grid; grid-template-columns: 1fr 1fr; align-items: stretch; }
  #side .stats { display: none; }
}
@media (max-width: 560px) {
  #side { grid-template-columns: 1fr; }
  .logo { font-size: 17px; }
  .pill { font-size: 13px; padding: 4px 9px; }
  .bar { width: 44px; }
  #topbar { gap: 7px; padding: 8px 10px; }
}
