:root {
    --primary: #2563eb; --bg-body: #f8fafc; --sidebar-w: 280px;
    /* Zentrale Design-Tokens (einheitlich über alle Module) */
    --bf-primary: #2563eb; --bf-success: #16a34a; --bf-warning: #d97706; --bf-danger: #dc2626;
    --bf-radius: 12px; --bf-radius-pill: 50rem;
    --bf-gap: 1rem; --bf-gap-sm: .5rem;
    --bf-shadow: 0 1px 3px rgba(0,0,0,.08); --bf-shadow-lg: 0 8px 24px rgba(0,0,0,.12);
    --bf-border: #e2e8f0; --bf-muted: #64748b;
}
[data-bs-theme="dark"] {
    --primary: #3b82f6; --bg-body: #0f172a;
    --bf-primary: #3b82f6; --bf-border: #1e293b; --bf-muted: #94a3b8;
    --bf-shadow: 0 1px 3px rgba(0,0,0,.4); --bf-shadow-lg: 0 8px 24px rgba(0,0,0,.5);
}

/* Globale Suche: auf Mobile flexibel schrumpfen statt fixe 260px (verhinderte,
   dass Topbar-Buttons rechts, Glocke, Einstempeln, Check-In, aus dem Bild
   gedrückt und unklickbar wurden). */
@media (max-width: 767.98px) {
    #global-search-wrap { width: auto !important; max-width: 100% !important; min-width: 0 !important; flex: 1 1 auto; }
    #global-search-results { position: fixed !important; left: 8px; right: 8px; width: auto !important; top: 60px; }
}
/* Auf sehr schmalen Handys (≤ 480px) die Volltext-Suche in der Topbar ausblenden,
   damit die Aktions-Buttons garantiert sichtbar & tippbar bleiben. Suche bleibt
   über die Kunden-/Rechnungslisten erreichbar. */
@media (max-width: 480px) {
    #global-search-wrap { display: none !important; }
}
/* Touch-Ziele: Icon-Buttons in der Topbar auf Mobil auf min. 40px anheben. */
@media (max-width: 767.98px) {
    #top-bar .btn { min-height: 40px; }
}
/* Onboarding-Wizard & KI-Modals: konsistente Rundung */
#m-onboarding .modal-content, #m-ai-summary .modal-content { border-radius: var(--bf-radius); }

/* ── Mobile Modals: Vollbild + scrollbar (≤ 575px) ─────────────────────────
   Ohne dies ragen lange Formular-Modals (Kunde/Rechnung/Einstellungen) über den
   Bildschirm hinaus und die Speichern/Abbrechen-Buttons am unteren Rand sind
   nicht erreichbar. Diese Regel gilt global für ALLE .modal-dialog.
   Breakpoint 767.98px (wie #m-cal-event), damit auch groessere Phones /
   Querformat den Safe-Area-Rand bekommen und das X nie unter der Notch liegt. */
