/* ===== Design-Tokens ===== */
:root {
  --bg: #0f0f11; --panel: #18181b; --panel2: #1f1f24; --line: #2a2a30;
  --text: #e6e6e9; --muted: #9a9aa3; --accent: #5b9dff; --accent-dim: #2c4a75;
  --err: #ff5d5d; --ok: #3fcf8e; --warn: #f5b941;
  --radius: 10px; --sidebar-w: 280px; --topbar-h: 44px;
  --shadow: 0 8px 30px rgba(0,0,0,.45);
}
[data-theme="light"] {
  --bg: #f4f4f6; --panel: #ffffff; --panel2: #ececef; --line: #d8d8de;
  --text: #1b1b1f; --muted: #6b6b74; --accent: #2f6fde; --accent-dim: #c5d8f7;
  --shadow: 0 8px 30px rgba(0,0,0,.15);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', sans-serif;
  overflow: hidden;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ===== Layout ===== */
/* #app folgt der WIRKLICH sichtbaren Höhe: core/viewport.js setzt --app-height
   aus visualViewport.height (== Layout-Höhe wenn keine Tastatur, kleiner solange
   die Tastatur offen ist). Dadurch passt der Inhalt immer exakt in den sichtbaren
   Bereich → iOS muss die Seite nicht hochschieben, und beim Schließen bleibt kein
   schwarzer --bg-Streifen. Fallback 100dvh/100vh, falls kein visualViewport. */
#app { display: flex; height: 100vh; height: 100dvh; height: var(--app-height, 100dvh); }
#sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w);
  background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; z-index: 30;
}
body.sidebar-collapsed #sidebar { display: none; }
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
/* isolation: Viewer-interne z-Indexe (Canvas-Toolbar 60, Praesentation 80 ...)
   bleiben im Inhaltsbereich gefangen — sonst bluten sie auf Mobil durch die
   geoeffnete Sidebar-Schublade (z 30) hindurch. */
#content { flex: 1; overflow: hidden; position: relative; isolation: isolate; }
/* Keep-Alive-Tabs: eine lebende Ansicht pro Tab. Jeder Host scrollt selbst.
   Versteckt wird mit visibility (NICHT display:none) — display:none kollabiert
   die Scroll-Container und die Position wäre beim Zurückwechseln weg. */
.view-host { position: absolute; inset: 0; overflow: auto; z-index: 1; }
.view-host.hidden {
  display: block !important;        /* .hidden-Utility überstimmen */
  visibility: hidden; pointer-events: none; z-index: 0;
}

/* ===== Sidebar ===== */
.sb-head {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; flex: 1; display: flex; align-items: center; gap: 8px; font-size: 15px; }
.sb-search { display: flex; gap: 4px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.sb-search input {
  flex: 1; background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  color: var(--text); padding: 6px 10px; outline: none; min-width: 0;
}
.sb-search input:focus { border-color: var(--accent); }
#sb-scroll { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.sb-section { border-bottom: 1px solid var(--line); }
.sb-section.grow { flex: 1; border-bottom: 0; display: flex; flex-direction: column; }
.sb-sec-head {
  padding: 7px 12px; font-size: 11px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--muted); cursor: pointer; user-select: none;
  display: flex; justify-content: space-between; align-items: center;
}
.sb-sec-head:hover { color: var(--text); }
.sb-tree-actions { display: flex; gap: 2px; }
#tree { flex: 1; overflow-y: auto; padding: 2px 4px 30px; outline: none; }
.sb-foot {
  display: flex; gap: 2px; padding: 8px 10px; border-top: 1px solid var(--line);
  justify-content: space-around;
}
#search-results { overflow-y: auto; max-height: 100%; flex: 1; }

/* ===== Topbar & Tabs ===== */
#topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 8px;
  padding: 0 10px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.topbar-spacer { flex: 1; }
/* Breite gedeckelt → drückt die Icons rechts nie in eine 2. Reihe. Segmente
   schrumpfen NICHT (flex:0 0 auto), damit Überlauf messbar bleibt und
   breadcrumb.js vom Anfang her ins „…" zusammenklappt. */
#breadcrumb {
  display: flex; align-items: center; gap: 2px; overflow: hidden;
  white-space: nowrap; max-width: 45%; min-width: 0;
}
#breadcrumb > * { flex: 0 0 auto; }
#breadcrumb button {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 3px 5px; border-radius: 6px; font-size: 13px;
}
#breadcrumb button:hover { background: var(--panel2); color: var(--text); }
#breadcrumb .crumb-sep { color: var(--line); }
/* Dateiname als letzte Reserve kürzen (wenn selbst Name+Elternordner zu breit). */
#breadcrumb .crumb-current { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
#breadcrumb.crumb-tight .crumb-current { flex: 0 1 auto; min-width: 0; }
#view-controls { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
#status { font-size: 12px; color: var(--muted); }
#status[data-type="ok"] { color: var(--ok); }
#status[data-type="err"] { color: var(--err); }

