/* ============================================================
   XRechnungen – Dashboard v4
   3-Spalten-Layout:  [1 Nav-links] [2 Grid-Content] [3 Panel-rechts]
       4 4 4 4 4 4 4   ← Topbar (base.html)
       1 2 2 2 2 2 3   ← dieses File (Sidebars stretch bis Footer)
       1 2 2 2 2 2 3
       5 5 5 5 5 5 5   ← Footer (base.html)
   ============================================================ */

/* ══════════════════════════════════════════════════════════════
   PAGE-OVERRIDE  (main.container auf volle Breite)
   ══════════════════════════════════════════════════════════════ */
body.page-dashboard main.container {
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    width: 100%;
}

/* ══════════════════════════════════════════════════════════════
   #db-layout-wrap  — Flex-Zeile: Spalte 1 + 2 + 3
   align-items: flex-start  →  Sidebars können sticky sein
   (align-items: stretch blockiert position:sticky auf Flex-Kindern)
   ══════════════════════════════════════════════════════════════ */
#db-layout-wrap {
    display: flex !important;
    flex-direction: row;
    align-items: flex-start;      /* ← Voraussetzung für sticky Sidebars */
    width: 100%;
    min-height: calc(100vh - 56px);
}

/* ══════════════════════════════════════════════════════════════
   SPALTE 1  — linke Navigation  (#db-sidebar)
   position:sticky direkt auf dem Flex-Child – zuverlässigste Methode.
   top/height werden durch JS auf echte topbar-Höhe gesetzt.
   ══════════════════════════════════════════════════════════════ */
#db-sidebar {
    width: 220px;
    flex-shrink: 0;
    align-self: flex-start;       /* ← verhindert stretch, ermöglicht sticky */
    position: sticky;
    top: 56px;                    /* wird durch JS aktualisiert */
    height: calc(100vh - 56px);   /* wird durch JS aktualisiert */
    overflow-x: hidden;
    overflow-y: auto;
    background: var(--surface);
    border-right: 1px solid var(--border);
    transition: width .22s ease;
    z-index: 100;
    box-sizing: border-box;
}
#db-layout-wrap.sbar-collapsed #db-sidebar { width: 44px; }
#db-layout-wrap.sbar-hidden    #db-sidebar { width: 0; border-right-width: 0; overflow: hidden; }

/* Innerer Container: nur noch Wrapper, kein sticky mehr nötig */
#db-sidebar-inner {
    min-height: 100%;
    box-sizing: border-box;
}
#db-layout-wrap.sbar-collapsed #db-sidebar-inner { overflow: hidden; }
#db-layout-wrap.sbar-hidden    #db-sidebar-inner  { overflow: hidden; }

/* ── Toggle-Button ──────────────────────────────────────────── */
.db-sbar-toggle {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 6px 8px 4px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-alt);
    position: sticky;
    top: 0;
    z-index: 1;
}
.db-sbar-toggle-btn {
    background: none; border: none; cursor: pointer;
    color: var(--muted); font-size: 1rem; padding: 3px 7px;
    border-radius: 5px; line-height: 1; transition: all .12s;
    display: inline-flex; align-items: center;
    height: 28px; white-space: nowrap; font-family: inherit;
}
.db-sbar-toggle-btn:hover { color: var(--fg); background: var(--surface); }
#db-layout-wrap.sbar-collapsed .db-sbar-toggle { justify-content: center; padding: 8px 4px; }

/* ── Icon + Text-Splits für Collapse ────────────────────────── */
.db-nav-icon { flex-shrink: 0; }
.db-nav-text { white-space: nowrap; overflow: hidden; min-width: 0; }

#db-layout-wrap.sbar-collapsed .db-nav-item {
    justify-content: center; padding: 9px 4px; gap: 0; position: relative;
}
#db-layout-wrap.sbar-collapsed .db-nav-text  { display: none; }
#db-layout-wrap.sbar-collapsed .db-nav-sep   { margin: 2px 6px; }

/* Tooltip im collapsed-Zustand */
#db-layout-wrap.sbar-collapsed .db-nav-item:hover::after {
    content: attr(title);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%; transform: translateY(-50%);
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--fg);
    padding: 4px 10px;
    border-radius: 6px;
    font-size: .8rem;
    white-space: nowrap;
    z-index: 500;
    box-shadow: 0 2px 8px rgba(0,0,0,.12);
    pointer-events: none;
}

