:root {
  --bg: #f4f1ff;
  --bg-glow: #e6defe;
  --surface: #ffffff;
  --text: #1d1640;
  --muted: #6b6488;
  --brand: #6c47ff;
  --brand-2: #ff4f8b;
  --tile-border: #d8d1f2;
  --tile-filled: #9d92dc;
  --correct: #10b3a3;
  --present: #ff8a3d;
  --absent: #8d87a8;
  --key: #e4defa;
  --key-text: #1d1640;
  --key-absent: #9e98b8;
  --key-absent-text: #f4f1ff;
  --shadow: rgba(40, 20, 110, 0.16);
  --rows: 6;
  --tile: max(34px, min(60px, calc((100vw - 56px) / 5), calc((100dvh - 390px) / var(--rows))));
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #120f24;
    --bg-glow: #221a4a;
    --surface: #1e1a3a;
    --text: #f3f0ff;
    --muted: #a49dc6;
    --tile-border: #3a3462;
    --tile-filled: #7369b8;
    --correct: #14b8a6;
    --present: #f5843a;
    --absent: #514b7c;
    --key: #433c72;
    --key-text: #f3f0ff;
    --key-absent: #1a1631;
    --key-absent-text: #5f5888;
    --shadow: rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  background: radial-gradient(ellipse 80% 45% at 50% 0%, var(--bg-glow), transparent 70%), var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* Header */

.top-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 12px 16px 4px;
}

.title { text-align: center; }
.title p { margin: 3px 0 0; font-size: 0.8rem; font-weight: 600; color: var(--muted); }

.logo {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  line-height: 1;
}
.logo span {
  display: inline-block;
  margin-left: 3px;
  padding: 2px 7px 3px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #ffffff;
  transform: rotate(-4deg);
}

.icon-button {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 2px 0 var(--shadow);
  font-weight: 800;
}
.icon-button svg { fill: currentColor; }

/* Difficulty */

