/* =========================================================================
   FLT Telefon-KI – Layout: App-Shell, Sidebar, Drawer, Seitenkopf,
   Inhaltsbereich, Raster, Anmeldeseite
   Markup: partials/header.ejs (öffnet die Shell), partials/footer.ejs
   (schließt sie), partials/page_header.ejs (Kopfleiste je Seite).
   ========================================================================= */

/* --- App-Shell ----------------------------------------------------------
   Desktop (≥ 1024 px): feste Sidebar links, fluider Inhalt rechts.
   Mobil/Tablet: Kopfzeile mit Menü-Button, Sidebar als Drawer (Checkbox
   #nav-toggle, ohne JS bedienbar; js/app.js ergänzt nur Escape/Resize). */
.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100vh;
    min-height: 100dvh;
}

@media (min-width: 1024px) {
    /* Sidebar-Spalte optisch bis zum Seitenende fortsetzen (Sidebar selbst haftet oben) */
    .app-shell:not(.app-shell-bare) {
        background: linear-gradient(90deg, var(--color-surface) calc(var(--sidebar-w) - 1px), var(--color-border) calc(var(--sidebar-w) - 1px), var(--color-border) var(--sidebar-w), transparent var(--sidebar-w));
    }
}

.app-main {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 100vh;
    min-height: 100dvh;
}

.app-main:focus {
    outline: none;
}

/* Marke (Sidebar, mobile Kopfzeile, Anmeldung) */
.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    min-height: var(--touch-min);
    color: var(--color-text);
    text-decoration: none;
    border-radius: var(--radius-md);
}

.brand:hover {
    color: var(--color-text);
}

.brand-mark {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 9px;
    color: #fff;
    background: linear-gradient(135deg, var(--navy-600) 0%, var(--navy-900) 100%);
    box-shadow: var(--shadow-highlight), 0 1px 2px rgb(10 31 71 / 0.25);
}

.brand-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.2;
}

.brand-name {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14.5px;
    font-weight: 650;
    letter-spacing: -0.012em;
    color: var(--gray-900);
    white-space: nowrap;
}

.brand-sub {
    margin-top: 1px;
    font-size: 11.5px;
    color: var(--color-text-tertiary);
    white-space: nowrap;
}

.brand-inverse .brand-name {
    color: #fff;
}

.brand-inverse .brand-sub {
    color: rgb(255 255 255 / 0.66);
}

.brand-inverse .brand-mark {
    color: var(--navy-800);
    background: #fff;
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.2);
}

.env-badge {
    display: inline-flex;
    align-items: center;
    height: 18px;
    padding: 0 6px;
    border-radius: 5px;
    background: var(--amber-100);
    color: var(--amber-800);
    box-shadow: inset 0 0 0 1px var(--amber-200);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: var(--tracking-caps);
    line-height: 1;
}

/* --- Sidebar ------------------------------------------------------------ */
.sidebar {
    grid-column: 1;
    grid-row: 1;
    position: sticky;
    top: 0;
    z-index: var(--z-sidebar);
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
}

.sidebar-head {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    height: var(--topbar-h);
    padding: 0 14px 0 16px;
    border-bottom: 1px solid var(--color-border);
}

.sidebar-nav {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 20px;
    padding: 16px 12px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.nav-section {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-section-label {
    padding: 0 10px 6px;
    font-size: var(--text-2xs);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    color: var(--color-text-tertiary);
}

.sidebar-nav a {
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 36px;
    padding: 6px 10px;
    border-radius: var(--radius-md);
    color: var(--gray-700);
    font-size: var(--text-base);
    font-weight: var(--weight-medium);
    line-height: 1.3;
    text-decoration: none;
    transition: background-color var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard);
}

.sidebar-nav a .icon {
    width: 18px;
    height: 18px;
    color: var(--gray-500);
    transition: color var(--duration-fast) var(--ease-standard);
}

.sidebar-nav a:hover {
    background: var(--gray-75);
    color: var(--gray-900);
}

.sidebar-nav a:hover .icon {
    color: var(--gray-700);
}

.sidebar-nav a.active,
.sidebar-nav a[aria-current='page'] {
    background: var(--navy-50);
    color: var(--navy-800);
    font-weight: var(--weight-semibold);
}

.sidebar-nav a.active .icon,
.sidebar-nav a[aria-current='page'] .icon {
    color: var(--navy-700);
}

.sidebar-nav a:focus-visible {
    outline-offset: -2px;
}

/* Benutzerblock unten */
.sidebar-user {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 12px;
    border-top: 1px solid var(--color-border);
}

.user-card {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    padding: 4px 4px 2px;
}

.user-meta {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.3;
}

.user-name {
    overflow: hidden;
    font-size: 13.5px;
    font-weight: var(--weight-semibold);
    color: var(--color-text);
    white-space: nowrap;
    text-overflow: ellipsis;
}

.user-role {
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
}

.user-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
}

