/* ================================================================
   Layout -- Sidebar + Top Bar + Main Content
   ================================================================ */

/* ---- App Shell ---- */
#app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows: var(--topbar-h) 1fr;
    grid-template-areas:
        "topbar  topbar"
        "sidebar main";
    height: 100dvh;
    height: 100svh;       /* small viewport height - iOS Safari sicherste Variante */
    background: var(--bg-base);
    overflow: hidden;
}

/* ================================================================
   SIDEBAR
   ================================================================ */
.sidebar {
    width: var(--sidebar-w);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 100;
    transition: transform var(--dur-normal) var(--ease-out);
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-header {
    padding: var(--sp-5) var(--sp-5) var(--sp-4);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    min-height: 60px;
}

.sidebar-family-name {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#sidebar-close {
    display: none;
}

/* ---- Navigation ---- */
.nav-list {
    list-style: none;
    margin: 0;
    padding: var(--sp-3) var(--sp-3);
    flex: 1;
}

.nav-list li {
    margin-bottom: 2px;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    color: var(--text-secondary);
    font-size: var(--text-sm);
    font-weight: 500;
    border-radius: var(--radius-md);
    transition: all var(--dur-fast) var(--ease-out);
    cursor: pointer;
    text-decoration: none;
    position: relative;
    overflow: hidden;
}

.nav-link::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, var(--accent-muted), transparent);
    opacity: 0;
    transition: opacity var(--dur-fast);
    border-radius: inherit;
}

.nav-link:hover {
    color: var(--text-primary);
    background: rgba(128,128,128,0.08);
}

.nav-link:hover::before {
    opacity: 0.5;
}

.nav-link.active {
    color: var(--accent);
    background: var(--accent-muted);
}

.nav-link.active::before {
    opacity: 1;
}

.nav-link svg {
    flex-shrink: 0;
    opacity: 0.7;
    transition: opacity var(--dur-fast);
}

.nav-link:hover svg,
.nav-link.active svg {
    opacity: 1;
}

.nav-link.active svg {
    stroke: var(--accent);
}

/* ---- Sidebar Footer ---- */
.sidebar-footer {
    padding: var(--sp-4) var(--sp-5);
    border-top: 1px solid var(--border);
}

#btn-logout {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-4);
    color: var(--text-muted);
    font-size: var(--text-sm);
    font-weight: 500;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--dur-fast);
}

#btn-logout:hover {
    color: var(--red);
    border-color: var(--red-muted);
    background: var(--red-muted);
}

/* ================================================================
   TOP BAR
   ================================================================ */
.topbar {
    grid-area: topbar;
    height: var(--topbar-h);
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--sp-6);
    z-index: 50;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

#menu-toggle {
    display: none;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: var(--sp-2);
    border-radius: var(--radius-sm);
    transition: color var(--dur-fast);
}

#menu-toggle:hover {
    color: var(--text-primary);
}

.topbar-title {
    font-size: var(--text-lg);
    font-weight: 600;
    color: var(--text-primary);
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
}

.avatar-btn {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    background: linear-gradient(135deg, var(--accent), var(--purple));
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: var(--text-sm);
    border: none;
    cursor: pointer;
    transition: box-shadow var(--dur-fast);
}

.avatar-btn:hover {
    box-shadow: var(--shadow-glow);
}

/* ================================================================
   MAIN CONTENT
   ================================================================ */
.main-content {
    grid-area: main;
    min-width: 0;           /* grid shrink fix */
    display: flex;
    flex-direction: column;
    /* Hoehe erbt von Grid-Row (1fr) */
    overflow: hidden;
}

.module {
    flex: 1;
    min-height: 0;          /* iOS flex bug: ohne min-height kein overflow-y */
    padding: var(--sp-6);
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    animation: fadeIn var(--dur-normal) var(--ease-out);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ---- Sidebar Overlay ---- */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 99;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur-normal);
}

.sidebar-overlay:not([hidden]) {
    opacity: 1;
    pointer-events: auto;
}

/* ================================================================
   AUTH PAGES
   ================================================================ */
.view-fullscreen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-base);
    padding: var(--sp-4);
    position: relative;
    overflow: hidden;
}

/* FIX: hidden attribute must override display:flex */
.view-fullscreen[hidden] {
    display: none !important;
}

/* Background decoration */
.view-fullscreen::before {
    content: '';
    position: absolute;
    width: 600px;
    height: 600px;
    background: radial-gradient(circle, var(--accent-glow) 0%, transparent 70%);
    top: -200px;
    right: -200px;
    pointer-events: none;
}

