:root {
  --bg: #1c1a17;
  --bg-soft: #24211d;
  --panel: #2a2723;
  --panel-border: #3a3630;
  --paper: #f2e9d8;
  --paper-dim: #e4d9c2;
  --ink: #2b2419;
  --ink-soft: #6b5f4c;
  --amber: #d9a441;
  --amber-soft: #f0c877;
  --teal: #5fa8a0;
  --danger: #c1584a;
  --text: #ece5d8;
  --text-dim: #a89d8a;
  --radius: 10px;
  --shadow: 0 4px 14px rgba(0,0,0,0.35);
  --font-display: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-body: -apple-system, "Inter", "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", Consolas, monospace;

  --c-amber: #d9a441;
  --c-teal: #5fa8a0;
  --c-rose: #c97b6d;
  --c-sage: #7f9c56;
  --c-violet: #8a72b0;
  --c-slate: #5f7885;
}

[data-theme="light"] {
  --bg: #eee6d6;
  --bg-soft: #e6dcc7;
  --panel: #f4ecdc;
  --panel-border: #d9cbb0;
  --paper: #fffdf8;
  --paper-dim: #f5eedd;
  --ink: #2b2419;
  --ink-soft: #6b5f4c;
  --text: #2b2419;
  --text-dim: #6b5f4c;
  --shadow: 0 4px 14px rgba(60,50,30,0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 20% 15%, rgba(217,164,65,0.05), transparent 40%),
    radial-gradient(circle at 80% 85%, rgba(95,168,160,0.05), transparent 40%);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button, input, textarea, select { font-family: inherit; }

/* ---------- Login ---------- */
#login-screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--bg); z-index: 100;
}
.login-card {
  background: var(--panel); border: 1px solid var(--panel-border); border-radius: var(--radius);
  padding: 40px 36px; width: min(360px, 90vw); box-shadow: var(--shadow); text-align: center;
}
.login-card .pin-icon { width: 36px; height: 36px; margin: 0 auto 14px; color: var(--amber); }
.login-card h1 { font-family: var(--font-display); font-size: 1.6rem; margin: 0 0 4px; }
.login-card p { color: var(--text-dim); font-size: 0.9rem; margin: 0 0 22px; }
.login-card input {
  width: 100%; padding: 12px 14px; border-radius: 7px; border: 1px solid var(--panel-border);
  background: var(--bg-soft); color: var(--text); font-size: 1rem; margin-bottom: 12px;
}
.login-card button {
  width: 100%; padding: 12px; border-radius: 7px; border: none; background: var(--amber);
  color: #1c1a17; font-weight: 600; font-size: 0.95rem; cursor: pointer; transition: background 0.15s;
}
.login-card button:hover { background: var(--amber-soft); }
.login-error { color: var(--danger); font-size: 0.85rem; min-height: 1.2em; margin-top: 4px; }

/* ---------- App Layout ---------- */
#app { display: none; height: 100vh; flex-direction: column; }
#app.visible { display: flex; }

header.topbar {
  display: flex; align-items: center; gap: 14px; padding: 10px 18px;
  background: var(--panel); border-bottom: 1px solid var(--panel-border); flex-shrink: 0;
}
.brand {
  font-family: var(--font-display); font-size: 1.2rem; display: flex; align-items: center; gap: 8px;
  color: var(--amber-soft); white-space: nowrap;
}
.brand svg { width: 19px; height: 19px; }
.mobile-back {
  display: none; align-items: center; justify-content: center;
}
.search-wrap { flex: 1; max-width: 420px; position: relative; }
.search-wrap input {
  width: 100%; padding: 8px 14px 8px 34px; border-radius: 8px; border: 1px solid var(--panel-border);
  background: var(--bg-soft); color: var(--text); font-size: 0.88rem;
}
.search-wrap svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 15px; height: 15px; color: var(--text-dim); }
.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

.icon-btn {
  width: 34px; height: 34px; display: flex; align-items: center; justify-content: center;
  border-radius: 7px; border: 1px solid var(--panel-border); background: var(--bg-soft);
  color: var(--text); cursor: pointer; transition: background 0.15s, transform 0.1s; flex-shrink: 0;
}
.icon-btn:hover { background: var(--panel-border); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 16px; height: 16px; }

