:root {
  --bg: #14110e;
  --bg-panel: #1e1a16;
  --bg-panel-2: #262019;
  --line: #3a3128;
  --text: #f0e6d8;
  --muted: #a4957f;
  --wood-1: #f0d5a4;
  --wood-2: #c69c62;
  --wood-3: #8a6636;
  --head: #d8422e;
  --head-dark: #92251a;
  --accent: #e8a33d;
  --win: #6fc47a;
  --lose: #e0685a;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 2rem 1rem 4rem;
  background:
    radial-gradient(1200px 600px at 50% -10%, #241d16 0%, transparent 70%),
    var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

main { max-width: 940px; margin: 0 auto; }

header { text-align: center; margin-bottom: 2rem; }

h1 {
  margin: 0;
  font-size: clamp(2rem, 6vw, 3rem);
  letter-spacing: -0.02em;
}
h1 span { color: var(--accent); }

.sub { margin: 0.4rem 0 0; color: var(--muted); font-size: 0.95rem; }

/* ---------- Brett ---------- */

.board-wrap {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.5rem 1rem 1rem;
}

#board {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-height: 96px;
  /* iOS markiert sonst beim langen Druecken Text und blendet das Lupen-Menue ein —
     genau die Geste, mit der hier gezielt wird. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  /* pan-y: senkrechtes Wischen scrollt weiter die Seite (der Browser schickt dann
     pointercancel und das Zielen bricht ab), waagerechtes Ziehen gehoert uns. */
  touch-action: pan-y;
}
#board:empty { min-height: 0; }   /* am Partieende uebernimmt das Ergebnisfeld */

.row {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 0;                 /* der Abstand kommt aus .gap, damit er anklickbar sein kann */
  min-height: 96px;
  position: relative;
}

/* Zwischenraum zwischen zwei Hölzern; bei Kayles der Klickpunkt für zwei benachbarte */
.gap {
  appearance: none;
  border: 0;
  background: none;
  padding: 0;
  width: clamp(6px, 1.6vw, 14px);
  height: 82px;
  position: relative;
  z-index: 1;
}

/* Bei Kayles ist die Lücke ein echtes Ziel und braucht Luft — sonst berühren
   sich die überstehenden Köpfe und man trifft sie versehentlich. */
#board.kayles .gap { width: clamp(18px, 2.6vw, 26px); }

button.gap { cursor: pointer; }

/* Markierung der Lücke: senkrechter Strich auf Stielhöhe, wird beim Überfahren kräftig */
#board.kayles button.gap::before {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: 26px;
  bottom: 6px;
  width: 2px;
  border-radius: 1px;
  background: var(--muted);
  opacity: 0.22;
  transition: opacity 0.14s ease, background 0.14s ease;
}
@media (hover: hover) {
  #board.kayles:not(.locked) button.gap:hover::before {
    opacity: 1;
    background: var(--accent);
  }
}

/* Beim Zielen per Finger markiert die Vorschau die Luecke selbst. */
#board.kayles:not(.locked) button.gap.preview::before {
  opacity: 1;
  background: var(--accent);
}
#board.kayles.locked button.gap::before { opacity: 0.1; }

/* Trefferfläche der Lücke bis auf Kopfhöhe hochziehen, damit dort keine toten
   Punkte bleiben. Liegt hinter den Hölzern (z-index), die Vorrang behalten. */
#board.kayles button.gap::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -14px;
  bottom: 0;
}

.row-label {
  position: absolute;
  left: 0.25rem;
  bottom: 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Ein Streichholz */
.match {
  appearance: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  width: clamp(8px, 2vw, 12px);
  height: 82px;
  border-radius: 3px 3px 2px 2px;
  background: linear-gradient(90deg, var(--wood-3), var(--wood-1) 40%, var(--wood-2));
  position: relative;
  transition: transform 0.14s ease, opacity 0.3s ease, filter 0.14s ease;
  transform-origin: bottom center;
}

.match::before {
  content: "";
  position: absolute;
  top: -9px;
  left: 50%;
  transform: translateX(-50%);
  width: 175%;
  height: 22px;
  border-radius: 50% 50% 45% 45%;
  background: radial-gradient(circle at 35% 30%, #ff7a5e, var(--head) 55%, var(--head-dark));
}

/* Vorschau der wegfallenden Hölzer — welche das sind, entscheidet das Spiel */
#board:not(.locked) .match.preview {
  transform: translateY(-14px) rotate(-3deg);
  filter: brightness(1.18);
}

#board.locked .match { cursor: default; }
span.match { cursor: default; }   /* Hölzer ohne zugehörigen Zug */

.match.taken {
  transform: translateY(60px) rotate(28deg) !important;
  opacity: 0;
  pointer-events: none;
}

.match.hint {
  outline: 2px dashed var(--accent);
  outline-offset: 4px;
  border-radius: 4px;
}

/* Kayles: das Holz besitzt die Fläche unter seinem überstehenden Kopf, damit
   ein Klick auf den Kopf zuverlässig EIN Holz nimmt und nicht in der Lücke landet. */
#board.kayles .match { z-index: 2; }
#board.kayles button.match::after {
  content: "";
  position: absolute;
  left: -6px;
  right: -6px;
  top: -14px;
  bottom: 0;
}

/* ---------- Status ---------- */

#status {
  margin: 1.2rem 0 0.2rem;
  text-align: center;
  font-size: 1.15rem;
  font-weight: 600;
  min-height: 1.6em;
}
#status.win { color: var(--win); }
#status.lose { color: var(--lose); }
#status:empty { display: none; }   /* am Partieende spricht das Ergebnisfeld */