.view-fullscreen::after {
    content: '';
    position: absolute;
    width: 400px;
    height: 400px;
    background: radial-gradient(circle, rgba(180, 142, 255, 0.1) 0%, transparent 70%);
    bottom: -100px;
    left: -100px;
    pointer-events: none;
}

.auth-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: var(--sp-10) var(--sp-8);
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow-xl);
    position: relative;
    z-index: 1;
}

.auth-logo {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    margin-bottom: var(--sp-8);
}

.auth-logo h1 {
    font-size: var(--text-2xl);
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.auth-logo svg {
    flex-shrink: 0;
}

/* ---- Auth Tabs ---- */
.tab-bar {
    display: flex;
    gap: var(--sp-1);
    margin-bottom: var(--sp-6);
    background: var(--bg-input);
    border-radius: var(--radius-md);
    padding: 3px;
}

.tab-bar .tab {
    flex: 1;
    padding: var(--sp-2) var(--sp-4);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-muted);
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--dur-fast);
}

.tab-bar .tab.active {
    background: var(--accent);
    color: white;
}

.tab-bar .tab:hover:not(.active) {
    color: var(--text-secondary);
}

/* ---- Auth Form ---- */
.auth-form .field {
    margin-bottom: var(--sp-4);
}

.auth-form label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: var(--sp-2);
}

.auth-form input[type="email"],
.auth-form input[type="password"],
.auth-form input[type="text"] {
    width: 100%;
    padding: var(--sp-3) var(--sp-4);
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: var(--text-base);
    font-family: var(--font-sans);
    transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
    outline: none;
}

.auth-form input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-muted);
}

.form-error {
    color: var(--red);
    font-size: var(--text-sm);
    padding: var(--sp-2) var(--sp-3);
    background: var(--red-muted);
    border-radius: var(--radius-sm);
    margin-bottom: var(--sp-3);
}

/* ---- Primary Button ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-6);
    font-size: var(--text-base);
    font-weight: 600;
    font-family: var(--font-sans);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--dur-fast) var(--ease-out);
    border: none;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), #5580E0);
    color: white;
    box-shadow: 0 2px 8px rgba(108, 156, 255, 0.3);
}

.btn-primary:hover {
    box-shadow: 0 4px 16px rgba(108, 156, 255, 0.5);
    transform: translateY(-1px);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-full {
    width: 100%;
}

.btn-sm {
    padding: var(--sp-2) var(--sp-4);
    font-size: var(--text-sm);
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.btn-ghost:hover {
    background: rgba(128,128,128,0.08);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--red);
    color: white;
}

.btn-danger:hover {
    background: #FF5252;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: var(--sp-2);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--dur-fast);
}

.icon-btn:hover {
    color: var(--text-primary);
    background: rgba(128,128,128,0.1);
}

/* ================================================================
   TOAST
   ================================================================ */
#toast-container {
    position: fixed;
    bottom: var(--sp-6);
    right: var(--sp-6);
    z-index: 500;
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    pointer-events: none;
}

.toast {
    background: var(--bg-elevated);
    color: var(--text-primary);
    padding: var(--sp-3) var(--sp-5);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    pointer-events: auto;
    animation: slideUp 0.3s var(--ease-spring);
    border-left: 3px solid var(--accent);
}

.toast-info    { border-left-color: var(--accent); }
.toast-success { border-left-color: var(--green); }
.toast-error   { border-left-color: var(--red); }
.toast-warning { border-left-color: var(--orange); }

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

/* ================================================================
   MODAL
   ================================================================ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(8px);
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
}

.modal-overlay.open {
    display: flex;
    animation: fadeIn var(--dur-fast);
}

.modal-overlay[hidden] {
    display: none;
}

.modal-box {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: var(--sp-8);
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    max-height: 90dvh;
    overflow-y: auto;
    box-shadow: var(--shadow-xl);
}

/* Auf Mobile: Modal endet ueber der Bottom-Nav */
@media (max-width: 768px) {
    .modal-overlay {
        /* Padding-bottom = Bottom-Nav-Hoehe damit Modal darueber liegt */
        padding-bottom: calc(var(--sp-4) + 64px + env(safe-area-inset-bottom));
        align-items: flex-end;   /* Modal am unteren Rand ausrichten */
    }
    .modal-box {
        max-height: calc(92dvh - 64px - env(safe-area-inset-bottom));
        border-radius: var(--radius-xl) var(--radius-xl) var(--radius-lg) var(--radius-lg);
        padding: var(--sp-5);
        margin-bottom: 0;
    }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--sp-6);
}

#modal-title {
    font-size: var(--text-xl);
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.modal-close {
    font-size: 1.5rem;
    color: var(--text-muted);
    background: none;
    border: none;
    cursor: pointer;
    padding: var(--sp-2);
    border-radius: var(--radius-sm);
    line-height: 1;
    transition: color var(--dur-fast), background var(--dur-fast);
}

