:root {
  --accent: #4f46e5;
  --accent-dark: #4338ca;
  --bg: #f4f5f9;
  --card: #ffffff;
  --text: #1e2130;
  --muted: #6b7280;
  --border: #dfe2ea;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 24, 40, .08), 0 4px 16px rgba(20, 24, 40, .06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.2;
  white-space: nowrap;
}
.btn:hover { background: #f0f1f6; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: var(--accent-dark); }
.btn.danger { color: var(--red); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.active {
  background: #eef2ff;
  border-color: var(--accent);
  color: var(--accent);
}
.btn:disabled { opacity: .5; cursor: default; }

input[type="text"], textarea, select {
  font: inherit;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  width: 100%;
}
input[type="text"]:focus, textarea:focus { outline: 2px solid #c7d2fe; border-color: var(--accent); }

/* ---------- App-Kopfzeile ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  flex-wrap: wrap;
}
.topbar .title {
  font-weight: 650;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}
.topbar .spacer { flex: 1; }

/* ---------- Bibliothek ---------- */
.lib {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}
.lib-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.lib-head h1 { font-size: 22px; margin: 8px 0; flex: 1; }
.scan-hint {
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 18px;
  line-height: 1.45;
}
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.card .thumb {
  aspect-ratio: 4 / 3;
  background: #eceef4 center top / cover no-repeat;
  border-bottom: 1px solid var(--border);
  display: block;
  width: 100%;
  object-fit: cover;
  object-position: top;
}
.card .card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.card .card-name { font-weight: 600; font-size: 15px; word-break: break-word; }
.card .card-meta { font-size: 12.5px; color: var(--muted); }
.card .card-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; }
.empty-lib {
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  font-size: 15px;
}
.dropzone.dragover { outline: 3px dashed var(--accent); outline-offset: -3px; border-radius: var(--radius); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 30, .45);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal {
  background: var(--card);
  border-radius: 14px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow: auto;
  padding: 20px;
}
.modal h2 { margin: 0 0 14px; font-size: 18px; }
.modal .row { margin-bottom: 14px; }
.modal label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: var(--muted); }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.progress-note { font-size: 14px; color: var(--muted); }

/* ---------- Seiten-Ansicht (Editor + Üben) ---------- */
.stage {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  flex: 1;
}
.page-wrap {
  position: relative;
  margin: 0 auto 24px;
  box-shadow: var(--shadow);
  background: #fff;
  line-height: 0;
  max-width: 100%;
}
.page-wrap img.page-img {
  display: block;
  width: 100%;
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
}
.overlay {
  position: absolute;
  inset: 0;
}
.page-label {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin: -14px 0 18px;
}

/* ---------- Editor ---------- */
.editor-layout {
  display: flex;
  height: calc(100dvh - 53px);
}
.editor-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.side-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--card);
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
}
.side-panel h3 { margin: 0 0 4px; font-size: 15px; }
.side-panel .hint { font-size: 12.5px; color: var(--muted); line-height: 1.45; margin: 8px 0; }
.side-panel .field { margin-bottom: 14px; }
.side-panel .field > label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.side-panel textarea { min-height: 90px; resize: vertical; }
.check-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check-row input { width: auto; }

.ebox {
  position: absolute;
  background: rgba(255, 255, 255, .92);
  border: 1.5px solid rgba(79, 70, 229, .65);
  border-radius: 2px;
  touch-action: none;
  cursor: move;
}
.ebox.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(79, 70, 229, .3);
  z-index: 10;
}
.ebox .num-badge {
  position: absolute;
  top: -9px;
  left: -9px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 11.5px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  pointer-events: none;
}
.ebox.no-answers .num-badge { background: var(--amber); }
.ebox .handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border: 1.5px solid var(--accent);
  border-radius: 50%;
  touch-action: none;
  display: none;
  z-index: 11;
}
.ebox.selected .handle { display: block; }
.handle.nw { top: -8px; left: -8px; cursor: nwse-resize; }
.handle.ne { top: -8px; right: -8px; cursor: nesw-resize; }
.handle.sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.handle.se { bottom: -8px; right: -8px; cursor: nwse-resize; }
.handle.n { top: -8px; left: 50%; margin-left: -7px; cursor: ns-resize; }
.handle.s { bottom: -8px; left: 50%; margin-left: -7px; cursor: ns-resize; }
.handle.w { left: -8px; top: 50%; margin-top: -7px; cursor: ew-resize; }
.handle.e { right: -8px; top: 50%; margin-top: -7px; cursor: ew-resize; }

.overlay.drawing { cursor: crosshair; }
.overlay.draw-armed { touch-action: none; cursor: crosshair; }
.draw-preview {
  position: absolute;
  border: 1.5px dashed var(--accent);
  background: rgba(79, 70, 229, .08);
  pointer-events: none;
}
.save-state { font-size: 12.5px; color: var(--muted); min-width: 86px; text-align: right; }

