/* Die Farben kommen aus dem Theme (assets/theme.js) und werden hier nur benutzt.
   Die Werte unten sind der Stand vor dem ersten Rendern. */
:root {
  --c-bg: #12141c;
  --c-surface: #1c1f2b;
  --c-tile: #252a39;
  --c-tileMarked: #3b5bdb;
  --c-text: #f1f3f7;
  --c-textMuted: #9aa3b8;
  --c-accent: #63e6be;
  --c-line: #333a4d;
  --c-accentText: #0b0d12;
  --c-markedText: #ffffff;
  --c-danger: #ff8787;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
  --cols: 5;
  --rows: 5;
  /* Kachelgröße: app.js rechnet sie aus Fensterbreite und -höhe aus und hält sie
     zwischen diesen Grenzen. Hier ändern, nicht im JavaScript. Auf sehr großen
     Schirmen hebt eine Media-Query weiter unten den Deckel an. */
  --cell-min: 52px;
  --cell-max: 148px;
  --cell: 7rem;
  --grid-gap: clamp(0.3rem, 0.8vw, 0.7rem);
  /* Außenabstand der Seite; auf Notch-Geräten wächst er um die sichere Zone. */
  --page-pad: clamp(0.75rem, 2vw, 2rem);
  /* Breiter als das werden Kopf, Werkzeugleiste und Fuß auch auf 4K nicht. */
  --page-max: 120rem;
  /* Mindesthöhe von Knöpfen und Feldern; mit Finger (pointer: coarse) 44px. */
  --hit: 2.5rem;
}
@media (pointer: coarse) {
  :root {
    --hit: 2.75rem;
  }
}

* {
  box-sizing: border-box;
}

html {
  /* Fließende Grundschrift: 16px bis 1600px Breite, dann bis 20px auf 4K. */
  font-size: clamp(1rem, 0.5rem + 0.5vw, 1.25rem);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: max(var(--page-pad), env(safe-area-inset-top)) max(var(--page-pad), env(safe-area-inset-right))
    max(var(--page-pad), env(safe-area-inset-bottom)) max(var(--page-pad), env(safe-area-inset-left));
  /* 100vh zählt auf Mobilgeräten die Adressleiste mit; dvh nicht. */
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--c-bg);
  color: var(--c-text);
  font-family: var(--font-body);
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  gap: clamp(0.6rem, 1.2vh, 1.25rem);
}
body > * {
  width: 100%;
  max-width: var(--page-max);
  margin-inline: auto;
}

a {
  color: var(--c-accent);
}

.skip-link {
  position: absolute;
  left: -9999px;
}
.skip-link:focus {
  left: 1rem;
  top: 1rem;
  z-index: 10;
  padding: 0.5rem 0.75rem;
  background: var(--c-surface);
  border-radius: var(--radius);
}

/* --- Kopf --- */
.brand {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}
.brand__logo {
  max-height: clamp(40px, 8vw, 68px);
  max-width: min(240px, 40vw);
  object-fit: contain;
}
.brand__text {
  min-width: 0;
}
.brand h1 {
  margin: 0;
  font-size: clamp(1.4rem, 1rem + 2vw, 2.4rem);
  line-height: 1.15;
  letter-spacing: -0.02em;
  overflow-wrap: anywhere;
}
.brand__subtitle {
  margin: 0.15rem 0 0;
  color: var(--c-textMuted);
}

/* --- Werkzeugleiste --- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.6rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  border-radius: var(--radius);
}
/* Auf breiten Schirmen ist der Einstellungs-Block unsichtbar: seine Felder fließen
   direkt in der Leiste. Der Auf/Zu-Knopf erscheint nur auf kleinen Schirmen. */
.toolbar__settings {
  display: contents;
}
.toolbar__toggle {
  display: none;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}
.field label {
  font-size: 0.8rem;
  color: var(--c-textMuted);
}
.field--check {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  align-self: center;
  min-height: var(--hit);
}
.field--check label,
.field--check span {
  font-size: 0.9rem;
  color: var(--c-text);
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-left: auto;
}