#last-move {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 1.4em;
}

#board-hint {
  margin: 0.5rem 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}
#board-hint:empty { display: none; }
#board-hint b { color: var(--text); font-weight: 600; }

/* Zielanzeige waehrend des Fingerdrucks: sagt vor dem Loslassen, was passiert. */
#aim {
  margin: 0.75rem auto 0;
  width: fit-content;
  max-width: 100%;
  padding: 0.35rem 0.85rem;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 600;
  color: #211703;
  background: var(--accent);
  border-radius: 999px;
}
#aim[hidden] { display: none; }
#aim.empty {
  color: var(--muted);
  background: var(--bg-panel-2);
  border: 1px solid var(--line);
  font-weight: 400;
}

/* ---------- Ergebnis ---------- */

#result {
  max-width: 34rem;
  margin: 1rem auto 0.25rem;
  padding: 1.1rem 1.25rem 1.25rem;
  text-align: center;
  background: var(--bg-panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  animation: result-in 0.28s ease both;
}
#result[hidden] { display: none; }
#result.win { border-color: var(--win); }
#result.lose { border-color: var(--lose); }

#result-title { margin: 0; font-size: 1.5rem; font-weight: 700; letter-spacing: -0.01em; }
#result.win #result-title { color: var(--win); }
#result.lose #result-title { color: var(--lose); }

#result-text { margin: 0.35rem 0 0; font-size: 0.95rem; }
#result-note, #result-score { margin: 0.45rem 0 0; color: var(--muted); font-size: 0.85rem; }
#result-note:empty { display: none; }
#result-score { font-variant-numeric: tabular-nums; }

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 1rem;
}

.result-actions button:not(.primary) { background: var(--bg-panel); }

@keyframes result-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* ---------- Bedienung ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  align-items: center;
  justify-content: center;
  margin: 1.25rem 0;
  padding: 1rem;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.field { display: flex; align-items: center; gap: 0.5rem; font-size: 0.9rem; }
.field > span { color: var(--muted); }
.field[hidden] { display: none; }   /* schlaegt das UA-Stylesheet nicht von allein */

select, button {
  /* iOS: kein grauer Tap-Blitz, keine Textauswahl beim langen Druecken und
     kein Doppeltipp-Zoom, der eine Schaltflaeche zum Zielobjekt macht. */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--bg-panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
}
@media (hover: hover) {
  select:hover, button:hover { border-color: var(--accent); }
}

button:disabled {
  opacity: 0.4;
  cursor: default;
  border-color: var(--line);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #211703;
  font-weight: 600;
}
@media (hover: hover) {
  button.primary:hover { filter: brightness(1.1); }
}

label.check { display: flex; align-items: center; gap: 0.4rem; cursor: pointer; font-size: 0.9rem; }
label.check input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ---------- Analyse ---------- */

#analysis {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
}
#analysis[hidden] { display: none; }

#analysis h2 { margin: 0 0 0.75rem; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }

.a-grid { display: grid; grid-template-columns: max-content 1fr; gap: 0.4rem 1rem; font-size: 0.92rem; }
.a-grid dt { color: var(--muted); }
.a-grid dd { margin: 0; font-variant-numeric: tabular-nums; }

.verdict-win { color: var(--win); font-weight: 600; }
.verdict-lose { color: var(--lose); font-weight: 600; }
.muted-note { color: var(--muted); }

@media (max-width: 560px) {
  .row { min-height: 76px; }
  .gap { height: 64px; }
  #board { min-height: 76px; }
}

code { background: var(--bg-panel-2); padding: 0.1em 0.4em; border-radius: 5px; font-size: 0.9em; }

/* ---------- Statistik + Theorie ---------- */

.stats { text-align: center; color: var(--muted); font-size: 0.9rem; margin: 0 0 1.25rem; }
.stats b { color: var(--text); font-variant-numeric: tabular-nums; }
.stats button { padding: 0.15rem 0.5rem; font-size: 0.8rem; margin-left: 0.5rem; }

.theory {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}
.theory summary { cursor: pointer; font-weight: 600; }
.theory h3 { font-size: 1rem; margin: 1.2rem 0 0.4rem; }
.theory p, .theory li { color: #d9cdba; font-size: 0.93rem; }
.theory table { border-collapse: collapse; width: 100%; margin-top: 0.5rem; font-size: 0.88rem; }
.theory th, .theory td { text-align: left; padding: 0.35rem 0.6rem; border-bottom: 1px solid var(--line); }
.theory th { color: var(--muted); font-weight: 500; }
.theory td.num { font-variant-numeric: tabular-nums; }

footer { text-align: center; color: var(--muted); font-size: 0.82rem; margin-top: 2rem; }
footer a { color: var(--muted); }

@media (max-width: 560px) {
  .match { height: 64px; }
  .row { min-height: 76px; }
}

/* Am Touchscreen laesst sich die Vorschau schlechter neben dem Finger erkennen —
   deshalb hebt sie das Holz weiter an und faerbt den Kopf. */
@media (hover: none) {
  #board:not(.locked) .match.preview {
    transform: translateY(-20px) rotate(-3deg);
    filter: brightness(1.3);
  }
}

@media (prefers-reduced-motion: reduce) {
  #result { animation: none; }
  .match { transition: opacity 0.2s linear; }
  #board:not(.locked) .match.preview { transform: none; }
  .match.taken { transform: none !important; }
}
