/* CRM-Tab: Layout auf Basecoat-Tokens. Einzige eigene Komponente: Follow-up-Puls. */
/* main ist global auf 1200px gedeckelt (style.css) – im CRM-Tab mehr Breite freigeben */
main:has(#crm-content:not(.hidden)) { max-width: 1680px; }
#crm-content { max-width: none; margin: 0 auto; padding: 20px 24px 32px; display: grid; gap: 14px; }
.crm-mono { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-variant-numeric: tabular-nums; }
.crm-muted { color: var(--color-muted-foreground); }
.crm-ico { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* Kopfzeile */
.crm-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.crm-title { font-size: 22px; font-weight: 600; letter-spacing: -.01em; margin: 0 auto 0 0; display: flex; align-items: baseline; gap: 8px; }
.crm-title small { font-weight: 400; font-size: 14px; }
.crm-search { width: 260px; }
.crm-mail-icon-state { position: absolute; right: 5px; top: 5px; width: 7px; height: 7px; border-radius: 50%; font-size: 0; }
#crm-mail-icon-btn { position: relative; }
#crm-mail-icon-btn.crm-mail-ok .crm-mail-icon-state { background: var(--crm-ok); }
#crm-mail-icon-btn.crm-mail-error .crm-mail-icon-state { background: var(--color-destructive); }
#crm-mail-icon-btn.crm-mail-syncing .crm-mail-icon-state { background: var(--crm-warn); }
#crm-mail-icon-btn.crm-mail-disconnected .crm-mail-icon-state { display: none; }

/* Filter-Chips */
.crm-filters { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.crm-filters .btn { gap: 8px; }
.crm-filters .btn[aria-pressed="true"] { background: var(--color-secondary); border-color: var(--color-foreground); }
.crm-n { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 12px; }
.crm-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.crm-dot-over { background: var(--color-destructive); }
.crm-dot-wait { background: var(--color-foreground); }
.crm-dot-warn { background: var(--crm-warn); }
.crm-view-tabs { margin-left: auto; }

/* Split */
.crm-split { display: grid; grid-template-columns: minmax(0, 11fr) minmax(0, 9fr); gap: 16px; align-items: start; }
.crm-split.crm-drawer-mode { grid-template-columns: 1fr; }
.crm-split.crm-drawer-mode #crm-detail-card { display: none; }
.crm-main { min-width: 0; }

/* Liste */
#crm-list-view { border: 1px solid var(--color-border); border-radius: var(--radius); overflow: visible; background: var(--color-card); }
.crm-table { table-layout: fixed; width: 100%; }
.crm-table td, .crm-table th { vertical-align: middle; }
.crm-table tbody tr { cursor: pointer; }
.crm-table tbody tr[data-state="selected"] td:first-child { box-shadow: inset 3px 0 0 var(--color-foreground); }
.crm-who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.crm-who .avatar { width: 44px; height: 44px; }
.crm-who .avatar > span { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 13px; }
.crm-who .crm-txt { min-width: 0; }
.crm-nm { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crm-sub { color: var(--color-muted-foreground); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crm-flags { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.crm-col-stage { width: 128px; }
.crm-col-pulse { width: 150px; }
.badge.crm-over { background: color-mix(in oklab, var(--color-destructive) 12%, transparent); color: var(--color-destructive); border-color: transparent; }
.badge.crm-soon { color: var(--crm-warn-text); border-color: color-mix(in oklab, var(--crm-warn) 60%, transparent); }
.badge .crm-ico { width: 12px; height: 12px; }
.crm-empty-row td { padding: 0; }

/* Quick-Log in der Zeile: nur bei Hover/Fokus sichtbar (Desktop) */
.crm-col-go { width: 40px; }
.crm-row-log { opacity: 0; transition: opacity .12s; }
.crm-table tbody tr:hover .crm-row-log, .crm-table tbody tr:focus-within .crm-row-log,
.crm-table tbody tr[data-state="selected"] .crm-row-log, .dropdown-menu:has([aria-expanded="true"]) .crm-row-log { opacity: 1; }
@media (hover: none) { .crm-row-log { opacity: 1; } }

/* Follow-up-Puls */
:root { --crm-ok: oklch(60% 0.15 155); --crm-warn: oklch(70% 0.16 70); --crm-warn-text: oklch(45% 0.12 70); }
html.dark { --crm-ok: oklch(72% 0.15 155); --crm-warn: oklch(78% 0.15 75); --crm-warn-text: oklch(78% 0.15 75); }
.crm-pulse { display: grid; gap: 4px; }
.crm-pulse .crm-track { position: relative; height: 6px; border-radius: 3px; background: var(--color-secondary); overflow: hidden; }
.crm-pulse .crm-fill { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 3px; background: var(--crm-ok); transition: width .5s cubic-bezier(.2,.7,.2,1); }
.crm-pulse .crm-fill.crm-hatched { background-image: repeating-linear-gradient(135deg, transparent 0 3px, rgba(0,0,0,.18) 3px 4px); }
.crm-pulse .crm-tick { position: absolute; top: 0; bottom: 0; left: 50%; width: 2px; background: var(--color-foreground); opacity: .5; }
.crm-pulse .crm-lbl { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 12px; color: var(--color-muted-foreground); display: flex; justify-content: space-between; }
.crm-pulse.warning .crm-fill { background: var(--crm-warn); }
.crm-pulse.overdue .crm-fill { background: var(--color-destructive); }
.crm-pulse.warning .crm-lbl b { color: var(--crm-warn-text); font-weight: 500; }
.crm-pulse.overdue .crm-lbl b { color: var(--color-destructive); font-weight: 500; }
.crm-pulse.none .crm-track { background: transparent; border-top: 1px dashed var(--color-border); height: 0; margin-top: 3px; }

/* Detail */
.crm-detail-card { padding: 20px; }
.crm-detail-head { display: flex; align-items: flex-start; gap: 14px; }
.crm-detail-head .avatar { width: 84px; height: 84px; }
.crm-detail-head .avatar > span { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 24px; }
.crm-detail-head .avatar.crm-zoomable { cursor: zoom-in; }
.crm-detail-head .crm-txt { flex: 1; min-width: 0; }
.crm-detail-head h2 { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
.crm-stage-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.crm-counter { text-align: right; flex-shrink: 0; }
.crm-counter .crm-num { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 24px; font-weight: 500; line-height: 1.1; letter-spacing: -.02em; }
.crm-counter .crm-lim { font-size: 12px; color: var(--color-muted-foreground); font-family: var(--font-mono, ui-monospace, Menlo, monospace); }
.crm-counter.warning .crm-num { color: var(--crm-warn-text); }
.crm-counter.overdue .crm-num { color: var(--color-destructive); }
.crm-counter.none .crm-num { color: var(--color-muted-foreground); }
.crm-detail-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.crm-detail-actions .crm-more { margin-left: auto; }
.crm-detail .alert { margin-top: 14px; }
.crm-alert-inline { display: flex; align-items: center; gap: 10px; }
.crm-alert-inline .crm-t { flex: 1; font-size: 14px; }
.crm-alert-inline .crm-t span { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 12px; color: var(--color-muted-foreground); margin-left: 6px; }
.crm-detail .tabs { margin-top: 16px; }
.crm-detail [role="tab"] .crm-n { margin-left: 6px; color: var(--color-muted-foreground); }
.crm-tl-head { display: flex; justify-content: flex-end; }

/* Timeline */
.crm-tl-day { font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 12px; color: var(--color-muted-foreground); text-transform: uppercase; letter-spacing: .06em; padding: 12px 0 6px 32px; }
.crm-tl-item { display: grid; grid-template-columns: 22px minmax(0, 1fr); gap: 10px; padding: 6px 0; position: relative; }
.crm-tl-item > div { min-width: 0; }
.crm-tl-item::before { content: ""; position: absolute; left: 10.5px; top: 28px; bottom: -6px; width: 1px; background: var(--color-border); }
.crm-tl-item:last-child::before { display: none; }
.crm-tl-item.crm-clickable { cursor: pointer; }
.crm-tl-ico { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--color-secondary); color: var(--color-secondary-foreground); }
.crm-tl-ico .crm-ico { width: 11px; height: 11px; }
.crm-tl-ico.crm-mail-out { background: var(--color-primary); color: var(--color-primary-foreground); }
.crm-tl-line { display: flex; gap: 8px; align-items: baseline; }
.crm-tl-line .crm-ttl { font-weight: 500; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crm-tl-line .crm-dir { font-size: 13px; color: var(--color-muted-foreground); white-space: nowrap; }
.crm-tl-line .crm-when { margin-left: auto; font-family: var(--font-mono, ui-monospace, Menlo, monospace); font-size: 12px; color: var(--color-muted-foreground); white-space: nowrap; }
.crm-tl-txt { color: var(--color-muted-foreground); font-size: 14px; margin-top: 2px; max-width: 80ch; white-space: pre-line; overflow-wrap: anywhere; }
.crm-tl-txt.crm-clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: normal; }
.crm-tl-open { margin-top: 2px; padding-inline: 0; height: auto; }

/* Details-Tab */
.crm-kv { display: grid; grid-template-columns: 110px 1fr; gap: 10px 14px; margin: 0; font-size: 14px; align-items: start; }
.crm-kv dt { color: var(--color-muted-foreground); font-size: 13px; padding-top: 2px; }
.crm-kv dd { margin: 0; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.crm-kv .crm-note { white-space: pre-line; max-width: 80ch; overflow-wrap: anywhere; }
.crm-kv a { color: inherit; text-decoration: underline; text-underline-offset: 3px; text-decoration-color: var(--color-border); }
.crm-kv a:hover { text-decoration-color: currentColor; }
.crm-kv .crm-limit { width: 64px; }
.crm-thumb { width: 100%; max-width: 340px; aspect-ratio: 85/55; border: 1px solid var(--color-border); border-radius: 6px; background: var(--color-secondary) center/cover no-repeat; cursor: zoom-in; }

/* Drawer */
#crm-detail-drawer > div { width: min(720px, 100vw); max-width: min(720px, 100vw); overflow-y: auto; }
#crm-detail-drawer .crm-detail { padding: 20px; }
#crm-detail-drawer .crm-detail-head { padding-right: 36px; }
#crm-detail-drawer .crm-drawer-close { position: absolute; top: 12px; right: 12px; }

/* Dialoge */
.crm-dialog-wide > article { width: min(720px, 96vw); max-width: none; }
.dialog > article {
    width: min(720px, calc(100vw - 2rem));
    max-width: calc(100vw - 2rem);
    box-sizing: border-box;
}
/* Der scrollbare Bereich braucht etwas Luft, damit Fokus- und Auswahlringe
   am Rand nicht abgeschnitten werden. */
.dialog > article > section {
    overflow-y: auto;
    padding-inline: 3px;
    margin-inline: -3px;
}
.crm-form { display: grid; gap: 12px; }
.crm-form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.crm-more-fields { border-top: 1px solid var(--color-border); padding-top: 10px; }
.crm-more-fields > summary { cursor: pointer; font-size: 14px; font-weight: 500; color: var(--color-muted-foreground); list-style: none; display: flex; align-items: center; gap: 6px; }
.crm-more-fields > summary::before { content: "▸"; font-size: 12px; transition: transform .15s; }
.crm-more-fields[open] > summary::before { transform: rotate(90deg); }
.crm-more-fields > summary::-webkit-details-marker { display: none; }
.crm-more-fields > .crm-form, .crm-more-fields > .crm-muted, .crm-more-fields > .crm-settings-actions { margin-top: 12px; }
/* Basecoat setzt [role=group] in .field auf flex-direction: column — Pillen bleiben nebeneinander. */
.crm-pill-toggle { display: flex; flex-direction: row; gap: 6px; }
.crm-pill-toggle .btn[aria-pressed="true"], .crm-type-btn[aria-pressed="true"] { background: var(--color-secondary); border-color: var(--color-foreground); }
.crm-type-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.crm-type-grid .btn { min-width: 0; }
.crm-avatar-preview { width: 96px; height: 96px; border-radius: 50%; background: var(--color-secondary) center/cover no-repeat; display: grid; place-items: center; font-size: 12px; color: var(--color-muted-foreground); }
.crm-card-preview { width: 100%; max-width: 320px; aspect-ratio: 85/55; border-radius: 6px; background: var(--color-secondary) center/cover no-repeat; display: grid; place-items: center; font-size: 12px; color: var(--color-muted-foreground); }
.crm-image-actions { display: flex; gap: 6px; margin-top: 6px; }
.crm-settings-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.crm-mail-info { display: grid; gap: 2px; font-size: 14px; }

/* Responsive */
@media (max-width: 1023px) {
    .crm-split { grid-template-columns: 1fr; }
    .crm-split #crm-detail-card { display: none; }
}
@media (max-width: 720px) {
    #crm-content { padding: 14px; }
    .crm-search { width: 100%; }
    .crm-col-stage { display: none; }
    .crm-col-pulse { width: 104px; }
    .crm-sub { white-space: normal; }
    .crm-form-row { grid-template-columns: 1fr; }
    .crm-type-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    #crm-settings-btn { flex: 0 0 44px; min-height: 44px; min-width: 44px; padding: 0.5rem; }
}

@media (prefers-reduced-motion: reduce) { .crm-pulse .crm-fill { transition: none; } }

/* Pipeline */
.crm-board { display: grid; grid-template-columns: repeat(4, minmax(220px, 1fr)); gap: 12px; overflow-x: auto; padding-bottom: 4px; }
.crm-board .crm-col { display: grid; gap: 8px; align-content: start; }
.crm-col-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px; font-size: 14px; font-weight: 500; }
.crm-col-head .crm-n { color: var(--color-muted-foreground); }
.crm-drop { display: grid; gap: 8px; min-height: 120px; border-radius: var(--radius); padding: 2px; }
.crm-drop.crm-over-drop { outline: 2px dashed var(--color-ring); outline-offset: -2px; }
.crm-kcard { text-align: left; width: 100%; cursor: grab; display: grid; gap: 10px; padding: 12px; }
.crm-kcard.crm-selected { box-shadow: 0 0 0 1px var(--color-foreground); }

/* Lightbox (Visitenkarte/Profilbild groß) */
.crm-lightbox { background: rgba(0,0,0,0.85); cursor: zoom-out; z-index: 200; }
.crm-lightbox img { max-width: 95vw; max-height: 90vh; border-radius: 8px; box-shadow: 0 4px 32px rgba(0,0,0,0.6); cursor: default; }
.crm-lightbox-close { position: fixed; top: 1rem; right: 1rem; width: 44px; height: 44px; border-radius: 50%; border: none; background: rgba(255,255,255,0.9); color: #333; font-size: 1.6rem; cursor: pointer; line-height: 1; box-shadow: 0 2px 8px rgba(0,0,0,0.3); }
.crm-lightbox-close:hover { background: white; }
@media (max-width: 600px) {
    .crm-lightbox-close { width: 48px; height: 48px; font-size: 1.8rem; top: max(1rem, env(safe-area-inset-top)); right: max(1rem, env(safe-area-inset-right)); }
}

/* Legacy-Overlays (Mail/Ordner/Debug) und Einstellungen */
.crm-empty-small { color: #888; text-align: center; padding: 1rem; font-size: 0.9rem; }
.crm-mail-error { margin-top: 6px; padding: 8px 12px; background: #fdecea; border: 1px solid #f5b7b1; border-radius: 6px; color: #8a1f15; font-size: 0.88rem; }
.crm-mail-error a { color: #8a1f15; text-decoration: underline; margin-left: 6px; }
.crm-mail-status { margin: 4px 0 0 0; color: #666; font-size: 0.85rem; min-height: 1.2em; }
.crm-mail-ssl-label { display: flex !important; align-items: center; gap: 6px; }
.crm-mail-ssl-label input[type="checkbox"] { width: auto; }
.crm-debug-modal { max-width: 720px; }
.crm-debug-pre { margin: 0; padding: 12px; max-height: 60vh; overflow: auto; background: #fafbfd; border: 1px solid #e3e7ee; border-radius: 6px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.82rem; color: #222; white-space: pre-wrap; word-break: break-word; }

/* KI-Assistent: Einstellungen */
.crm-ai-key-row { grid-template-columns: 1fr auto; align-items: end; }
.crm-ai-privacy { font-size: 12px; margin: 0; }
#crm-ai-profile, #crm-ai-signature { font-family: inherit; }

/* KI-Assistent: Entwurfs-Dialog */
.crm-draft-steer { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.crm-draft-status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--color-muted-foreground); }
.crm-draft-status::before { content: ""; width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: crm-spin .8s linear infinite; }
#crm-draft-body { min-height: 260px; font-family: inherit; line-height: 1.45; }
.crm-draft-loading #crm-draft-subject, .crm-draft-loading #crm-draft-body {
    color: transparent;
    background: linear-gradient(90deg, var(--color-muted) 25%, var(--color-secondary) 50%, var(--color-muted) 75%);
    background-size: 200% 100%;
    animation: crm-shimmer 1.4s ease-in-out infinite;
}
@keyframes crm-shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes crm-spin { to { transform: rotate(360deg); } }
@media (max-width: 640px) { .crm-draft-steer { grid-template-columns: 1fr; } }

/* KI-Assistent: Radar */
.crm-dot-low { background: var(--color-muted-foreground); opacity: .55; }
.crm-radar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.crm-radar-head h3 { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; margin: 0; }
.crm-radar-head h3 .crm-muted { font-weight: 400; font-size: 13px; }
.crm-radar-head-actions { display: flex; gap: 4px; }
.crm-radar-item { display: flex; align-items: flex-start; gap: 12px; padding: 10px 0; border-top: 1px solid var(--color-border); }
.crm-radar-item:first-child { border-top: 0; padding-top: 0; }
.crm-radar-item .avatar { width: 36px; height: 36px; flex-shrink: 0; }
.crm-radar-txt { flex: 1; min-width: 0; display: grid; gap: 2px; }
.crm-radar-who { display: flex; align-items: center; gap: 6px; font-size: 14px; min-width: 0; }
.crm-radar-who .crm-nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crm-radar-hook { font-weight: 500; }
.crm-radar-reason { font-size: 13px; }
.crm-radar-actions { display: flex; gap: 6px; flex-shrink: 0; }
@media (max-width: 640px) {
    .crm-radar-item { flex-wrap: wrap; }
    .crm-radar-actions { width: 100%; justify-content: flex-end; }
}

/* Panel „Neue Absender": Zähler-Badge neben Name/Adresse */
.crm-sender-count { margin-left: 4px; }