.mini-btn {
  width: 22px; height: 22px; border-radius: 5px; border: 1px solid var(--panel-border);
  background: var(--bg-soft); color: var(--text); cursor: pointer; font-size: 0.95rem; line-height: 1;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.mini-btn:hover { background: var(--amber); color: #1c1a17; border-color: var(--amber); }

.btn-primary {
  display: flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 8px; border: none;
  background: var(--amber); color: #1c1a17; font-weight: 600; font-size: 0.85rem; cursor: pointer;
}
.btn-primary:hover { background: var(--amber-soft); }

.small-btn {
  padding: 6px 12px; border-radius: 6px; border: 1px solid var(--panel-border);
  background: var(--bg-soft); color: var(--text); font-size: 0.78rem; cursor: pointer; white-space: nowrap;
}
.small-btn:hover { background: var(--panel-border); }

/* ---------- Body: three panes ---------- */
.body-wrap { flex: 1; display: flex; overflow: hidden; }

.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 12px 6px;
}
.section-header { margin-top: 6px; padding-top: 10px; border-top: 1px solid var(--panel-border); }
.panel-title { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-dim); }

/* Pane 1: Notizbücher + Abschnitte */
.nb-panel {
  width: 220px; flex-shrink: 0; background: var(--bg-soft); border-right: 1px solid var(--panel-border);
  overflow-y: auto; display: flex; flex-direction: column;
}
#notebook-list, #section-list { padding: 0 8px; }

.nav-item {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; border-radius: 7px; cursor: pointer;
  font-size: 0.86rem; color: var(--text-dim); margin-bottom: 2px; transition: background 0.15s, color 0.15s;
}
.nav-item svg { width: 15px; height: 15px; flex-shrink: 0; }
.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item.active { background: var(--panel); color: var(--text); font-weight: 600; }

.color-swatch {
  width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; background: var(--sw, var(--amber));
}

.section-row { position: relative; }
.section-row .nav-item { border-left: 3px solid var(--sw, var(--amber)); border-radius: 0 7px 7px 0; padding-left: 9px; }
.section-row .edit-dot {
  opacity: 0; margin-left: auto; width: 20px; height: 20px; display: flex; align-items: center; justify-content: center;
  border-radius: 5px; flex-shrink: 0;
}
.section-row:hover .edit-dot, .nb-row:hover .edit-dot { opacity: 1; }
.edit-dot:hover { background: var(--panel-border); }
.edit-dot svg { width: 13px; height: 13px; }

.nb-row { position: relative; }
.nb-row .nav-item { padding-left: 10px; }

.nav-divider { height: 1px; background: var(--panel-border); margin: 10px 10px; }

/* Pane 2: Seitenliste */
.pages-panel {
  width: 280px; flex-shrink: 0; background: var(--bg-soft); border-right: 1px solid var(--panel-border);
  overflow-y: auto; display: flex; flex-direction: column;
}
.page-list { flex: 1; padding: 4px 8px 16px; }

