:root {
  --accent: #2f6fec;
  --accent-soft: rgba(47, 111, 236, .13);
  --bg: #f2f2f7;
  --bar: #fbfbfd;
  --border: #dcdce1;
  --text: #1c1c1e;
  --muted: #8e8e93;
  --danger: #e03131;
  --pad: 24px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

input { -webkit-user-select: text; user-select: text; font: inherit; }
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
svg { display: block; }
[hidden] { display: none !important; }

.view { position: absolute; inset: 0; display: flex; flex-direction: column; }

/* ================= BIBLIOTHEK ================= */
.lib-header {
  padding: calc(env(safe-area-inset-top) + 22px) 32px 14px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.lib-header h1 { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -.5px; }
.lib-actions { display: flex; gap: 10px; align-items: center; }

.search {
  display: flex; align-items: center; gap: 6px;
  background: #e3e3e8; border-radius: 10px; padding: 0 10px; height: 38px;
  color: var(--muted);
}
.search svg { width: 18px; height: 18px; }
.search input { border: 0; background: transparent; outline: none; width: 180px; color: var(--text); font-size: 15px; }

.primary-btn {
  display: flex; align-items: center; gap: 4px;
  height: 38px; padding: 0 16px 0 10px;
  border: 0; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 15px;
}
.primary-btn svg { width: 20px; height: 20px; }
.primary-btn:active { opacity: .8; }

.lib-scroll {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 10px 32px calc(env(safe-area-inset-bottom) + 40px);
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 34px 28px;
}
.empty { color: var(--muted); text-align: center; margin-top: 60px; }

.nb-card { display: flex; flex-direction: column; align-items: center; text-align: center; min-width: 0; }
.nb-open { border: 0; background: none; padding: 0; width: 100%; }

.nb-cover {
  --cover: #3b5bdb;
  position: relative;
  width: 100%;
  aspect-ratio: 0.77;
  border-radius: 4px 12px 12px 4px;
  background: var(--cover);
  box-shadow: 0 1px 2px rgba(0,0,0,.18), 0 8px 18px rgba(0,0,0,.13);
  overflow: hidden;
  transition: transform .15s ease;
}
.nb-cover::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 11%;
  background: linear-gradient(90deg, rgba(0,0,0,.28), rgba(0,0,0,.06) 75%, rgba(255,255,255,.18));
}
.nb-cover::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(140deg, rgba(255,255,255,.2), transparent 45%);
  pointer-events: none;
}
.nb-label {
  position: absolute; top: 17%; left: 21%; right: 10%;
  background: rgba(255,255,255,.93);
  border-radius: 4px;
  padding: 9px 7px;
  font-size: 13px; font-weight: 600; color: #2c2c2e;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  word-break: break-word;
}
.nb-open:active .nb-cover { transform: scale(.96); }
.nb-cover.new {
  background: #fff; box-shadow: none;
  border: 2px dashed #c4c7d0;
  display: grid; place-items: center; color: var(--accent);
}
.nb-cover.new::before, .nb-cover.new::after { display: none; }
.nb-cover.new svg { width: 42px; height: 42px; }

