:root {
  --bg: #14162b;
  --bg2: #1d2042;
  --line: #2c3060;
  --ink: #f5efe3;
  --muted: #a0a3cc;
  --card: #f6efe2;
  --cardink: #26213d;
  --tile: #ffcd57;
  --tileedge: #c9921f;
  --tileink: #2b1d05;
  --accent: #ff7a66;
  --empty: #282c54;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
#app {
  max-width: 640px; margin: 0 auto; min-height: 100dvh;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
main { flex: 1; display: flex; flex-direction: column; padding: 0 16px 16px; min-height: 0; }
h1, h2, h3 { font-family: "Fraunces", Georgia, serif; margin: 0; font-weight: 700; }
a { color: inherit; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- shared bars ---------- */
.top { display: flex; align-items: center; justify-content: space-between; padding: 22px 0 18px; }
.logo { font-size: 36px; letter-spacing: -0.02em; }
.logo span { color: var(--tile); }
.chip {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; font-size: 14px;
}
.bar { display: flex; align-items: center; gap: 12px; padding: 14px 0 12px; }
.back {
  flex: none; width: 42px; height: 42px; display: grid; place-items: center;
  border-radius: 12px; background: var(--bg2); text-decoration: none;
  font-size: 30px; line-height: 1; padding-bottom: 4px;
}
.ttl { flex: 1; min-width: 0; }
.ttl h2 { font-size: 21px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ttl small { color: var(--muted); font-size: 13px; }

/* ---------- home ---------- */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.card {
  display: flex; gap: 14px; align-items: center; padding: 12px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 16px; text-decoration: none;
}
.thumb {
  width: 84px; height: 84px; flex: none; object-fit: contain;
  image-rendering: pixelated; border-radius: 8px; background: var(--empty);
}
.card h2 { font-size: 20px; }
.card p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.card .warn, .warn { color: var(--accent); }
.card.bad { display: block; color: var(--accent); }
.hint { color: var(--muted); font-size: 14px; text-align: center; margin: 22px 0 0; }

/* ---------- word map ---------- */
.mapwrap { overflow: hidden; border-radius: 14px; margin-top: 4px; transition: border-radius 1s; }
.map {
  position: relative; display: grid; gap: 6px;
  grid-template-columns: repeat(var(--mc), 1fr);
  transition: gap 1s cubic-bezier(.3,.7,.2,1);
}
.sector {
  position: relative; display: block; overflow: hidden; text-decoration: none;
  aspect-ratio: var(--pc) / var(--pr); border-radius: 8px; background: var(--bg2);
  transition: border-radius 1s;
}
.cells {
  position: absolute; inset: 0; display: grid; gap: 2px; padding: 2px;
  grid-template-columns: repeat(var(--c, var(--pc)), 1fr);
  grid-template-rows: repeat(var(--r, var(--pr)), 1fr);
  transition: gap .6s, padding .6s;
}
.cells i { background: var(--empty); border-radius: 2px; transition: border-radius .6s; }
.sector.done .cells { gap: 0; padding: 0; }
.sector.done .cells i { border-radius: 0; }
.lbl {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; padding: 6px; gap: 2px;
}
.lbl b {
  font-family: "Fraunces", Georgia, serif; font-size: clamp(11px, 3vw, 16px); line-height: 1.1;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
  text-shadow: 0 1px 6px var(--bg);
}
.lbl small { color: var(--tile); font-size: 12px; font-weight: 600; text-shadow: 0 1px 6px var(--bg); }
.sector.fresh { animation: fresh 1.4s ease .9s; }
@keyframes fresh { 40% { filter: brightness(1.5); } }

.map.merge { gap: 0; }
.map.merge .sector { border-radius: 0; }
.mapwrap.merge { border-radius: 0; }
.instant, .instant * { transition: none !important; animation: none !important; }
.orig {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
  opacity: 0; transition: opacity 2.4s ease; pointer-events: none;
}
.map.picture .orig { opacity: 1; }

.mapfoot { text-align: center; padding: 20px 0 8px; color: var(--muted); font-size: 14px; }
.mapfoot h3 { color: var(--ink); font-size: 26px; margin-bottom: 12px; animation: rise .6s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
.seg { display: inline-flex; background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.seg button { border: 0; background: none; padding: 8px 16px; border-radius: 999px; font-size: 14px; }
.seg button.on { background: var(--tile); color: var(--tileink); font-weight: 600; }
.warnbox {
  background: #3a1f2e; border: 1px solid #6b2f45; border-radius: 12px;
  padding: 10px 14px; font-size: 14px; margin-bottom: 10px;
}
.warnbox ul { margin: 6px 0 0; padding-left: 18px; }

/* ---------- puzzle ---------- */
.boardwrap { display: flex; justify-content: center; }
.board {
  width: min(100%, 560px, calc((100dvh - 380px) * var(--cols) / var(--rows)));
  display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: 6px;
  border-radius: 10px; overflow: hidden;
  transition: gap .8s cubic-bezier(.3,.7,.2,1);
}
.sq {
  position: relative; aspect-ratio: 1; border: 0; padding: 0; overflow: hidden;
  border-radius: 10px; background: var(--bg2); color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--line);
  transition: background-color .35s, border-radius .8s, transform .15s, box-shadow .15s;
}
.sq .txt {
  position: absolute; inset: 5px; display: flex; align-items: center; justify-content: center;
  text-align: center; line-height: 1.15; overflow: hidden;
}
.sq .txt > span { display: block; max-width: 100%; overflow-wrap: normal; }
.sq.clipped .txt > span { overflow-wrap: anywhere; }
.sq .prompt { font-weight: 500; }
.sq.clipped::after { content: "⋯"; position: absolute; right: 6px; bottom: 0; font-size: 16px; opacity: .55; }
.sq.over { transform: scale(1.07); box-shadow: 0 0 0 3px var(--tile); z-index: 2; }
.board.placing .sq:not(.solved) { box-shadow: inset 0 0 0 2px var(--tile); }
.sq.solved { background: var(--c); color: var(--ci); cursor: default; box-shadow: none; }
.sq.solved { cursor: pointer; }
.sq.solved .txt { font-weight: 700; }
.sq.solved.showp .txt { font-weight: 500; font-style: italic; }
.board.complete .sq.showp .txt { opacity: 1; }
.sq .ans { display: block; margin-top: 4px; font-style: normal; font-weight: 800; }
.board.complete .sq.showp { box-shadow: inset 0 0 0 2px var(--ci); z-index: 1; }
.sq.pop { animation: pop .38s ease; }
@keyframes pop { 0% { transform: scale(.82); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }
.sq.shake { animation: shake .36s ease; }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-3px); } 80% { transform: translateX(3px); } }
.board.complete { gap: 0; }
.board.complete .sq { border-radius: 0; }
.board.complete .sq .txt { opacity: 0; transition: opacity .7s; }
.board.complete .sq.clipped::after { content: none; }

.sq.flip { animation: flip .34s ease-in-out; }
@keyframes flip { 50% { transform: rotateY(90deg) scale(.94); } }
.board.failed { animation: shake .45s ease; }
.board.failed .sq:not(.solved) { background: #3a1f2e; box-shadow: inset 0 0 0 1px #6b2f45; }
.board.failed .sq.solved { filter: grayscale(1) brightness(.6); transition: filter .4s; }

.tools {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; max-width: 560px; margin: 12px auto 0;
}
.tools[hidden] { display: none; }
.chip:disabled { opacity: .4; }
.strikes { display: flex; align-items: center; gap: 7px; }
.slbl { color: var(--muted); font-size: 13px; margin-right: 2px; }
.strikes i {
  position: relative; width: 20px; height: 20px; border-radius: 50%;
  box-shadow: inset 0 0 0 2px var(--line); transition: background .2s, box-shadow .2s;
}
.strikes i.used { background: var(--accent); box-shadow: none; }
.strikes i.used::before, .strikes i.used::after {
  content: ""; position: absolute; left: 4px; right: 4px; top: 9px; height: 2px;
  background: var(--bg); border-radius: 1px; transform: rotate(45deg);
}
.strikes i.used::after { transform: rotate(-45deg); }
.strikes i.bump { animation: bump .4s ease; }
@keyframes bump { 40% { transform: scale(1.45); } }

.sector.struck { animation: shake .45s ease .15s, struck 1.4s ease; }
@keyframes struck { 0%, 30% { box-shadow: inset 0 0 0 3px var(--accent); } }

.tray {
  flex: 1; min-height: 120px; overflow-y: auto;
  display: flex; flex-wrap: wrap; align-content: flex-start; justify-content: center;
  gap: 9px; padding: 14px 2px 8px;
}
.tile {
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  border: 0; border-radius: 12px; padding: 11px 15px;
  background: var(--tile); color: var(--tileink); font-weight: 650; font-size: 16px; line-height: 1;
  box-shadow: 0 3px 0 var(--tileedge); cursor: grab;
  transition: opacity .15s, transform .15s, box-shadow .15s;
}
.tile.sel { transform: translateY(-3px); box-shadow: 0 0 0 3px var(--ink), 0 6px 0 var(--tileedge); }
.tile.lifted { opacity: .25; }
.ghost {
  position: fixed; left: 0; top: 0; z-index: 50; margin: 0; pointer-events: none;
  box-shadow: 0 14px 28px rgba(0,0,0,.45); cursor: grabbing;
}
.ghost.home { transition: transform .22s cubic-bezier(.3,.7,.2,1); }
.trayhint { width: 100%; text-align: center; color: var(--muted); font-size: 13px; margin-top: 6px; }

.donebar { text-align: center; padding: 22px 0 10px; animation: rise .5s ease both; }
.donebar p { margin: 0 0 14px; font-family: "Fraunces", Georgia, serif; font-size: 24px; font-weight: 700; }
.tray[hidden], .donebar[hidden] { display: none; }
.donehint { display: block; margin-top: 14px; color: var(--muted); font-size: 13px; }
.btn {
  display: inline-block; border: 0; background: var(--tile); color: var(--tileink);
  font-weight: 700; padding: 14px 24px; border-radius: 14px; text-decoration: none;
  box-shadow: 0 3px 0 var(--tileedge);
}

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 24px;
  background: rgba(10,11,25,.72); animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  background: var(--card); color: var(--cardink); border-radius: 18px; padding: 24px 22px 16px;
  max-width: 420px; width: 100%; font-size: 21px; line-height: 1.35;
}
.sheet p { margin: 0 0 14px; }
.sheet small { color: #6f6a86; font-size: 13px; }
.sheet.out { text-align: center; padding-bottom: 8px; animation: rise .4s ease both; }
.sheet.out h3 { color: #c2412f; font-size: 28px; margin-bottom: 8px; }
.err { padding: 40px 20px; text-align: center; color: var(--muted); }
.err b { color: var(--accent); display: block; margin-bottom: 8px; }
