:root {
  --bg-elev: #ffffff;
  --bg-elev-2: #faf6ea;
  --fg: #2a2a2a;
  --fg-dim: #5a5a5a;
  --fg-muted: #8a8a8a;
  --accent: #b97a05;
  --accent-dim: rgba(243, 198, 65, 0.28);
  --warn: #c98a05;
  --bad: #c8453a;
  --line: rgba(60, 40, 0, 0.18);
  --path: rgba(185, 122, 5, 0.55);
  --path-found: rgba(185, 122, 5, 0.9);
  --path-extra: rgba(201, 138, 5, 0.65);

  --grid-gap: 4px;
  --cell-max: 80px;
  --cell-radius: 8px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: linear-gradient(160deg, #fff8df 0%, #ffe7a8 60%, #f3c641 100%);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 540px;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: var(--safe-top);
  padding-bottom: calc(var(--safe-bot) + 8px);
  display: flex;
  flex-direction: column;
}

/* Top bar */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
}
.topbar__side {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.topbar__side--right { justify-content: flex-end; }
.topbar .title {
  min-width: 0;
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  margin: 0 2px;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.progress {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  background: var(--bg-elev);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(180, 130, 0, 0.12);
  font-variant-numeric: tabular-nums;
}
.progress--complete {
  border-color: var(--accent);
  background: rgba(243, 198, 65, 0.28);
}
@media (min-width: 400px) {
  .progress { font-size: 14px; padding: 5px 12px; }
}
.iconbtn {
  appearance: none;
  -webkit-appearance: none;
  background: var(--bg-elev);
  border: 1.5px solid var(--line);
  color: var(--fg);
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: transform 80ms ease, background 120ms ease;
  box-shadow: 0 2px 6px rgba(180, 130, 0, 0.12);
}
@media (min-width: 400px) {
  .iconbtn { width: 40px; height: 40px; flex-basis: 40px; }
  .topbar { gap: 8px; padding: 8px 12px; }
  .topbar .title { font-size: 18px; }
}
.iconbtn svg { width: 20px; height: 20px; }
.iconbtn:active { transform: scale(0.94); }
.iconbtn--accent {
  border-color: var(--accent);
  color: var(--accent);
}

/* Board */
.board-wrap {
  margin: 8px auto 8px;
  padding: 4px;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}
.board-stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols, 6), var(--cell, 48px));
  grid-auto-rows: var(--cell, 48px);
  gap: var(--grid-gap);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.board::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -6px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--line) 18%, var(--line) 82%, transparent);
  pointer-events: none;
}
.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 500;
  color: var(--fg);
  border-radius: var(--cell-radius);
  background: transparent;
  z-index: 2;
  letter-spacing: 0.5px;
}
.cell .glyph {
  position: relative;
  z-index: 3;
  pointer-events: none;
  transition: transform 90ms ease;
}
.cell .ring {
  position: absolute;
  inset: 4px;
  border-radius: 999px;
  background: #ffffff;
  border: 2px solid rgba(60, 40, 0, 0.22);
  z-index: 2;
  pointer-events: none;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}
.cell.selected .ring {
  background: var(--accent-dim);
  border-color: var(--accent);
}
.cell.head .ring {
  background: rgba(243, 198, 65, 0.55);
  transform: scale(1.06);
}
.cell.found .glyph { opacity: 0.55; }
.cell.found .ring {
  border-color: rgba(185, 122, 5, 0.5);
  background: rgba(243, 198, 65, 0.28);
}
.cell.flash-good .ring {
  background: rgba(243, 198, 65, 0.7) !important;
  border-color: var(--accent) !important;
}
.cell.flash-extra .ring {
  background: rgba(201, 138, 5, 0.35) !important;
  border-color: var(--warn) !important;
}
.cell.flash-bad .ring {
  background: rgba(200, 69, 58, 0.35) !important;
  border-color: var(--bad) !important;
}
.cell.hint .ring {
  border-color: var(--warn);
  background: rgba(243, 198, 65, 0.4);
}