.user-actions form {
    display: flex;
    margin: 0;
}

.user-actions .btn {
    width: 100%;
    justify-content: flex-start;
}

/* Drawer-Steuerung (nur < 1024 px sichtbar) */
input[type='checkbox'].nav-toggle,
.mobile-bar,
.sidebar-backdrop,
.sidebar-close {
    display: none;
}

.menu-btn,
.sidebar-close {
    place-items: center;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    color: var(--gray-700);
    cursor: pointer;
    transition: background-color var(--duration-fast) var(--ease-standard);
    -webkit-tap-highlight-color: transparent;
}

.menu-btn:hover,
.sidebar-close:hover {
    background: var(--gray-75);
}

@media (max-width: 1023.98px) {
    .app-shell {
        grid-template-columns: minmax(0, 1fr);
        padding-top: var(--mobilebar-h);
    }

    .app-main {
        grid-column: 1;
        min-height: calc(100dvh - var(--mobilebar-h));
    }

    .menu-btn {
        display: inline-grid;
    }

    input[type='checkbox'].nav-toggle {
        display: block;
        position: fixed;
        top: 8px;
        left: 12px;
        width: 40px;
        height: 40px;
        margin: 0;
        opacity: 0;
        z-index: var(--z-mobilebar);
        pointer-events: none;
    }

    .mobile-bar {
        position: fixed;
        top: 0;
        right: 0;
        left: 0;
        z-index: var(--z-mobilebar);
        display: flex;
        align-items: center;
        gap: 6px;
        height: var(--mobilebar-h);
        padding: 0 12px 0 8px;
        background: rgb(255 255 255 / 0.92);
        border-bottom: 1px solid var(--color-border);
        -webkit-backdrop-filter: saturate(1.8) blur(12px);
        backdrop-filter: saturate(1.8) blur(12px);
    }

    .mobile-bar .brand-sub {
        display: none;
    }

    /* Avatar öffnet ebenfalls den Drawer (Benutzerblock unten); Trefferfläche 40 px */
    .mobile-bar .mobile-avatar {
        position: relative;
        margin-left: auto;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .mobile-bar .mobile-avatar::after {
        content: '';
        position: absolute;
        inset: 50% auto auto 50%;
        width: var(--touch-min);
        height: var(--touch-min);
        transform: translate(-50%, -50%);
    }

    .nav-toggle:focus-visible ~ .mobile-bar .menu-btn {
        outline: 2px solid var(--color-focus);
        outline-offset: 0;
    }

    /* Drawer offen: der Menü-Button liegt unter der Sidebar – Fokusrahmen am
       Schließen-Button zeigen, solange der (unsichtbare) Schalter fokussiert ist */
    .nav-toggle:checked:focus-visible ~ .sidebar .sidebar-close {
        outline: 2px solid var(--color-focus);
        outline-offset: -2px;
    }

    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        width: min(300px, 86vw);
        height: auto;
        box-shadow: var(--shadow-xl);
        transform: translateX(-104%);
        visibility: hidden;
        transition: transform var(--duration-slow) var(--ease-out), visibility 0s linear var(--duration-slow);
    }

    .nav-toggle:checked ~ .sidebar {
        transform: none;
        visibility: visible;
        transition: transform var(--duration-slow) var(--ease-out), visibility 0s;
    }

    .sidebar-head {
        height: var(--mobilebar-h);
        padding-right: 8px;
    }

    .sidebar-close {
        display: inline-grid;
        margin-left: auto;
    }

    .sidebar-nav a {
        min-height: var(--touch-min);
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: var(--z-backdrop);
        background: rgb(16 24 40 / 0.42);
        opacity: 0;
        pointer-events: none;
        transition: opacity var(--duration-slow) var(--ease-out);
    }

    .nav-toggle:checked ~ .sidebar-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    body:has(.nav-toggle:checked) {
        overflow: hidden;
    }
}

/* Shell ohne Anmeldung (z. B. Fehlerseite für Abgemeldete) */
.app-shell-bare {
    grid-template-columns: minmax(0, 1fr);
    padding-top: 0;
}

.app-shell-bare .app-main {
    grid-column: 1;
}

.bare-bar {
    display: flex;
    align-items: center;
    height: var(--topbar-h);
    padding: 0 var(--page-px);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
}

