:root {
    --bg-app: #f6f8fc;
    --bg-surface: #ffffff;
    --bg-subtle: #eaf1fb;
    --bg-hover: #f1f3f4;
    --text-primary: #181a1f;
    --text-secondary: #585d66;
    --border-color: #dde1e8;
    --accent: #0b57d0;
    --accent-hover: #0842a0;
    --accent-text: #ffffff;
    --danger: #b3261e;
    --warn-bg: #fef7e0;
    --warn-border: #e3a008;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.14);
    --radius: 14px;
    --topbar-h: 60px;
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
    --bg-app: #111317;
    --bg-surface: #191c22;
    --bg-subtle: #232936;
    --bg-hover: #21252e;
    --text-primary: #eceff4;
    --text-secondary: #9aa2b1;
    --border-color: #2b303b;
    --accent: #8ab4f8;
    --accent-hover: #aecbfa;
    --accent-text: #0f172a;
    --danger: #f28b82;
    --warn-bg: #2b2410;
    --warn-border: #b08900;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    font-family: var(--font-sans);
    background: var(--bg-app);
    color: var(--text-primary);
    line-height: 1.45;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea {
    font-family: inherit;
    color: inherit;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.hidden { display: none !important; }
.no-margin { margin: 0; }
.stack-md { display: flex; flex-direction: column; gap: 12px; }
.stack-sm { display: flex; flex-direction: column; gap: 10px; }
.stack-xs { display: flex; flex-direction: column; gap: 8px; }
.row-gap { display: flex; gap: 8px; align-items: center; }
.wrap { flex-wrap: wrap; }
.divider { width: 100%; border: 0; border-top: 1px solid var(--border-color); margin: 8px 0; }

/* Topbar */
.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 10px;
    height: var(--topbar-h);
    padding: 0 14px;
    background: var(--bg-app);
    border-bottom: 1px solid var(--border-color);
}

.brand {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 1.05rem;
    white-space: nowrap;
    min-width: 0;
}

.brand-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.mobile-only-btn { display: none; }

.search-box {
    flex: 1;
    max-width: 620px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 4px 10px;
    box-shadow: var(--shadow-sm);
}

.search-box input {
    flex: 1;
    min-width: 0;
    border: 0;
    background: transparent;
    outline: none;
    font-size: 0.95rem;
    min-height: 36px;
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 44px;
    min-height: 44px;
    padding: 0 10px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: transparent;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease;
}

.icon-btn:hover { background: var(--bg-hover); }

.btn-primary {
    background: var(--accent);
    color: var(--accent-text);
    border: 0;
    border-radius: 10px;
    min-height: 44px;
    padding: 10px 18px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    min-height: 40px;
    padding: 8px 14px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease;
}

.btn-secondary:hover { background: var(--bg-hover); }

.btn-danger-outline {
    color: var(--danger);
    border-color: var(--danger);
}

.btn-compact {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 0.88rem;
}

.btn-inline {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* Hauptlayout */
.app-shell {
    display: grid;
    grid-template-columns: 264px 1fr;
    min-height: calc(100dvh - var(--topbar-h));
}

.app-shell.sidebar-collapsed { grid-template-columns: 74px 1fr; }

.sidebar {
    padding: 14px 12px;
    border-right: 1px solid var(--border-color);
    background: var(--bg-app);
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    max-height: calc(100dvh - var(--topbar-h));
}

.sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, 0.45);
}

.compose-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    min-height: 46px;
    background: var(--accent);
    color: var(--accent-text);
    border: 0;
    border-radius: 12px;
    padding: 10px 16px;
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 6px;
    white-space: nowrap;
}

.compose-btn:hover { background: var(--accent-hover); }

.sidebar-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px 2px;
}

.sidebar-section-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.sidebar-add-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    min-height: 32px;
    padding: 4px 8px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-surface);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

.sidebar-add-btn:hover { background: var(--bg-hover); }

.sidebar-group-title {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-secondary);
    padding: 10px 10px 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.folder-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 42px;
    padding: 8px 12px;
    border-radius: 10px;
    border: 0;
    background: transparent;
    cursor: pointer;
    text-align: left;
    font-size: 0.9rem;
    width: 100%;
}