select,
input[type="text"],
input[type="url"],
textarea {
  font: inherit;
  font-size: 0.95rem;
  min-height: var(--hit);
  padding: 0.45rem 0.6rem;
  color: var(--c-text);
  background: var(--c-bg);
  border: 1px solid var(--c-line);
  border-radius: 10px;
  max-width: 100%;
}
.toolbar select {
  min-width: 7rem;
}
textarea {
  width: 100%;
  min-height: 6rem;
  resize: vertical;
  line-height: 1.5;
}
input[type="checkbox"] {
  width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  accent-color: var(--c-accent);
}
input[type="color"] {
  width: 3rem;
  height: 2.25rem;
  padding: 0;
  border: 1px solid var(--c-line);
  border-radius: 8px;
  background: none;
}
input[type="file"] {
  font: inherit;
  font-size: 0.9rem;
  max-width: 100%;
}

.btn {
  font: inherit;
  font-size: 0.92rem;
  min-height: var(--hit);
  padding: 0.5rem 0.9rem;
  color: var(--c-text);
  background: var(--c-tile);
  border: 1px solid var(--c-line);
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn--primary {
  background: var(--c-accent);
  color: var(--c-accentText);
  border-color: transparent;
  font-weight: 600;
}
.btn--danger {
  color: var(--c-danger);
}
/* Hover nur, wo es eine Maus gibt — sonst bleibt der Rand nach dem Tippen hängen. */
@media (hover: hover) {
  .btn:hover {
    border-color: var(--c-accent);
  }
  .tile:hover:not(:disabled) {
    border-color: var(--c-accent);
  }
}
:focus-visible {
  outline: 3px solid var(--c-accent);
  outline-offset: 2px;
}

/* --- Meldungen --- */
.status,
.error {
  margin: 0;
  min-height: 1.4em;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.status {
  color: var(--c-accent);
}
.error {
  color: var(--c-danger);
  font-weight: 500;
}

/* --- Karte --- */
/* main bekommt die Resthöhe des Fensters; die Karte wird darin zentriert und so
   bemessen, dass sie ohne Scrollen hineinpasst. */
main {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: safe center;
}
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  justify-content: center;
  gap: var(--grid-gap);
}
.tile {
  /* app.js setzt --font-scale je Kachel nach Textlänge: kurze Begriffe groß,
     lange klein und mehrzeilig. */
  --font-scale: 0.115;
  display: flex;
  align-items: safe center; /* passt der Text nicht, bleibt wenigstens sein Anfang sichtbar */
  justify-content: center;
  text-align: center;
  padding: clamp(0.15rem, calc(var(--cell) * 0.07), 0.6rem);
  font: inherit;
  font-size: clamp(0.4rem, calc(var(--cell) * var(--font-scale)), 1.25rem);
  font-weight: 600;
  line-height: 1.2;
  color: var(--c-text);
  background: var(--c-tile);
  border: 1px solid var(--c-line);
  border-radius: var(--radius);
  cursor: pointer;
  overflow: hidden;
  overflow-wrap: anywhere;
  hyphens: auto;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}
.tile[aria-pressed="true"] {
  background: var(--c-tileMarked);
  color: var(--c-markedText);
  border-color: transparent;
}
.tile--free {
  font-style: italic;
  cursor: default;
}
/* Innen liegender Rahmen, damit der Fokusring außen sichtbar bleibt. */
.tile--winning {
  box-shadow: inset 0 0 0 3px var(--c-accent);
}

/* --- Fuß --- */
.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--c-line);
  color: var(--c-textMuted);
  font-size: 0.85rem;
}
.footer p {
  margin: 0;
  overflow-wrap: anywhere;
}
.brand-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.brand-links a,
.colophon a {
  display: inline-block;
  padding-block: 0.25rem;
}
.colophon {
  margin-left: auto;
}