/* Collapsed: Profil-Details */
#db-layout-wrap.sbar-collapsed .db-prof-info,
#db-layout-wrap.sbar-collapsed .db-prof-foot  { display: none; }
#db-layout-wrap.sbar-collapsed .db-prof       { justify-content: center; padding: 10px 4px; gap: 0; }
#db-layout-wrap.sbar-collapsed .db-prof-upgrade { display: none; }

/* ══════════════════════════════════════════════════════════════
   SPALTE 2  — Haupt-Content (#db-content-area)
   ══════════════════════════════════════════════════════════════ */
#db-content-area {
    flex: 1 1 0%;
    min-width: 0;
    padding: 16px;
    box-sizing: border-box;
}

/* ══════════════════════════════════════════════════════════════
   SPALTE 3  — rechtes Panel (#db-sidebar-right)
   position:sticky direkt auf dem Flex-Child – gleiche Methode wie links.
   ══════════════════════════════════════════════════════════════ */
#db-sidebar-right {
    width: 40px;
    flex-shrink: 0;
    align-self: flex-start;       /* ← verhindert stretch, ermöglicht sticky */
    position: sticky;
    top: 56px;                    /* wird durch JS aktualisiert */
    height: calc(100vh - 56px);   /* wird durch JS aktualisiert */
    overflow-x: hidden;
    overflow-y: auto;
    background: var(--surface);
    border-left: 1px solid var(--border);
    transition: width .22s ease;
    z-index: 100;
    box-sizing: border-box;
}
#db-layout-wrap.rbar-expanded #db-sidebar-right { width: 200px; }

/* Innerer Container: nur noch Wrapper */
#db-rbar-inner {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

/* Toggle-Button rechts */
.db-rbar-toggle {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 8px 4px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-alt);
    position: sticky;
    top: 0;
    z-index: 1;
    flex-shrink: 0;
}
.db-rbar-toggle-btn {
    background: none; border: none; cursor: pointer;
    color: var(--muted); font-size: 1rem; padding: 3px 7px;
    border-radius: 5px; line-height: 1; transition: all .12s;
    display: inline-flex; align-items: center;
    height: 28px; font-family: inherit;
}
.db-rbar-toggle-btn:hover { color: var(--fg); background: var(--surface); }

/* Inhalt im rechten Panel */
.db-rbar-content {
    padding: 12px 10px;
    flex: 1;
    display: none;
}
#db-layout-wrap.rbar-expanded .db-rbar-content { display: block; }

.db-rbar-section { margin-bottom: 16px; }
.db-rbar-section-title {
    font-size: .68rem; font-weight: 700; letter-spacing: .06em;
    text-transform: uppercase; color: var(--muted);
    margin-bottom: 8px; padding-bottom: 4px;
    border-bottom: 1px solid var(--border);
}
.db-rbar-link,
.db-rbar-link:link,
.db-rbar-link:visited {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 8px; border-radius: 6px;
    font-size: .82rem; color: var(--muted);
    text-decoration: none; transition: all .12s;
    white-space: nowrap; overflow: hidden;
}
.db-rbar-link:hover { background: var(--surface-alt); color: var(--fg); }

/* ══════════════════════════════════════════════════════════════
   16-SPALTEN-EXPLICIT-GRID  (#db-main)
   ══════════════════════════════════════════════════════════════ */
#db-main {
    display: grid !important;
    grid-template-columns: repeat(16, 1fr);
    grid-auto-rows: 80px;
    gap: 8px;
    position: relative;
    align-items: start;
    width: 100%;
    box-sizing: border-box;
    /* Lade-Sprung vermeiden: erst sichtbar wenn JS positioniert hat (Fallback: <noscript>) */
    opacity: 0;
    transition: opacity .15s ease;
}
#db-main.grid-ready { opacity: 1; }

#db-main > .db-section {
    grid-column: 1 / span 16;
    min-width: 0;
    box-sizing: border-box;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    margin-bottom: 0;
}