/* ---------- Übungsmodus ---------- */
.practice-layout { padding-bottom: 90px; }
.pbox {
  position: absolute;
  background: #fff;
  border: 1.5px solid #c3c8d4;
  border-radius: 2px;
  display: flex;
  align-items: stretch;
}
.pbox input.answer-input {
  flex: 1;
  min-width: 0;
  border: none;
  border-radius: 0;
  padding: 0 4px;
  background: transparent;
  height: 100%;
  width: 100%;
}
.pbox input.answer-input:focus { outline: 2px solid #c7d2fe; }
.pbox .peek-btn {
  position: absolute;
  top: -11px;
  right: -11px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: #fff;
  box-shadow: var(--shadow);
  font-size: 13px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  z-index: 12;
}
.pbox .pnum {
  position: absolute;
  top: -9px;
  left: -9px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  background: #6b7280;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 12;
}
.pbox.revealed { background: transparent; border-style: dashed; }
.pbox.revealed input.answer-input { visibility: hidden; }
.pbox.correct { border-color: var(--green); box-shadow: 0 0 0 2.5px rgba(22, 163, 74, .45); }
.pbox.correct .pnum { background: var(--green); }
.pbox.wrong { border-color: var(--red); box-shadow: 0 0 0 2.5px rgba(220, 38, 38, .5); }
.pbox.wrong .pnum { background: var(--red); }
.judge-bar {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  display: flex;
  gap: 6px;
  z-index: 13;
}
.judge-bar button {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 7px;
  padding: 3px 12px;
  font-size: 15px;
  box-shadow: var(--shadow);
}
.judge-bar button.sel-right { background: var(--green-bg); border-color: var(--green); }
.judge-bar button.sel-wrong { background: var(--red-bg); border-color: var(--red); }
.expected-label {
  position: absolute;
  top: calc(100% + 3px);
  left: 0;
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid #fca5a5;
  font-size: 12.5px;
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
  z-index: 13;
  max-width: 340px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.practice-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  z-index: 60;
}
.practice-bar .progress { font-size: 13.5px; color: var(--muted); }

/* ---------- Auswertung ---------- */
.result-score {
  text-align: center;
  margin: 6px 0 16px;
}
.result-score .big { font-size: 40px; font-weight: 750; }
.result-score .pct { color: var(--muted); font-size: 15px; margin-top: 2px; }
.result-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.result-table th, .result-table td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.result-table td.status { font-size: 16px; width: 30px; }
.muted { color: var(--muted); }

/* ---------- Druckansicht ---------- */
.print-controls {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.print-controls label { display: flex; align-items: center; gap: 6px; }
.print-stage { max-width: 900px; margin: 0 auto; padding: 20px 16px; }
.print-page { margin: 0 auto 24px; box-shadow: var(--shadow); background: #fff; }
.print-page canvas { display: block; width: 100%; height: auto; }
.solution-sheet { padding: 34px 40px; }
.solution-sheet h2 { margin: 0 0 4px; font-size: 20px; }
.solution-sheet .sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }
.solution-sheet ol { columns: 2; column-gap: 40px; margin: 0; padding-left: 24px; font-size: 14.5px; line-height: 1.9; }

@media print {
  body { background: #fff; }
  .topbar, .print-controls, #toast { display: none !important; }
  .print-stage { max-width: none; padding: 0; margin: 0; }
  .print-page {
    box-shadow: none;
    margin: 0;
    break-after: page;
    page-break-after: always;
  }
  .print-page:last-child { break-after: auto; page-break-after: auto; }
  .solution-sheet { padding: 0; }
  .solution-sheet ol { column-gap: 60px; }
}
@page { margin: 10mm; }

/* ---------- Login / Setup ---------- */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  background: var(--card);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
}
.auth-logo { font-size: 40px; text-align: center; margin-bottom: 6px; }
.auth-card h1 { font-size: 22px; text-align: center; margin: 0 0 6px; }
.auth-sub { color: var(--muted); font-size: 14px; text-align: center; margin: 0 0 20px; line-height: 1.45; }
.auth-field { margin-bottom: 14px; }
.auth-field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: var(--muted); }
.auth-error { color: var(--red); font-size: 13.5px; min-height: 18px; margin-bottom: 10px; }

/* ---------- Navigation / Benutzer ---------- */
.user-chip {
  font-size: 13px;
  color: var(--muted);
  background: #eef0f6;
  padding: 6px 12px;
  border-radius: 999px;
  white-space: nowrap;
}

/* ---------- Benutzerverwaltung ---------- */
.admin-form {
  display: grid;
  grid-template-columns: 130px 1fr 1fr 1fr;
  gap: 8px;
}
@media (max-width: 720px) { .admin-form { grid-template-columns: 1fr 1fr; } }
.parent-checks { margin-top: 10px; }
.user-row {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.user-row .user-info { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex: 1; min-width: 220px; }
.role-badge {
  font-size: 11.5px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.role-admin { background: #ede9fe; color: #6d28d9; }
.role-parent { background: #dbeafe; color: #1d4ed8; }
.role-child { background: #dcfce7; color: #15803d; }

/* ---------- Statistiken ---------- */
.child-tabs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.score-pill {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 12.5px;
  white-space: nowrap;
}
.score-good { background: var(--green-bg); color: var(--green); }
.score-mid { background: #fef3c7; color: var(--amber); }
.score-bad { background: var(--red-bg); color: var(--red); }

/* ---------- Kind-Mehrfachauswahl ---------- */
.card.sel { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e2130;
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 500;
  box-shadow: 0 6px 24px rgba(0,0,0,.3);
  max-width: 90vw;
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .editor-layout { flex-direction: column; height: auto; }
  .editor-main { order: 1; }
  .side-panel {
    order: 2;
    width: auto;
    border-left: none;
    border-top: 1px solid var(--border);
    position: sticky;
    bottom: 0;
    max-height: 45dvh;
    box-shadow: 0 -4px 16px rgba(20,24,40,.08);
    z-index: 40;
  }
  .stage { padding: 10px; }
  .topbar { padding: 8px 10px; gap: 6px; }
  .btn { padding: 8px 10px; font-size: 13px; }
}
