/* CRM — handgeschriebenes CSS nach Design-Briefing.
   Flat, keine Schatten, Trennung über 1px-Borders, kompakte Dichte. */

:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-fg: #ffffff;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #d97706;
  --warning-bg: #fef3c7;
  --info: #0891b2;
  --info-bg: #cffafe;
  --shadow: none; /* Flat, seriös: keine Schatten in beiden Themes */
  --radius: 10px;
  --radius-sm: 6px;
  --sidebar-w: 230px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

[data-theme='dark'] {
  /* Neutrales Grau (kein Blaustich), #151515 Hintergrund */
  --bg: #151515;
  --surface: #1c1c1c;
  --surface-2: #232323;
  --border: #2e2e2e;
  --text: #e6e6e6;
  --text-muted: #9a9a9a;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --success: #4ade80;
  --success-bg: #16301f;
  --danger: #f87171;
  --danger-bg: #3a1d1d;
  --warning: #fbbf24;
  --warning-bg: #332a14;
  --info: #38bdf8;
  --info-bg: #16282f;
  --shadow: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #151515;
    --surface: #1c1c1c;
    --surface-2: #232323;
    --border: #2e2e2e;
    --text: #e6e6e6;
    --text-muted: #9a9a9a;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --success: #4ade80;
    --success-bg: #16301f;
    --danger: #f87171;
    --danger-bg: #3a1d1d;
    --warning: #fbbf24;
    --warning-bg: #332a14;
    --info: #38bdf8;
    --info-bg: #16282f;
    --shadow: none;
  }
}

/* ---- Basis ---- */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
}

h1 { font-size: 1.5rem; font-weight: normal; margin: 0 0 16px; }
h2 { font-size: 1.15rem; font-weight: normal; margin: 0 0 12px; }
h3 { font-size: 1rem; font-weight: normal; margin: 0 0 8px; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

code, pre, .mono { font-family: var(--mono); }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 1px 5px; font-size: 0.9em; }
pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; overflow-x: auto; }
pre code { border: none; background: none; padding: 0; }

hr, .sep { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

.pre-line { white-space: pre-line; }
.hint { color: var(--text-muted); font-size: 0.9em; }

/* Das hidden-Attribut muss auch Elemente mit eigenem display schlagen
   (z. B. .field mit display:flex). */
[hidden] { display: none !important; }

/* ---- App-Shell: Sidebar + Topbar + Content ---- */

.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
  display: inline-block;
  flex-shrink: 0;
}

.sidebar-nav { padding: 8px; flex: 1; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  margin-bottom: 2px;
}
.sidebar-nav a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar-nav a.active { background: var(--primary); color: var(--primary-fg); }

.nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  border-radius: 999px;
  font-size: 0.75rem;
  padding: 0 7px;
  line-height: 1.5;
}
.sidebar-nav a.active .nav-badge { background: rgba(255, 255, 255, 0.25); }

.sidebar-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.8rem;
}

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar .user-email { color: var(--text-muted); font-size: 0.9rem; }
.topbar form { margin: 0; }

.spacer { flex: 1; }

.icon-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1.1rem;
  padding: 4px 9px;
  cursor: pointer;
  line-height: 1.4;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border); }

.menu-toggle { display: none; }