/* Section-Body scrollbar */
.db-section-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    transition: max-height .25s ease, padding .2s ease, opacity .2s ease;
}
.db-section.collapsed > .db-section-body {
    max-height: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    opacity: 0;
    pointer-events: none;
    overflow: hidden;
}

/* ══════════════════════════════════════════════════════════════
   DRAG-GHOST
   ══════════════════════════════════════════════════════════════ */
.db-ghost {
    background: color-mix(in srgb, var(--primary) 12%, transparent);
    border: 2px dashed var(--primary);
    border-radius: 10px;
    pointer-events: none;
    z-index: 5;
    opacity: .8;
}

/* ══════════════════════════════════════════════════════════════
   COLLAPSE-BUTTON
   ══════════════════════════════════════════════════════════════ */
.db-collapse-btn {
    background: none; border: none; cursor: pointer;
    color: var(--muted); font-size: .82rem; padding: 3px 7px;
    border-radius: 4px; line-height: 1; flex-shrink: 0;
    transition: color .12s, background .12s;
    display: inline-flex; align-items: center; justify-content: center;
    width: 26px; height: 26px;
}
.db-collapse-btn:hover { color: var(--fg); background: var(--surface-alt); }
.db-section.collapsed .db-collapse-btn { color: var(--primary); }
/* Larger touch target on mobile */
@media (max-width: 768px) {
    .db-collapse-btn {
        width: 40px; height: 40px;
        font-size: 1rem;
        margin-left: auto;
    }
}

/* ══════════════════════════════════════════════════════════════
   RESIZE-HANDLES  (E, S, SE)
   ══════════════════════════════════════════════════════════════ */
.db-resize-e, .db-resize-s, .db-resize-se {
    position: absolute;
    display: none;
    z-index: 20;
    background: var(--primary);
    opacity: 0;
    transition: opacity .15s;
}
.db-resize-e  { right: -4px; top: 20%; bottom: 20%; width: 6px; cursor: col-resize; border-radius: 3px; }
.db-resize-s  { bottom: -4px; left: 20%; right: 20%; height: 6px; cursor: row-resize; border-radius: 3px; }
.db-resize-se { right: -4px; bottom: -4px; width: 12px; height: 12px; cursor: se-resize; border-radius: 2px; }

.db-layout-edit .db-resize-e,
.db-layout-edit .db-resize-s,
.db-layout-edit .db-resize-se { display: block; }

.db-layout-edit .db-section:hover .db-resize-e,
.db-layout-edit .db-section:hover .db-resize-s,
.db-layout-edit .db-section:hover .db-resize-se { opacity: .55; }

.db-resize-e:hover,  .db-resize-e.resizing,
.db-resize-s:hover,  .db-resize-s.resizing,
.db-resize-se:hover, .db-resize-se.resizing { opacity: 1 !important; }

body.db-resizing-e  * { cursor: col-resize !important; user-select: none !important; }
body.db-resizing-s  * { cursor: row-resize !important; user-select: none !important; }
body.db-resizing-se * { cursor: se-resize  !important; user-select: none !important; }
body.db-moving      * { cursor: grabbing   !important; user-select: none !important; }

/* ══════════════════════════════════════════════════════════════
   EDIT-MODUS
   · Sections leicht ausgegraut
   · Section-Body: pointer-events none (kein versehentliches Klicken)
   · Section-Header: cursor grab (ganzer Header verschiebbar)
   · Overlay über Section-Body verhindert Interaktionen visuell
   ══════════════════════════════════════════════════════════════ */
.db-layout-edit .db-section {
    outline: 1px dashed color-mix(in srgb, var(--primary) 40%, var(--border));
    outline-offset: 1px;
}
.db-layout-edit .db-section.is-dragging { opacity: .3; }

/* Section-Body im Edit-Modus: Interaktionen deaktiviert */
.db-layout-edit .db-section-body {
    pointer-events: none;
    user-select: none;
    opacity: .55;
}

/* Section-Header: Cursor signalisiert Verschiebbarkeit */
.db-layout-edit .db-section-hd {
    cursor: grab;
    background: color-mix(in srgb, var(--primary) 4%, var(--surface));
}
.db-layout-edit .db-section-hd:active { cursor: grabbing; }

