/* Wordgrid — a daily five-letter word puzzle. */

:root {
  color-scheme: light dark;

  --bg: #ffffff;
  --fg: #1a1a1b;
  --fg-dim: #787c7e;
  --line: #d3d6da;
  --line-strong: #878a8c;
  --key-bg: #d3d6da;
  --key-fg: #1a1a1b;
  --tile-empty-border: #d3d6da;
  --tile-filled-border: #878a8c;
  --overlay: rgba(255, 255, 255, .92);
  --shadow: 0 6px 28px rgba(0, 0, 0, .18);

  --hit: #6aaa64;
  --near: #c9b458;
  --miss: #787c7e;
  --on-mark: #ffffff;

  --tile-size: clamp(48px, min(15vw, 9.5vh), 62px);
  --gap: 5px;
  --radius: 4px;
  --reveal: 300ms;
}

/* High contrast: the green/yellow pair is the single worst thing about this
   game for the ~8% of men with red-green colour deficiency. Orange/blue is
   distinguishable under every common form of it. */
:root[data-contrast="high"] {
  --hit: #f5793a;
  --near: #85c0f9;
  --on-mark: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121213;
    --fg: #ffffff;
    --fg-dim: #818384;
    --line: #3a3a3c;
    --line-strong: #565758;
    --key-bg: #818384;
    --key-fg: #ffffff;
    --tile-empty-border: #3a3a3c;
    --tile-filled-border: #565758;
    --overlay: rgba(18, 18, 19, .92);
    --shadow: 0 6px 28px rgba(0, 0, 0, .6);
    --miss: #3a3a3c;
  }
}

:root[data-theme="dark"] {
  --bg: #121213;
  --fg: #ffffff;
  --fg-dim: #818384;
  --line: #3a3a3c;
  --line-strong: #565758;
  --key-bg: #818384;
  --key-fg: #ffffff;
  --tile-empty-border: #3a3a3c;
  --tile-filled-border: #565758;
  --overlay: rgba(18, 18, 19, .92);
  --shadow: 0 6px 28px rgba(0, 0, 0, .6);
  --miss: #3a3a3c;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* --- top bar ------------------------------------------------------------ */

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 12px;
  height: 52px;
  border-bottom: 1px solid var(--line);
  flex: none;
}

.wordmark {
  margin: 0;
  font-size: clamp(22px, 6vw, 30px);
  font-weight: 700;
  letter-spacing: .01em;
  text-align: center;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
}

.icon-btn {
  appearance: none;
  background: none;
  border: 0;
  padding: 6px;
  cursor: pointer;
  color: var(--fg);
  border-radius: 50%;
  display: inline-flex;
  line-height: 0;
}
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:hover { background: color-mix(in srgb, var(--fg) 10%, transparent); }
.icon-btn:focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }

/* --- board -------------------------------------------------------------- */

main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.board-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 8px;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile-size));
  gap: var(--gap);
}

.row {
  display: grid;
  grid-template-columns: repeat(5, var(--tile-size));
  gap: var(--gap);
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--tile-empty-border);
  border-radius: var(--radius);
  font-size: calc(var(--tile-size) * .55);
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
  background: transparent;
  color: var(--fg);
}

.tile.filled {
  border-color: var(--tile-filled-border);
  animation: pop 100ms ease-out;
}

@keyframes pop {
  from { transform: scale(.9); }
  40%  { transform: scale(1.08); }
  to   { transform: scale(1); }
}

/* Flip on reveal. The colour is applied at the halfway point by JS adding
   .revealed, so the face changes while the tile is edge-on. */
.tile.flipping { animation: flip var(--reveal) ease-in forwards; }

