/* Layout: one screen. Header HUD on top, the night-desert play field in the
   middle (stars, dunes, a carpet-border ground line), the typing bar below.
   A single dark look by choice: the game is set at night. */
:root {
  --night: #16183d;
  --night-deep: #0c0d24;
  --dusk: #2b2560;
  --dune-back: #3a2f5a;
  --dune-front: #4a3550;
  --sand: #e2bd7c;
  --saffron: #f5a83b;
  --carpet: #a3202f;
  --carpet-deep: #5e1220;
  --ink: #f6efe1;
  --muted: #aaa6cf;
  --line: rgba(246, 239, 225, 0.14);
  --panel: rgba(16, 17, 46, 0.86);
  --hit: #8de6a6;
  --danger: #ff7062;

  --f-display: 'Unbounded', 'Arial Black', system-ui, sans-serif;
  --f-body: 'Onest', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  --r: 10px;
  --app-h: 100%;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  background: var(--night-deep);
  color: var(--ink);
  font-family: var(--f-body);
  font-size: 15px;
  overflow: hidden;
  overscroll-behavior: none;
}

.app {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: var(--app-h);
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--night-deep);
}

/* ---------- header / HUD ---------- */
.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-block: 8px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  border-bottom: 1px solid var(--line);
}
.brand {
  font-family: var(--f-display);
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.02em;
  color: var(--saffron);
  white-space: nowrap;
}
.back {
  flex: none;
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  white-space: nowrap;
}
.back:hover { color: var(--ink); border-color: var(--muted); }
.hud {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 18px;
  margin: 0;
}
.stat { display: flex; flex-direction: column; align-items: center; min-width: 40px; }
.stat dt {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}
.stat dd {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 700;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}
.combo.is-hot dd { color: var(--saffron); }
#hud-time-box.is-low dd { color: var(--danger); }
.lives { display: flex; gap: 4px; height: 22px; align-items: center; }
.life {
  width: 10px; height: 10px;
  background: var(--carpet);
  transform: rotate(45deg);
  box-shadow: 0 0 0 1.5px var(--sand) inset;
}
.life.is-lost { background: transparent; box-shadow: 0 0 0 1.5px var(--line) inset; }