.nav-item-main {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    overflow: hidden;
}

.nav-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.nav-item:hover { background: var(--bg-hover); }
.nav-item.active { background: var(--bg-subtle); font-weight: 600; color: var(--accent); }

.nav-count {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
}

.app-shell.sidebar-collapsed .nav-label,
.app-shell.sidebar-collapsed .nav-count,
.app-shell.sidebar-collapsed .compose-label,
.app-shell.sidebar-collapsed .sidebar-section-header,
.app-shell.sidebar-collapsed .sidebar-group-title { display: none; }

/* Content Workspace */
.workspace {
    display: grid;
    grid-template-columns: minmax(320px, 430px) 1fr;
    background: var(--bg-surface);
    margin: 10px;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    overflow: hidden;
    min-height: calc(100dvh - var(--topbar-h) - 20px);
}

.mail-list-pane {
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    max-height: calc(100dvh - var(--topbar-h) - 20px);
    min-width: 0;
}

/* Horizontal Mailbox Switcher Bar */
.account-strip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-color);
    overflow-x: auto;
    scrollbar-width: none;
    background: var(--bg-app);
}

.account-strip::-webkit-scrollbar { display: none; }

.account-tab {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 6px 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-surface);
    font-size: 0.84rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    font-variant-numeric: tabular-nums;
}

.account-tab:hover { background: var(--bg-hover); }

.account-tab.active {
    background: var(--accent);
    color: var(--accent-text);
    border-color: var(--accent);
    font-weight: 600;
}

.pane-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border-color);
}

.pane-title {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.sync-status-bar {
    padding: 6px 14px;
    border-bottom: 1px solid var(--border-color);
}

.mail-list {
    overflow-y: auto;
    flex: 1;
}

.mail-list-empty {
    padding: 24px 18px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
}

.mail-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 68px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.12s ease;
}

.mail-row:hover { background: var(--bg-hover); }
.mail-row.active { background: var(--bg-subtle); }
.mail-row.unread .mail-sender,
.mail-row.unread .mail-subject { font-weight: 700; color: var(--text-primary); }

.mail-row-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    font-size: 0.92rem;
}

.mail-sender {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mail-date {
    color: var(--text-secondary);
    font-size: 0.78rem;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
}

.mail-subject {
    font-size: 0.92rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mail-meta-snippet {
    color: var(--text-secondary);
    font-size: 0.83rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Reader Pane */
.reader-pane {
    display: flex;
    flex-direction: column;
    padding: 18px 20px;
    overflow-y: auto;
    max-height: calc(100dvh - var(--topbar-h) - 20px);
    gap: 14px;
    min-width: 0;
}

.reader-mobile-bar {
    display: none;
}

.reader-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    flex-wrap: wrap;
}

.reader-subject {
    margin: 0 0 6px 0;
    font-size: 1.25rem;
    text-wrap: balance;
}

.tracking-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 10px 14px;
    border-radius: 10px;
    background: var(--warn-bg);
    border: 1px solid var(--warn-border);
    font-size: 0.88rem;
}

.mail-iframe {
    width: 100%;
    min-height: 380px;
    flex: 1;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: #ffffff;
}

.attachments-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.att-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 38px;
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background: var(--bg-app);
    text-decoration: none;
    font-size: 0.85rem;
}

/* Login & Modals */
.login-card {
    max-width: 420px;
    margin: 48px auto;
    padding: 28px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; }
.field input, .field select, .field textarea {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-app);
    font-size: 0.95rem;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
    background: rgba(0, 0, 0, 0.48);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal-card {
    width: 100%;
    max-width: 700px;
    max-height: 90dvh;
    overflow-y: auto;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.modal-footer-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
}

.totp-qr { max-width: 180px; border-radius: 8px; }

.totp-input {
    padding: 10px 12px;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-app);
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    padding: 10px 0;
    border-bottom: 1px solid var(--border-color);
}