#tab-strip {
  display: flex; align-items: flex-end; gap: 2px; padding: 4px 8px 0;
  background: var(--panel); border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: thin; min-height: 34px;
}
.tab {
  display: flex; align-items: center; gap: 6px; padding: 5px 8px 5px 12px;
  background: var(--panel2); border: 1px solid var(--line); border-bottom: 0;
  border-radius: 8px 8px 0 0; cursor: pointer; max-width: 200px; min-width: 90px;
  font-size: 12.5px; user-select: none; flex-shrink: 0;
}
.tab.active { background: var(--bg); border-color: var(--accent-dim); }
.tab .tab-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab .tab-x { border: 0; background: none; color: var(--muted); cursor: pointer;
  border-radius: 4px; padding: 0 4px; }
.tab .tab-x:hover { color: var(--err); background: var(--panel); }
.tab-add { border: 0; background: none; color: var(--muted); font-size: 17px;
  cursor: pointer; padding: 3px 9px; border-radius: 6px; flex-shrink: 0; }
.tab-add:hover { background: var(--panel2); color: var(--text); }

/* ===== Buttons, Inputs, Badges ===== */
.icon-btn {
  background: none; border: 0; border-radius: 7px; cursor: pointer;
  padding: 5px 8px; color: var(--muted); font-size: 15px; position: relative; line-height: 1;
}
.icon-btn:hover { background: var(--panel2); color: var(--text); }
.btn {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 14px; cursor: pointer; font-size: 13.5px;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.12); border-color: var(--accent); }
.btn-danger { background: var(--err); border-color: var(--err); color: #fff; }
.input {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  color: var(--text); padding: 8px 11px; width: 100%; outline: none; font: inherit;
}
.input:focus { border-color: var(--accent); }
.field-label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.badge {
  position: absolute; top: -2px; right: -2px; background: var(--err); color: #fff;
  font-size: 9.5px; border-radius: 8px; padding: 0 4px; min-width: 14px; text-align: center;
}

/* ===== Toasts ===== */
#toast-wrap {
  position: fixed; bottom: 18px; right: 18px; display: flex;
  flex-direction: column; gap: 8px; z-index: 1000; max-width: 340px;
}
.toast {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 11px 15px; box-shadow: var(--shadow); cursor: pointer;
  opacity: 0; transform: translateY(8px); transition: all .22s ease; font-size: 13.5px;
}
.toast.show { opacity: 1; transform: none; }
.toast-err { border-left: 3px solid var(--err); }
.toast-ok { border-left: 3px solid var(--ok); }
.toast-info { border-left: 3px solid var(--accent); }

/* ===== Modals ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 500;
  display: grid; place-items: center; padding: 16px;
}
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  width: min(440px, 94vw); max-height: 88vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-wide { width: min(760px, 94vw); }
.modal-head { display: flex; align-items: center; padding: 14px 18px 0; }
.modal-title { font-weight: 700; font-size: 15.5px; flex: 1; }
.modal-x { background: none; border: 0; color: var(--muted); font-size: 19px; cursor: pointer;
  border-radius: 6px; padding: 0 6px; }
.modal-x:hover { color: var(--text); }
.modal-body { padding: 12px 18px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 4px 18px 16px; }

/* ===== Kontextmenü ===== */
.ctx-menu {
  position: fixed; z-index: 600; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); padding: 5px; min-width: 200px;
  max-height: 80vh; overflow-y: auto;
}
.ctx-item {
  display: flex; align-items: center; gap: 9px; padding: 7px 11px; border-radius: 7px;
  cursor: pointer; font-size: 13.5px; white-space: nowrap;
}
.ctx-item:hover { background: var(--panel2); }
.ctx-item.disabled { opacity: .4; pointer-events: none; }
.ctx-item.danger { color: var(--err); }
.ctx-sep { height: 1px; background: var(--line); margin: 5px 8px; }
/* Touch: größere Tap-Ziele im Kontextmenü */
@media (pointer: coarse) {
  .ctx-menu { min-width: 230px; }
  .ctx-item { padding: 11px 13px; font-size: 15px; }
}