/* --- Seite: Kopfleiste + Inhalt ----------------------------------------- */
.page {
    flex: 1 0 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-topbar);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px 16px;
    min-height: var(--topbar-h);
    padding: 8px var(--page-px);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
}

.topbar-heading {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
}

.topbar-title-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px 8px;
    min-width: 0;
}

/* Titel und Untertitel werden nie abgeschnitten – wird es eng, brechen sie
   um bzw. rutschen Pill und Aktionen in die nächste Zeile */
.topbar-title {
    min-width: 0;
    font-size: var(--text-xl);
    font-weight: var(--weight-semibold);
    line-height: 1.3;
    letter-spacing: -0.016em;
    overflow-wrap: break-word;
}

.topbar-subtitle {
    font-size: var(--text-sm);
    line-height: 1.4;
    color: var(--color-text-tertiary);
    overflow-wrap: break-word;
}

.topbar-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    flex-shrink: 0;
}

.topbar-actions form {
    display: inline-flex;
    margin: 0;
}

/* Brotkrumen (in der Titelzeile vor dem h1) */
.crumbs {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.crumbs ol {
    display: flex;
    align-items: center;
    gap: 4px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.crumbs li {
    display: flex;
    align-items: center;
    gap: 4px;
}

.crumbs a {
    font-size: var(--text-xl);
    font-weight: var(--weight-medium);
    line-height: 1.3;
    letter-spacing: -0.016em;
    color: var(--color-text-tertiary);
    text-decoration: none;
    border-radius: var(--radius-xs);
}

.crumbs a:hover {
    color: var(--color-text);
}

@media (max-width: 767.98px), (pointer: coarse) {
    /* unsichtbare Trefferfläche ≥ 40 px */
    .crumbs a {
        position: relative;
    }

    .crumbs a::after {
        content: '';
        position: absolute;
        inset: 50% -4px auto;
        height: var(--touch-min);
        transform: translateY(-50%);
    }
}

.crumbs-sep {
    color: var(--gray-350);
}

.page-body {
    container-type: inline-size;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--page-gap);
    min-width: 0;
    padding: 24px var(--page-px) 40px;
}

/* Abschnittsüberschrift zwischen Kartengruppen */
.section-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px 16px;
}

.section-title {
    font-size: var(--text-md);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-tight);
}

.section-desc {
    margin-top: 2px;
    font-size: var(--text-sm);
    color: var(--color-text-tertiary);
}

@media (max-width: 1023.98px) {
    .topbar {
        position: static;
        -webkit-backdrop-filter: none;
        backdrop-filter: none;
        background: var(--color-surface);
    }
}

@media (max-width: 639.98px) {
    .topbar {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
        padding: 16px;
    }

    .topbar-title,
    .crumbs a {
        font-size: var(--text-xl);
    }

    /* Aktionen der Kopfleiste über die volle Breite (gleich breite Buttons) */
    .topbar-actions {
        flex-wrap: wrap;
        width: 100%;
    }

    .topbar-actions > .btn,
    .topbar-actions > form {
        flex: 1 1 auto;
    }

    .topbar-actions > form .btn {
        width: 100%;
    }

    .page-body {
        gap: 16px;
        padding: 16px 16px 32px;
    }
}

/* --- Fußzeile ------------------------------------------------------------ */
.app-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 4px 16px;
    margin-top: auto;
    padding: 16px var(--page-px) 20px;
    border-top: 1px solid var(--color-border-subtle);
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
}

.app-footer-claim {
    font-weight: var(--weight-medium);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    font-size: var(--text-2xs);
    color: var(--color-text-tertiary);
}

/* --- Raster ------------------------------------------------------------- */
.grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--page-gap);
    align-items: start;
}

.grid-auto {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--grid-min, 320px)), 1fr));
    gap: var(--page-gap);
    align-items: start;
}

.grid-auto-sm {
    --grid-min: 240px;
}

.grid-auto-lg {
    --grid-min: 420px;
}

/* höchstens drei Spalten (z. B. 9 Karten = 3 × 3 statt 4 + 4 + 1) */
.grid-auto.grid-max-3 {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, max(var(--grid-min, 320px), (100% - 2 * var(--page-gap)) / 3)), 1fr));
}

/* steht nach .grid/.grid-auto, damit es dort greift */
.grid-stretch {
    align-items: stretch;
}

/* Spaltenzahl richtet sich nach der Breite des umgebenden Containers
   (.page-body, .card-body oder .cq), nicht nach dem Viewport. */