.content {
  padding: 24px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.page-head h1 { margin-bottom: 0; }
.page-head form { margin: 0; }

/* ---- Cards ---- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin: 16px 0;
  overflow: hidden;
}
.card.narrow { max-width: 720px; }

.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.card-head form { margin: 0; }

.card-body { padding: 16px; }
.card-body.tight { padding: 0; }
.card-body > :first-child { margin-top: 0; }
.card-body > :last-child { margin-bottom: 0; }

/* ---- Stat-Grid (KPI-Kacheln) ---- */

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin: 16px 0;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat .label {
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.72rem;
  color: var(--text-muted);
}
.stat .value { font-size: 1.7rem; font-weight: 700; margin: 2px 0; }
.stat .value.danger { color: var(--danger); }
.stat .sub { font-size: 0.82rem; color: var(--text-muted); }

/* ---- Subnav (Zeitraum-Umschalter, Settings-Tabs) ---- */

.subnav {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin: 16px 0 0;
}
.subnav a {
  padding: 7px 14px;
  color: var(--text-muted);
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.subnav a:hover { color: var(--text); text-decoration: none; }
.subnav a.active {
  color: var(--text);
  background: var(--surface);
  border-color: var(--border);
  margin-bottom: -1px;
  padding-bottom: 8px;
}

/* ---- Tabellen ---- */

table.data { width: 100%; border-collapse: collapse; }

table.data th {
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 600;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

table.data td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td form { margin: 0; display: inline-block; }

/* ---- Archiv (eingeklappte Erinnerungen auf der Kundenseite) ---- */

.archive { border-top: 1px solid var(--border); }
.archive > summary {
  cursor: pointer;
  padding: 10px 16px;
  font-size: 0.82rem;
  color: var(--text-muted);
  list-style-position: inside;
}
.archive > summary:hover { color: var(--text); }
.archive table.data td { color: var(--text-muted); }
.archive table.data td strong { font-weight: 600; }

/* ---- Badges ---- */

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 1px 10px;
  font-size: 0.78rem;
  white-space: nowrap;
}
.badge.ok, .badge.sent, .badge.accepted, .badge.enabled { background: var(--success-bg); color: var(--success); }
.badge.err, .badge.failed, .badge.error { background: var(--danger-bg); color: var(--danger); }
.badge.pending, .badge.processing, .badge.rejected { background: var(--warning-bg); color: var(--warning); }
.badge.muted, .badge.cancelled, .badge.disabled { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.badge.info { background: var(--info-bg); color: var(--info); }

/* ---- Buttons ---- */

.btn {
  display: inline-block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 7px 14px;
  font-size: 0.9rem;
  line-height: 1.4;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }

.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-fg); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { opacity: 0.9; }

.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }

.btn.sm { padding: 3px 10px; font-size: 0.82rem; }

.danger-text { color: var(--danger); }

.btn-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.btn-row.right { justify-content: flex-end; }
.btn-row form { margin: 0; }

/* ---- Formulare ---- */

.form-grid { display: grid; gap: 14px; }
.form-grid .cols-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 0.85rem; font-weight: 600; }
.field .hint { font-size: 0.8rem; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="date"], input[type="search"], textarea, select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 7px 10px;
  font: inherit;
  width: 100%;
}
textarea { resize: vertical; }
textarea.mono { font-family: var(--mono); font-size: 0.9rem; }

input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: var(--primary);
}

input[type="file"] { font-size: 0.85rem; color: var(--text-muted); max-width: 220px; }

.input-row { display: flex; gap: 8px; }
.w-narrow { width: 80px; flex-shrink: 0; }

.check-label { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 0.85rem; }
.check-label input { width: auto; }

.search-form { display: flex; gap: 8px; align-items: center; flex: 1; max-width: 420px; font-weight: normal; }
.upload-form { display: flex; gap: 8px; align-items: center; font-weight: normal; }

/* ---- Flash-Meldungen ---- */

.flash {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin: 0 0 16px;
}
.flash.success { background: var(--success-bg); color: var(--success); border-color: transparent; }
.flash.error { background: var(--danger-bg); color: var(--danger); border-color: transparent; }
.flash.info { background: var(--info-bg); color: var(--info); border-color: transparent; }

/* ---- Definition-List (Detail-Ansichten) ---- */

.dl { display: grid; grid-template-columns: 180px 1fr; gap: 8px 16px; }
.dl > div:nth-child(odd) { color: var(--text-muted); }

/* ---- Notiz-Listen & Prosa (gerendertes Markdown) ---- */

.note-list { list-style: none; margin: 0; padding: 0; }
.note-list li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.note-list li:last-child { border-bottom: none; }

.prose h1 { font-size: 1.3rem; margin: 16px 0 8px; }
.prose h2 { font-size: 1.15rem; margin: 16px 0 8px; }
.prose h3, .prose h4, .prose h5, .prose h6 { font-size: 1rem; margin: 12px 0 6px; font-weight: 600; }
.prose h1:first-child, .prose h2:first-child, .prose h3:first-child { margin-top: 0; }
.prose blockquote {
  border-left: 3px solid var(--border);
  margin: 8px 0;
  padding: 4px 14px;
  color: var(--text-muted);
}
.prose ul, .prose ol { padding-left: 24px; }

/* ---- Breadcrumb ---- */

.breadcrumb { margin-bottom: 12px; color: var(--text-muted); font-size: 0.9rem; }
.crumb-sep { margin: 0 6px; }

/* ---- Empty-State ---- */

.empty-state { text-align: center; padding: 24px 16px; }
.empty-state .empty-glyph { font-size: 2rem; color: var(--text-muted); }
.empty-state p { margin: 8px 0; }
.empty-state .btn { margin-top: 8px; }

/* ---- Token-Box (Secrets) ---- */

.token-box {
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 12px;
  word-break: break-all;
}

/* ---- Modals (natives <dialog>) ---- */

dialog.modal {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  width: min(560px, calc(100vw - 32px));
}
dialog.modal::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog.modal .card-head { border-bottom: 1px solid var(--border); }
dialog.modal .modal-body { padding: 16px; }

/* ---- Auth-Seiten (Login/Setup) ---- */

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 400px;
}
.auth-card h1 { font-size: 1.25rem; margin: 0 0 16px; }

