:root {
  --bg: #ffffff;
  --bg-side: #f5f6f4;
  --bg-hover: #e9ece8;
  --bg-active: #d7e8e2;
  --text: #1d2420;
  --muted: #69736d;
  --line: #dde2de;
  --accent: #1f6f5c;
  --accent-text: #ffffff;
  --ts-bg: #f8faf9;
  --danger: #c62828;
  --shadow: 0 6px 24px rgba(0,0,0,.14);
  --side-w: 300px;
  --tree-hover: #e5f3ff;      /* Explorer: Maus darüber */
  --tree-sel: #cce8ff;        /* Explorer: ausgewählt */
  --tree-sel-border: #99d1ff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161a18; --bg-side: #1d2220; --bg-hover: #262c29; --bg-active: #23443b;
    --text: #e4e9e6; --muted: #98a39c; --line: #2f3633; --accent: #4fb397; --accent-text: #0e1a16;
    --ts-bg: #1b201e; --danger: #ef7373; --shadow: 0 6px 24px rgba(0,0,0,.5);
    --tree-hover: #2a2d2e; --tree-sel: #0f3a5c; --tree-sel-border: #1d5f91;
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) .entry-body mark,
  :root:not([data-theme="light"]) .entry-body span[style*="background"] { color: #1d2420; }
}
:root[data-theme="dark"] {
  --bg: #161a18; --bg-side: #1d2220; --bg-hover: #262c29; --bg-active: #23443b;
  --text: #e4e9e6; --muted: #98a39c; --line: #2f3633; --accent: #4fb397; --accent-text: #0e1a16;
  --ts-bg: #1b201e; --danger: #ef7373; color-scheme: dark;
  --tree-hover: #2a2d2e; --tree-sel: #0f3a5c; --tree-sel-border: #1d5f91;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }

/* ---------- Kopfzeile ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg-side); flex: none;
}
.brand { font-weight: 700; color: var(--accent); letter-spacing: .2px; }
.search { flex: 1; max-width: 420px; margin-left: auto; }
.search input {
  width: 100%; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--text); font-size: 16px; /* 16px: kein Zoom auf iPhone */
}
.save-state { font-size: 12px; color: var(--muted); min-width: 80px; text-align: right; }
.save-state.err { color: var(--danger); }
.net-state { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 10px; background: #fff3c4; color: #7a5a00; white-space: nowrap; }
.net-state.off { background: #ffd6d6; color: #a00000; }
.icon-btn { border: 0; background: none; font-size: 20px; padding: 2px 6px; border-radius: 6px; }
.icon-btn:hover { background: var(--bg-hover); }
.small-btn {
  border: 1px solid var(--line); background: var(--bg); border-radius: 7px; padding: 4px 10px; font-size: 13px;
}
.small-btn:hover { background: var(--bg-hover); }
.primary-btn {
  border: 0; background: var(--accent); color: var(--accent-text); border-radius: 7px; padding: 6px 14px; font-weight: 600;
}

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }
.sidebar {
  width: var(--side-w); flex: none; background: var(--bg-side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-actions { display: flex; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tree { overflow: auto; flex: 1; padding: 6px 4px 40px; }
.main { flex: 1; overflow: auto; min-width: 0; -webkit-overflow-scrolling: touch; }
.backdrop { display: none; }
body.side-hidden .sidebar { display: none; }

/* ---------- Baum (im Stil des Windows-Explorers) ---------- */
.tree-target {
  display: flex; align-items: center; gap: 6px; padding: 4px 10px; font-size: 12px; color: var(--muted);
  border-bottom: 1px solid var(--line); min-height: 26px;
}
.tree-target b { color: var(--text); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-target button { border: 0; background: none; color: var(--muted); padding: 0 4px; font-size: 14px; margin-left: auto; }
.tree ul { list-style: none; margin: 0; padding: 0; }
.node {
  display: flex; align-items: center; gap: 6px; height: 30px;
  padding: 0 4px 0 calc(var(--depth, 0) * 18px + 2px);
  border: 1px solid transparent; border-radius: 3px; font-size: 14px;
  cursor: pointer; user-select: none; -webkit-user-select: none; white-space: nowrap;
}
.node:hover { background: var(--tree-hover); }
.node.selected { background: var(--tree-sel); border-color: var(--tree-sel-border); }
.node.active { background: var(--tree-sel); border-color: var(--tree-sel-border); font-weight: 600; }
.node.drop { outline: 2px dashed var(--accent); outline-offset: -2px; }
.node .twisty {
  width: 20px; height: 26px; flex: none; display: flex; align-items: center; justify-content: center;
  color: var(--text); border-radius: 3px;
}
.node .twisty:hover { background: var(--bg-hover); }
.node .twisty svg { width: 14px; height: 14px; transition: transform .12s; }
.node.open .twisty svg { transform: rotate(90deg); }
.node .ico { flex: none; width: 20px; height: 20px; display: flex; }
.node .ico svg { width: 20px; height: 20px; }
.node .label { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.node .offline-mark { font-size: 12px; margin-left: 4px; flex: none; }
.node .more {
  border: 0; background: none; color: var(--text); padding: 0 8px; height: 26px; border-radius: 3px; opacity: 0; font-size: 18px; line-height: 1;
}
.node .more:hover { background: var(--bg-hover); }
.node:hover .more, .node.active .more, .node.selected .more { opacity: 1; }
@media (hover: none) { .node .more { opacity: 1; } .node { height: 40px; } }
.tree-empty { color: var(--muted); padding: 10px; font-size: 13px; }

/* ---------- Seite ---------- */
.empty { color: var(--muted); padding: 40px; text-align: center; }
.page { max-width: 1100px; margin: 0 auto; padding: 14px 20px 120px; }
.crumbs { font-size: 12px; color: var(--muted); }
.crumbs span + span::before { content: " › "; }
.page-title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-title-row h1 { font-size: 22px; margin: 2px 0 8px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.page-actions { display: flex; gap: 8px; }

.toolbar {
  position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 2px; align-items: center;
  background: var(--bg); padding: 6px 0; border-bottom: 1px solid var(--line); margin-bottom: 8px;
}
.toolbar button {
  min-width: 32px; height: 32px; border: 1px solid transparent; background: none; border-radius: 6px; padding: 0 6px;
}
.toolbar button:hover { background: var(--bg-hover); border-color: var(--line); }
.toolbar .swatch::before {
  content: ""; display: inline-block; width: 16px; height: 16px; border-radius: 4px; background: var(--c);
  border: 1px solid rgba(0,0,0,.2); vertical-align: middle;
}
.toolbar .fg { color: var(--c); font-weight: 700; }
.toolbar .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }

/* ---------- Eintragstabelle ---------- */
.entries { width: 100%; border-collapse: collapse; table-layout: fixed; }
.entries .c-ts { width: 118px; }
.entries td { border-bottom: 1px solid var(--line); vertical-align: top; padding: 8px 10px; }
.entries tr.flash td { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: var(--bg-active); } to { background: transparent; } }
.ts {
  background: var(--ts-bg); color: var(--muted); font-size: 13px; line-height: 1.35;
  font-variant-numeric: tabular-nums; position: relative;
}
.ts .d { color: var(--text); font-weight: 600; }
.ts .wd { font-size: 11px; }
.ts .ts-btn { border: 0; background: none; padding: 0; text-align: left; color: inherit; }
.ts input { width: 100%; font-size: 13px; }
.ts .del {
  display: block; margin-top: 6px; border: 0; background: none; color: var(--muted); padding: 2px 0; font-size: 12px; opacity: 0;
}
tr:hover .ts .del, tr:focus-within .ts .del { opacity: 1; }
@media (hover: none) { .ts .del { opacity: .7; } }
.ts .del:hover { color: var(--danger); }

.entry-body { outline: none; min-height: 1.5em; overflow-wrap: anywhere; }
.entry-body:empty::before { content: "Text eingeben …"; color: var(--muted); }
.entry-body p { margin: 0 0 .5em; }
.entry-body p:last-child { margin-bottom: 0; }
.entry-body h3 { font-size: 17px; margin: .4em 0 .3em; }
.entry-body h4 { font-size: 15px; margin: .4em 0 .3em; }
.entry-body ul, .entry-body ol { margin: .2em 0 .5em; padding-left: 1.4em; }
.entry-body mark { background: #ffff00; color: inherit; padding: 0 1px; border-radius: 2px; }
.entry-body img { max-width: 100%; max-height: 420px; border-radius: 6px; display: block; margin: 6px 0; cursor: zoom-in; }
.entry-body a { color: var(--accent); }
.entry-body table { border-collapse: collapse; margin: 6px 0; max-width: 100%; display: block; overflow-x: auto; }
.entry-body th, .entry-body td { border: 1px solid #c8ccd0; padding: 4px 8px; vertical-align: top; text-align: left; }
.entry-body th { background: var(--bg-hover); font-weight: 600; }
.entry-body:focus { box-shadow: inset 3px 0 0 var(--accent); padding-left: 6px; margin-left: -6px; }

/* ---------- Suche ---------- */
.ts .von {
  display: inline-block; margin-top: 4px; font-size: 11px; padding: 1px 6px; border-radius: 10px;
  background: var(--tree-sel); border: 1px solid var(--tree-sel-border); color: var(--text); white-space: nowrap;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- KI-Zugang ---------- */
.ki-hint { color: var(--muted); max-width: 70ch; }
#kiView h2 { font-size: 17px; margin: 24px 0 8px; display: flex; align-items: center; gap: 10px; }
.ki-new { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.ki-new input {
  flex: 1; min-width: 180px; max-width: 320px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--bg); color: var(--text); font-size: 16px;
}
.ki-token { border: 2px solid var(--accent); border-radius: 10px; padding: 10px 14px; margin: 10px 0 14px; background: var(--ts-bg); }
.ki-token p { margin: 0 0 8px; }
.copy-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; align-items: center; margin: 8px 0; }
.copy-row .lbl { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.copy-row code {
  font-size: 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
  overflow-wrap: anywhere; user-select: all;
}
.ki-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.ki-table td { padding: 7px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.ki-table td.act { text-align: right; white-space: nowrap; width: 1%; }
.ki-table td.when { white-space: nowrap; color: var(--muted); width: 1%; }
.ki-table tr.off { opacity: .55; }
.ki-table tr.err td { color: var(--danger); }
.ki-table .muted, .muted { color: var(--muted); font-size: 12px; }
.small-btn.danger { color: var(--danger); }
@media (max-width: 760px) { .ki-log td.when { white-space: normal; } }

.results { list-style: none; padding: 0; }
.results li { padding: 10px 6px; border-bottom: 1px solid var(--line); cursor: pointer; }
.results li:hover { background: var(--bg-hover); }
.results .r-path { font-size: 12px; color: var(--muted); }
.results .r-snip { margin-top: 2px; }

/* ---------- Menü / Dialog / Lightbox ---------- */
.menu {
  position: fixed; z-index: 50; background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); padding: 4px; min-width: 190px;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 12px; border-radius: 5px; }
.menu button:hover { background: var(--bg-hover); }
.menu button.danger { color: var(--danger); }
.menu .menu-sep { border-top: 1px solid var(--line); margin: 4px 6px; padding-top: 4px; font-size: 11px; color: var(--muted); }
.menu .menu-sep:empty { padding-top: 0; }
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); box-shadow: var(--shadow); width: min(420px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h2 { font-size: 17px; margin: 0 0 10px; }
#askText { white-space: pre-wrap; font-weight: 500; line-height: 1.4; }
#askInput { width: 100%; box-sizing: border-box; font-size: 16px; padding: 7px 9px; margin-bottom: 4px; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; }
#askOk.danger { background: #d00000; border-color: #d00000; }
dialog select { width: 100%; font-size: 15px; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
/* PDF-"Ausdruck": Seiten wie Blätter Papier */
.entry-body img[alt*=" – Seite "] { display: block; border: 1px solid #c8ccd0; box-shadow: 0 2px 6px rgba(0,0,0,.15); margin: 8px 0; background: #fff; }
.lightbox {
  position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.88); display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.lightbox img { max-width: 96vw; max-height: 96vh; }
[hidden] { display: none !important; }

/* ---------- Handy ---------- */
@media (max-width: 760px) {
  .brand { display: none; }
  .sidebar {
    position: absolute; z-index: 20; inset: 0 auto 0 0; width: min(86vw, 340px);
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  body.side-open .sidebar { transform: none; }
  body.side-open .backdrop { display: block; position: absolute; inset: 0; z-index: 15; background: rgba(0,0,0,.3); }
  body.side-hidden .sidebar { display: flex; }
  .page { padding: 10px 12px 140px; }
  .entries .c-ts { width: 74px; }
  .entries td { padding: 8px 6px; }
  .ts { font-size: 12px; }
  .toolbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin-left: -12px; margin-right: -12px; padding-left: 8px; padding-right: 8px; }
  .toolbar::-webkit-scrollbar { display: none; }
  .toolbar button { min-width: 38px; height: 38px; flex: none; }
  .toolbar .sep { flex: none; }
  .page-title-row h1 { font-size: 20px; overflow-wrap: break-word; flex-basis: 100%; margin-bottom: 0; }
  .page-actions { margin-left: auto; margin-bottom: 6px; }
  .entry-body { font-size: 16px; }
}
