@font-face {
  font-family: 'Barlow Condensed';
  src: url('../fonts/barlow-condensed-latin-500-normal.woff2') format('woff2');
  font-weight: 500;
  font-display: swap;
}
@font-face {
  font-family: 'Barlow Condensed';
  src: url('../fonts/barlow-condensed-latin-600-normal.woff2') format('woff2');
  font-weight: 600;
  font-display: swap;
}
@font-face {
  font-family: 'Barlow';
  src: url('../fonts/barlow-latin-400-normal.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Barlow';
  src: url('../fonts/barlow-latin-600-normal.woff2') format('woff2');
  font-weight: 600;
  font-display: swap;
}

:root {
  color-scheme: light dark;
  --papier: #e6efec;
  --mm: rgb(77 150 164 / 0.13);
  --mm-cm: rgb(77 150 164 / 0.22);
  --blatt: #f7faf8;
  --feld: #fdfefd;
  --linie: #b5d3d8;
  --linie-dick: #4f8f9c;
  --tusche: #1f2b5c;
  --text: #1f2b5c;
  --leise: #5b6f86;
  --erledigt: #a8bcc4;
  --stift: #c8402f;
  --fehler-grund: #f6d5cf;
  --hl: #e3f0f2;
  --hl-stark: #cfe5e9;
  --fokus: #e08a1e;
  --schatten: 0 1px 0 rgb(31 43 92 / 0.08), 0 6px 18px -8px rgb(31 43 92 / 0.25);
  --zahl: 'Barlow Condensed', 'Arial Narrow', sans-serif;
  --schrift: 'Barlow', system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --papier: #111a2c;
    --mm: rgb(110 160 210 / 0.08);
    --mm-cm: rgb(110 160 210 / 0.14);
    --blatt: #172338;
    --feld: #1b2940;
    --linie: #2c4060;
    --linie-dick: #5f86ad;
    --tusche: #d6e4f3;
    --text: #d6e4f3;
    --leise: #93a8c2;
    --erledigt: #4a5f7a;
    --stift: #ef7a64;
    --fehler-grund: #4a2630;
    --hl: #213353;
    --hl-stark: #2a4066;
    --fokus: #f2a33a;
    --schatten: 0 1px 0 rgb(0 0 0 / 0.3), 0 8px 22px -10px rgb(0 0 0 / 0.7);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: 'kopf' 'leiste' 'tisch' 'fuss';
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(12px, env(safe-area-inset-left));
  font-family: var(--schrift);
  color: var(--text);
  background-color: var(--papier);
  background-image:
    linear-gradient(var(--mm-cm) 1px, transparent 1px),
    linear-gradient(90deg, var(--mm-cm) 1px, transparent 1px),
    linear-gradient(var(--mm) 1px, transparent 1px),
    linear-gradient(90deg, var(--mm) 1px, transparent 1px);
  background-size: 40px 40px, 40px 40px, 8px 8px, 8px 8px;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

button, input { font: inherit; color: inherit; }

/* Kopf */
.kopf {
  grid-area: kopf;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  align-items: baseline;
  column-gap: 12px;
  width: min(100%, 60rem);
  margin: 0 auto;
  padding: 10px 2px 4px;
}
.titel {
  margin: 0;
  font-family: var(--zahl);
  font-weight: 600;
  font-size: 1.75rem;
  line-height: 1;
  letter-spacing: 0.01em;
}
.raetsel {
  grid-column: 1;
  margin: 2px 0 0;
  font-size: 0.9rem;
  color: var(--leise);
  min-height: 1.2em;
}
.status {
  grid-column: 2;
  grid-row: 1 / span 2;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-variant-numeric: tabular-nums;
}
.zeit { font-family: var(--zahl); font-weight: 600; font-size: 1.6rem; line-height: 1; }
.bestzeit { font-size: 0.82rem; color: var(--leise); }

/* Knöpfe */
.knopf {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 14px;
  border: 1.5px solid var(--tusche);
  border-radius: 7px;
  background: var(--blatt);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  touch-action: manipulation;
}
.knopf svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.knopf:disabled { opacity: 0.4; cursor: default; }
.knopf:focus-visible, .pille input:focus-visible + span, .motiv:focus-visible, .teilen:focus-visible, .teilen-feld input:focus-visible { outline: 3px solid var(--fokus); outline-offset: 2px; }
.knopf-voll { background: var(--tusche); color: var(--blatt); }
.knopf-rund { padding: 0; width: 44px; }
.knopf-hilfe { border-radius: 50%; font-family: var(--zahl); font-size: 1.35rem; }

.leiste {
  grid-area: leiste;
  display: flex;
  gap: 8px;
  width: min(100%, 60rem);
  margin: 0 auto;
  padding: 6px 0;
}
.knopf-neu { margin-left: auto; }

/* Spielfläche */
.tisch {
  grid-area: tisch;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto auto;
  grid-template-areas: 'flaeche' 'meldung' 'werkzeuge';
  width: min(100%, 60rem);
  margin: 0 auto;
  min-height: 0;
}
.flaeche {
  grid-area: flaeche;
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  min-width: 0;
  padding: 6px 0;
  overflow: hidden;
}
.rahmen {
  overflow: hidden;
  border-radius: 6px;
  box-shadow: var(--schatten);
  background: var(--blatt);
}
.rahmen.gezoomt { overflow: auto; overscroll-behavior: contain; }
.warten {
  position: absolute;
  inset: auto 0 45%;
  margin: 0;
  text-align: center;
  font-family: var(--zahl);
  font-size: 1.3rem;
  color: var(--leise);
}
.flaeche[aria-busy] .rahmen { opacity: 0.25; }

.brett {
  display: grid;
  grid-template-columns: var(--hb) repeat(var(--n), var(--z));
  grid-template-rows: var(--hh) repeat(var(--n), var(--z));
  width: max-content;
  padding: 0 calc(var(--z) * 0.3) calc(var(--z) * 0.3) 0;
  background: var(--blatt);
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: pan-x pan-y;
  font-family: var(--zahl);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.ecke {
  grid-row: 1;
  grid-column: 1;
  position: sticky;
  top: 0;
  left: 0;
  z-index: 3;
  background: var(--blatt);
}
.hinweis {
  position: sticky;
  z-index: 2;
  background: var(--blatt);
  color: var(--text);
  font-size: calc(var(--z) * 0.6);
  line-height: 1.04;
  display: flex;
}
.hinweis-spalte {
  grid-row: 1;
  top: 0;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: calc(var(--z) * 0.14);
}
.hinweis-zeile {
  grid-column: 1;
  left: 0;
  justify-content: flex-end;
  align-items: center;
  gap: calc(var(--z) * 0.2);
  padding: 0 calc(var(--z) * 0.22) 0 calc(var(--z) * 0.12);
}
.hinweis.hl { background: var(--hl-stark); }
.hinweis-spalte.hl { border-radius: 4px 4px 0 0; }
.hinweis-zeile.hl { border-radius: 4px 0 0 4px; }
.hinweis span { transition: color 0.2s; }
.hinweis span.aus, .hinweis.erledigt span { color: var(--erledigt); }
.hinweis.falsch span { color: var(--stift); }

.zelle {
  position: relative;
  background: var(--feld);
  border-right: 1px solid var(--linie);
  border-bottom: 1px solid var(--linie);
  touch-action: none;
}
.zelle.rand-l { border-left: 1.5px solid var(--linie-dick); }
.zelle.rand-o { border-top: 1.5px solid var(--linie-dick); }
.zelle.dick-r, .zelle.rand-r { border-right: 1.5px solid var(--linie-dick); }
.zelle.dick-u, .zelle.rand-u { border-bottom: 1.5px solid var(--linie-dick); }
.zelle.hl { background: var(--hl); }
.zelle::after { content: ''; position: absolute; inset: 0; }
.zelle[data-s='f']::after { inset: 1px; background: var(--tusche); border-radius: 1px; }
.zelle[data-s='x']::after {
  inset: 22%;
  background:
    linear-gradient(45deg, transparent 43%, var(--stift) 43% 57%, transparent 57%),
    linear-gradient(-45deg, transparent 43%, var(--stift) 43% 57%, transparent 57%);
  opacity: 0.85;
}
.zelle.fehler { background: var(--fehler-grund); }
.zelle.fehler[data-s='f']::after { background: var(--stift); }
.zelle.fehler[data-s='f']::before {
  content: '';
  position: absolute;
  inset: 34%;
  z-index: 1;
  border-radius: 50%;
  background: var(--feld);
}
.brett:focus-visible .zelle.cursor { box-shadow: inset 0 0 0 2.5px var(--fokus); z-index: 1; }

/* Gelöst: das Bild färbt sich diagonal ein */
.brett.geloest .zelle { border-color: transparent; transition: border-color 0.6s, background-color 0.5s; background: var(--feld); }
.brett.geloest .zelle[data-s='x']::after { opacity: 0; transition: opacity 0.4s; }
.brett.geloest .zelle.bild { background: var(--farbe); transition: background-color 0.45s ease-out var(--verz), border-color 0.6s; }
.brett.geloest .zelle.bild::after { opacity: 0; transition: opacity 0.45s ease-out var(--verz); }
.brett.geloest .hinweis span { color: var(--erledigt); }
.brett.geloest .zelle.cursor { box-shadow: none; }
.tisch.fertig .werkzeuge { display: none; }
.brett.sofort .zelle, .brett.sofort .zelle::after { transition: none !important; }
@media (prefers-reduced-motion: reduce) {
  .brett .zelle, .brett .zelle::after, .hinweis span { transition: none !important; }
}

.ansage {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Meldung */
.meldung {
  grid-area: meldung;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 14px;
  margin: 4px auto 0;
  padding: 10px 14px;
  width: min(100%, 34rem);
  background: var(--blatt);
  border: 1.5px solid var(--tusche);
  border-radius: 8px;
}
.meldung[hidden] { display: none; }
.meldung h2 { margin: 0; font-family: var(--zahl); font-weight: 600; font-size: 1.5rem; }
.meldung p { margin: 0; flex: 1 1 8rem; color: var(--leise); font-variant-numeric: tabular-nums; }
.meldung-aktionen { display: flex; gap: 8px; flex: 1 1 100%; }
.meldung-aktionen .knopf { flex: 1; }

/* Werkzeuge */
.werkzeuge {
  grid-area: werkzeuge;
  display: flex;
  gap: 8px;
  justify-content: center;
  padding: 8px 0 6px;
}
.werkzeug { flex: 0 1 8.5rem; }
.werkzeug[aria-pressed='true'] { background: var(--tusche); color: var(--blatt); }
.werkzeug .voll { fill: currentColor; stroke: none; }
.werkzeug[data-werkzeug='cross'] svg { stroke: var(--stift); stroke-width: 2.6; }
.werkzeug[data-werkzeug='cross'][aria-pressed='true'] svg { stroke: currentColor; }
.knopf-zoom { flex: 0 0 auto; padding: 0; width: 48px; }
.knopf-zoom[aria-pressed='true'] { background: var(--hl-stark); }
.knopf-zoom[hidden] { display: none; }

/* Fußzeile */
.fuss {
  grid-area: fuss;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: min(100%, 60rem);
  margin: 0 auto;
  padding: 4px 2px 8px;
  font-size: 0.85rem;
  color: var(--leise);
}
.teilen {
  margin: -4px 0;
  padding: 4px 0;
  border: 0;
  background: none;
  color: inherit;
  font-size: inherit;
  text-decoration: underline;
  text-underline-offset: 0.15em;
  cursor: pointer;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 40px);
  translate: -50% 0;
  z-index: 5;
  width: max-content;
  max-width: calc(100% - 32px);
  margin: 0;
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--tusche);
  color: var(--blatt);
  font-size: 0.92rem;
  line-height: 1.4;
  text-align: center;
  box-shadow: var(--schatten);
  pointer-events: none;
}
.toast:empty { display: none; }
.teilen-feld {
  width: min(calc(100% - 32px), 24rem);
  padding: 14px 16px 16px;
  border: 1.5px solid var(--tusche);
  border-radius: 10px;
  background: var(--blatt);
  color: var(--text);
  box-shadow: var(--schatten);
}
.teilen-feld label { display: block; margin-bottom: 6px; font-size: 0.88rem; color: var(--leise); }
.teilen-feld input {
  width: 100%;
  min-height: 44px;
  padding: 0 10px;
  border: 1.5px solid var(--linie-dick);
  border-radius: 7px;
  background: var(--feld);
}

/* Dialoge */
.wahl {
  width: min(calc(100% - 24px), 28rem);
  max-height: calc(100dvh - 24px);
  padding: 18px 18px 14px;
  border: 1.5px solid var(--tusche);
  border-radius: 10px;
  background: var(--blatt);
  color: var(--text);
  box-shadow: var(--schatten);
}
.wahl::backdrop { background: rgb(17 26 44 / 0.45); }
.anleitung { width: min(calc(100% - 24px), 34rem); padding: 0; }
.anleitung[open] { display: flex; }
.anleitung-form { display: flex; flex-direction: column; width: 100%; min-height: 0; }
.anleitung-kopf {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px 8px 18px;
  border-bottom: 1px solid var(--linie);
}
.anleitung .anleitung-kopf h2 { margin: 0; }
.anleitung-kopf .knopf { border-color: transparent; background: none; }
.anleitung-inhalt { min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 18px 12px; line-height: 1.45; }
.anleitung h3 { margin: 16px 0 4px; font-family: var(--zahl); font-weight: 600; font-size: 1.2rem; }
.anleitung h4 { margin: 10px 0 2px; font-size: 0.95rem; color: var(--leise); }
.anleitung p { margin: 0 0 6px; }
.anleitung ul { margin: 0; padding-left: 18px; }
.anleitung li { margin-bottom: 4px; }
.anleitung .tasten { margin-top: 4px; }
.anleitung-fuss { display: flex; justify-content: flex-end; padding: 10px 18px 14px; border-top: 1px solid var(--linie); }
.beispiel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  max-width: 24rem;
  margin: 14px auto 4px;
}
.beispiel-bild svg { display: block; width: 100%; height: auto; }
.beispiel-pfeil { font-size: 1.4rem; color: var(--leise); }
.beispiel figcaption { grid-column: 1 / -1; font-size: 0.85rem; color: var(--leise); text-align: center; }
.mini-feld { fill: var(--feld); }
.mini-linie { stroke: var(--linie); stroke-width: 0.04; fill: none; }
.mini-rand { stroke: var(--linie-dick); stroke-width: 0.06; fill: none; }
.mini-voll { fill: var(--tusche); }
.mini-kreuz { stroke: var(--stift); stroke-width: 0.09; stroke-linecap: round; opacity: 0.85; }
.mini-zahl { fill: var(--text); font-family: var(--zahl); font-weight: 600; font-size: 0.6px; text-anchor: middle; dominant-baseline: central; }
.wahl h2 { margin: 0 0 12px; font-family: var(--zahl); font-weight: 600; font-size: 1.6rem; }
.wahl-gruppe { border: 0; margin: 0 0 12px; padding: 0; }
.wahl-gruppe legend { padding: 0; margin-bottom: 6px; font-weight: 600; }
.pillen, .reiter { display: flex; flex-wrap: wrap; gap: 6px; }
.reiter { margin-bottom: 14px; }
.pille { position: relative; flex: 1 1 0; min-width: 4.2rem; }
.pille input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.pille span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 10px;
  border: 1.5px solid var(--tusche);
  border-radius: 7px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.pille input:checked + span { background: var(--tusche); color: var(--blatt); }
.erklaerung { margin: 8px 0 0; font-size: 0.88rem; color: var(--leise); line-height: 1.4; }
.schalter {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  font-weight: 600;
}
.schalter input { width: 22px; height: 22px; accent-color: var(--tusche); }
.wahl-aktionen { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.tasten {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  margin: 16px 0 0;
  font-size: 0.88rem;
}
.tasten dt { font-weight: 600; white-space: nowrap; }
.tasten dd { margin: 0; color: var(--leise); }
kbd {
  display: inline-block;
  min-width: 1.5em;
  padding: 0 4px;
  border: 1px solid var(--linie-dick);
  border-radius: 4px;
  font: inherit;
  font-size: 0.85em;
  text-align: center;
}

.motive {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(5.2rem, 1fr));
  gap: 8px;
}
.motiv {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px 6px;
  border: 1.5px solid var(--linie);
  border-radius: 8px;
  background: var(--feld);
  cursor: pointer;
}
.motiv[aria-current='true'] { border-color: var(--tusche); }
.motiv-bild { width: 44px; height: 44px; display: grid; }
.motiv-bild svg { width: 100%; height: 100%; }
.motiv-leer {
  border: 1px solid var(--linie-dick);
  background-image:
    linear-gradient(var(--linie) 1px, transparent 1px),
    linear-gradient(90deg, var(--linie) 1px, transparent 1px);
  background-size: calc(100% / var(--n)) calc(100% / var(--n));
}
.motiv-name { font-weight: 600; font-size: 0.85rem; }
.motiv-groesse { font-size: 0.75rem; color: var(--leise); font-variant-numeric: tabular-nums; }

@media (max-width: 440px) {
  .leiste .text { display: none; }
  .leiste .knopf { padding: 0 10px; }
  .werkzeug { padding: 0 8px; font-size: 0.9rem; }
}

/* Querformat auf dem Handy: Kopf und Leiste in einer Zeile, Werkzeuge rechts neben dem Feld */
@media (orientation: landscape) and (max-height: 540px) {
  body {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: 'kopf leiste' 'tisch tisch' 'fuss fuss';
  }
  .kopf { width: auto; margin: 0; padding: 6px 2px 2px; column-gap: 16px; }
  .titel { font-size: 1.35rem; }
  .zeit { font-size: 1.3rem; }
  .leiste { width: auto; margin: 0 0 0 auto; padding: 6px 0 2px; }
  .leiste .text { display: none; }
  .tisch {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: 'flaeche werkzeuge' 'flaeche meldung';
    width: 100%;
    column-gap: 12px;
  }
  .werkzeuge { flex-direction: column; justify-content: center; padding: 0; }
  .werkzeug { flex: 0 0 auto; justify-content: flex-start; }
  .meldung { width: 15rem; margin: 0 0 6px; }
  .fuss { padding: 0 2px 4px; }
}
