:root {
  --bg: #161a35;
  --bg-2: #232a52;
  --frame: #0e1127;
  --paper: #f5ecda;
  --paper-2: #ebdfc6;
  --ink: #2a2340;
  --text: #f3eee4;
  --muted: #a7a6c4;
  --gold: #f2c45a;
  --gold-deep: #c98f1c;
  --line: rgba(243, 238, 228, 0.14);
  --cell: 60px;
  --tcell: 44px;
  --serif: "Fraunces", "Iowan Old Style", "Palatino", Georgia, serif;
  --sans: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { touch-action: manipulation; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--sans);
  color: var(--text);
  background:
    radial-gradient(1200px 700px at 50% -10%, #2d3566 0%, transparent 60%),
    radial-gradient(900px 600px at 110% 110%, #2a1f4a 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  display: flex;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(560px, 100vw);
  padding: 18px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- header ---------- */
.top { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.brand h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 800;
  font-size: 2.3rem;
  letter-spacing: -0.02em;
  line-height: 1;
}
.stitch-i { position: relative; color: var(--gold); }
.subtitle { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; font-weight: 600; }

.modes { display: flex; gap: 2px; padding: 3px; border-radius: 999px; background: rgba(0, 0, 0, 0.25); border: 1px solid var(--line); }
.modes button {
  font: inherit; font-size: 0.85rem; font-weight: 700;
  border: 0; background: transparent; color: var(--muted);
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
}
.modes button[aria-selected="true"] { background: var(--text); color: var(--ink); }
.modes button.done::after { content: " ✓"; color: var(--gold-deep); }
.modes button.done[aria-selected="false"]::after { color: var(--gold); }

/* ---------- board ---------- */
.stage { display: flex; justify-content: center; }
.board {
  --n: 6;
  position: relative;
  width: min(100%, 460px);
  aspect-ratio: 1;
  padding: 6px;
  border-radius: 20px;
  background: var(--frame);
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.05) inset;
}
.layer { position: absolute; inset: 6px; }
.cells { display: grid; grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr); gap: 3px; }
.cell { border-radius: 7px; }
.cell.open {
  background: linear-gradient(160deg, var(--paper) 0%, var(--paper-2) 100%);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.06);
}
.cell.block {
  background:
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.025) 0 4px, transparent 4px 9px);
}

.slot {
  position: absolute;
  background: linear-gradient(160deg, var(--paper) 0%, var(--paper-2) 100%);
  border: 2px dashed rgba(42, 35, 64, 0.2);
  border-radius: 9px;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}
.slot.target { border-color: var(--gold-deep); background: #f7e2ab; animation: pulse 1.4s ease-in-out infinite; }
.slot.drop { border-style: solid; border-color: var(--gold-deep); background: #f2cf73; animation: none; }
@keyframes pulse { 50% { background: var(--paper); } }

/* ---------- tiles ---------- */
.tile {
  --h: 30;
  position: relative;
  display: inline-flex;
  align-items: stretch;
  height: var(--tcell);
  border-radius: 10px;
  font-family: var(--serif);
  font-weight: 650;
  font-size: calc(var(--tcell) * 0.5);
  color: var(--ink);
  text-transform: uppercase;
  background: linear-gradient(175deg, hsl(var(--h) 85% 82%) 0%, hsl(var(--h) 70% 70%) 100%);
  box-shadow:
    inset 0 1.5px 0 rgba(255, 255, 255, 0.6),
    inset 0 -3px 0 hsl(var(--h) 45% 52% / 0.55),
    0 4px 10px -4px rgba(0, 0, 0, 0.5);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  transition: transform 0.18s cubic-bezier(.3,.7,.4,1.4), box-shadow 0.18s, opacity 0.2s;
}
.tile span { width: var(--tcell); display: grid; place-items: center; padding-bottom: 2px; }
.tile::after { /* seam marks between letters inside a piece */
  content: ""; position: absolute; inset: 22% 0; pointer-events: none;
  background: repeating-linear-gradient(90deg, transparent 0 calc(var(--tcell) - 1px), hsl(var(--h) 40% 45% / 0.12) calc(var(--tcell) - 1px) var(--tcell));
}
.tile.selected {
  transform: translateY(-4px) scale(1.04);
  box-shadow: inset 0 1.5px 0 rgba(255, 255, 255, 0.6), inset 0 -3px 0 hsl(var(--h) 45% 52% / 0.55),
    0 0 0 3px var(--gold), 0 14px 22px -8px rgba(0, 0, 0, 0.6);
  z-index: 3;
}
.tile.locked { cursor: default; }
.tile.locked::before {
  content: ""; position: absolute; top: 5px; right: 5px; width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink); opacity: 0.45;
}
.tile.dragging { cursor: grabbing; transition: none; z-index: 50; box-shadow: 0 0 0 3px var(--gold), 0 22px 30px -10px rgba(0, 0, 0, 0.65); }

.tiles .tile { position: absolute; --tcell: var(--cell); height: auto; }
.tiles .tile span { width: auto; flex: 1; }
.tiles .tile::after { background: repeating-linear-gradient(90deg, transparent 0 calc(var(--cell) + 2px), hsl(var(--h) 40% 45% / 0.12) calc(var(--cell) + 2px) calc(var(--cell) + 3px)); }

/* ---------- stitches ---------- */
.stitches { pointer-events: none; }
.stitch {
  position: absolute;
  width: calc(var(--cell) * 0.26);
  height: calc(var(--cell) * 0.26);
  transform: translate(-50%, -50%);
  animation: stitch-in 0.35s cubic-bezier(.3,.7,.4,1.5) both;
}
.stitch::before, .stitch::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 100%; height: 3px; margin: -1.5px 0 0 -50%;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--gold-deep), var(--gold) 50%, var(--gold-deep));
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
}
.stitch::before { transform: rotate(45deg); }
.stitch::after { transform: rotate(-45deg); }
@keyframes stitch-in { from { transform: translate(-50%, -50%) scale(0) rotate(-90deg); opacity: 0; } }