.path-layer {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}
.path-layer .path {
  fill: none;
  stroke: var(--path);
  stroke-width: 14;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.7;
}
.path-layer .path--found {
  stroke: var(--path-found);
  stroke-width: 6;
  opacity: 0.28;
}
.path-layer .path--extra {
  stroke: var(--path-extra);
  stroke-width: 10;
  opacity: 0.4;
}

.current-word {
  margin: 4px 0 14px;
  text-align: center;
  font-weight: 700;
  font-size: 34px;
  line-height: 1.1;
  letter-spacing: 4px;
  min-height: 44px;
  color: var(--fg);
  text-transform: uppercase;
}
@media (min-width: 400px) {
  .current-word { font-size: 38px; letter-spacing: 5px; min-height: 48px; }
}

.found-words {
  margin: 18px 4px 4px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 8px;
  min-height: 28px;
  width: 100%;
  max-width: 100%;
}
.found-words .found-word {
  display: inline-flex;
  align-items: center;
  background: var(--bg-elev);
  border: 1.5px solid var(--accent);
  color: var(--accent);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.5px;
  box-shadow: 0 2px 6px rgba(180, 130, 0, 0.12);
}
.found-words .found-word--extra {
  border-style: dashed;
  border-color: var(--warn);
  color: var(--warn);
  font-weight: 600;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(48px + var(--safe-bot));
  transform: translateX(-50%) translateY(20px);
  background: #ffffff;
  color: var(--fg);
  padding: 10px 16px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 50;
  white-space: nowrap;
  max-width: 92vw;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 8px 24px rgba(180, 130, 0, 0.22);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast--good { border-color: var(--accent); color: var(--accent); }
.toast--extra { border-color: var(--warn); color: var(--warn); }
.toast--bad { border-color: var(--bad); color: var(--bad); }

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(40, 25, 0, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.overlay[hidden] { display: none; }
.overlay__panel {
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  background: var(--bg-elev);
  border: 1.5px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(60, 40, 0, 0.25);
}
.overlay__panel--small { max-height: 60vh; }
.overlay__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.overlay__head h2 { margin: 0; font-size: 16px; }
.overlay__text { padding: 16px; color: var(--fg-dim); font-size: 15px; margin: 0; }
.overlay__actions {
  display: flex;
  gap: 10px;
  padding: 12px 16px 16px;
  justify-content: flex-end;
}
.btn {
  appearance: none;
  background: var(--bg-elev-2);
  color: var(--fg);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn--primary {
  background: #f3c641;
  color: #4a3a00;
  border-color: #b97a05;
}
.puzzle-list {
  list-style: none;
  margin: 0;
  padding: 8px 8px 16px;
  overflow-y: auto;
}
.puzzle-list li {
  padding: 14px 14px;
  margin: 4px 8px;
  border-radius: 12px;
  background: var(--bg-elev-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  border: 1.5px solid var(--line);
}
.puzzle-list li.active {
  border-color: var(--accent);
  color: var(--accent);
}
.puzzle-list small {
  color: var(--fg-muted);
  font-size: 12px;
  font-weight: 400;
}

/* Win banner (non-blocking, leaves the board visible) */
.win-banner {
  margin: 14px 12px calc(8px + var(--safe-bot));
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-elev);
  border: 1.5px solid var(--accent);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(180, 130, 0, 0.22);
  animation: win-pop 320ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.win-banner[hidden] { display: none; }
.win-banner__text {
  font-weight: 700;
  color: var(--accent);
  font-size: 14px;
  flex: 1 1 auto;
  min-width: 0;
}
.win-banner .btn { flex: 0 0 auto; }
@media (min-width: 400px) {
  .win-banner__text { font-size: 15px; }
}
@keyframes win-pop {
  from { transform: scale(0.96); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

/* Größenraster: kleinere Zellen bei sehr engen Viewports */
@media (max-width: 320px) { :root { --grid-gap: 3px; } }
@media (min-width: 700px) { :root { --cell-max: 64px; } }