.modal-close:hover {
    color: var(--text-primary);
    background: rgba(128,128,128,0.1);
}

.modal-title {
    font-size: var(--text-xl);
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

/* ================================================================
   DASHBOARD
   ================================================================ */
.dashboard {
    max-width: 800px;
    margin: 0 auto;
    padding: var(--sp-4) 0;
}

.dashboard-greeting {
    margin-bottom: var(--sp-8);
}

.dashboard-greeting h2 {
    font-size: var(--text-3xl);
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 var(--sp-1);
}

.dashboard-greeting p {
    font-size: var(--text-base);
    color: var(--text-muted);
    margin: 0;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--sp-4);
}

.dashboard-tile {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: var(--sp-6) var(--sp-5);
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: var(--sp-3);
    cursor: pointer;
    transition: all var(--dur-fast) var(--ease-out);
    text-decoration: none;
    color: inherit;
    box-shadow: var(--shadow-sm);
}

.dashboard-tile:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-md);
    border-color: var(--border-strong);
}

.tile-icon {
    width: 56px;
    height: 56px;
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.tile-icon svg {
    width: 28px;
    height: 28px;
}

.tile-title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text-primary);
}

.tile-desc {
    font-size: var(--text-xs);
    color: var(--text-muted);
    line-height: 1.4;
}

/* ================================================================
   BOTTOM NAV (Mobile)
   ================================================================ */
.bottom-nav {
    display: none;
}

@media (max-width: 768px) {
    .bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 90;
        background: var(--bg-surface);
        border-top: 1px solid var(--border);
        padding: var(--sp-1) 0;
        padding-bottom: calc(var(--sp-1) + env(safe-area-inset-bottom));
        gap: 0;
        /* Auto-hide: per JS toggle .nav-hidden */
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        will-change: transform;
    }

    .bottom-nav.nav-hidden {
        transform: translateY(calc(100% + env(safe-area-inset-bottom)));
    }

    .bottom-nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        padding: var(--sp-2) var(--sp-1);
        background: none;
        border: none;
        color: var(--text-muted);
        font-family: var(--font-sans);
        font-size: 0.65rem;
        cursor: pointer;
        transition: color var(--dur-fast);
        text-decoration: none;
        -webkit-tap-highlight-color: transparent;
    }

    .bottom-nav-item svg {
        width: 22px;
        height: 22px;
        flex-shrink: 0;
    }

    .bottom-nav-item.active {
        color: var(--accent);
    }

    .bottom-nav-item:active {
        color: var(--accent);
        transform: scale(0.92);
    }

    /* Mehr-Button hat einen kleinen Badge-Stil */
    .bottom-nav-item.more svg {
        opacity: 0.7;
    }
}

/* ================================================================
   RESPONSIVE
   ================================================================ */
@media (max-width: 768px) {
    #app {
        grid-template-columns: 1fr;
        grid-template-areas:
            "topbar"
            "main";
    }

    .sidebar {
        /* Sidebar bleibt position: fixed fuer Slide-In, Grid ignoriert sie */
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    #sidebar-close {
        display: flex;
    }

    #menu-toggle {
        display: flex;
    }

    .module {
        padding: var(--sp-4);
        /* Letzter Inhalt nicht hinter Bottom-Nav: scroll-padding + padding */
        padding-bottom: calc(var(--sp-4) + 64px + env(safe-area-inset-bottom));
        scroll-padding-bottom: calc(64px + env(safe-area-inset-bottom));
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 480px) {
    .module {
        padding: var(--sp-3);
    }

    .auth-card {
        padding: var(--sp-6);
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--sp-3);
    }

    .dashboard-greeting h2 {
        font-size: var(--text-2xl);
    }

    .tile-icon {
        width: 48px;
        height: 48px;
    }

    .tile-icon svg {
        width: 24px;
        height: 24px;
    }
}

/* ================================================================
   LIGHT MODE OVERRIDES
   ================================================================ */
@media (prefers-color-scheme: light) {
    .modal-overlay {
        background: rgba(0,0,0,0.4);
    }

    .sidebar-overlay {
        background: rgba(0,0,0,0.3);
    }

    .toast {
        background: var(--bg-card);
        box-shadow: var(--shadow-lg);
    }

    .btn-primary {
        box-shadow: 0 2px 8px rgba(74, 127, 229, 0.25);
    }

    .btn-primary:hover {
        box-shadow: 0 4px 16px rgba(74, 127, 229, 0.35);
    }

    .dashboard-tile {
        box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    }

    .dashboard-tile:hover {
        box-shadow: 0 4px 16px rgba(0,0,0,0.1);
    }
}
