/* Stellenwerte – Web. Layout & Darstellung (Portierung der iOS-Optik). */

@font-face {
  font-family: 'Grundschrift';
  src: url('../assets/fonts/Grundschrift-Regular.otf') format('opentype');
  font-weight: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Grundschrift';
  src: url('../assets/fonts/Grundschrift-Bold.otf') format('opentype');
  font-weight: bold;
  font-display: swap;
}

:root {
  --bg: rgb(242, 241, 229);
  --fg: rgb(41, 43, 52);
  --panel: #ffffff;
  --panel-fg: rgb(41, 43, 52);
  --frame: rgb(200, 200, 200);
  --box-bg: #ffffff;
  --tool-bg: rgba(215, 215, 215, 0.6);
  --border: rgba(0, 0, 0, 0.12);
  --e: rgb(63, 139, 129);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: rgb(41, 43, 52);
    --fg: rgb(240, 240, 240);
    --panel: rgb(48, 50, 60);
    --panel-fg: rgb(238, 238, 238);
    --frame: rgb(120, 122, 132);
    --box-bg: #ffffff;
    --tool-bg: rgba(70, 72, 82, 0.7);
    --border: rgba(255, 255, 255, 0.14);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0; height: 100%; width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  /* Systemschrift für Menüs, Einstellungen und Leisten (wie iOS);
     Grundschrift nur für Stellenwerttafel, Zahlwörter und Zifferneingabe (siehe unten) */
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}

#app {
  position: fixed; inset: 0;
  display: grid;
  grid-template-columns: 1fr 56px;
  grid-template-rows: auto auto 1fr auto;
  grid-template-areas:
    "zahlwort toolbar"
    "logisch  toolbar"
    "board    toolbar"
    "modebar  einstellungen";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Zahlwort ---------- */