.page-item {
  padding: 10px 12px; border-radius: 8px; cursor: pointer; margin-bottom: 3px; position: relative;
  border: 1px solid transparent;
}
.page-item:hover { background: var(--panel); }
.page-item.active { background: var(--panel); border-color: var(--panel-border); }
.page-item .pt { font-size: 0.87rem; font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 5px; }
.page-item .pt svg { width: 12px; height: 12px; color: var(--amber); flex-shrink: 0; }
.page-item .snip { font-size: 0.76rem; color: var(--text-dim); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-item .pd { font-family: var(--font-mono); font-size: 0.65rem; color: var(--text-dim); margin-top: 4px; opacity: 0.8; }
.page-item .ctx { font-size: 0.68rem; color: var(--text-dim); margin-top: 2px; font-style: italic; }

.trash-actions { padding: 8px 12px 0; }

/* Pane 3: Editor */
.editor-panel {
  flex: 1; background: var(--paper); color: var(--ink); display: flex; flex-direction: column; overflow: hidden;
  position: relative;
}
.editor-toolbar {
  display: none; align-items: center; gap: 4px; padding: 8px 16px; border-bottom: 1px solid var(--paper-dim); flex-wrap: wrap;
}
.editor-toolbar.visible { display: flex; }
.editor-toolbar .spacer { flex: 1; }
.editor-toolbar #save-status { font-family: var(--font-mono); font-size: 0.7rem; color: var(--ink-soft); margin-right: 8px; }

.tool-btn {
  width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; border-radius: 6px;
  border: none; background: transparent; color: var(--ink-soft); cursor: pointer; font-weight: 700; font-size: 0.85rem;
}
.tool-btn:hover { background: var(--paper-dim); color: var(--ink); }
.tool-btn.active { background: var(--amber); color: #1c1a17; }
.tool-btn svg { width: 15px; height: 15px; }

.editor-body { display: none; padding: 22px 40px; overflow-y: auto; flex: 1; }
.editor-body.visible { display: block; }

#page-title {
  width: 100%; border: none; background: transparent; font-family: var(--font-display); font-size: 1.8rem;
  color: var(--ink); outline: none;
}
#page-title::placeholder { color: var(--ink-soft); opacity: 0.55; }
.page-meta { font-family: var(--font-mono); font-size: 0.72rem; color: var(--ink-soft); margin: 4px 0 18px; }

.page-content-editable {
  width: 100%; min-height: 400px; outline: none; font-size: 0.97rem; line-height: 1.7;
  color: var(--ink); font-family: var(--font-body);
}
.page-content-editable .text-line { min-height: 1.7em; white-space: pre-wrap; word-break: break-word; }
.page-content-editable .todo-line {
  display: flex; align-items: flex-start; gap: 9px; min-height: 1.7em;
}
.page-content-editable .todo-checkbox {
  margin-top: 6px; width: 16px; height: 16px; accent-color: var(--amber); cursor: pointer; flex-shrink: 0;
}
.page-content-editable .todo-text { flex: 1; white-space: pre-wrap; word-break: break-word; }
.page-content-editable .todo-line.checked .todo-text { text-decoration: line-through; color: var(--ink-soft); }
.tool-btn.active[data-list] { background: var(--amber); color: #1c1a17; }
.toolbar-sep { width: 1px; align-self: stretch; background: var(--paper-dim); margin: 4px 4px; }

.page-content-editable .heading-line {
  font-family: var(--font-display); font-weight: 700; font-size: 1.3rem; margin: 10px 0 4px; min-height: 1.4em;
}
.page-content-editable .quote-line {
  border-left: 3px solid var(--amber); padding-left: 12px; font-style: italic; color: var(--ink-soft);
  margin: 6px 0; min-height: 1.7em;
}

.table-block { margin: 12px 0 16px; }
.table-block table { border-collapse: collapse; width: 100%; }
.table-block td {
  border: 1px solid var(--paper-dim); padding: 7px 11px; min-width: 60px; vertical-align: top;
  outline: none; font-size: 0.92rem;
}
.table-block td:focus { background: rgba(217, 164, 65, 0.1); }
.table-controls { display: flex; gap: 6px; margin-top: 5px; flex-wrap: wrap; }
.table-btn {
  font-size: 0.68rem; padding: 3px 9px; border-radius: 5px; border: 1px solid var(--paper-dim);
  background: transparent; color: var(--ink-soft); cursor: pointer;
}
.table-btn:hover { background: var(--paper-dim); color: var(--ink); }
.table-btn.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.editor-placeholder {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  color: var(--ink-soft); text-align: center; font-size: 0.9rem;
}
.editor-placeholder svg { width: 46px; height: 46px; opacity: 0.4; }

.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center; height: 60%;
  color: var(--text-dim); text-align: center; gap: 10px; padding: 20px;
}
.empty-state svg { width: 36px; height: 36px; opacity: 0.5; }
.empty-state .sub { font-size: 0.8rem; }

/* ---------- Dialog ---------- */
#dialog-overlay {
  position: fixed; inset: 0; background: rgba(15,13,10,0.55); display: none;
  align-items: center; justify-content: center; z-index: 95; padding: 20px;
}
#dialog-overlay.visible { display: flex; }
.dialog-card {
  background: var(--panel); border: 1px solid var(--panel-border); border-radius: var(--radius);
  padding: 22px; width: min(340px, 100%); box-shadow: var(--shadow);
}
.dialog-card h3 { font-family: var(--font-display); margin: 0 0 14px; font-size: 1.1rem; }
.dialog-card input {
  width: 100%; padding: 10px 12px; border-radius: 7px; border: 1px solid var(--panel-border);
  background: var(--bg-soft); color: var(--text); font-size: 0.9rem; margin-bottom: 14px;
}
.color-row { display: flex; gap: 8px; margin-bottom: 18px; }
.color-dot { width: 24px; height: 24px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.color-dot.selected { border-color: var(--text); }
.dialog-actions { display: flex; align-items: center; gap: 8px; }
.dialog-actions .spacer { flex: 1; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--panel); border: 1px solid var(--panel-border); color: var(--text); padding: 10px 18px;
  border-radius: 8px; font-size: 0.85rem; box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; z-index: 200;
}
#toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Responsive: single-column mobile navigation ---------- */
@media (max-width: 900px) {
  .nb-panel, .pages-panel, .editor-panel { width: 100%; }
  .body-wrap { position: relative; }
  .nb-panel, .pages-panel, .editor-panel { display: none; }
  .body-wrap.show-notebooks .nb-panel { display: flex; flex-direction: column; }
  .body-wrap.show-pages .pages-panel { display: flex; flex-direction: column; }
  .body-wrap.show-editor .editor-panel { display: flex; }
  .search-wrap { display: none; }
  .brand span.full { display: none; }
}
@media (min-width: 901px) {
  .nb-panel, .pages-panel, .editor-panel { display: flex !important; flex-direction: column; }
}