@container (min-width: 640px) {
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@container (min-width: 960px) {
    .grid-3 {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@container (min-width: 1120px) {
    .grid-4 {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}

.span-2 {
    grid-column: span 2;
}

.span-full {
    grid-column: 1 / -1;
}

@container (max-width: 639.98px) {
    .span-2 {
        grid-column: auto;
    }
}

/* Container für Größenabfragen (page-body und card-body sind es bereits) */
.cq {
    container-type: inline-size;
}

/* Hauptspalte + Seitenspalte (z. B. Formular + Details) */
.split {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--page-gap);
    align-items: start;
}

@container (min-width: 980px) {
    .split {
        grid-template-columns: minmax(0, 1fr) minmax(300px, 380px);
    }
}

@media (min-width: 1024px) {
    @container (min-width: 980px) {
        .split-sticky > :last-child {
            position: sticky;
            top: calc(var(--topbar-h) + 24px);
        }
    }
}

/* --- Anmeldeseite (login.ejs) -------------------------------------------- */
.auth-body {
    background: var(--color-surface);
}

.auth {
    display: grid;
    grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
    min-height: 100vh;
    min-height: 100dvh;
}

.auth-aside {
    position: relative;
    isolation: isolate;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 48px;
    padding: 40px clamp(32px, 4vw, 64px);
    overflow: hidden;
    color: #fff;
    background: linear-gradient(160deg, var(--navy-700) 0%, var(--navy-900) 55%, var(--navy-950) 100%);
}

.auth-aside::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    background-image: linear-gradient(rgb(255 255 255 / 0.06) 1px, transparent 1px),
        linear-gradient(90deg, rgb(255 255 255 / 0.06) 1px, transparent 1px);
    background-size: 44px 44px;
    -webkit-mask-image: radial-gradient(ellipse 80% 70% at 30% 35%, #000 10%, transparent 75%);
    mask-image: radial-gradient(ellipse 80% 70% at 30% 35%, #000 10%, transparent 75%);
}

.auth-aside::after {
    content: '';
    position: absolute;
    right: -20%;
    bottom: -30%;
    z-index: -1;
    width: 80%;
    aspect-ratio: 1;
    border-radius: 50%;
    background: radial-gradient(circle, rgb(59 130 246 / 0.35) 0%, transparent 65%);
}

.auth-hero {
    max-width: 30rem;
}

.auth-eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 18px;
    padding: 4px 10px 4px 8px;
    border-radius: var(--radius-full);
    background: rgb(255 255 255 / 0.08);
    box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.14);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    color: rgb(255 255 255 / 0.85);
}

.auth-eyebrow::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #4ade80;
    box-shadow: 0 0 0 3px rgb(74 222 128 / 0.2);
}

.auth-headline {
    font-size: clamp(26px, 2.4vw, 34px);
    font-weight: var(--weight-semibold);
    line-height: 1.18;
    letter-spacing: -0.024em;
    color: #fff;
}

.auth-lead {
    margin-top: 14px;
    font-size: var(--text-md);
    line-height: var(--leading-relaxed);
    color: rgb(255 255 255 / 0.72);
}

.auth-features {
    display: grid;
    gap: 14px;
    margin: 32px 0 0;
    padding: 0;
    list-style: none;
}

.auth-features li {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: var(--text-base);
    color: rgb(255 255 255 / 0.86);
}

.auth-feature-icon {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-md);
    background: rgb(255 255 255 / 0.08);
    box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.12);
    color: #fff;
}

.auth-aside-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    font-size: var(--text-xs);
    color: rgb(255 255 255 / 0.6);
}

.auth-claim {
    font-weight: var(--weight-semibold);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: rgb(255 255 255 / 0.75);
}

.auth-main {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
}

.auth-card {
    width: 100%;
    max-width: 380px;
}

.auth-title {
    font-size: var(--text-3xl);
    font-weight: var(--weight-semibold);
    letter-spacing: -0.022em;
}

.auth-subtitle {
    margin-top: 6px;
    color: var(--color-text-tertiary);
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 18px;
    margin-top: 28px;
}

.auth-form .btn {
    margin-top: 6px;
}

.auth-help {
    display: flex;
    gap: 8px;
    margin-top: 28px;
    padding-top: 20px;
    border-top: 1px solid var(--color-border-subtle);
    font-size: var(--text-sm);
    color: var(--color-text-tertiary);
}

.auth-help .icon {
    margin-top: 2px;
    color: var(--gray-400);
}

@media (max-width: 899.98px) {
    .auth {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: auto 1fr;
    }

    .auth-aside {
        gap: 0;
        padding: 18px 16px;
    }

    .auth-aside::after {
        display: none;
    }

    .auth-hero,
    .auth-aside-foot {
        display: none;
    }

    .auth-main {
        justify-content: flex-start;
        padding: 40px 16px;
    }
}