.auth-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  margin-bottom: 20px;
}

/* ---- Fehlerseiten ---- */

.error-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 24px;
  text-align: center;
}
.error-code { font-size: 5rem; font-weight: 700; color: var(--primary); line-height: 1; }

/* ---- Mobile ---- */

@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .menu-toggle { display: inline-block; }

  .content { padding: 16px; }
  .form-grid .cols-2 { grid-template-columns: 1fr; }
  .dl { grid-template-columns: 120px 1fr; }
  .topbar .user-email { display: none; }
}

/* ---- Kundendetail: zweispaltiges Layout (links Daten, rechts Wiki) ---- */

.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 430px);
  gap: 16px;
  align-items: start;
}
.detail-main { min-width: 0; }
.detail-main > .card:first-child { margin-top: 0; }
.detail-notes { min-width: 0; }

.notes-panel {
  position: sticky;
  top: 72px; /* unter der Topbar */
  margin: 0;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 88px);
  overflow: hidden;
}

.notes-nav {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  max-height: 40%;
  overflow: auto;
  flex-shrink: 0;
}
.notes-nav-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  margin-bottom: 8px;
}

.notes-content {
  padding: 16px;
  overflow: auto;
  flex: 1;
}
.notes-content .page-head { margin-bottom: 8px; }
.notes-content h2 { font-size: 1.15rem; }
.notes-content > :first-child { margin-top: 0; }
.notes-content > :last-child { margin-bottom: 0; }

.breadcrumb.sm { font-size: 0.8rem; margin-bottom: 8px; }
.section-label {
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin: 16px 0 6px;
}
.notes-subpages { margin-top: 12px; border-top: 1px solid var(--border); }

/* ---- Wiki-Baum (Seiten & Unterseiten) ---- */

.wiki-tree { list-style: none; margin: 0; padding: 0; }
.wiki-tree .wiki-tree { margin-left: 12px; padding-left: 8px; border-left: 1px solid var(--border); }
.wiki-tree li { margin: 1px 0; }

.wiki-node {
  display: block;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.wiki-node:hover { background: var(--surface-2); text-decoration: none; }
.wiki-node.active { background: var(--primary); color: var(--primary-fg); }

/* ---- Dropzone (Dokument-Upload) ---- */

.dropzone-form { display: grid; gap: 10px; margin-bottom: 16px; }

.dropzone {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  padding: 22px 16px;
  min-height: 116px;
  cursor: pointer;
  color: var(--text-muted);
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.dropzone:hover { border-color: var(--primary); }
.dropzone.has-file { border-style: solid; border-color: var(--primary); color: var(--text); }

.dropzone > .dropzone-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  opacity: 0;
  cursor: pointer;
}
.dropzone-icon { font-size: 1.7rem; line-height: 1; }
.dropzone-text { font-size: 0.9rem; }
.dropzone-link { color: var(--primary); text-decoration: underline; }
.dropzone-name { font-weight: 600; color: var(--text); word-break: break-all; }

.dropzone-actions { display: flex; gap: 8px; align-items: center; }
.dropzone-group { flex: 1; }

/* ---- Dokument-Gruppen ---- */

.doc-groups { display: grid; gap: 16px; }
.doc-group { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.doc-group-head {
  padding: 8px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.88rem;
}
.doc-list { list-style: none; margin: 0; padding: 0; }
.doc-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.doc-item:last-child { border-bottom: none; }
/* Dateiname wächst und darf umbrechen; die Aktionen bleiben als geschlossene
   Gruppe erhalten und rutschen bei Platzmangel gemeinsam in die nächste Zeile
   (dort horizontal), statt einzeln übereinander zu stapeln. */
.doc-meta { flex: 1 1 200px; min-width: 0; }
.doc-meta a { word-break: break-word; }
.doc-actions { flex: 0 0 auto; }

/* ---- Vollbild-Drag-Overlay ---- */

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(2, 6, 23, 0.6);
  backdrop-filter: blur(2px);
}
.drop-overlay.show { display: flex; }
.drop-overlay-inner {
  border: 3px dashed rgba(255, 255, 255, 0.85);
  border-radius: var(--radius);
  padding: 44px 60px;
  text-align: center;
  color: #fff;
  pointer-events: none;
}
.drop-overlay-icon { font-size: 3rem; line-height: 1; }
.drop-overlay-title { font-size: 1.6rem; font-weight: 700; margin-top: 10px; }
.drop-overlay-sub { opacity: 0.85; margin-top: 4px; }

/* Zweispaltiges Kundendetail bricht früh um (rechtes Panel braucht Breite). */
@media (max-width: 980px) {
  .detail-layout { grid-template-columns: 1fr; }
  .notes-panel { position: static; max-height: none; }
  .notes-nav { max-height: none; }
}
