:root {
  --bg: #f5f5f4;
  --paper: #ffffff;
  --fg: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1f2937;
  --accent-fg: #ffffff;
  --primary: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #f59e0b;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --paper: #ffffff; /* Schreibfläche bleibt weiß (lesbar für die KI) */
    --fg: #f3f4f6;
    --muted: #9ca3af;
    --line: #262a33;
    --accent: #e5e7eb;
    --accent-fg: #0f1115;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
input, select, textarea { font: inherit; color: inherit; }

.view { position: fixed; inset: 0; display: flex; flex-direction: column; padding-top: var(--safe-top); }

/* ---- Topbar ---- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.topbar h1 { font-size: 20px; margin: 0; flex: 1; font-weight: 700; }
.iconbtn { font-size: 24px; line-height: 1; width: 40px; height: 40px; border-radius: 10px; }
.iconbtn:active { background: var(--line); }
.titleinput {
  flex: 1; min-width: 0; border: 0; background: transparent; font-size: 17px; font-weight: 600; padding: 6px 4px;
  outline: none;
}
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.offline { background: var(--warn); } .dot.error { background: var(--danger); }

/* ---- Liste ---- */
.searchrow { padding: 10px 14px 4px; }
.searchrow input {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none;
}
.notelist { flex: 1; overflow-y: auto; padding: 8px 14px 100px; -webkit-overflow-scrolling: touch; }
.card {
  background: var(--paper); color: #111827; border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0,0,0,.06); display: flex; gap: 12px; align-items: center;
}
.card .thumb { width: 54px; height: 72px; border-radius: 6px; background: #fff; border: 1px solid #e5e7eb; object-fit: cover; flex-shrink: 0; }
.card .body { flex: 1; min-width: 0; }
.card .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .s { font-size: 13px; color: #6b7280; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .m { font-size: 12px; color: #9ca3af; margin-top: 4px; display: flex; gap: 8px; }
.card .tag { background: #eef2ff; color: #3730a3; border-radius: 6px; padding: 1px 6px; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.fab {
  position: fixed; right: 20px; bottom: calc(24px + var(--safe-bottom)); width: 60px; height: 60px; border-radius: 50%;
  background: var(--accent); color: var(--accent-fg); font-size: 34px; line-height: 1; box-shadow: 0 6px 16px rgba(0,0,0,.25);
}

/* ---- Editor ---- */
.editbar { padding: 6px 8px; }
.menu { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center; }
.menu input { flex: 1; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); outline: none; min-width: 0; }
.danger { color: var(--danger); font-weight: 600; padding: 8px 10px; }

.canvaswrap {
  flex: 1; position: relative; margin: 8px 10px 6px; border-radius: var(--radius); overflow: hidden;
  background: var(--paper);
  background-image: repeating-linear-gradient(to bottom, transparent 0 31px, #e9ecf2 31px 32px);
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
  touch-action: none;
}
.canvaswrap canvas { position: absolute; inset: 0; touch-action: none; }
.photo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; object-position: top center; pointer-events: none; }
.pagebadge {
  position: absolute; right: 8px; top: 8px; font-size: 12px; background: rgba(17,24,39,.75); color: #fff;
  border-radius: 999px; padding: 2px 8px; pointer-events: none;
}

.tools { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 4px 10px; flex-wrap: nowrap; overflow-x: auto; }
.tools.pages { padding-top: 0; }
.toolgroup { display: flex; gap: 4px; align-items: center; }
.tool {
  min-width: 38px; height: 38px; padding: 0 8px; border-radius: 10px; font-size: 18px; display: flex; align-items: center; justify-content: center;
  border: 1px solid transparent; white-space: nowrap;
}
.tool.active { background: var(--line); border-color: var(--muted); }
.tool:active { background: var(--line); }
.wbtn i { display: block; background: var(--fg); border-radius: 50%; }
.cbtn { position: relative; }
.cbtn::before { content: ""; width: 20px; height: 20px; border-radius: 50%; background: var(--c); border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.2); }
.cbtn.active::before { box-shadow: 0 0 0 2px var(--fg); }
label.tool { cursor: pointer; }

.aibar {
  display: flex; gap: 8px; padding: 6px 10px calc(10px + var(--safe-bottom)); align-items: center; border-top: 1px solid var(--line);
}
.aibar select {
  flex: 1; min-width: 0; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none;
}
.primary { background: var(--primary); color: #fff; font-weight: 600; padding: 10px 14px; border-radius: 10px; white-space: nowrap; }
.primary:disabled { opacity: .6; }
.secondary { background: var(--line); font-weight: 600; padding: 10px 12px; border-radius: 10px; white-space: nowrap; }

/* ---- Ergebnis-Sheet ---- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; height: 78%; background: var(--bg); border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 24px rgba(0,0,0,.25); display: flex; flex-direction: column; z-index: 20;
  padding-bottom: var(--safe-bottom);
}
.sheethead { display: flex; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tabs { flex: 1; display: flex; gap: 4px; }
.tab { padding: 8px 12px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--line); color: var(--fg); }
#sheetText {
  flex: 1; margin: 10px; padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); color: #111827;
  resize: none; outline: none; line-height: 1.45; font-size: 15px;
}
.sheetactions { display: flex; gap: 6px; padding: 0 10px 10px; overflow-x: auto; }
.sheetactions button { background: var(--line); border-radius: 10px; padding: 10px 12px; font-weight: 600; white-space: nowrap; }

/* ---- Overlay / Toast ---- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; gap: 14px; }
.spinner { width: 42px; height: 42px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%); background: #111827; color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; max-width: 90%; text-align: center;
}
.mic-on { background: var(--danger) !important; color: #fff !important; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .6; } }