/* ===== Command Palette / Quick Switcher ===== */
.palette-overlay {
  position: fixed; inset: 0; z-index: 700; background: rgba(0,0,0,.45);
  display: flex; justify-content: center; padding-top: 12vh;
}
.palette {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  width: min(560px, 92vw); max-height: 60vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow); overflow: hidden; height: fit-content;
}
.palette input {
  background: none; border: 0; border-bottom: 1px solid var(--line);
  color: var(--text); padding: 14px 18px; font-size: 15px; outline: none;
}
.palette-list { overflow-y: auto; padding: 6px; }
.palette-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  border-radius: 8px; cursor: pointer; font-size: 13.5px;
}
.palette-item.sel, .palette-item:hover { background: var(--panel2); }
.palette-item .pi-hint { margin-left: auto; color: var(--muted); font-size: 11.5px; }

/* ===== Scrollbars ===== */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ===== Mobile ===== */
#sidebar-backdrop { display: none; }
#mobile-fab {
  display: none; position: fixed; bottom: 22px; right: 22px; z-index: 90;
  width: 54px; height: 54px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; font-size: 26px; box-shadow: var(--shadow);
}
#btn-hamburger { display: none; }
#btn-topbar-more { display: none; }
/* Eingeklappte Sidebar: Hamburger in der Topbar holt sie zurück (Strg+B geht auch) */
body.sidebar-collapsed #btn-hamburger { display: inline-flex; }
@media (max-width: 768px) {
  #btn-hamburger { display: block; }
  #btn-collapse { display: none; }
  #mobile-fab { display: block; }
  /* Schmale Topbar: seltene Knoepfe wandern ins ⋯-Menue (kein Umbruch mehr,
     der frei ueber dem Inhalt schwebt — problem1/problem2.jpg) */
  #btn-palette, #btn-split, #btn-shortcuts, #btn-fullscreen { display: none; }
  #btn-topbar-more { display: inline-flex; }
  #status { display: none; }
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; transform: translateX(-100%);
    transition: transform .22s ease; box-shadow: var(--shadow);
  }
  body.sidebar-open #sidebar { transform: none; }
  body.sidebar-open #sidebar-backdrop {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 25;
  }
}