@keyframes flip {
  0%   { transform: rotateX(0); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

.tile.revealed { border-color: transparent; color: var(--on-mark); }
.tile.revealed.hit  { background: var(--hit); }
.tile.revealed.near { background: var(--near); }
.tile.revealed.miss { background: var(--miss); }

.row.invalid { animation: shake 500ms; }

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}

.tile.bounce { animation: bounce 700ms; }

@keyframes bounce {
  0%, 20%  { transform: translateY(0); }
  40%      { transform: translateY(-26px); }
  50%      { transform: translateY(6px); }
  60%      { transform: translateY(-14px); }
  80%      { transform: translateY(3px); }
  100%     { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .tile.flipping, .tile.bounce, .row.invalid, .tile.filled { animation: none; }
}

/* --- keyboard ----------------------------------------------------------- */

.keyboard {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 6px max(8px, env(safe-area-inset-bottom));
  max-width: 500px;
  width: 100%;
  margin: 0 auto;
}

.kb-row {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.kb-row .spacer { flex: .5; }

.key {
  appearance: none;
  border: 0;
  border-radius: var(--radius);
  background: var(--key-bg);
  color: var(--key-fg);
  font-family: inherit;
  font-size: clamp(11px, 3.4vw, 14px);
  font-weight: 700;
  text-transform: uppercase;
  height: clamp(48px, 7.5vh, 58px);
  flex: 1;
  min-width: 0;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 120ms ease;
}

.key.wide { flex: 1.5; }
.key svg { width: 20px; height: 20px; fill: currentColor; }
.key:active { filter: brightness(.88); }
.key:focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }

.key.hit  { background: var(--hit);  color: var(--on-mark); }
.key.near { background: var(--near); color: var(--on-mark); }
.key.miss { background: var(--miss); color: #ffffff; }

/* --- toast -------------------------------------------------------------- */

.toast {
  position: fixed;
  top: 68px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 13px 18px;
  border-radius: var(--radius);
  font-weight: 700;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 60;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { opacity: 1; }

/* --- dialogs ------------------------------------------------------------ */

.dialog {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  padding: 16px 20px 22px;
  width: min(92vw, 420px);
  box-shadow: var(--shadow);
  position: relative;
}
.dialog::backdrop { background: rgba(0, 0, 0, .5); }

.dialog h2 {
  margin: 4px 0 14px;
  font-size: 17px;
  text-transform: uppercase;
  letter-spacing: .06em;
  text-align: center;
  font-weight: 700;
}
.dialog h3 {
  margin: 20px 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  text-align: center;
  font-weight: 700;
}
.dialog p { margin: 10px 0; font-size: 14px; line-height: 1.5; }
.dialog .fine { font-size: 13px; color: var(--fg-dim); }

.dialog-close-form { position: absolute; top: 8px; right: 10px; margin: 0; }
.dialog-close { font-size: 26px; line-height: 1; padding: 2px 8px; }

.example { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.example-row { display: flex; gap: 4px; margin-bottom: 4px; }
.example .tile {
  --tile-size: 36px;
  width: 36px; height: 36px;
  font-size: 20px;
  border-width: 2px;
}
.example .tile.revealed:not(.hit):not(.near):not(.miss) {
  background: transparent;
  color: var(--fg);
  border-color: var(--tile-filled-border);
}

/* --- stats -------------------------------------------------------------- */

.stats-row { display: flex; justify-content: center; }
.stat { flex: 1; text-align: center; padding: 0 4px; }
.stat-value { font-size: 32px; font-weight: 400; line-height: 1.1; }
.stat-label { font-size: 11px; line-height: 1.3; margin-top: 2px; }

.dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.dist-key { width: 10px; text-align: right; flex: none; }
.dist-bar {
  background: var(--fg-dim);
  color: #ffffff;
  padding: 1px 8px;
  text-align: right;
  font-weight: 700;
  min-width: 26px;
  border-radius: 2px;
}
.dist-bar.current { background: var(--hit); }

.stats-footer {
  display: flex;
  align-items: center;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  gap: 12px;
}
.countdown { flex: 1; text-align: center; }
.countdown-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.countdown-clock { font-size: 28px; font-variant-numeric: tabular-nums; }

.btn-share {
  flex: 1;
  appearance: none;
  border: 0;
  border-radius: 4px;
  background: var(--hit);
  color: var(--on-mark);
  font-family: inherit;
  font-size: 17px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 14px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn-share svg { width: 22px; height: 22px; fill: currentColor; }
.btn-share:focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }

/* --- settings ----------------------------------------------------------- */

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}
.setting-name { font-size: 15px; }
.setting-note { font-size: 12px; color: var(--fg-dim); margin-top: 2px; }

.switch { flex: none; cursor: pointer; display: inline-flex; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 44px; height: 24px;
  background: var(--fg-dim);
  border-radius: 12px;
  position: relative;
  transition: background-color 150ms ease;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #ffffff;
  transition: transform 150ms ease;
}
.switch input:checked + .switch-track { background: var(--hit); }
.switch input:checked + .switch-track::after { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--fg); outline-offset: 2px; }
.switch input:disabled + .switch-track { opacity: .4; cursor: not-allowed; }

/* Very short viewports: give the board what little room there is. */
@media (max-height: 600px) {
  .topbar { height: 44px; }
  .wordmark { font-size: 20px; }
  .keyboard { gap: 6px; padding-top: 4px; }
  .key { height: 44px; }
}