.zahlwort {
  grid-area: zahlwort;
  /* unten etwas Luft, damit Unterlängen (g, z) nicht abgeschnitten werden */
  padding: 8px 16px 0.35em 20px;
  min-height: 0;
  max-height: 20vh;
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  font-size: clamp(16px, 4.4vw, 34px);
  overflow-wrap: normal;
  word-break: keep-all;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
.zw-seg { white-space: nowrap; }
.zahlwort { cursor: pointer; }
.zahlwort, .board, .digit-btn { font-family: 'Grundschrift', -apple-system, BlinkMacSystemFont, sans-serif; }
/* Logisches Zahlwort: direkt unter dem Zahlwort, Wörter durch Leerzeichen getrennt */
.zahlwort-logisch { grid-area: logisch; padding-top: 4px; column-gap: 0.3em; }
.zw-teil { white-space: nowrap; display: inline-flex; }
/* Beide Zeilen sichtbar: etwas kleiner, damit die Tafel Platz behält */
#app.zw-doppelt .zahlwort { max-height: 13vh; font-size: clamp(14px, 3.6vw, 28px); }

/* ---------- Board ---------- */
.board {
  grid-area: board;
  position: relative;
  overflow: visible;
  touch-action: none;
  min-height: 0;
}
.layer { position: absolute; inset: 0; pointer-events: none; }
.bar-layer, .col-layer, .btn-layer, .label-layer { pointer-events: none; }
.bar-layer > *, .col-layer > *, .btn-layer > *, .label-layer > * { pointer-events: auto; }

/* ---------- Kästchen (Ziffernbox) ---------- */
.kaestchen {
  position: absolute;
  background: var(--box-bg);
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  font-weight: bold;
  border: 0 solid var(--frame);
}
.kaestchen.bordered { border: 2px solid var(--frame); }
.bar-layer:not(.hide-frames) .kaestchen {
  box-shadow: inset 0 0 0 2px var(--frame);
}
.wheel { position: absolute; inset: 0; }
.wheel-cell {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums;
  will-change: transform;
  line-height: 1;
}
/* Grundschrift sitzt optisch zu hoch → Text generell um 0.1em nach unten
   (padding-top 0.2em bei align-items:center verschiebt zentrierten Text um 0.1em). */
.wheel-cell, .sw-label, .hidden-zero { padding-top: 0.2em; }
.zw-seg { display: inline-block; transform: translateY(0.1em); }
.kaestchen-punkt {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: flex-end;
  padding-right: 2%;
  color: #000; pointer-events: none; font-weight: bold;
}
.kaestchen-cover {
  position: absolute; inset: 0;
  background: rgb(245, 244, 240);
  border: 3px solid var(--frame);
  display: flex; align-items: center; justify-content: center;
  color: #000; z-index: 5;
}
.kaestchen-cover.correct { background: rgb(184, 226, 151); }
.kaestchen-cover.wrong { background: rgb(255, 135, 135); }

.hidden-zero {
  position: absolute;
  background: var(--box-bg);
  display: flex; align-items: center; justify-content: center;
  font-weight: bold; opacity: 0.9;
  box-shadow: inset 0 0 0 2px var(--frame);
}
.bar-layer.hide-frames .hidden-zero { box-shadow: none; }

/* ---------- Stellenwert-Spalte ---------- */
.stellenwert {
  position: absolute;
  overflow: visible;
  touch-action: none;
  cursor: pointer;
  background: transparent;
}
.stellenwert.bordered { border: 2px solid var(--frame); }
.sw-menge {
  position: absolute;
  border-radius: 50%;
  border: 3px solid #fff;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: bold; overflow: hidden;
  will-change: opacity;
}
.sw-menge.is-image { border: none; border-radius: 0; background: transparent !important; }
.sw-menge.is-image img { width: 100%; height: 100%; object-fit: contain; }
/* Zahl und Name: Quadrate mit abgerundeten Ecken und Systemschrift wie in der iOS-App;
   die Schriftgröße setzt stellenwert.js passend zur Breite */
.sw-menge.is-text {
  border-radius: 14%;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  font-weight: 500; letter-spacing: -0.02em; white-space: nowrap;
}
.sw-ghost { pointer-events: none; z-index: 30; }
.sw-cover {
  position: absolute; inset: 0;
  background: rgb(245, 244, 240);
  border: 2px solid var(--frame);
  z-index: 6;
}

/* ---------- Trennstriche ---------- */
.strich { position: absolute; background: #000; }
@media (prefers-color-scheme: dark) { .strich { background: #000; } }

/* ---------- Kanten-Buttons ---------- */
.edge-btn {
  position: absolute;
  border: none; background: transparent; padding: 0;
  color: var(--fg); opacity: 0.35; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.edge-btn img { width: 100%; height: 100%; object-fit: contain; }
.edge-btn:active { opacity: 0.7; }

/* ---------- Farbpunkte ---------- */
.sw-label {
  position: absolute;
  display: flex; align-items: center; justify-content: center;
  opacity: 0.6; font-weight: bold; pointer-events: none;
  overflow: hidden; line-height: 1;
  white-space: nowrap;
}
/* Ausgeschriebene Bezeichnung zweizeilig an der Wortfuge (Hundert-/tausender) */
.sw-label.zweizeilig { flex-direction: column; line-height: 1.02; padding-top: 0.12em; }

.color-dot {
  position: absolute; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.6);
  opacity: 0.35; cursor: pointer; padding: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.color-dot:active { opacity: 0.8; }

/* ---------- Dienes-Canvas ---------- */
.dienes-canvas { position: absolute; touch-action: none; }

/* ---------- Werkzeugleiste ---------- */
.toolbar {
  grid-area: toolbar;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px; padding: 8px 6px;
  overflow-y: auto;
}
@media (max-height: 620px) { .toolbar { gap: 7px; justify-content: flex-start; } }
.tool-btn {
  position: relative;
  width: 40px; height: 40px; flex: 0 0 auto;
  border: none; border-radius: 8px;
  background: var(--tool-bg);
  color: var(--fg);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  padding: 3px;
}
.tool-btn img { width: 100%; height: 100%; object-fit: contain; }
.tool-btn:active { transform: scale(0.94); }
.tool-btn.recording { background: #34c759 !important; }
.tool-btn.correct { background: transparent; }
.tool-btn.disabled { opacity: 0.4; }
/* Per Einstellung ausgeblendete Werkzeuge / Darstellungen */
.tool-btn.is-off, .mode-btn.is-off, .mode-bar.is-off { display: none !important; }
/* Einstellungsschutz: Ring füllt sich beim Gedrückthalten */
#btn-settings { -webkit-touch-callout: none; touch-action: none; }
.hold-ring { position: absolute; inset: -4px; width: calc(100% + 8px); height: calc(100% + 8px);
  pointer-events: none; transform: rotate(-90deg); overflow: visible; }
.hold-ring circle { fill: none; stroke: var(--e); stroke-width: 3.5; stroke-dasharray: 100; stroke-dashoffset: 100; opacity: 0; }
#btn-settings.holding .hold-ring circle { opacity: 1; stroke-dashoffset: 0; transition: stroke-dashoffset 2s linear; }
.hinweis {
  position: fixed; z-index: 120; max-width: 70vw;
  background: var(--panel); color: var(--panel-fg);
  padding: 8px 12px; border-radius: 8px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25); pointer-events: none;
}
.counter {
  position: absolute; top: -6px; right: -6px;
  min-width: 18px; height: 18px; padding: 0 3px;
  border-radius: 9px; background: rgb(88, 181, 164);
  color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

/* Einstellungs-Button unten rechts, unter der Werkzeugleiste (wie iOS) */
.settings-slot {
  grid-area: einstellungen;
  display: flex; align-items: center; justify-content: center;
  padding: 8px 6px 14px;
}

/* ---------- Modus-Leiste: Umschalter mit Bildern (wie iOS) ---------- */
.mode-bar {
  grid-area: modebar;
  justify-self: center; align-self: center;
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: 2px; padding: 3px; margin: 8px 10px 14px;
  background: rgba(120,120,128,0.16); border-radius: 11px;
}
.mode-btn {
  width: 56px; height: 38px; padding: 3px;
  border: none; border-radius: 8px;
  background: transparent; color: var(--fg);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: manipulation;
}
.mode-btn svg, .mode-btn img { width: 30px; height: 30px; object-fit: contain; display: block; }
.mode-btn.is-active { background: var(--panel); box-shadow: 0 1px 3px rgba(0,0,0,0.22); }
@media (prefers-color-scheme: dark) { .mode-btn.is-active { background: rgb(99, 99, 102); } }

/* ---------- Aufnahme-Label ---------- */
.record-label {
  position: fixed;
  top: 20vh; left: 50%; transform: translateX(-50%);
  background: rgb(255, 135, 135);
  color: #000; padding: 8px 18px; border-radius: 8px;
  font-size: 15px; z-index: 50; opacity: 0.95;
}

/* ---------- Overlays ---------- */
.settings-overlay, .palette-overlay, .input-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.28);
  display: flex; align-items: center; justify-content: center;
}
.settings-panel {
  background: var(--panel); color: var(--panel-fg);
  width: min(400px, 92vw); max-height: 82vh; overflow-y: auto;
  border-radius: 16px; padding: 6px 0 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
}
/* Einstellungen als Popover über dem Button (Position setzt app.js) */
.settings-overlay { background: rgba(0,0,0,0.12); }
#settings-overlay .settings-panel { position: fixed; }
.settings-pfeil {
  position: fixed; width: 0; height: 0;
  border-left: 9px solid transparent; border-right: 9px solid transparent;
  border-top: 10px solid var(--panel);
  filter: drop-shadow(0 3px 2px rgba(0,0,0,0.12));
}
.settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px 8px;
}
.settings-header h2 { margin: 0; font-size: 20px; }
.settings-close {
  border: none; background: transparent; color: var(--panel-fg);
  font-size: 28px; line-height: 1; cursor: pointer;
}
.settings-list { padding: 0 8px; }
.settings-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 12px; border-top: 1px solid var(--border);
  cursor: pointer;
}
.settings-text { flex: 1; min-width: 0; }
.settings-section {
  padding: 16px 12px 6px; font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em; opacity: 0.6;
}
.settings-section-sub { padding: 0 12px 8px; font-size: 12px; opacity: 0.6; }
.settings-section + .settings-row, .settings-section-sub + .settings-row { border-top: none; }
.settings-icon {
  width: 28px; height: 28px; flex: 0 0 28px; align-self: flex-start; margin-top: 2px;
  display: flex; align-items: center; justify-content: center; color: var(--panel-fg);
}
.settings-icon img { width: 28px; height: 28px; object-fit: contain; }
.settings-row.is-disabled { opacity: 0.45; cursor: default; }
.toggle:disabled { cursor: default; }
.settings-row-reset { flex-wrap: wrap; cursor: default; }
.settings-btn {
  border: 1px solid var(--border); background: transparent; color: var(--panel-fg);
  border-radius: 8px; padding: 6px 12px; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.settings-btn.danger { color: rgb(200, 40, 40); border-color: rgba(200, 40, 40, 0.4); }
@media (prefers-color-scheme: dark) { .settings-btn.danger { color: rgb(255, 120, 110); } }
.settings-confirm { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; flex: 1 1 100%; justify-content: flex-end; }
.settings-confirm span { flex: 1 1 auto; font-size: 13px; font-weight: 600; }
.settings-title { font-weight: 600; font-size: 15px; }
.settings-sub { font-size: 12px; opacity: 0.7; margin-top: 2px; }
.toggle { width: 46px; height: 28px; appearance: none; -webkit-appearance: none;
  background: rgba(120,120,128,0.32); border-radius: 16px; position: relative; cursor: pointer; flex: 0 0 auto; transition: background .2s; }
.toggle:checked { background: #34c759; }
.toggle::after { content: ''; position: absolute; top: 2px; left: 2px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.toggle:checked::after { transform: translateX(18px); }

/* Segmented Control (Auswahl mit mehreren Werten, z. B. Tausendertrennung) */
.settings-row-segmented { flex-wrap: wrap; cursor: default; }
.settings-row-segmented .settings-text { flex-basis: calc(100% - 38px); }
.settings-row-segmented .segmented { margin-left: 38px; }
.segmented {
  display: flex; flex: 1 1 100%;
  background: rgba(120,120,128,0.16); border-radius: 9px; padding: 2px; gap: 2px;
}
.segment {
  flex: 1 1 0; min-height: 32px; padding: 4px 6px;
  border: none; border-radius: 7px; background: transparent;
  color: var(--panel-fg); font-family: inherit; font-size: 13px; font-weight: 600; line-height: 1.2;
  cursor: pointer; touch-action: manipulation;
  transition: background .15s;
}
.segment.is-active { background: var(--panel); box-shadow: 0 1px 3px rgba(0,0,0,0.22); }
@media (prefers-color-scheme: dark) { .segment.is-active { background: rgb(99, 99, 102); } }

.palette-panel {
  background: var(--panel); border-radius: 12px; padding: 12px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
}
.swatch { width: 44px; height: 44px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.6); cursor: pointer; }

.input-panel {
  background: var(--panel); border-radius: 12px; padding: 8px;
  display: flex; gap: 6px; flex-wrap: wrap; max-width: 92vw;
  justify-content: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
}
.digit-btn {
  width: 44px; height: 44px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font-size: 22px; font-family: inherit;
  cursor: pointer;
}
.digit-btn:active { background: var(--e); color: #fff; }

/* ---------- Teilen & Scannen (QR) ---------- */
/* Button oben rechts, mittig über der Werkzeugspalte (56 px breit, Button 40 px) */
.teilen-btn {
  position: fixed; z-index: 20;
  top: calc(env(safe-area-inset-top) + 8px);
  right: calc(env(safe-area-inset-right) + 8px);
}
#app.mit-teilen .toolbar { padding-top: 56px; }
.teilen-overlay { position: fixed; inset: 0; z-index: 110; background: rgba(0,0,0,0.12); }
.teilen-panel {
  position: fixed; overflow-y: auto;
  background: var(--panel); color: var(--panel-fg);
  border-radius: 16px; padding: 6px 0 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
}
.teilen-panel.ist-menue { padding: 6px; }
.teilen-pfeil {
  position: fixed; width: 0; height: 0;
  border-left: 9px solid transparent; border-right: 9px solid transparent;
  border-bottom: 10px solid var(--panel);
  filter: drop-shadow(0 -2px 2px rgba(0,0,0,0.08));
}
.teilen-menue-btn {
  display: flex; align-items: center; gap: 12px; width: 100%;
  border: none; background: transparent; color: var(--panel-fg);
  padding: 10px 12px; border-radius: 10px; text-align: left; font: inherit; cursor: pointer;
}
.teilen-menue-btn + .teilen-menue-btn { border-top: 1px solid var(--border); border-radius: 0 0 10px 10px; }
.teilen-menue-btn:active { background: rgba(120,120,128,0.16); }
.teilen-menue-btn > span { display: flex; flex-direction: column; }
.teilen-menue-titel { font-weight: 600; font-size: 15px; }
.teilen-menue-sub { font-size: 12px; opacity: 0.7; }
.teilen-inhalt { padding: 0 16px; display: flex; flex-direction: column; align-items: stretch; gap: 10px; }
.teilen-qr { align-self: center; background: #fff; border-radius: 12px; padding: 6px; line-height: 0; }
.teilen-qr canvas { display: block; image-rendering: pixelated; }
.teilen-link {
  font-size: 11px; opacity: 0.75; word-break: break-all; text-align: center;
  -webkit-user-select: text; user-select: text;
}
.teilen-knoepfe { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.teilen-option { padding-left: 0; padding-right: 0; }
.teilen-video { position: relative; background: #000; border-radius: 12px; overflow: hidden; aspect-ratio: 4 / 3; }
.teilen-video video { width: 100%; height: 100%; object-fit: cover; display: block; }
.teilen-video::after { /* Zielrahmen */
  content: ''; position: absolute; inset: 18%; border: 3px solid rgba(255,255,255,0.85); border-radius: 14px;
}
.teilen-status { font-size: 13px; text-align: center; opacity: 0.85; }
.teilen-toast {
  position: fixed; z-index: 130; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 72px);
  transform: translateX(-50%); max-width: calc(100vw - 24px);
  display: flex; align-items: center; gap: 12px;
  background: rgb(41, 43, 52); color: #fff; padding: 10px 12px 10px 16px; border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3); font-size: 14px;
}
.teilen-toast-btn {
  border: none; background: rgba(255,255,255,0.16); color: #fff; font: inherit; font-weight: 600;
  padding: 6px 10px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
@media (prefers-color-scheme: dark) { .teilen-toast { background: rgb(238, 238, 238); color: rgb(41, 43, 52); } .teilen-toast-btn { background: rgba(0,0,0,0.1); color: rgb(41, 43, 52); } }

/* Kleinere Bildschirme */
@media (max-width: 520px) {
  #app { grid-template-columns: 1fr 48px; }
  .tool-btn { width: 36px; height: 36px; }
  .teilen-btn { right: calc(env(safe-area-inset-right) + 6px); }
  #app.mit-teilen .toolbar { padding-top: 50px; }
  .mode-btn { width: 42px; }
  .mode-bar { margin: 8px 4px 12px; }
}