@media (max-width: 767.98px) {
    /* FIX (bewaehrtes Rezept wie #m-cal-event): Modals auf Mobil NICHT randlos-
       Vollbild (sonst liegt der Header/X bei viewport-fit=cover unter der
       Statusleiste/Notch und ist nicht antippbar). Stattdessen Dialog mit
       Safe-Area-Rand rundum, Inhalt scrollt, Header/Footer bleiben sichtbar. */
    .modal-dialog:not(.modal-fullscreen) {
        margin: 0.5rem !important;
        margin-top: calc(0.5rem + env(safe-area-inset-top, 0px)) !important;
        margin-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px)) !important;
        max-width: calc(100% - 1rem) !important;
        width: auto !important;
        min-height: auto !important;
    }
    /* EIGENTLICHE URSACHE des Kunden-Popup-Bugs: zentrierte Modals
       (modal-dialog-centered) schoben bei langen Formularen den Header samt
       X ueber den oberen Bildschirmrand (Statusleiste/Notch) -> X unsichtbar/
       nicht antippbar. Auf Mobil daher oben statt mittig ausrichten. */
    .modal-dialog-centered { align-items: flex-start !important; }
    .modal-dialog:not(.modal-fullscreen) .modal-content {
        max-height: calc(100dvh - 1rem - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
        border-radius: 14px !important;
        border: 0;
    }
    /* Auch echte Bootstrap-Fullscreen-Modals: Header aus der Notch schieben */
    .modal-fullscreen .modal-header, .modal-fullscreen .modal-content { padding-top: env(safe-area-inset-top, 0px); }
    .modal-body { overflow-y: auto; -webkit-overflow-scrolling: touch; }
    .modal-header { position: sticky; top: 0; z-index: 3; background: var(--bs-body-bg, #fff); }
    .modal-header .btn-close {
        padding: 1rem; margin: -0.5rem -0.4rem -0.5rem auto;
        min-width: 44px; min-height: 44px; background-size: 1rem;
        position: relative; z-index: 4;
    }
    .modal-footer { position: sticky; bottom: 0; background: var(--bs-body-bg, #fff); z-index: 2;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px)); }
}

/* Terminart-Farbe „Vor Ort/Besuch" (bg-purple ist kein Bootstrap-Standard) */
.bg-purple { background-color: #8b5cf6 !important; }

body { font-family: 'Inter', sans-serif; background: var(--bg-body); height: 100vh; width: 100vw; overflow: hidden; margin: 0; padding: 0; color: #334155; }

/* BULLETPROOF LAYOUT */
#app { display: flex; flex-direction: row; height: 100vh; width: 100vw; overflow: hidden; }

#sidebar { 
    width: var(--sidebar-w); 
    min-width: var(--sidebar-w); 
    flex-shrink: 0; 
    background: white; 
    border-right: 1px solid #e2e8f0; 
    display: flex; 
    flex-direction: column; 
    z-index: 1040; 
    transition: transform 0.3s ease, width 0.3s ease, min-width 0.3s ease; 
}

#main { 
    flex: 1; 
    min-width: 0; 
    display: flex; 
    flex-direction: column; 
    height: 100vh; 
    position: relative; 
    overflow: hidden; 
}

#main-content-wrapper {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    position: relative;
    /* z-index absichtlich weggelassen, kein Stacking-Context, damit position:fixed
       Overlays (Template-Builder, Modals) direkt über der Sidebar erscheinen können */
}

/* ZWINGT JEDE SEITE IN DAS RASTER */
#main-content-wrapper > div[id^="page-"]:not(.hidden) {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 !important;
    width: 100% !important;
    max-width: 100% !important;
    position: relative !important;
    top: auto !important; left: auto !important; bottom: auto !important; right: auto !important;
    inset: auto !important;
    transform: none !important;
    margin: 0 !important;
    /* z-index:1 entfernt, würde Stacking-Context erzeugen und position:fixed
       Overlays hinter der Sidebar (z-index:1040) einschließen */
}

/* COMPONENTS */
.card-std { background: white; border: none; border-radius: 16px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); margin-bottom: 20px; padding: 24px; }
.nav-btn { width: 100%; text-align: left; padding: 12px 24px; border: none; background: transparent; color: #64748b; font-weight: 500; display: flex; align-items: center; margin-bottom: 4px; border-radius: 0 24px 24px 0; white-space: nowrap; }
.nav-btn:hover { background: #f1f5f9; color: var(--primary); }
.nav-btn.active { background: #eff6ff; color: var(--primary); font-weight: 600; border-right: 3px solid var(--primary); }
.hidden { display: none !important; }

/* NOTIFICATIONS (GLOCKE) */
.notif-dropdown { width: 320px; max-height: 400px; overflow-y: auto; overflow-x: hidden; padding: 0; border-radius: 12px; }
.notif-item { padding: 12px 16px; border-bottom: 1px solid #e2e8f0; cursor: pointer; transition: background 0.2s; }
.notif-item:hover { background: #f8fafc; }
.notif-item:last-child { border-bottom: none; }

/* LOGIN & UI */
#login-overlay { position: fixed; inset: 0; background: #f8fafc; z-index: 9999; display: flex; align-items: center; justify-content: center; }
.avatar-initials { width: 40px; height: 40px; border-radius: 12px; background: #e0e7ff; color: #4338ca; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
#sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 1035; display: none; backdrop-filter: blur(2px); }
#sidebar-overlay.show { display: block; }

/* MOBILE ANPASSUNG */
@media (max-width: 991.98px) { 
    #sidebar { 
        position: fixed; 
        top: 0; bottom: 0; left: 0; 
        transform: translateX(-100%); 
        width: 280px; 
        box-shadow: 0 0 20px rgba(0,0,0,0.1); 
    } 
    #sidebar.open, #sidebar.active { transform: translateX(0) !important; }
    #main { width: 100vw; }
    #main-content-wrapper { padding: 15px; } 
    .card-std { padding: 20px; border-radius: 12px; margin-bottom: 15px; } 
    #tour-map { min-height: 300px; }
    .notif-dropdown { width: 280px; }
}

/* ══════════════════════════════════════════════════════════════════════════
   GLOBALER MOBIL-SCHUTZ (≤ 767px), modulübergreifend, additiv, bricht Desktop
   NICHT (nur innerhalb dieser Media-Query). Fängt die häufigsten Bruchmuster ab,
   ohne jedes Modul einzeln anfassen zu müssen.
   ═════════════════════════════════════════════════════════════════════════ */
@media (max-width: 767.98px) {
    /* 1) Kein horizontales Gesamt-Scrollen der Seite (verhindert "verschobene" UI) */
    html, body, #app, #main, #main-content-wrapper { max-width: 100vw; overflow-x: hidden; }

    /* 2) Feste min-width auf Karten/Panels aufheben, sie sprengen sonst den Screen.
          Tabellen bleiben ausgenommen: die dürfen in .table-responsive scrollen. */
    .card, .card-std, .modal-content, .panel, .box, [class*="card"]:not(table):not(.table) {
        min-width: 0 !important;
        max-width: 100% !important;
    }

    /* 3) Button-Toolbars/Leisten dürfen umbrechen statt Buttons abzuschneiden.
          (greift auf Bootstrap-Flex-Utilities, die Buttons in einer Reihe halten) */
    .d-flex.flex-nowrap:not(.table-responsive):not(#top-bar),
    .btn-toolbar, .btn-group-wrap {
        flex-wrap: wrap !important;
    }

    /* 4) Breite Inhaltsbereiche mit vielen Buttons horizontal scrollbar machen,
          statt sie zu überlaufen (Toolbars mit text-nowrap). */
    .toolbar-scroll, .nav-scroll, .tabs-scroll {
        overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap !important;
    }

    /* 5) Tabellen immer horizontal scrollbar (falls table-responsive vergessen wurde) */
    table { max-width: 100%; }
    .table-responsive { -webkit-overflow-scrolling: touch; }

    /* 6) Bilder/Medien nie breiter als der Container */
    img, video, canvas, svg, iframe { max-width: 100%; height: auto; }

    /* 7) Touch-Ziele: Buttons/Links min. 40px hoch (bessere Trefferquote) */
    .btn, .nav-btn, .dropdown-item, a.btn { min-height: 40px; }

    /* 8) Eingabefelder mind. 16px Schrift → verhindert Auto-Zoom auf iOS beim Fokus */
    input, select, textarea { font-size: 16px !important; }

    /* 9) Dropdown-Menüs nicht breiter als der Screen */
    .dropdown-menu { max-width: calc(100vw - 16px); }

    /* 10) Feste 260px-Suchfelder o.ä. generell entschärfen */
    [style*="width:260px"], [style*="width: 260px"] { max-width: 100% !important; }
}

/* DESKTOP COLLAPSE */
@media (min-width: 992px) {
    body.sidebar-collapsed #sidebar { width: 80px; min-width: 80px; }
    body.sidebar-collapsed .sidebar-text, 
    body.sidebar-collapsed .text-uppercase,
    body.sidebar-collapsed .user-info-box { display: none !important; }
    body.sidebar-collapsed .nav-btn { padding-left: 0; padding-right: 0; justify-content: center; }
    body.sidebar-collapsed .nav-btn i { margin-right: 0 !important; font-size: 1.4rem; }
    body.sidebar-collapsed .sidebar-header { justify-content: center !important; padding: 0.9rem 0 !important; }
    body.sidebar-collapsed .sidebar-header > div:first-child { gap: 0 !important; justify-content: center !important; width: 100% !important; }
    body.sidebar-collapsed .sidebar-header .sidebar-text { display: none !important; }
    body.sidebar-collapsed .sidebar-header img { margin: 0 auto !important; height: 42px !important; width: 42px !important; object-fit: contain !important; border-radius: 11px !important; display: block !important; box-shadow: 0 2px 8px rgba(0,0,0,.12); }
    body.sidebar-collapsed #nav-links { padding-left: 0.5rem; padding-right: 0.5rem; }
}

/* UTIL CLASSES */
.hover-bg:hover { background-color: rgba(0,0,0,0.02); }
.cursor-pointer { cursor: pointer; }
.task-done { text-decoration: line-through; color: #94a3b8; }
.task-prio-1 { border-left: 4px solid #ef4444 !important; } 
.task-prio-2 { border-left: 4px solid #f59e0b !important; } 
.task-prio-3 { border-left: 4px solid #10b981 !important; }

/* SPECIAL CARDS */
.note-card { background: #fffbeb; border: 1px solid #fcd34d; border-radius: 8px; padding: 15px; cursor: pointer; transition: 0.2s; height: 100%; position: relative; overflow:hidden; }
.note-card:hover { transform: translateY(-3px); box-shadow: 0 4px 12px rgba(251, 191, 36, 0.2); }

/* NOTE ATTACHMENTS (MINI FIXED) */
.note-attach-wrapper { position: relative; display: inline-block; margin-right: 5px; margin-bottom: 5px; }
.note-attach-thumb { width: 60px !important; height: 60px !important; min-width: 60px; object-fit: cover; border-radius: 8px; border: 2px solid #e2e8f0; cursor: zoom-in; transition: all 0.2s; background: #fff; display: block; }
.note-attach-thumb:hover { border-color: var(--primary); }
.note-file-link { width: 60px !important; height: 60px !important; min-width: 60px; display: flex; align-items: center; justify-content: center; background: #f1f5f9; border-radius: 8px; border: 2px solid #e2e8f0; color: #64748b; font-size: 1.5rem; text-decoration: none; display: block; }
.note-file-link:hover { background: #e2e8f0; color: var(--primary); }
.note-attach-del { position: absolute; top: -8px; right: -8px; background: #ef4444; color: white; border-radius: 50%; width: 22px; height: 22px; font-size: 14px; display: flex; align-items: center; justify-content: center; cursor: pointer; border: 2px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.15); z-index: 20; transition: transform 0.2s; }
.note-attach-del:hover { background: #dc2626; transform: scale(1.1); }

/* CUSTOMER DOCUMENTS PREVIEW */
.doc-card { width: 100px; cursor: pointer; transition: transform 0.2s; margin-bottom: 10px; }
.doc-card:hover { transform: translateY(-3px); }
.doc-thumb-img { width: 100px; height: 100px; object-fit: cover; border-radius: 12px; border: 1px solid #e2e8f0; background: white; }
.doc-thumb-icon { width: 100px; height: 100px; display: flex; align-items: center; justify-content: center; background: #f8fafc; border-radius: 12px; border: 1px solid #e2e8f0; color: #94a3b8; font-size: 2.5rem; }
.doc-card-name { font-size: 0.75rem; text-align: center; margin-top: 6px; font-weight: 600; color: #475569; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

/* TOUR & CALENDAR */
#tour-map { height: 100%; width: 100%; border-radius: 0; z-index: 1; }
.tour-item { border-left: 3px solid transparent; transition: 0.2s; cursor: pointer; }
.tour-item:hover { background-color: #f8fafc; border-left-color: var(--primary); }
.tour-badge { width: 28px; height: 28px; background: var(--primary); color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: bold; flex-shrink: 0; }

/* FULLCALENDAR OVERRIDES */
.fc-header-toolbar { display: none !important; } 
.fc-col-header-cell { padding: 10px 0 !important; background: #f8fafc; border: none !important; color: #64748b; font-weight: 600; text-transform: uppercase; font-size: 0.75rem; }
.fc-daygrid-day { border: 1px solid #f1f5f9 !important; } 
.fc-event { border: none !important; padding: 2px 4px; font-size: 0.85rem; border-radius: 4px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); } 
.fc-day-today { background-color: #f0f9ff !important; }

/* MINI CALENDAR */
#cal-mini .fc-toolbar-title { font-size: 1rem !important; font-weight: 800; color: #1e293b; padding-left: 5px; } 
#cal-mini .fc-button { padding: 0 !important; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; background: transparent; color: #64748b; border: none; border-radius: 50%; margin: 0 2px; }
#cal-mini .fc-button:hover { background: #f1f5f9; color: var(--primary); } 
#cal-mini .fc-header-toolbar { display: flex !important; margin-bottom: 15px !important; align-items: center; }
#cal-mini .fc-daygrid-day-number { font-size: 0.75rem; text-decoration: none; color: #475569; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; margin: 0 auto; cursor: pointer; }
#cal-mini .fc-daygrid-day-number:hover { background-color: #f1f5f9; border-radius: 50%; } 
#cal-mini .fc-day-today .fc-daygrid-day-number { background: var(--primary) !important; color: white !important; border-radius: 50%; font-weight: bold; }
#cal-mini .fc-daygrid-day, #cal-mini .fc-scrollgrid, #cal-mini .fc-theme-standard td, #cal-mini .fc-theme-standard th { border: none !important; }

/* CHAT STYLES */
.chat-msg { background: white; padding: 10px 15px; border-radius: 12px; max-width: 80%; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
.chat-msg.self { background: #eff6ff; align-self: flex-end; border: 1px solid #dbeafe; }
.chat-msg.system { background: #f1f5f9; align-self: center; font-size: 0.85rem; color: #64748b; }
.chat-msg.other { background: #ffffff; align-self: flex-start; border: 1px solid #e2e8f0; }

/* MOBILE & DESKTOP BEHAVIOR */
.mobile-header { display: none; align-items: center; padding: 12px 20px; background: white; border-bottom: 1px solid #e2e8f0; justify-content: space-between; flex-shrink: 0; }

/* DARK MODE OVERRIDES */
[data-bs-theme="dark"] #sidebar { background-color: var(--bs-body-bg) !important; border-color: var(--bs-border-color) !important; }
[data-bs-theme="dark"] .sidebar-header, [data-bs-theme="dark"] .sidebar-bottom-area { border-color: var(--bs-border-color) !important; }
[data-bs-theme="dark"] .nav-btn { color: #adb5bd; }
[data-bs-theme="dark"] .nav-btn:hover { background-color: var(--bs-tertiary-bg); color: var(--bs-body-color); }
[data-bs-theme="dark"] .nav-btn.active { background-color: rgba(59, 130, 246, 0.15); color: var(--primary); border-right-color: var(--primary); }
[data-bs-theme="dark"] .notif-dropdown { background-color: var(--bs-body-bg); border-color: var(--bs-border-color); }
[data-bs-theme="dark"] .notif-item { border-bottom-color: var(--bs-border-color); }
[data-bs-theme="dark"] .notif-item:hover { background-color: var(--bs-tertiary-bg); }

.fade-in { animation: fadeIn 0.3s ease-in; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
/* Pro-Firma Menue-Editor: ausgeblendete Menuepunkte */
.menu-cfg-hidden { display: none !important; }