.settings-row-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.settings-row-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.status-msg { font-size: 0.86rem; color: var(--text-secondary); }
.error-msg { font-size: 0.86rem; color: var(--danger); font-weight: 500; }

/* Mobile Bottom Bar (hidden on desktop) */
.mobile-bottom-bar { display: none; }

/* Smartphone & Tablet Responsive Breakpoint */
@media (max-width: 900px) {
    :root { --topbar-h: 48px; }

    body { padding-bottom: calc(52px + env(safe-area-inset-bottom)); }

    .topbar {
        padding: 0 8px;
        gap: 6px;
        flex-wrap: wrap;
        height: auto;
        min-height: var(--topbar-h);
    }

    .brand { font-size: 0.98rem; }
    .mobile-only-btn { display: inline-flex; }

    /* Auf Mobile Search-Row sauber einklappbar */
    .search-box {
        order: 10;
        flex: 1 0 100%;
        max-width: none;
        margin: 0 0 8px 0;
        display: none;
    }
    .search-box.mobile-search-open { display: flex; }

    .app-shell, .app-shell.sidebar-collapsed {
        grid-template-columns: 1fr;
        min-height: calc(100dvh - var(--topbar-h) - 52px);
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(82vw, 300px);
        max-height: 100dvh;
        z-index: 45;
        transform: translateX(-100%);
        transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
        box-shadow: var(--shadow-md);
        padding-top: 16px;
    }
    .sidebar.mobile-open { transform: translateX(0); }

    .workspace {
        grid-template-columns: 1fr;
        margin: 0;
        border: 0;
        border-radius: 0;
        min-height: calc(100dvh - var(--topbar-h) - 52px);
    }

    /* Master-Detail: Auf Smartphone immer eine Vollbild-Ansicht statt gequetschtem Split */
    .mail-list-pane {
        border-right: 0;
        max-height: calc(100dvh - var(--topbar-h) - 52px);
    }
    .reader-pane {
        display: none;
        padding: 12px 14px 20px;
        max-height: calc(100dvh - var(--topbar-h) - 52px);
    }
    .workspace.mobile-reading .mail-list-pane { display: none; }
    .workspace.mobile-reading .reader-pane { display: flex; }

    .reader-mobile-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        padding-bottom: 8px;
        border-bottom: 1px solid var(--border-color);
    }

    .reader-header-actions { display: none; }

    .account-tab { min-height: 44px; padding: 8px 12px; }
    .nav-item { min-height: 46px; }
    .mail-row { padding: 14px; min-height: 74px; }
    .btn-secondary { min-height: 44px; }
    .btn-compact { min-height: 36px; }

    /* iOS Safari Zoom bei Fokus vermeiden */
    .field input, .field select, .field textarea, .search-box input, .totp-input {
        font-size: 16px;
    }

    .grid-2 { grid-template-columns: 1fr; }

    .login-card {
        margin: 20px 14px;
        padding: 20px;
    }

    /* Bottom Sheet Modals */
    .modal-backdrop {
        align-items: flex-end;
        padding: 0;
    }
    .modal-sheet {
        max-width: 100%;
        max-height: 92dvh;
        border-radius: 20px 20px 0 0;
        padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
        border-left: 0;
        border-right: 0;
        border-bottom: 0;
    }

    /* Mobile Bottom Tab Bar (<= 52px + safe area; kombiniert mit 48px Topbar <= 15% Viewport) */
    .mobile-bottom-bar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 30;
        height: calc(52px + env(safe-area-inset-bottom));
        padding-bottom: env(safe-area-inset-bottom);
        background: var(--bg-surface);
        border-top: 1px solid var(--border-color);
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        align-items: center;
    }

    .bottom-tab {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-height: 46px;
        padding: 4px 2px;
        border: 0;
        background: transparent;
        color: var(--text-secondary);
        font-size: 11px;
        font-weight: 500;
        cursor: pointer;
        white-space: nowrap;
    }

    .bottom-tab.active {
        color: var(--accent);
        font-weight: 600;
    }

    .bottom-tab-primary {
        color: var(--accent);
        font-weight: 600;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        transition: none !important;
        animation: none !important;
    }
}