.icon-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  width: 38px; height: 38px;
  border-radius: 50%;
  display: grid; place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn svg path { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .spk { fill: currentColor; }
.icon-btn .cross { display: none; }
.icon-btn[aria-pressed="true"] .wave { display: none; }
.icon-btn[aria-pressed="true"] .cross { display: inline; }
.icon-btn[aria-pressed="true"] { color: var(--muted); }

/* ---------- play field ---------- */
.field {
  position: relative;
  overflow: hidden;
  min-height: 0;
  background:
    radial-gradient(120% 70% at 50% 110%, #5a2f4f 0%, transparent 60%),
    linear-gradient(180deg, var(--night-deep) 0%, var(--night) 55%, var(--dusk) 100%);
}
.sky { position: absolute; inset: 0; width: 100%; height: 100%; }
.dunes {
  position: absolute;
  left: 0; right: 0;
  bottom: 26px;
  width: 100%;
  height: 16%;
  min-height: 60px;
}
.dune-back { fill: var(--dune-back); }
.dune-front { fill: var(--dune-front); }

/* The ground line is a carpet border: a row of octagonal göl motifs. */
.ground {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 26px;
  background-color: var(--carpet);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='26' viewBox='0 0 40 26'%3E%3Cpath d='M14 4h12l6 6v6l-6 6H14l-6-6v-6z' fill='%235e1220'/%3E%3Cpath d='M20 7l6 6-6 6-6-6z' fill='%23e2bd7c'/%3E%3Cpath d='M20 10.5l2.5 2.5-2.5 2.5-2.5-2.5z' fill='%23a3202f'/%3E%3Cpath d='M0 13l3-3 3 3-3 3zM34 13l3-3 3 3-3 3z' fill='%23e2bd7c'/%3E%3C/svg%3E"),
    linear-gradient(180deg, var(--sand) 0 2px, transparent 2px calc(100% - 2px), var(--sand) calc(100% - 2px));
  background-repeat: repeat-x, no-repeat;
  background-position: center, 0 0;
}
.field.is-hurt .ground { animation: hurt 0.35s; }
@keyframes hurt { 50% { filter: brightness(1.8) saturate(1.4); } }

.layer { position: absolute; inset: 0; }

.word {
  position: absolute;
  left: 0; top: 0;
  will-change: transform;
  font-family: var(--f-display);
  font-weight: 500;
  font-size: clamp(16px, 2.2vw + 8px, 24px);
  line-height: 1;
  padding: 7px 11px 8px;
  border-radius: 6px;
  color: var(--ink);
  background: rgba(12, 13, 36, 0.55);
  border: 1px solid var(--line);
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}
.word.is-place { color: var(--sand); }
.word.is-target {
  border-color: var(--saffron);
  background: rgba(12, 13, 36, 0.85);
  box-shadow: 0 0 0 3px rgba(245, 168, 59, 0.18);
}
.word .typed { color: var(--hit); }

.pop {
  position: absolute;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-width: 180px;
  animation: pop 1.5s ease-out forwards;
}
.pop b { font-family: var(--f-display); font-weight: 700; font-size: 15px; color: var(--hit); }
.pop span { font-size: 15px; color: var(--ink); }
.pop i { font-style: normal; font-family: var(--f-display); font-size: 12px; color: var(--saffron); }
@keyframes pop {
  0% { opacity: 0; transform: translateY(6px); }
  12% { opacity: 1; transform: translateY(0); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-26px); }
}
.burst {
  position: absolute;
  width: 8px; height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
  pointer-events: none;
  box-shadow: 0 0 0 2px var(--saffron);
  animation: burst 0.55s ease-out forwards;
}
@keyframes burst { to { transform: scale(9); opacity: 0; } }
.crash {
  position: absolute;
  font-family: var(--f-display);
  font-size: 20px;
  color: var(--danger);
  pointer-events: none;
  padding: 7px 11px;
  animation: crash 0.7s ease-in forwards;
}
@keyframes crash { to { opacity: 0; transform: translateY(14px) scaleY(0.4); filter: blur(2px); } }
.banner {
  position: absolute;
  left: 50%; top: 38%;
  transform: translate(-50%, -50%);
  font-family: var(--f-display);
  font-weight: 800;
  font-size: clamp(28px, 6vw, 56px);
  color: var(--saffron);
  pointer-events: none;
  white-space: nowrap;
  animation: banner 1.4s ease-out forwards;
}
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -40%) scale(0.9); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%); }
}

