:root {
  --bg: #0d0f14;
  --surface: #161920;
  --border: #2a2f3d;
  --accent: #c9a96e;
  --accent2: #7eb8c9;
  --text: #e8e2d5;
  --muted: #6b7280;
  --tile-bg: #1e2330;
  --tile-hover: #252c3d;
  --empty: #0a0c10;
  --radius: 6px;
  --gap: 5px;
  --tile-size: min(18vw, 88px);
}

body.light {
  --bg: #f0ece3;
  --surface: #faf7f2;
  --border: #d4cab8;
  --accent: #9a6f2e;
  --accent2: #3a7a95;
  --text: #2a2420;
  --muted: #8a7d6e;
  --tile-bg: #ede8df;
  --tile-hover: #e4ddd2;
  --empty: #ddd8ce;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'DM Mono', monospace;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 20px 16px 32px;
  overflow-x: hidden;
  transition: background 0.3s, color 0.3s;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(ellipse 80% 60% at 20% 10%, rgba(201,169,110,0.06) 0%, transparent 60%),
    radial-gradient(ellipse 60% 80% at 80% 90%, rgba(126,184,201,0.05) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

.wrapper {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 440px;
}

header {
  text-align: center;
  margin-bottom: 16px;
}

h1 {
  font-family: 'Playfair Display', serif;
  font-size: clamp(1.5rem, 5vw, 2.2rem);
  font-weight: 400;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 2px;
}

.subtitle {
  font-size: 0.68rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.pattern-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.pat-btn {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  border: 2px solid var(--border);
  cursor: pointer;
  overflow: hidden;
  padding: 0;
  background: none;
  transition: border-color 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}
.pat-btn:hover { border-color: var(--muted); transform: scale(1.08); }
.pat-btn.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.pat-btn canvas { display: block; width: 100%; height: 100%; }

.stats {
  display: flex;
  justify-content: center;
  gap: 28px;
  margin-bottom: 14px;
}
.stat { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.stat-label { color: var(--muted); font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.15em; }
.stat-value { color: var(--accent); font-weight: 500; font-size: 1rem; font-family: 'DM Mono', monospace; }

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  margin-bottom: 16px;
}

.board {
  display: grid;
  grid-template-columns: repeat(4, var(--tile-size));
  grid-template-rows: repeat(4, var(--tile-size));
  gap: var(--gap);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: var(--gap);
  box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(201,169,110,0.08);
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  background: var(--tile-bg);
  border: 1px solid var(--border);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.tile:not(.empty):hover {
  transform: scale(1.04);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4), 0 0 0 1px rgba(201,169,110,0.3);
  z-index: 2;
}
.tile:not(.empty):active { transform: scale(0.96); }
.tile.empty {
  background: var(--empty);
  border-color: rgba(255,255,255,0.04);
  cursor: default;
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.5);
}
.tile canvas { display: block; width: 100%; height: 100%; }
.tile .num {
  position: absolute;
  bottom: 3px;
  right: 3px;
  font-size: 0.52rem;
  font-family: 'DM Mono', monospace;
  font-weight: 500;
  color: #fff;
  background: rgba(0,0,0,0.55);
  line-height: 1;
  padding: 2px 3px;
  border-radius: 3px;
  pointer-events: none;
}
.tile.correct {
  border-color: rgba(201,169,110,0.6);
  box-shadow: 0 0 0 1px rgba(201,169,110,0.25), inset 0 0 6px rgba(201,169,110,0.08);
}
.tile.hint-tile {
  box-shadow: 0 0 0 2px var(--accent2), 0 0 12px rgba(126,184,201,0.5);
  border-color: var(--accent2);
}

#fwCanvas { position: fixed; inset: 0; pointer-events: none; z-index: 100; display: none; }

.win-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(13,15,20,0.85);
  border-radius: 10px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  backdrop-filter: blur(4px);
  z-index: 20;
}
.win-overlay.show { display: flex; }
.win-title { font-family: 'Playfair Display', serif; font-size: 1.8rem; color: var(--accent); }
.win-sub { font-size: 0.68rem; color: var(--muted); letter-spacing: 0.12em; text-transform: uppercase; }
.win-moves { font-size: 1rem; color: var(--accent2); font-family: 'DM Mono', monospace; }

.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

button {
  font-family: 'DM Mono', monospace;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 9px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}
button:hover { background: var(--tile-hover); border-color: var(--muted); }
button.primary {
  background: rgba(201,169,110,0.12);
  border-color: rgba(201,169,110,0.4);
  color: var(--accent);
}
button.primary:hover { background: rgba(201,169,110,0.22); }
button:disabled { opacity: 0.35; cursor: default; }

.img-row { display: flex; gap: 6px; margin-top: 12px; width: 100%; }
.img-row input {
  flex: 1;
  font-family: 'DM Mono', monospace;
  font-size: 0.65rem;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  outline: none;
  min-width: 0;
}
.img-row input:focus { border-color: var(--muted); }
.img-row input::placeholder { color: var(--muted); }
.msg { text-align: center; font-size: 0.65rem; color: var(--muted); margin-top: 8px; min-height: 1em; }
.msg.err { color: #e07070; }