.nb-name {
  margin-top: 12px; max-width: 100%;
  display: inline-flex; align-items: center; gap: 2px;
  border: 0; background: none; padding: 2px 4px; border-radius: 6px;
  font-size: 15px; font-weight: 600;
}
.nb-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nb-name svg { width: 16px; height: 16px; flex: none; color: var(--muted); }
.nb-name:active { background: #e3e3e8; }
.nb-date { font-size: 12px; color: var(--muted); margin-top: 2px; }
.nb-new-label { margin-top: 12px; font-size: 15px; font-weight: 600; color: var(--accent); }

/* ================= EDITOR ================= */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: env(safe-area-inset-top) 8px 0;
  height: calc(50px + env(safe-area-inset-top));
  background: var(--bar);
}
.tb-group { display: flex; gap: 2px; align-items: center; }
.tb-group.right { justify-content: flex-end; }
.title-btn {
  border: 0; background: none; font-weight: 600; font-size: 16px;
  padding: 6px 10px; border-radius: 8px; max-width: 40vw;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.title-btn:active { background: #ececf0; }

.icon-btn {
  width: 42px; height: 42px; border: 0; border-radius: 10px;
  background: transparent; color: var(--accent);
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--accent-soft); }
.icon-btn:disabled { color: #c7c7cc; background: transparent; }
.icon-btn.active { background: var(--accent-soft); }

.toolbar {
  display: flex; align-items: center; justify-content: center;
  height: 54px; padding: 0 10px; gap: 4px;
  background: var(--bar);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tools { display: flex; gap: 4px; }
.tool-btn {
  width: 46px; height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: #48484a;
  display: grid; place-items: center;
}
.tool-btn.active { background: var(--accent-soft); color: var(--accent); }
.tool-btn svg { width: 26px; height: 26px; }
.sep { width: 1px; height: 28px; background: var(--border); margin: 0 10px; flex: none; }

.tool-options { display: flex; align-items: center; gap: 8px; flex: none; }
.opt-group { display: flex; align-items: center; gap: 6px; }
.opt-group + .opt-group { margin-left: 10px; }
.hint { color: var(--muted); font-size: 14px; white-space: nowrap; }

.swatch {
  --c: #000;
  position: relative;
  width: 30px; height: 30px; padding: 0; border: 0; border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.14);
  margin: 0 3px;
}
.swatch.active::after {
  content: ""; position: absolute; inset: -5px;
  border-radius: 50%; border: 2.5px solid var(--accent);
}
.size-btn {
  width: 36px; height: 36px; border: 0; border-radius: 9px;
  background: transparent; display: grid; place-items: center;
}
.size-btn span { display: block; border-radius: 50%; background: #48484a; }
.size-btn.active { background: var(--accent-soft); }
.size-btn.active span { background: var(--accent); }

/* Erweiterte Werkzeug-Optionen */
.opt-drop {
  display: inline-flex; align-items: center; gap: 5px;
  border: 0; background: #ececf1; color: #2c2c2e;
  border-radius: 10px; padding: 6px 9px 6px 7px; font-size: 13px; font-weight: 500; white-space: nowrap;
}
.opt-drop.dashed span { text-decoration: underline dashed; text-underline-offset: 3px; }
.opt-chip {
  border: 0; background: #ececf1; color: #48484a;
  border-radius: 16px; padding: 6px 12px; font-size: 13px; font-weight: 500; white-space: nowrap;
}
.opt-chip.on { background: var(--accent-soft); color: var(--accent); }
.seg.full { display: flex; }
.opt-chip.square { width: 34px; height: 34px; padding: 0; border-radius: 9px; font-size: 16px; }
.opt-chip.square + .opt-chip.square { margin-left: 4px; }
.size-a { color: #48484a; font-weight: 600; line-height: 1; }
.size-btn.active .size-a { color: var(--accent); }
.font-sample { font-size: 14px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; }

/* Text-Werkzeug */
.page.text-cursor, .page:has(.text-edit) { cursor: text; }
.text-edit {
  position: absolute; z-index: 6;
  margin: 0; padding: 0; border: 0; border-radius: 2px;
  background: rgba(255,255,255,.35);
  outline: 1.5px dashed var(--accent); outline-offset: 4px;
  resize: none; overflow: hidden; white-space: pre-wrap; overflow-wrap: break-word;
  touch-action: auto; user-select: text; -webkit-user-select: text;
}
.seg.full button { flex: 1; }

.size-pop { padding: 12px 14px; width: 220px; }
.size-pop-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.size-pop-head b { color: #1c1c1e; font-weight: 600; }
.size-pop-preview { height: 76px; display: grid; place-items: center; }
.size-pop-preview span { display: block; border-radius: 50%; }
.size-pop input[type=range] { width: 100%; accent-color: var(--accent); }

.palette-title { font-size: 12px; font-weight: 600; color: var(--muted); margin: 10px 2px 6px; text-transform: uppercase; letter-spacing: .03em; }
.palette-title:first-child { margin-top: 0; }
.palette.wide { width: 308px; }
.palette.wide .palette-grid { grid-template-columns: repeat(8, 1fr); gap: 7px; }
.palette.wide .palette-grid .swatch { width: 28px; height: 28px; }

.pen-pop, .shape-pop { padding: 8px; width: 250px; }
.pen-pop-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  border: 0; background: none; border-radius: 9px; padding: 8px 10px; text-align: left; color: #2c2c2e;
}
.pen-pop-item span { display: flex; flex-direction: column; }
.pen-pop-item small { color: var(--muted); font-size: 12px; }
.pen-pop-item.active, .shape-pop-item.active { background: var(--accent-soft); color: var(--accent); }
.shape-pop { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.shape-pop .palette-title, .shape-pop .seg { grid-column: 1 / -1; }
.shape-pop-item {
  border: 0; background: none; border-radius: 9px; padding: 8px 2px 6px; color: #2c2c2e;
  display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 11.5px;
}

.seg { display: inline-flex; background: #e6e6eb; border-radius: 9px; padding: 2px; }
.seg button {
  border: 0; background: transparent; padding: 6px 12px; border-radius: 7px;
  font-size: 13px; font-weight: 500; white-space: nowrap;
}
.seg button.active { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); }

.editor-body { flex: 1; display: flex; min-height: 0; }

.thumbs {
  width: 168px; flex: none;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: #ececf1; border-right: 1px solid var(--border);
  padding: 16px 12px calc(env(safe-area-inset-bottom) + 16px);
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}
.thumb { border: 0; background: none; padding: 0; display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.thumb canvas {
  width: 116px; height: 164px; border-radius: 3px; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.thumb.current canvas { outline: 3px solid var(--accent); outline-offset: 2px; }
.thumb.current { color: var(--accent); font-weight: 600; }

.stage { flex: 1; position: relative; min-width: 0; }
.scroller {
  position: absolute; inset: 0;
  overflow: auto;
  background: #e5e5ea;
  touch-action: none;
  overscroll-behavior: contain;
}
.pages { padding-top: var(--pad); }

.page {
  position: relative;
  margin: 0 auto var(--pad);
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 8px 26px rgba(0,0,0,.08);
  cursor: crosshair;
}
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page canvas.live { pointer-events: none; }
.page canvas.live.blend { mix-blend-mode: multiply; }

.add-page {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  margin: 0 auto calc(var(--pad) * 2);
  height: 64px;
  border: 2px dashed #b8bbc6; border-radius: 12px;
  background: rgba(255,255,255,.55); color: #5d616c;
  font-size: 15px; font-weight: 500;
}
.add-page svg { width: 20px; height: 20px; }

.page-indicator {
  position: absolute; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  background: rgba(40,40,45,.72); color: #fff;
  font-size: 13px; font-weight: 500;
  padding: 5px 12px; border-radius: 20px;
  pointer-events: none; opacity: 0; transition: opacity .35s;
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.page-indicator.show { opacity: 1; }

/* Lasso-Auswahl */
.sel-box {
  position: absolute;
  border: 1.5px dashed var(--accent);
  border-radius: 4px;
  background: rgba(47,111,236,.05);
  pointer-events: none;
  z-index: 2;
}
.sel-menu {
  position: absolute; left: 50%; bottom: 100%;
  transform: translate(-50%, -10px);
  display: flex;
  background: #2c2c2e; border-radius: 10px; overflow: hidden;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  pointer-events: auto;
  white-space: nowrap;
}
.sel-menu button {
  border: 0; background: none; color: #fff;
  padding: 9px 14px; font-size: 14px;
}
.sel-menu button + button { border-left: 1px solid rgba(255,255,255,.15); }
.sel-menu button:active { background: rgba(255,255,255,.15); }

/* ================= MENÜS / POPOVER ================= */
.floating {
  position: fixed; z-index: 100;
  background: rgba(252,252,254,.97);
  -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px);
  border-radius: 14px;
  box-shadow: 0 0 0 .5px rgba(0,0,0,.12), 0 12px 40px rgba(0,0,0,.22);
  padding: 6px;
  animation: pop .14s ease-out;
}
@keyframes pop { from { opacity: 0; transform: scale(.96); } }
.menu { min-width: 250px; }
.menu-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; border: 0; background: none;
  padding: 10px 12px; border-radius: 9px;
  text-align: left; font-size: 15px;
}
.menu-item:hover, .menu-item:active { background: #ebebf0; }
.menu-item.danger { color: var(--danger); }
.mi-icon { width: 22px; display: grid; place-items: center; color: var(--accent); }
.menu-item.danger .mi-icon { color: var(--danger); }
.mi-icon svg { width: 20px; height: 20px; }
.mi-label { flex: 1; }
.mi-check { color: var(--accent); }
.mi-check svg { width: 18px; height: 18px; }
.menu-head { font-size: 12px; color: var(--muted); padding: 10px 12px 4px; text-transform: uppercase; letter-spacing: .4px; }
.menu-sep { height: 1px; background: var(--border); margin: 6px 4px; }
.switch {
  width: 42px; height: 26px; border-radius: 13px; background: #d1d1d6;
  position: relative; transition: background .2s; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 22px; height: 22px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s;
}
.switch.on { background: #34c759; }
.switch.on::after { transform: translateX(16px); }

.palette { padding: 10px; width: 236px; }
.palette-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.palette-grid .swatch { margin: 0; width: 30px; height: 30px; }
.palette-custom {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 12px; font-size: 14px; color: #48484a;
}
.palette-custom input { width: 44px; height: 30px; border: 0; padding: 0; background: none; }

/* ================= MODAL ================= */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.32);
  display: grid; place-items: center;
  padding: 20px;
  animation: fade .15s;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: min(520px, 100%);
  max-height: calc(100% - 20px);
  overflow-y: auto;
  background: #f2f2f7;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal-head {
  position: sticky; top: 0; z-index: 1;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 12px 14px;
  background: rgba(242,242,247,.95);
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.modal-head .link-btn:last-child { justify-self: end; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 16px; padding: 6px; justify-self: start; }
.link-btn.strong { font-weight: 600; }
.modal-body { padding: 18px 20px 24px; display: flex; flex-direction: column; gap: 18px; }
.preview-row { display: flex; justify-content: center; }
.nb-cover.big { width: 130px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field > span { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.field input[type=text] {
  border: 0; border-radius: 10px; background: #fff;
  padding: 12px 14px; font-size: 16px; outline: none;
}
.field input[type=text]:focus { box-shadow: 0 0 0 2px var(--accent); }
.choices { display: flex; flex-wrap: wrap; gap: 10px; }
.choices .swatch { margin: 4px; width: 34px; height: 34px; }
.templates { gap: 12px; }
.tpl {
  border: 0; background: none; padding: 0;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 13px; color: #48484a;
}
.tpl-prev {
  width: 72px; height: 100px; border-radius: 4px; background-color: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.18);
}
.tpl.active .tpl-prev { outline: 3px solid var(--accent); outline-offset: 2px; }
.tpl.active { color: var(--accent); font-weight: 600; }
.tpl-lined { background-image: repeating-linear-gradient(to bottom, transparent 0 8px, #c3ccd9 8px 9px); background-position: 0 12px; }
.tpl-grid {
  background-image: linear-gradient(#d3dae4 1px, transparent 1px), linear-gradient(90deg, #d3dae4 1px, transparent 1px);
  background-size: 8px 8px;
}
.tpl-dotted { background-image: radial-gradient(circle, #9fabbb 1px, transparent 1.3px); background-size: 8px 8px; }

/* ================= TOAST ================= */
.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 14px); z-index: 300;
  transform: translate(-50%, -20px);
  background: rgba(40,40,45,.9); color: #fff;
  padding: 10px 16px; border-radius: 12px;
  font-size: 14px; max-width: 90vw; text-align: center;
  opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ================= DRUCK / PDF ================= */
#printArea { display: none; }
@media print {
  @page { size: A4; margin: 0; }
  html, body { position: static; overflow: visible; height: auto; background: #fff; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; }
  #printArea img { display: block; width: 210mm; height: 296.5mm; break-after: page; page-break-after: always; }
  #printArea img:last-child { break-after: auto; page-break-after: auto; }
}

@media (max-width: 640px) {
  .lib-header, .lib-scroll { padding-left: 18px; padding-right: 18px; }
  .search input { width: 110px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 26px 18px; }
  .toolbar { justify-content: flex-start; }
  .thumbs { width: 130px; }
  .thumb canvas { width: 90px; height: 127px; }
}

/* ================= KI-LÖSER ================= */
.tool-btn.ai-tool { color: #9c36b5; }
.tool-btn.ai-tool.active { background: rgba(156,54,181,.13); color: #9c36b5; }

.chip-btn {
  display: inline-flex; align-items: center; gap: 5px;
  height: 34px; padding: 0 12px 0 9px;
  border: 0; border-radius: 9px;
  background: #ececf1; color: #3a3a3c;
  font-size: 13px; font-weight: 500; white-space: nowrap;
}
.chip-btn svg { width: 18px; height: 18px; }
.chip-btn:active { background: #e0e0e6; }

.ai-box {
  position: absolute; z-index: 3;
  border: 2px dashed #9c36b5; border-radius: 6px;
  background: rgba(156,54,181,.06);
  pointer-events: none;
  animation: aiPulse 1.6s ease-in-out infinite;
}
@keyframes aiPulse { 50% { background: rgba(156,54,181,.13); } }
.ai-pill {
  position: absolute; left: 50%; top: 100%;
  transform: translate(-50%, 10px);
  display: flex; align-items: center; gap: 10px;
  padding: 7px 8px 7px 14px;
  background: #2c2c2e; color: #fff; border-radius: 22px;
  font-size: 14px; white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  pointer-events: auto;
}
.ai-box.writing .ai-text::after { content: " ✍️"; }
.ai-spin {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.3); border-top-color: #e599f7;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.ai-cancel {
  border: 0; background: rgba(255,255,255,.15); color: #fff;
  border-radius: 14px; padding: 5px 10px; font-size: 13px;
}

.help { margin: 0; font-size: 14px; line-height: 1.45; color: #48484a; }
.help.small { font-size: 12.5px; color: var(--muted); }
.secondary-btn {
  border: 0; border-radius: 10px; background: #fff; color: var(--accent);
  padding: 12px; font-size: 15px; font-weight: 500;
}
.danger-link { border: 0; background: none; color: var(--danger); font-size: 14px; align-self: center; padding: 6px; }
.field input[type=password] {
  border: 0; border-radius: 10px; background: #fff;
  padding: 12px 14px; font-size: 16px; outline: none;
}
.field input[type=password]:focus { box-shadow: 0 0 0 2px var(--accent); }

/* Handschrift-Setup */
.modal.wide { width: min(820px, 100%); }
.hand-progress { font-size: 13px; color: var(--muted); }
.hand-grid {
  display: grid; grid-template-columns: repeat(auto-fill, 72px);
  gap: 12px; justify-content: center;
}
.hand-cell {
  background: #fff; border-radius: 10px; overflow: hidden;
  box-shadow: 0 0 0 1px rgba(0,0,0,.08);
}
.hand-cell.done { box-shadow: 0 0 0 2px #34c759; }
.hand-label {
  display: flex; align-items: center; justify-content: space-between;
  padding: 2px 4px 0 8px; height: 24px;
  font-size: 15px; font-weight: 600; color: #3a3a3c;
  background: #f7f7fa; border-bottom: 1px solid #ececf0;
}
.hand-clear {
  border: 0; background: none; color: var(--muted);
  width: 22px; height: 22px; padding: 0; font-size: 17px; line-height: 1;
  visibility: hidden;
}
.hand-cell.done .hand-clear { visibility: visible; }
.hand-cell canvas { display: block; touch-action: none; cursor: crosshair; }
.hand-preview {
  width: 100%; height: 110px; border-radius: 10px; background: #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.08);
}
.field input#handSample { border: 0; border-radius: 10px; background: #fff; padding: 10px 12px; font-size: 15px; outline: none; }

/* ================= STARTBILDSCHIRM ================= */
.welcome {
  align-items: center; justify-content: center;
  overflow-y: auto; padding: calc(env(safe-area-inset-top) + 24px) 20px 24px;
  background: radial-gradient(1200px 600px at 20% -10%, #dfe8ff, transparent), var(--bg);
}
.welcome-card {
  width: min(480px, 100%); margin: auto;
  background: #fff; border-radius: 20px; padding: 32px 28px 24px;
  box-shadow: 0 20px 60px rgba(30,50,120,.15);
  display: flex; flex-direction: column; gap: 16px;
}
.welcome-logo { width: 64px; height: 64px; border-radius: 16px; }
.welcome-card h1 { margin: 0; font-size: 28px; letter-spacing: -.4px; }
.ai-accent { color: var(--accent); }
.welcome-sub { margin: 0; color: #48484a; line-height: 1.5; }
.welcome-steps { margin: 0; padding-left: 20px; color: #3a3a3c; line-height: 1.6; font-size: 15px; }
.welcome-steps a, .help a { color: var(--accent); }
.welcome .field input { background: #f2f2f7; }
.primary-btn.big { height: 48px; justify-content: center; font-size: 16px; padding: 0 20px; }
.primary-btn:disabled { opacity: .6; }
.form-error { color: var(--danger); font-size: 14px; min-height: 0; }
.form-error:empty { display: none; }
.welcome-back { color: var(--muted); font-size: 14px; text-decoration: none; align-self: center; }
.model-row { display: flex; gap: 8px; align-items: center; }
.model-row select {
  flex: 1; min-width: 0; border: 0; border-radius: 10px; background: #fff;
  padding: 11px 12px; font: inherit; font-size: 15px;
}

/* ================= AUSWAHL: GRÖSSE ÄNDERN ================= */
.sel-handle {
  position: absolute;
  width: 18px; height: 18px; margin: -9px 0 0 -9px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.25);
  pointer-events: auto;
  touch-action: none;
  z-index: 1;
}
.sel-handle::before { content: ""; position: absolute; inset: -12px; } /* größere Trefferfläche für Finger */
.sel-handle.nw { left: 0;    top: 0;    cursor: nwse-resize; }
.sel-handle.ne { left: 100%; top: 0;    cursor: nesw-resize; }
.sel-handle.sw { left: 0;    top: 100%; cursor: nesw-resize; }
.sel-handle.se { left: 100%; top: 100%; cursor: nwse-resize; }

/* ================= IMPORT ================= */
.secondary-btn.small {
  display: flex; align-items: center; gap: 4px;
  height: 38px; padding: 0 14px 0 10px;
  font-size: 15px; font-weight: 600;
  box-shadow: inset 0 0 0 1.5px #d6dbe6;
}
.secondary-btn.small svg { width: 20px; height: 20px; }

/* ================= KI-TUTOR (CHAT) ================= */
textarea { -webkit-user-select: text; user-select: text; font: inherit; }

.chat {
  width: 370px; flex: none;
  display: flex; flex-direction: column; min-height: 0;
  background: #fbfbfd;
  border-left: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.chat-head {
  display: flex; align-items: center; gap: 2px;
  padding: 6px 6px 6px 14px; border-bottom: 1px solid var(--border);
}
.chat-title { flex: 1; display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 15px; color: #9c36b5; }
.chat-title svg { width: 20px; height: 20px; }
#chatClose svg { transform: rotate(180deg); }

.chat-log {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 14px 12px; display: flex; flex-direction: column; gap: 12px;
  -webkit-user-select: text; user-select: text;
}
.chat-empty { margin: auto; text-align: center; color: var(--muted); font-size: 14px; line-height: 1.5; padding: 20px; }
.chat-empty b { color: var(--text); font-size: 16px; }
.chat-empty-icon { color: #9c36b5; display: grid; place-items: center; margin-bottom: 8px; }

.msg { max-width: 92%; font-size: 14.5px; line-height: 1.5; }
.msg.user {
  align-self: flex-end;
  background: var(--accent); color: #fff;
  padding: 8px 12px; border-radius: 16px 16px 4px 16px;
}
.msg.model {
  align-self: flex-start;
  background: #fff; color: var(--text);
  padding: 10px 13px; border-radius: 16px 16px 16px 4px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.06), 0 2px 6px rgba(0,0,0,.04);
}
.msg.error { background: #fff4f4; box-shadow: 0 0 0 1px #ffc9c9; }
.msg-text { white-space: pre-wrap; word-wrap: break-word; }
.msg-text.md { white-space: normal; }
.md p { margin: 0 0 8px; }
.md p:last-child, .md ul:last-child, .md ol:last-child { margin-bottom: 0; }
.md h4 { margin: 10px 0 6px; font-size: 15px; }
.md h4:first-child { margin-top: 0; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 20px; }
.md li { margin: 2px 0; }
.md code { background: #f1f1f5; padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.md pre { background: #f1f1f5; padding: 8px 10px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.msg-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg-img { display: block; max-width: 180px; max-height: 140px; border-radius: 8px; background: #fff; }
.msg-imgs .msg-img:not(:only-child) { width: 84px; height: 84px; object-fit: cover; }
.msg-att { font-size: 12px; opacity: .85; margin-bottom: 3px; }
.msg-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.msg-actions button {
  border: 0; background: #f1f1f5; color: #3a3a3c;
  border-radius: 8px; padding: 5px 9px; font-size: 12.5px;
}
.msg-actions button:active { background: #e4e4ea; }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: #b9a3c9; animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.chat-suggest { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 10px; }
.chat-suggest button {
  border: 0; background: #f3e8f8; color: #862e9c;
  border-radius: 14px; padding: 6px 11px; font-size: 13px;
}
.chat-compose { border-top: 1px solid var(--border); padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 7px; }
.chat-attach { display: flex; flex-wrap: wrap; gap: 6px; }
.att-chip span { max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 0; border-radius: 10px; padding: 4px 8px;
  background: #ececf1; color: var(--muted); font-size: 12.5px;
}
.att-chip.on { background: #e6eeff; color: var(--accent); }
.att-chip img { width: 30px; height: 30px; object-fit: cover; border-radius: 5px; background: #fff; }
.att-chip button { border: 0; background: none; color: inherit; font-size: 16px; padding: 0 2px; }
.chat-input { display: flex; align-items: flex-end; gap: 8px; }
.chat-input textarea {
  flex: 1; resize: none; border: 0; outline: none;
  background: #fff; border-radius: 18px; padding: 9px 14px;
  font-size: 15px; line-height: 1.4; max-height: 140px;
  box-shadow: inset 0 0 0 1px #dcdce1;
}
.chat-input textarea:focus { box-shadow: inset 0 0 0 1.5px var(--accent); }
.chat-send {
  width: 38px; height: 38px; flex: none; border: 0; border-radius: 50%;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.chat-send.stop { background: #3a3a3c; }
.chat-upload {
  width: 38px; height: 38px; flex: none; border: 0; border-radius: 50%;
  background: #ececf1; color: #3a3a3c; display: grid; place-items: center;
}
.chat-upload:active { background: #e0e0e6; }
.att-chip.draw { background: #f3e8f8; color: #862e9c; font-weight: 500; }
.draw-card {
  display: flex; align-items: center; gap: 8px; margin-top: 6px;
  background: #f3e8f8; color: #862e9c; border-radius: 10px; padding: 8px 10px; font-size: 13px; font-weight: 500;
}
.md p + .draw-card, .md ul + .draw-card { margin-top: 8px; }
.draw-card:first-child { margin-top: 0; }
.draw-card.busy svg { animation: wiggle 1s infinite ease-in-out; }
@keyframes wiggle { 0%, 100% { transform: rotate(-8deg); } 50% { transform: rotate(8deg); } }
.chat.drop { box-shadow: inset 0 0 0 3px var(--accent); }
.chat.drop .chat-log { background: #f0f5ff; }

/* Schmale Bildschirme: Chat liegt über dem Heft */
@media (max-width: 760px) {
  .editor-body { position: relative; }
  .chat {
    position: absolute; inset: 0 0 0 auto; z-index: 20;
    width: min(100%, 420px);
    box-shadow: -10px 0 40px rgba(0,0,0,.15);
  }
}

/* Netlify blendet auf kostenlosen Konten ein "Powered by Netlify"-Badge ein –
   in der App verdeckt es den Senden-Knopf des Chats, daher hier ausblenden. */
#nl-badge-frame { display: none !important; }

/* Start-Anzeige (Laden / Fehler) */
.boot {
  position: fixed; inset: 0; z-index: 1000;
  display: grid; place-items: center; padding: 24px;
  background: #f2f2f7; font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
}
.boot[hidden] { display: none; }
.boot-card { max-width: 520px; width: 100%; text-align: center; color: #3a3a3c; }
.boot-card p { font-size: 17px; margin: 14px 0; }
.boot-card pre {
  text-align: left; white-space: pre-wrap; word-break: break-word;
  background: #fff; border-radius: 12px; padding: 12px; font-size: 12.5px; color: #c92a2a;
  box-shadow: 0 1px 3px rgba(0,0,0,.08); max-height: 40vh; overflow: auto;
}
.boot-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }
.boot-actions button {
  border: 0; border-radius: 10px; padding: 10px 16px; font-size: 15px; font-weight: 600;
  background: #2f6fe8; color: #fff;
}
.boot-actions button + button { background: #e5e5ea; color: #1c1c1e; }

/* ---------------------------- Konto ---------------------------- */
.auth-card { gap: 14px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form .help.small { margin: -6px 0 0; min-height: 0; }
.auth-form .help.small:empty { display: none; }
.auth-guest { align-self: center; color: var(--muted); font-size: 14px; }
.account-btn {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  border: 0; background: transparent; padding: 0; color: var(--accent); font-size: 15px; font-weight: 500;
}
.account-btn.guest { background: var(--accent-soft); border-radius: 18px; padding: 7px 12px 7px 9px; }
.account-btn .avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 15px;
}
.account-btn .sync-dot {
  position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--bg, #f2f2f7); background: #8e8e93;
}
.account-btn.ok .sync-dot { background: #2f9e44; }
.account-btn.syncing .sync-dot { background: #1e6ff1; animation: pulse 1s infinite ease-in-out; }
.account-btn.offline .sync-dot { background: #f59f00; }
.account-btn.error .sync-dot { background: #e03131; }
@keyframes pulse { 50% { opacity: .35; } }
.modal.small { max-width: 420px; }
.modal-body form, form.modal-body { display: flex; flex-direction: column; gap: 12px; }
.pw-fields { display: flex; flex-direction: column; gap: 10px; }
.primary-btn.danger { background: #e03131; }

/* ---------------------------- Wartung & Admin ---------------------------- */
.maint-card { text-align: center; align-items: center; }
.maint-icon { font-size: 54px; line-height: 1; }
.maint-card .primary-btn { align-self: stretch; }
.maint-banner {
  border: 0; border-radius: 14px; padding: 5px 11px; margin-left: 12px;
  background: #fff4e6; color: #d9480f; font-size: 13px; font-weight: 600; white-space: nowrap;
}
.modal.admin { max-width: 760px; }
.admin-card { background: #fff; border-radius: 14px; padding: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.08); display: flex; flex-direction: column; gap: 10px; }
.admin-card + .admin-card { margin-top: 14px; }
.admin-row-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.admin-row-head .help.small { margin: 2px 0 0; }
.admin-card textarea { width: 100%; resize: vertical; border: 0; border-radius: 10px; background: #f2f2f7; padding: 9px 11px; font: inherit; font-size: 15px; }
.admin-maint-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.admin-users { display: flex; flex-direction: column; }
.admin-user { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 2px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.admin-user:first-child { border-top: 0; }
.admin-user.locked .au-email { color: #c92a2a; }
.au-main { min-width: 0; flex: 1 1 260px; }
.au-email { font-weight: 600; word-break: break-all; }
.au-badges { display: flex; gap: 5px; margin: 3px 0; flex-wrap: wrap; }
.au-badges:empty { display: none; }
.badge { font-size: 11.5px; font-weight: 600; border-radius: 6px; padding: 2px 7px; }
.badge.admin { background: #e7f0ff; color: #1c4fd8; }
.badge.locked { background: #ffe3e3; color: #c92a2a; }
.badge.self { background: #ececf1; color: #48484a; }
.au-meta { font-size: 12.5px; color: var(--muted); }
.au-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.au-actions .chip-btn.warn { color: #d9480f; }
.au-actions .chip-btn.danger { color: #c92a2a; }


/* ---------------------------- Google-Anmeldung ---------------------------- */
.google-wrap { display: flex; flex-direction: column; gap: 14px; }
.google-wrap[hidden] { display: none; }
.google-btn { display: flex; justify-content: center; min-height: 44px; }
.google-wrap.busy .google-btn { opacity: .5; pointer-events: none; }
.auth-or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.auth-or::before, .auth-or::after { content: ""; flex: 1; height: 1px; background: var(--border); }