/* ---------- overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-block: 24px 48px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: rgba(8, 9, 28, 0.55);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  z-index: 5;
}
.panel {
  width: 100%;
  max-width: 620px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-block: auto;
}
.panel-tight { max-width: 360px; align-items: center; text-align: center; }

.title {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 800;
  font-size: clamp(44px, 11vw, 92px);
  line-height: 0.95;
  letter-spacing: -0.02em;
  text-wrap: balance;
  color: var(--ink);
}
.title span { color: var(--saffron); }
.title-sm {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 800;
  font-size: clamp(28px, 6vw, 44px);
  text-wrap: balance;
}
.tagline { margin: -6px 0 0; font-size: 17px; color: var(--muted); max-width: 46ch; }

.options { display: flex; flex-direction: column; gap: 12px; }
.seg {
  border: 0;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.seg legend {
  padding: 0 0 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}
.seg button {
  appearance: none;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 10px 12px;
  border-radius: var(--r);
  border: 1px solid var(--line);
  background: rgba(246, 239, 225, 0.04);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  min-width: 0;
}
.seg button b { font-weight: 700; font-size: 15px; }
.seg button small { font-size: 12.5px; color: var(--muted); line-height: 1.35; }
.seg button[aria-pressed="true"] {
  border-color: var(--saffron);
  background: rgba(245, 168, 59, 0.12);
}
.seg button[aria-pressed="true"] b { color: var(--saffron); }
.seg-small button { padding-block: 8px; }

.start-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.hint { margin: 0; color: var(--muted); font-size: 13.5px; }
.hint b { color: var(--ink); font-variant-numeric: tabular-nums; }
.error { margin: 0; color: var(--danger); }
.row { display: flex; flex-wrap: wrap; gap: 10px; }

.btn-primary, .btn-secondary, .btn-ghost {
  appearance: none;
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  cursor: pointer;
  padding: 12px 26px;
  border: 1px solid transparent;
}
.btn-primary {
  font-family: var(--f-display);
  font-size: 17px;
  background: var(--saffron);
  color: #241505;
}
.btn-primary:hover:not(:disabled) { background: #ffb954; }
.btn-primary:disabled { opacity: 0.55; cursor: progress; }
.btn-secondary { background: var(--ink); color: var(--night-deep); padding: 10px 18px; }
.btn-secondary:disabled { opacity: 0.5; }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line); }
:focus-visible { outline: 2px solid var(--saffron); outline-offset: 2px; }

/* results */
.result-big { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.result-big span {
  font-family: var(--f-display);
  font-weight: 800;
  font-size: clamp(44px, 12vw, 80px);
  line-height: 1;
  color: var(--saffron);
  font-variant-numeric: tabular-nums;
}
.result-big small { color: var(--muted); font-size: 14px; text-transform: uppercase; letter-spacing: 0.1em; }
.new-best { margin: -8px 0 0; color: var(--hit); font-weight: 700; }
.result-grid {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  background: var(--night);
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: hidden;
}
.result-grid div { padding: 10px 12px; box-shadow: 0 0 0 0.5px var(--line); }
.result-grid dt { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.result-grid dd { margin: 2px 0 0; font-family: var(--f-display); font-weight: 700; font-size: 20px; font-variant-numeric: tabular-nums; }


.list-title { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); font-weight: 500; }
.learned {
  list-style: none;
  margin: -8px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 4px 16px;
}
.learned li { display: flex; gap: 8px; align-items: baseline; padding: 4px 0; border-bottom: 1px dotted var(--line); min-width: 0; }
.learned b { font-family: var(--f-display); font-weight: 500; font-size: 14px; color: var(--sand); white-space: nowrap; }
.learned span { color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- typing bar ---------- */
.bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--night-deep);
  border-top: 1px solid var(--line);
}
.letters { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; max-width: 620px; width: 100%; margin: 0 auto; }
.letters button {
  appearance: none;
  font-family: var(--f-display);
  font-size: 17px;
  padding: 8px 0;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(246, 239, 225, 0.06);
  color: var(--sand);
  cursor: pointer;
  touch-action: manipulation;
}
.letters button:active { background: rgba(245, 168, 59, 0.25); }
.type {
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  font-family: var(--f-display);
  font-size: 20px;
  text-align: center;
  padding: 12px 16px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  background: rgba(246, 239, 225, 0.06);
  color: var(--ink);
}
.type:focus { outline: none; border-color: var(--saffron); }
.type::placeholder { color: var(--muted); opacity: 0.6; }
.type.is-wrong { border-color: var(--danger); animation: shake 0.25s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ---------- small screens ---------- */
@media (max-width: 560px) {
  .brand { display: none; }
  .back { padding: 6px 9px; font-size: 12px; }
  .back span + span { display: none; }
  .hud { gap: 2px 12px; justify-content: space-between; }
  .stat { min-width: 0; }
  .stat dt { font-size: 9px; }
  .stat dd { font-size: 14px; }
  #hud-level-box { display: none; }
  .seg button small { font-size: 11.5px; }
  .type { font-size: 18px; padding-block: 10px; }
  .letters button { font-size: 16px; padding-block: 7px; }
}
@media (max-height: 520px) {
  .letters button { padding-block: 4px; }
  .bar { padding-block: 6px; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .type.is-wrong { animation: none; }
  .pop { animation-duration: 0.9s; }
  .burst, .crash { animation-duration: 0.2s; }
  .banner { animation-duration: 0.9s; }
  .field.is-hurt .ground { animation: none; }
}