/* --- Dialoge --- */
.dialog {
  width: min(46rem, 94vw);
  max-height: min(90vh, 90dvh);
  padding: 0;
  color: var(--c-text);
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  border-radius: var(--radius);
}
.dialog::backdrop {
  background: rgb(0 0 0 / 0.55);
}
.dialog__body {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1.25rem;
  max-height: min(90vh, 90dvh);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.dialog h2 {
  margin: 0;
  font-size: 1.2rem;
}
.dialog__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}
.spacer {
  flex: 1;
}
.hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--c-textMuted);
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(14rem, 100%), 1fr));
  gap: 0.9rem;
}
.colors {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  margin: 0;
  padding: 0.75rem 1rem 1rem;
  border: 1px solid var(--c-line);
  border-radius: var(--radius);
}
.colors legend {
  padding: 0 0.4rem;
  font-size: 0.8rem;
  color: var(--c-textMuted);
}
.colors label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--hit);
  font-size: 0.85rem;
}

/* --- Große Schirme: Kacheln dürfen wachsen, damit 4K nicht winzig wirkt --- */
@media (min-width: 2000px) {
  :root {
    --cell-max: 190px;
  }
}
@media (min-width: 3000px) {
  :root {
    --cell-max: 240px;
  }
}

/* --- Kompakt: schmale Schirme und flache Querformate (Handy quer) --- */
/* Schmale Schirme: Kacheln nicht unter Fingergröße, lieber ein Stück scrollen. */
@media (max-width: 640px) {
  :root {
    --cell-min: 64px;
  }
}

@media (max-width: 640px), ((orientation: landscape) and (max-height: 520px)) {
  /* Leiste als Zweispalter: oben Auf/Zu-Knopf und „Neue Karte“, dann die
     (einklappbaren) Einstellungen, darunter die übrigen Knöpfe. */
  .toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 0.75rem;
    gap: 0.6rem;
  }
  .toolbar__toggle {
    grid-row: 1;
    grid-column: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    text-align: left;
  }
  .toolbar__toggle::after {
    flex: none;
  }
  .toolbar__toggle::after {
    content: "";
    width: 0.55em;
    height: 0.55em;
    border: solid currentColor;
    border-width: 0 2px 2px 0;
    transform: translateY(-25%) rotate(45deg);
    transition: transform 0.15s ease;
  }
  .toolbar__toggle[aria-expanded="true"]::after {
    transform: translateY(25%) rotate(-135deg);
  }
  .toolbar__settings {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.6rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--c-line);
  }
  .toolbar:not(.toolbar--open) .toolbar__settings {
    display: none;
  }
  .toolbar select {
    width: 100%;
    min-width: 0;
  }
  .field--check {
    grid-column: 1 / -1;
  }
  .actions {
    display: contents; /* die Knöpfe werden selbst Rasterzellen der Leiste */
  }
  .actions .btn {
    min-width: 0;
    padding-inline: 0.5rem;
  }
  #btn-new {
    grid-row: 1;
    grid-column: 2;
  }
  .colophon {
    margin-left: 0;
  }
  .brand {
    gap: 0.6rem 0.9rem;
  }
}

/* --- Handy hochkant: Dialoge als Bogen von unten, Knöpfe immer erreichbar --- */
@media (max-width: 640px) {
  .dialog {
    width: 100%;
    max-width: 100%;
    max-height: 100dvh;
    margin: auto 0 0;
    border-width: 1px 0 0;
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .dialog__body {
    max-height: 100dvh;
    padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom));
  }
  .dialog__actions {
    position: sticky;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin: 0 -1rem calc(-1 * max(1rem, env(safe-area-inset-bottom)));
    padding: 0.75rem 1rem max(0.75rem, env(safe-area-inset-bottom));
    background: var(--c-surface);
    border-top: 1px solid var(--c-line);
  }
  .dialog__actions .spacer {
    display: none;
  }
  .dialog__actions .btn--primary {
    grid-column: 1 / -1;
  }
  .colors label {
    flex: 1 1 40%;
    justify-content: space-between;
  }
}

