:root {
  --cell-size: 30px;
  --label-w: 34px;
  --border: #cbd5e1;
  --grid-line: rgba(148, 163, 184, 0.35); /* feine Zell-Trennlinien */
  --grid-major: #475569;                  /* 10er-/5er-Hilfslinien */
  --panel-bg: #ffffff;
  --page-bg: #f3f4f6;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --text: #111827;
  --muted: #6b7280;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--page-bg);
  color: var(--text);
}

.app-header {
  padding: 16px 24px;
  background: #111827;
  color: #fff;
}
.app-header h1 { margin: 0; font-size: 1.4rem; }
.subtitle { margin: 4px 0 0; color: #cbd5e1; font-size: 0.9rem; }
.back-link {
  display: inline-block;
  margin-bottom: 8px;
  color: #cbd5e1;
  text-decoration: none;
  font-size: 0.85rem;
}
.back-link:hover { color: #fff; text-decoration: underline; }

.layout {
  display: flex;
  gap: 20px;
  padding: 20px;
  align-items: flex-start;
}

.sidebar {
  flex: 0 0 300px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel {
  background: var(--panel-bg);
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 16px;
}
.panel h2 { margin: 0 0 12px; font-size: 1rem; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 0.85rem; }
.field span { color: var(--muted); }
.field input[type="text"],
.field input[type="number"],
.field select {
  padding: 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 0.9rem;
}
.field input[type="color"] {
  width: 100%;
  height: 38px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 2px;
  cursor: pointer;
}
.field.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.field.checkbox span { color: var(--text); }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.per-row-colors {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
  max-height: 260px;
  overflow-y: auto;
}
.per-row-colors.hidden { display: none; }
.per-row-colors .row-color { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; }
.per-row-colors .row-color input[type="color"] { width: 40px; height: 28px; padding: 1px; }

.btn {
  display: block;
  width: 100%;
  padding: 10px;
  margin-bottom: 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }

.hint { font-size: 0.75rem; color: var(--muted); margin: 4px 0 0; }

.tool-list { list-style: none; padding: 0; margin: 0 0 10px; }
.tool {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  margin-bottom: 4px;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}
.tool:hover { background: #f9fafb; }
.tool.active { border-color: var(--accent); background: #eff6ff; }
.tool-swatch {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border: 1px solid #9ca3af;
  background: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: "Segoe UI Symbol", "DejaVu Sans", sans-serif;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  color: #111827;
}
.tool-label { flex: 1; }

.main { flex: 1; min-width: 0; }
.pattern-title { margin: 0 0 12px; }

.grid-wrapper {
  overflow: auto;               /* Scrollbalken bei grossen Rastern */
  max-height: calc(100vh - 180px);
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 12px;
}

.stage {
  position: relative;
  width: max-content;
}

.grid {
  display: grid;
  gap: 0;
  width: max-content;
  position: relative;
  z-index: 1;
  /* schliesst die offenen rechten/unteren Kanten (Zell-Linien sind oben+links) */
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
}

/* Referenzbild-Overlay: deckt exakt die ZELLflaeche ab (ohne die
   Reihenbezeichner-Spalten links/rechts) und skaliert mit.
   Klicks gehen durch (pointer-events: none), damit das Gitter bedienbar bleibt. */
.image-overlay {
  position: absolute;
  top: 0;
  left: var(--label-w);
  right: var(--label-w);
  height: 100%;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
  pointer-events: none;
  opacity: 0.5;
  display: none;
  z-index: 2; /* Standard: vor dem Gitter */
}
.image-overlay.active { display: block; }
.image-overlay.behind { z-index: 0; }

/* Reihenbezeichner links/rechts – bleiben beim horizontalen Scrollen sichtbar. */
.row-label {
  height: var(--cell-size);
  min-width: var(--label-w);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: calc(var(--cell-size) * 0.38);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  background: #fff;
  position: sticky;
  z-index: 5;
  user-select: none;
}
.row-label.left { left: 0; box-shadow: 1px 0 0 #e5e7eb; }
.row-label.right { right: 0; box-shadow: -1px 0 0 #e5e7eb; }
/* Jede 10. Reihe im Bezeichner hervorheben (schnelles vertikales Zaehlen). */
.row-label.major { color: var(--text); font-weight: 700; }

/* Hervorgehobene senkrechte Linie alle 10 Spalten (von rechts gezaehlt) und
   dezente horizontale Hilfslinie alle 5 Reihen. Via inset-Schatten, damit
   nichts das Layout verschiebt. Reihenfolge: erst feine Linien, dann Major. */
.cell.col-tenth {
  box-shadow:
    inset 2px 0 0 var(--grid-major),
    inset 0 1px 0 var(--grid-line);
  z-index: 2;
}
.cell.row-fifth {
  box-shadow:
    inset 1px 0 0 var(--grid-line),
    inset 0 2px 0 var(--grid-major);
  z-index: 2;
}
.cell.col-tenth.row-fifth {
  box-shadow:
    inset 2px 0 0 var(--grid-major),
    inset 0 2px 0 var(--grid-major);
  z-index: 2;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  /* Hauchduenne Trennlinien via inset-Schatten (oben + links), ohne
     doppelte Raender oder Layout-Verschiebung. */
  box-shadow: inset 1px 0 0 var(--grid-line), inset 0 1px 0 var(--grid-line);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  position: relative;
  font-family: "Segoe UI Symbol", "DejaVu Sans", sans-serif;
  line-height: 1;
}
.cell:hover {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 6;
}
/* Vorschau des aktuell gewaehlten Stich-Symbols beim Ueberfahren leerer Zellen. */
.cell:hover::after {
  content: attr(data-preview);
  position: absolute;
  color: var(--accent);
  opacity: 0.5;
  font-weight: 700;
  font-size: calc(var(--cell-size) * 0.68);
  pointer-events: none;
}

/* Wenn das Bild hinter dem Gitter liegt, Zellen leicht durchscheinen lassen. */
.grid.image-behind .cell { opacity: 0.7; }

/* Grosses Stich-Symbol (positiver Overlay-Stich) mittig in der Zelle. */
.cell .mark-big {
  font-weight: 700;
  font-size: calc(var(--cell-size) * 0.68);
}

/* Kleiner Hinweis (negativer Stich) oben rechts – NICHT kursiv. */
.cell .mark-hint {
  position: absolute;
  top: 0;
  right: 2px;
  font-size: calc(var(--cell-size) * 0.36);
  font-style: normal;
  z-index: 3;
}

/* "+1 Umschlag"-Markierung (Kreis) mittig – sc-Masche mit Zusatz-Umschlag. */
.cell .mark-yo {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: calc(var(--cell-size) * 0.6);
  line-height: 1;
  z-index: 2;
}

/* Kurzes Feedback, wenn ein Stich wegen Kollision blockiert wird. */
.cell.collision-flash {
  animation: collisionFlash 0.35s ease-out;
}
@keyframes collisionFlash {
  0%   { box-shadow: inset 0 0 0 3px #ef4444; }
  100% { box-shadow: inset 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Generische Sichtbarkeits-Hilfsklasse. */
.hidden { display: none !important; }

/* Modus-Umschalter Stich / Farbe. */
.mode-switch {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}
.mode-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
}
.mode-btn:hover { background: #f9fafb; }
.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Einzelfarben-Palette. */
.palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-top: 8px;
}
.swatch-wrap { position: relative; display: flex; }
.swatch {
  width: 100%;
  height: 34px;
  border: 2px solid #d1d5db;
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
}
.swatch.active {
  border-color: var(--text);
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--accent);
}
.swatch.eraser {
  background: #fff;
  font-size: 1rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
}
/* Verstecktes color-Input liegt ueber dem Swatch, wird per dblclick geoeffnet. */
.swatch-picker {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  border: 0;
}

/* Im Farb-Modus: Cursor als Hinweis, dass gefaerbt wird. */
.grid.color-mode .cell { cursor: cell; }