/* Buttons/Inputs im Header bleiben klickbar (collapse) */
.db-layout-edit .db-section-hd button,
.db-layout-edit .db-section-hd input  { pointer-events: auto; cursor: pointer; }

/* Spalten-Führungslinien */
.db-layout-edit #db-main::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    background-image: repeating-linear-gradient(
        to right,
        color-mix(in srgb, var(--primary) 5%, transparent) 0,
        color-mix(in srgb, var(--primary) 5%, transparent) calc(100% / 16 - 0.5px),
        transparent calc(100% / 16 - 0.5px),
        transparent calc(100% / 16)
    );
}
.db-layout-edit #db-main > .db-section { z-index: 1; }

/* ── Edit-Toolbar ───────────────────────────────────────────── */
.db-edit-bar {
    display: none;
    align-items: center; gap: 8px; flex-wrap: wrap;
    padding: 8px 14px; margin-bottom: 10px;
    background: color-mix(in srgb, var(--primary) 7%, var(--surface));
    border: 1px solid color-mix(in srgb, var(--primary) 22%, var(--border));
    border-radius: 8px; font-size: .8rem; color: var(--muted);
}
.db-layout-edit .db-edit-bar { display: flex; }
.db-edit-bar-hint { flex: 1; }

/* ── Layout-Controls (Toolbar oben rechts) ──────────────────── */
.db-layout-controls {
    display: flex; justify-content: flex-end;
    gap: 6px; margin-bottom: 10px; flex-wrap: wrap;
    align-items: center;
}

/* Preset-Dropdown */
.db-preset-select {
    font-size: .79rem; padding: 5px 10px;
    background: var(--surface-alt); border: 1px solid var(--border);
    border-radius: 6px; cursor: pointer; color: var(--muted);
    font-family: inherit; transition: all .12s;
    height: 30px; outline: none;
    appearance: auto;
}
.db-preset-select:hover,
.db-preset-select:focus { border-color: var(--primary); color: var(--fg); }

.btn-edit-layout {
    display: inline-flex; align-items: center; gap: 5px;
    font-size: .79rem; padding: 5px 12px;
    background: var(--surface-alt); border: 1px solid var(--border);
    border-radius: 6px; cursor: pointer; color: var(--muted);
    transition: all .12s; white-space: nowrap;
    font-family: inherit; font-weight: 600; height: 30px;
}
.btn-edit-layout:hover { border-color: var(--primary); color: var(--primary); }
.db-layout-edit .btn-edit-layout {
    background: var(--btn-bg); color: var(--btn-fg);
    border-color: var(--btn-bg);
}

.btn-reset-layout {
    display: none;
    font-size: .75rem; padding: 5px 10px; height: 30px;
    background: none; border: 1px solid var(--border);
    border-radius: 6px; cursor: pointer; color: var(--muted);
    font-family: inherit; transition: all .12s; white-space: nowrap;
}
.btn-reset-layout:hover { border-color: var(--error); color: var(--error); }
.db-layout-edit .btn-reset-layout {
    display: inline-flex; align-items: center; gap: 4px;
}

/* ══════════════════════════════════════════════════════════════
   RESPONSIVE
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 1100px) {
    #db-sidebar-right { display: none; }
}

@media (max-width: 900px) {
    #db-sidebar { width: 44px; }
    #db-layout-wrap.sbar-hidden #db-sidebar { width: 0; }
    #db-main { grid-template-columns: repeat(8, 1fr); }
    #db-main > .db-section { grid-column: 1 / -1 !important; }
    .db-resize-e, .db-resize-s, .db-resize-se { display: none !important; }
    .btn-edit-layout, .db-preset-select { display: none; }
}

@media (max-width: 480px) {
    #db-layout-wrap { flex-direction: column; align-items: flex-start; }
    #db-sidebar { display: none; }
    #db-content-area { padding: 10px; }
    #db-main {
        grid-template-columns: 1fr;
        grid-auto-rows: auto;
        gap: 10px;
    }
    #db-main > .db-section {
        grid-column: 1 / -1 !important;
        grid-row: auto !important;
        min-height: 0 !important;
    }
    .db-layout-controls { display: none; }
}