/* ===== Inline-SVG-Icons (ui/icons.js) ===== */
svg.ico {
  width: 16px; height: 16px; flex-shrink: 0;
  vertical-align: -3px; display: inline-block;
}
.icon-btn svg.ico { vertical-align: -2.5px; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
.btn svg.ico { width: 14px; height: 14px; vertical-align: -2px; }
.ctx-ico svg.ico { width: 15px; height: 15px; color: var(--muted); }
.ctx-item:hover .ctx-ico svg.ico { color: var(--text); }
.ctx-item.danger .ctx-ico svg.ico { color: var(--err); }
.sb-sec-head svg.ico { width: 13px; height: 13px; vertical-align: -2px; color: var(--muted); }
.modal-title svg.ico { vertical-align: -2px; }

/* Dateityp-Farben — dezent, eine Farbe pro Familie */
.ico-folder { color: #e3b341; }
.ico-md     { color: var(--accent); }
.ico-doc    { color: #6ea8e8; }
.ico-sheet  { color: #3fcf8e; }
.ico-slides { color: #e08a47; }
.ico-pdf    { color: #e35a5a; }
.ico-img    { color: #e08a47; }
.ico-media  { color: #d76fa8; }
.ico-canvas { color: #8b6dcf; }
.ico-board  { color: #3fcf8e; }
.ico-form   { color: #4fb8c8; }
.ico-zip    { color: #9a9aa3; }
.ico-mail   { color: #6ea8e8; }
.ico-cal    { color: #e35a5a; }
.ico-user   { color: #4fb8c8; }
.ico-code   { color: #dcc14a; }
.ico-file   { color: var(--muted); }

/* Fokus-Ringe für Tastaturnutzer — dezent, nur bei :focus-visible */
.icon-btn:focus-visible, .btn:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* ===== Presence-Pill (Echtzeit: verbundene Geräte) ===== */
#presence-indicator {
  display: flex; align-items: center; gap: 4px;
  background: var(--accent-dim); color: var(--accent);
  border-radius: 20px; padding: 2px 9px; font-size: 12px; font-weight: 600;
  margin-right: 6px;
}
#presence-indicator.hidden { display: none; }
#presence-indicator svg.ico { width: 13px; height: 13px; }
#presence-indicator { cursor: pointer; user-select: none; }

/* „Wer ist online?"-Liste (Popup unter der Presence-Pill) */
.presence-list { min-width: 210px; }
.presence-list-head {
  padding: 5px 11px 6px; font-size: 11px; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--muted);
}
.presence-item { cursor: default; }
.presence-item:hover { background: transparent; }
.presence-dot {
  width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; background: #29c46a;
  box-shadow: 0 0 0 3px rgba(41,196,106,.18);
}
.presence-info { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.presence-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.presence-self { color: var(--muted); font-weight: 400; }
.presence-time { font-size: 11.5px; color: var(--muted); }

/* ===== Tastenkürzel-Hilfe ===== */
.sc-help { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 28px; }
.sc-help .sc-sec h3 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); margin: 12px 0 6px;
}
.sc-row { display: flex; align-items: baseline; gap: 10px; padding: 2.5px 0; font-size: 13px; }
.sc-keys { flex-shrink: 0; min-width: 150px; }
.sc-keys kbd {
  background: var(--panel2); border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 5px; padding: 1px 6px; font-size: 11.5px; font-family: inherit;
}
.sc-or { color: var(--muted); margin: 0 3px; font-size: 11px; }
.sc-label { color: var(--text); }
@media (max-width: 720px) { .sc-help { grid-template-columns: 1fr; } }

/* ===== Formation-Picker (Geteilte Ansicht, 5×5) ===== */
.fp-help { color: var(--muted); font-size: 12.5px; margin: 0 0 12px; }
.formation-grid {
  display: grid; gap: 5px; width: min(330px, 80vw); aspect-ratio: 1;
  margin: 0 auto; touch-action: none; user-select: none;
}
.formation-cell {
  border: 1.5px dashed var(--line); border-radius: 7px;
  transition: background .08s ease, border-color .08s ease;
}
.formation-cell.preview { border-color: var(--accent); background: var(--accent-dim); }
.formation-cell.filled { border-style: solid; border-color: transparent; cursor: pointer; }
.formation-cell.filled:hover { filter: brightness(1.18); }

/* ===== Geteilte Ansicht: Panes + Trenner ===== */
/* Im Split volle Fläche: Topbar + Tab-Strip verstecken (Beenden via
   Befehlspalette/Shortcut oder Pane-× bis ein Bereich übrig ist). */
body.split-active #topbar, body.split-active #tab-strip { display: none; }
#split-grid { position: relative; gap: 5px; background: var(--bg); padding: 5px; }
.split-pane {
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
  border: 1px solid var(--line); border-radius: 9px; overflow: hidden;
  background: var(--panel);
}
.split-pane-head {
  display: flex; align-items: center; gap: 4px;
  padding: 3px 5px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.split-pane-pick {
  flex: 1; min-width: 0; background: transparent; color: var(--text);
  border: 0; font: inherit; font-size: 12.5px; padding: 4px 6px; outline: none;
  cursor: pointer; display: flex; align-items: center; gap: 6px; text-align: left;
  border-radius: 6px;
}
.split-pane-pick:hover { background: var(--panel2); }
.split-pane-pick svg.ico { width: 14px; height: 14px; flex-shrink: 0; }
.split-pane-pick .spp-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split-pane-pick .spp-empty { color: var(--muted); }
.split-pane-close { padding: 3px 5px; }
.split-pane-close svg.ico { width: 13px; height: 13px; }
.split-pane-frame { flex: 1; border: 0; width: 100%; background: var(--bg); }
.split-divider { position: absolute; z-index: 30; }
.split-divider-v { width: 9px; top: 5px; bottom: 5px; cursor: col-resize; }
.split-divider-h { height: 9px; left: 5px; right: 5px; cursor: row-resize; }
.split-divider::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  background: transparent; border-radius: 2px; transition: background .12s ease;
}
.split-divider-v::after { width: 3px; }
.split-divider-h::after { height: 3px; }
.split-divider:hover::after, .split-divider.dragging::after { background: var(--accent); }
.split-divider-cover { position: fixed; inset: 0; z-index: 999; }
.split-divider-cover.col { cursor: col-resize; }
.split-divider-cover.row { cursor: row-resize; }

/* Kontextmenü: Farbfeld "Transparent" (Schachbrett) */
.ctx-swatch {
  display: inline-block; width: 13px; height: 13px; border-radius: 4px;
  border: 1px solid var(--line); vertical-align: -2px;
}
.ctx-swatch-none {
  background:
    linear-gradient(45deg, var(--line) 25%, transparent 25%, transparent 75%, var(--line) 75%) 0 0/8px 8px,
    linear-gradient(45deg, var(--line) 25%, transparent 25%, transparent 75%, var(--line) 75%) 4px 4px/8px 8px;
}
.icon-btn.on { color: var(--accent); background: var(--accent-dim); }

/* ===== Palm Rejection: App-weit nichts selektierbar beim Zeichnen ===== */
body.so-drawing-mode, body.so-drawing,
body.so-drawing-mode *, body.so-drawing * {
  -webkit-user-select: none !important; user-select: none !important;
  -webkit-touch-callout: none !important;
}