#level-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 700;
}
.pips { display: flex; gap: 3px; }
.pip {
  width: 14px;
  height: 8px;
  border-radius: 4px;
  background: var(--tile-border);
}
/* Filled pips run from teal (easy) through blue and violet to pink (hard). */
.pip.on:nth-child(1) { background: #10b3a3; }
.pip.on:nth-child(2) { background: #2fa4d4; }
.pip.on:nth-child(3) { background: #4b84f5; }
.pip.on:nth-child(4) { background: #6c5cff; }
.pip.on:nth-child(5) { background: #9b4ff2; }
.pip.on:nth-child(6) { background: #d24bc6; }
.pip.on:nth-child(7) { background: #ff4f8b; }
.level-label { color: var(--text); }
.badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #ffffff;
  font-size: 0.72rem;
}

/* Board */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 8px 16px;
  position: relative;
}

#board { display: grid; gap: 6px; }
.row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: 6px; }

.tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  border: 2px solid var(--tile-border);
  border-radius: 12px;
  background: transparent;
  font-size: calc(var(--tile) * 0.5);
  font-weight: 800;
  text-transform: uppercase;
  user-select: none;
}

.tile[data-state="filled"] { border-color: var(--tile-filled); background: var(--surface); animation: pop 120ms ease-out; }
.tile[data-state="correct"],
.tile[data-state="present"],
.tile[data-state="absent"] {
  color: #ffffff;
  border-color: transparent;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18);
}
.tile[data-state="correct"] { background: var(--correct); }
.tile[data-state="present"] { background: var(--present); }
.tile[data-state="absent"] { background: var(--absent); }

.tile.flip { animation: flip 300ms ease-in-out; }
.row.shake { animation: shake 350ms; }

@keyframes pop { from { transform: scale(0.85); } to { transform: scale(1); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

@media (prefers-reduced-motion: reduce) {
  .tile, .row { animation: none !important; }
}

/* Toast */

#toast {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  font-size: 0.9rem;
  width: max-content;
  max-width: calc(100vw - 32px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms;
  z-index: 2;
}
#toast.show { opacity: 1; }

/* Mode buttons */

#mode-bar { display: flex; gap: 8px; min-height: 34px; }
#mode-bar button {
  padding: 6px 15px;
  border: 2px solid var(--brand);
  border-radius: 999px;
  background: transparent;
  color: var(--brand);
  font-size: 0.85rem;
  font-weight: 700;
}

@media (prefers-color-scheme: dark) {
  #mode-bar button { color: #b9a8ff; border-color: #8f74ff; }
}

/* Keyboard */

#keyboard {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px 12px;
  display: grid;
  gap: 7px;
}
.key-row { display: flex; gap: 5px; justify-content: center; touch-action: manipulation; }
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: var(--key);
  color: var(--key-text);
  box-shadow: 0 3px 0 var(--shadow);
  font-weight: 800;
  font-size: 0.95rem;
  text-transform: uppercase;
}
.key:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--shadow); }
.key.wide { flex: 1.6 1 0; font-size: 0.75rem; }
.key-row .spacer { flex: 0.5 1 0; }
.key[data-state="correct"] { background: var(--correct); color: #ffffff; }
.key[data-state="present"] { background: var(--present); color: #ffffff; }
.key[data-state="absent"] { background: var(--key-absent); color: var(--key-absent-text); box-shadow: none; }

/* Dialogs */

dialog {
  width: min(400px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 24px;
  border: 0;
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
dialog::backdrop { background: rgba(18, 12, 50, 0.55); }
dialog h2 { margin: 0 0 12px; font-size: 1.3rem; font-weight: 800; }
dialog h3 { margin: 18px 0 8px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
dialog p { margin: 0 0 12px; line-height: 1.45; }

.legend, .week { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 8px; }
.legend li { display: flex; align-items: center; gap: 10px; line-height: 1.35; }
.week li { line-height: 1.4; }
.week strong { display: block; }
.tile.sample { --tile: 34px; flex: none; border-radius: 9px; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.stat-row strong { display: block; font-size: 1.8rem; font-weight: 800; }
.stat-row span { font-size: 0.72rem; color: var(--muted); }

#result-line { font-weight: 700; }
#result-line:empty { display: none; }

#distribution { display: grid; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; font-weight: 700; }
.dist-row > span { width: 10px; }
.dist-row .bar {
  min-width: 24px;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--absent);
  color: #ffffff;
  text-align: right;
}
.dist-row .bar.current { background: linear-gradient(90deg, var(--brand), var(--brand-2)); }

#countdown-line { margin-top: 16px; color: var(--muted); }
#countdown { color: var(--text); font-variant-numeric: tabular-nums; }

.dialog-actions { display: flex; gap: 8px; margin-top: 6px; }
dialog button {
  flex: 1;
  padding: 12px 14px;
  border: 2px solid var(--tile-border);
  border-radius: 12px;
  background: transparent;
  font-weight: 800;
}
dialog button.primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #ffffff;
}
dialog button[hidden] { display: none; }
#help-dialog button { width: 100%; }

#share-fallback p { margin: 14px 0 8px; font-size: 0.85rem; color: var(--muted); }
#share-fallback img { display: block; width: 100%; border-radius: 12px; }
#share-fallback[hidden] { display: none; }

dialog .text-button {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 8px;
  border: 0;
  color: var(--muted);
  font-weight: 700;
}

/* Header button groups */

.bar-side { display: flex; gap: 8px; }
.bar-side.end { justify-content: flex-end; }

/* Past puzzles (calendar) */

.lv1 { --lv: #10b3a3; }
.lv2 { --lv: #2fa4d4; }
.lv3 { --lv: #4b84f5; }
.lv4 { --lv: #6c5cff; }
.lv5 { --lv: #9b4ff2; }
.lv6 { --lv: #d24bc6; }
.lv7 { --lv: #ff4f8b; }

body.archive-open { overflow: hidden; }

#archive {
  position: fixed;
  inset: 0;
  z-index: 5;
  overflow-y: auto;
  background: radial-gradient(ellipse 80% 45% at 50% 0%, var(--bg-glow), transparent 70%), var(--bg);
}
#archive[hidden] { display: none; }

.archive-inner {
  max-width: 500px;
  margin: 0 auto;
  padding: 12px 16px 28px;
}

.archive-top {
  display: grid;
  grid-template-columns: 38px 1fr 38px;
  align-items: center;
}
.archive-top h2 { margin: 0; text-align: center; font-size: 1.3rem; font-weight: 800; }
.icon-button svg path { fill: currentColor; }

#archive-summary {
  margin: 8px 0 16px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
}

.month-bar {
  display: grid;
  grid-template-columns: 38px 1fr 38px;
  align-items: center;
  margin-bottom: 12px;
}
.month-bar h3 { margin: 0; text-align: center; font-size: 1.05rem; font-weight: 800; }
.month-bar .icon-button { font-size: 1.4rem; line-height: 1; }
.month-bar .icon-button:disabled { opacity: 0.3; cursor: default; }

#calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}

.weekday {
  padding-bottom: 6px;
  border-bottom: 4px solid var(--lv);
  border-radius: 0 0 2px 2px;
  text-align: center;
  font-size: 0.78rem;
  font-weight: 800;
  color: var(--muted);
}

.cal-day {
  position: relative;
  aspect-ratio: 1 / 1.1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0 0 4px;
  border: 2px solid transparent;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 2px 0 var(--shadow);
  font-weight: 800;
  overflow: hidden;
}
/* Level colour along the bottom of each day */
.cal-day::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: var(--lv);
}
.cal-date { font-size: 1rem; }
.cal-mark { min-height: 0.9em; font-size: 0.68rem; line-height: 1; }

.cal-day.won { background: var(--correct); color: #ffffff; }
.cal-day.lost { background: var(--absent); color: #ffffff; }
.cal-day.playing { border-color: var(--present); }
.cal-day.playing .cal-mark { color: var(--present); letter-spacing: 1px; }
.cal-day.today { border-color: var(--brand); }
.cal-day.today.playing { border-color: var(--brand); }
.cal-day.current { outline: 3px solid var(--brand-2); outline-offset: 2px; }
.cal-day:hover:not(.locked) { transform: translateY(-1px); }

.cal-day.locked {
  background: transparent;
  box-shadow: none;
  color: var(--muted);
  opacity: 0.35;
}
.cal-day.locked::after { display: none; }

.calendar-key {
  list-style: none;
  margin: 20px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  font-size: 0.8rem;
  color: var(--muted);
}
.calendar-key li { display: flex; align-items: center; gap: 8px; }
.key-swatch {
  flex: none;
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-radius: 5px;
  background: var(--surface);
}
.key-swatch.won { background: var(--correct); }
.key-swatch.lost { background: var(--absent); }
.key-swatch.playing { border-color: var(--present); }
.key-swatch.new { border-color: var(--tile-border); }
.cal-day.new:not(.today) { border-color: var(--tile-border); }

.archive-note { margin: 16px 0 0; font-size: 0.8rem; line-height: 1.45; color: var(--muted); }
