:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --border: #e2e8f0;
  --danger: #dc2626;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --accent: #818cf8;
    --accent-text: #1e1b4b;
    --border: #334155;
    --danger: #f87171;
    --warn-bg: #451a03;
    --warn-text: #fcd34d;
  }
}

* { box-sizing: border-box; margin: 0; }

/* display:flex auf main/… würde das hidden-Attribut sonst aushebeln */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overscroll-behavior-y: none;
}

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
header h1 {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 22px;
  padding: 4px 10px;
  cursor: pointer;
  line-height: 1;
}
.icon-btn:active { opacity: 0.5; }

#sync-status { font-size: 12px; color: var(--muted); }
#sync-status.error { color: var(--danger); }

main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }

/* Login */
.login-box {
  margin: auto;
  padding: 24px;
  max-width: 340px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-box p { color: var(--muted); font-size: 14px; }

input, textarea, button.primary, button.danger {
  font: inherit;
  color: inherit;
}
input[type="password"], input[type="search"], input[type="text"], #new-note-path {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  width: 100%;
  outline: none;
}
input:focus { border-color: var(--accent); }

button.primary {
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}
.error { color: var(--danger); font-size: 14px; }

/* Liste */
#search {
  margin: 10px 14px;
  width: auto;
}
#note-list {
  list-style: none;
  padding: 0 0 90px;
}
#note-list li {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
}
#note-list li:active { background: var(--bg); }
#note-list li.group-head {
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 14px 16px 6px;
  border-bottom: none;
  cursor: default;
}
#note-list .note-name { font-weight: 550; }
#note-list .note-snippet { color: var(--muted); font-size: 13px; margin-top: 2px; }
#note-list .note-text { flex: 1; min-width: 0; }
#note-list .note-name, #note-list .note-snippet {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.note-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.note-time { color: var(--muted); font-size: 12px; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 8px; flex-shrink: 0; }
.badge.dirty { background: none; color: var(--accent); font-size: 16px; padding: 0; }
.badge.conflict { background: var(--warn-bg); color: var(--warn-text); }

#ptr-hint {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.15s;
}
#ptr-hint.visible { max-height: 40px; padding: 8px; }

.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 28px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

#new-note-form {
  position: fixed;
  left: 14px;
  right: 90px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
}
#new-note-form button { flex-shrink: 0; }

/* Lese-Ansicht */
#reader {
  padding: 18px 18px calc(100px + env(safe-area-inset-bottom));
  max-width: 44rem;
  margin: 0 auto;
  width: 100%;
  line-height: 1.65;
  font-size: 16px;
  overflow-wrap: break-word;
}
#reader h1 { font-size: 1.6em; margin: 0.6em 0 0.4em; line-height: 1.25; }
#reader h2 { font-size: 1.3em; margin: 0.9em 0 0.35em; line-height: 1.3; }
#reader h3 { font-size: 1.1em; margin: 0.9em 0 0.3em; }
#reader h4, #reader h5, #reader h6 { font-size: 1em; margin: 0.8em 0 0.2em; }
#reader p { margin: 0.55em 0; }
#reader ul, #reader ol { margin: 0.55em 0; padding-left: 1.4em; }
#reader li { margin: 0.25em 0; }
#reader li.task { list-style: none; margin-left: -1.4em; display: flex; gap: 9px; align-items: baseline; }
#reader li.task input {
  width: 19px; height: 19px;
  accent-color: var(--accent);
  flex-shrink: 0;
  transform: translateY(3px);
}
#reader li.task .done { color: var(--muted); text-decoration: line-through; }
#reader code {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.88em;
}
#reader pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
  margin: 0.7em 0;
}
#reader pre code { background: none; border: none; padding: 0; }
#reader blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.7em 0;
  padding: 2px 0 2px 14px;
  color: var(--muted);
}
#reader hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }
#reader a { color: var(--accent); }
#reader a.wikilink { cursor: pointer; text-decoration: none; border-bottom: 1px dashed var(--accent); }

.conflict-banner {
  padding: 10px 14px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 13px;
}

/* Editor */
#view-editor { overflow: hidden; }

#md-toolbar {
  display: flex;
  gap: 4px;
  padding: 6px 10px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  overflow-x: auto;
}
#md-toolbar button {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  padding: 8px 12px;
  cursor: pointer;
  flex-shrink: 0;
  min-width: 42px;
}
#md-toolbar button:active { background: var(--accent); color: var(--accent-text); }
#editor {
  flex: 1;
  width: 100%;
  padding: 14px;
  border: none;
  outline: none;
  resize: none;
  background: var(--bg);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 15px;
  line-height: 1.55;
}
#view-editor footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
#editor-status { font-size: 12px; color: var(--muted); }
button.danger {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}

#toast {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  max-width: 90vw;
  z-index: 10;
}