.board.won .cell.open { animation: wave 0.9s ease-in-out both; animation-delay: calc(var(--d) * 70ms); }
.board.won .tile { animation: tile-wave 0.9s ease-in-out both; animation-delay: calc(var(--d) * 70ms); }
@keyframes wave { 40% { background: #fff7e0; box-shadow: 0 0 22px 2px rgba(242, 196, 90, 0.7); } }
@keyframes tile-wave { 40% { transform: translateY(-5px); filter: brightness(1.12); } }

/* ---------- tray ---------- */
.tray {
  display: flex; flex-wrap: wrap; justify-content: center; align-content: flex-start; gap: 10px;
  min-height: calc(var(--tcell) * 2 + 34px);
  padding: 14px 12px;
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--line);
  transition: background-color 0.2s;
}
.tray.drop { background: rgba(242, 196, 90, 0.08); }
.tray-empty { align-self: center; margin: auto; color: var(--muted); font-size: 0.9rem; font-weight: 600; }

/* ---------- controls ---------- */
.controls { display: flex; justify-content: center; gap: 10px; }
button { -webkit-tap-highlight-color: transparent; }
.controls button, .win-buttons button {
  font: inherit; font-weight: 700; font-size: 0.95rem;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s;
}
.controls button:hover { background: rgba(255, 255, 255, 0.12); }
.controls button:active { transform: translateY(1px); }
.controls button:disabled { opacity: 0.35; cursor: default; }
.controls span { color: var(--gold); margin-right: 2px; }

.rules { color: var(--muted); font-size: 0.92rem; line-height: 1.5; border-top: 1px solid var(--line); padding-top: 10px; }
.rules summary { cursor: pointer; font-weight: 700; color: var(--text); }
.rules ul { padding-left: 20px; margin: 8px 0 0; }
.rules b { color: var(--text); }

/* ---------- win ---------- */
.win {
  position: fixed; inset: var(--mc-top, 0) 0 0 0; z-index: 40;
  display: grid; place-items: end center;
  padding: 16px 16px max(24px, env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(10, 12, 30, 0.75), rgba(10, 12, 30, 0) 70%);
  animation: fade 0.4s ease both;
}
.win[hidden] { display: none; }
.win-card {
  position: relative;
  width: min(420px, 100%);
  padding: 22px 22px 20px;
  text-align: center;
  border-radius: 22px;
  color: var(--ink);
  background: linear-gradient(170deg, #fff8e8, var(--paper-2));
  box-shadow: 0 30px 60px -10px rgba(0, 0, 0, 0.6);
  animation: rise 0.5s cubic-bezier(.3,.7,.3,1.2) both 0.15s;
}
.win-kicker { margin: 0; font-size: 0.8rem; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; color: var(--gold-deep); }
.win-card h2 { margin: 2px 0 6px; font-family: var(--serif); font-weight: 800; font-size: 2.2rem; letter-spacing: -0.02em; }
.win-stats { margin: 0 0 16px; color: #5b5470; font-weight: 600; }
.win-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.win-buttons button { color: var(--ink); border-color: rgba(42, 35, 64, 0.2); background: rgba(42, 35, 64, 0.04); }
.win-buttons .primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.win-buttons button[hidden] { display: none; }
.win-close {
  position: absolute; top: 8px; right: 10px; border: 0; background: none; font-size: 1.6rem;
  color: #8a839c; cursor: pointer; line-height: 1;
}
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  padding: 10px 16px; border-radius: 12px; background: var(--text); color: var(--ink); font-weight: 700;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

@media (max-width: 420px) {
  .app { gap: 12px; padding-top: 12px; }
  .brand h1 { font-size: 1.9rem; }
  .modes button { padding: 6px 9px; font-size: 0.8rem; }
  .tray { gap: 8px; padding: 12px 8px; }
  .controls button { padding: 9px 12px; font-size: 0.9rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ---------- options row: past days + difficulty ---------- */
.options { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.days {
  flex: 1; min-width: 0;
  display: flex; gap: 6px;
  overflow-x: auto; scrollbar-width: none;
  scroll-behavior: smooth;
  padding: 2px;
  -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
          mask-image: linear-gradient(90deg, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
}
.days::-webkit-scrollbar { display: none; }
.days[hidden] { display: none; }
.day {
  flex: 0 0 auto;
  display: grid; justify-items: center; gap: 1px;
  min-width: 42px; padding: 4px 6px 5px;
  font: inherit; color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 11px;
  cursor: pointer;
}
.day:first-child { margin-left: 12px; }
.day:last-child { margin-right: 12px; }
.day small { font-size: 0.62rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.day b { font-family: var(--serif); font-size: 1.05rem; font-weight: 650; color: var(--text); line-height: 1; }
.day.started b::after { content: ""; display: block; width: 4px; height: 4px; margin: 3px auto 0; border-radius: 50%; background: var(--muted); }
.day.solved { background: linear-gradient(170deg, #f7d37a, #d9a43a); border-color: transparent; color: #6b4a0c; }
.day.solved b { color: var(--ink); }
.day[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--text); }

.difficulty { margin-left: auto; flex: 0 0 auto; display: flex; gap: 2px; padding: 3px; border-radius: 999px; background: rgba(0, 0, 0, 0.25); border: 1px solid var(--line); }
.difficulty button { font: inherit; font-size: 0.8rem; font-weight: 700; border: 0; background: transparent; color: var(--muted); padding: 6px 11px; border-radius: 999px; cursor: pointer; }
.difficulty button[aria-checked="true"][data-difficulty="easy"] { background: #8fd6a4; color: #163322; }
.difficulty button[aria-checked="true"][data-difficulty="hard"] { background: #f08a7a; color: #3a1210; }

/* ---------- shuffle ---------- */
.controls button.icon { padding: 10px 13px; }
.controls button.icon span { margin: 0; }
.controls button[hidden] { display: none; }

/* ---------- live word colors (Easy) ---------- */
.tile { overflow: hidden; }
.tile span { transition: background-color 0.3s, color 0.3s; }
.tile span.wg {
  background: linear-gradient(175deg, #9fe0a6 0%, #5fb46d 100%);
  box-shadow: inset 0 1.5px 0 rgba(255, 255, 255, 0.5), inset 0 -3px 0 #3f8a4c;
  color: #10301a;
}
.tile span.wy {
  background: linear-gradient(175deg, #ffe28a 0%, #e9b73c 100%);
  box-shadow: inset 0 1.5px 0 rgba(255, 255, 255, 0.5), inset 0 -3px 0 #b78518;
  color: #3d2a04;
}

/* ---------- local testing panel ---------- */
.dev { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: 0.8rem; color: var(--muted); border: 1px dashed var(--line); border-radius: 12px; padding: 8px 10px; }
.dev button { font: inherit; font-weight: 700; color: var(--text); background: rgba(255, 255, 255, 0.06); border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; cursor: pointer; }
.dev button.danger { color: #f3a99d; }

.rules .key { padding: 0 5px; border-radius: 4px; font-weight: 700; }
.rules .key.g { background: #5fb46d; color: #10301a; }
.rules .key.y { background: #e9b73c; color: #3d2a04; }

@media (max-width: 420px) {
  .day { min-width: 38px; padding: 3px 5px 4px; }
  .difficulty button { padding: 5px 9px; }
}