/* --- Handy quer und andere sehr flache Fenster: Werkzeuge links, Karte rechts ---
   app.js liest --layout und gibt der Karte dann die volle Fensterhöhe. */
@media (orientation: landscape) and (max-height: 520px) {
  body {
    --layout: side;
    display: grid;
    grid-template-columns: minmax(13rem, 24rem) minmax(0, 1fr);
    grid-template-rows: auto auto auto auto 1fr;
    grid-template-areas:
      "brand card"
      "toolbar card"
      "status card"
      "error card"
      "footer card";
    align-items: start;
    gap: 0.5rem 1rem;
  }
  body > * {
    margin-inline: 0;
  }
  .brand {
    grid-area: brand;
  }
  .toolbar {
    grid-area: toolbar;
  }
  .status {
    grid-area: status;
  }
  .error {
    grid-area: error;
  }
  .footer {
    grid-area: footer;
    align-self: end;
  }
  main {
    grid-area: card;
    /* Karte bleibt stehen, während man in der linken Spalte scrollt. */
    position: sticky;
    top: max(var(--page-pad), env(safe-area-inset-top));
  }
  .brand__logo {
    max-height: 36px;
  }
  .brand h1 {
    font-size: 1.3rem;
  }
  .brand__subtitle {
    display: none;
  }
  .footer {
    font-size: 0.75rem;
  }
  .colophon {
    display: none;
  }
}

/* --- Zugänglichkeit --- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
@media (prefers-contrast: more) {
  .tile,
  .btn,
  select,
  input,
  textarea,
  .toolbar,
  .dialog {
    border-width: 2px;
  }
  .tile[aria-pressed="true"] {
    border-color: var(--c-text);
  }
  .tile--winning {
    box-shadow: inset 0 0 0 4px var(--c-text);
  }
  .field label,
  .hint,
  .footer,
  .brand__subtitle,
  .colors legend {
    color: var(--c-text);
  }
  :focus-visible {
    outline-width: 4px;
    outline-offset: 3px;
  }
}
@media (forced-colors: active) {
  .tile,
  .btn {
    border: 2px solid ButtonText;
  }
  .tile[aria-pressed="true"] {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
    border-color: Highlight;
  }
  .tile--winning {
    box-shadow: inset 0 0 0 4px CanvasText;
  }
  .btn--primary {
    border-width: 3px;
  }
}

/* --- Druck: Karte füllt die Seite, Kopf bleibt sichtbar --- */
@media print {
  @page {
    margin: 12mm;
  }
  html {
    font-size: 12pt;
  }
  body {
    display: block;
    min-height: 0;
    padding: 0;
    background: #fff;
    color: #000;
  }
  body > * {
    max-width: none;
  }
  a {
    color: #000;
    text-decoration: none;
  }
  .toolbar,
  .status,
  .error,
  .brand-links,
  .colophon,
  .skip-link {
    display: none !important;
  }
  .brand {
    margin-bottom: 6mm;
  }
  .brand__subtitle,
  .footer {
    color: #333;
  }
  main {
    display: block;
    position: static;
  }
  .grid {
    grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
    grid-auto-rows: auto;
    gap: 3mm;
    width: 100%;
    max-width: calc(var(--cols) * 42mm);
    margin: 0 auto;
  }
  .tile {
    aspect-ratio: 1;
    padding: 2mm;
    font-size: 11pt;
    color: #000;
    background: #fff;
    border: 0.4mm solid #000;
    border-radius: 3mm;
    box-shadow: none;
    break-inside: avoid;
  }
  .tile[aria-pressed="true"] {
    background: #ddd;
    color: #000;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .tile--winning {
    box-shadow: inset 0 0 0 1mm #000;
  }
  .footer {
    margin-top: 5mm;
    padding-top: 2mm;
    border-top: 0.3mm solid #000;
  }
}
