:root {
    --bg-main: #1a1a2e;
    --bg-card: #22223b;
    --bg-input: #2a2a3e;
    --bg-hover: #2e2e45;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0b0;
    --accent: #4a6fa5;
    --accent-light: #5b8cc9;
    --border: #3a3a50;
    --danger: #e74c3c;
    --success: #27ae60;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-main);
    color: var(--text-primary);
    min-height: 100vh;
}

/* Header */
.header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
}
.header h1 {
    font-size: 1.2rem;
    font-weight: 600;
    flex: 1;
    min-width: 0;            /* erlaubt Ellipsis im Flex-Row statt Layout-Bruch */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.header-actions { display: flex; gap: 0.75rem; align-items: center; }
.search-wrap {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}
.header-logout {
    flex-shrink: 0;
    margin-left: auto;  /* drückt Logout immer ganz nach rechts */
}
.header-actions select,
.header-actions input[type="search"] {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.4rem 0.6rem;
    font-size: 0.85rem;
}
.header-actions input[type="search"] { min-width: 12rem; }

/* Scope-Badges in der Mail-Liste (Such-Treffer) */
.mail-scope-badge {
    display: inline-block;
    font-size: 0.65rem;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    margin-right: 0.4rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    vertical-align: middle;
}
.mail-scope-badge.live    { background: #1e3a5f; color: #b6d4ff; }
.mail-scope-badge.archive { background: #4a3a5f; color: #d4b6ff; }
.mail-scope-badge.trash   { background: #5f3a3a; color: #ffb6b6; }
.btn {
    padding: 0.4rem 0.8rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    background: var(--accent);
    color: #fff;
}
.btn:hover { background: var(--accent-light); }

/* Primary-Button: prominenter, leuchtende Farbe */
.btn.btn-primary {
    background: var(--accent-light);
    color: #fff;
    font-weight: 600;
    padding: 0.5rem 1rem;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.btn.btn-primary:hover { background: #6ba0d4; }
.btn.btn-primary:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}
.refresh-icon {
    display: inline-block;
    font-size: 1.1rem;
    line-height: 1;
}
.btn.btn-primary.is-loading .refresh-icon {
    animation: refresh-spin 0.8s linear infinite;
}
@keyframes refresh-spin {
    to { transform: rotate(360deg); }
}
.btn-icon {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0.3rem;
}
.btn-icon:hover { color: var(--text-primary); }

/* Layout */
.main-layout {
    display: flex;
    height: calc(100vh - 52px);
}

/* Folder sidebar */
.folder-sidebar {
    width: 240px;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
    background: var(--bg-card);
    padding: 0.5rem 0;
}
.section-group {
    margin-bottom: 1rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid var(--border);
}
.section-group:last-child { border-bottom: none; }
.section-header {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.75rem 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
}
.section-icon { font-size: 0.9rem; }
.folder-section { margin-bottom: 0.25rem; }
.folder-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-primary);
    border-left: 3px solid transparent;
}
.folder-item:hover { background: var(--bg-hover); }
.folder-item.active {
    background: var(--bg-hover);
    border-left-color: var(--accent-light);
}
.folder-icon { width: 1.1rem; text-align: center; }
.folder-toggle {
    width: 0.9rem;
    text-align: center;
    color: var(--text-secondary);
    transition: transform 0.2s;
    display: inline-block;
}
.folder-toggle.open { transform: rotate(90deg); }
.folder-label {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.folder-count {
    font-size: 0.75rem;
    color: var(--text-secondary);
    background: var(--bg-input);
    padding: 0.1rem 0.45rem;
    border-radius: 10px;
    min-width: 1.5rem;
    text-align: center;
}
.folder-children {
    display: none;
    padding-left: 1.25rem;
}
.folder-children.open { display: block; }
.folder-item.child {
    padding: 0.35rem 0.75rem;
    font-size: 0.82rem;
    color: var(--text-secondary);
}
.folder-item.child .folder-label { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.folder-item.child.active { color: var(--text-primary); }
.folder-item.grandchild { padding-left: 1.75rem; font-size: 0.78rem; }
.folder-children .folder-children { padding-left: 0.75rem; }
.folder-trash-btn {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0 0.25rem;
    font-size: 0.9rem;
    transition: color 0.15s, opacity 0.15s;
}
.folder-item:hover .folder-trash-btn { opacity: 1; }
.folder-trash-btn:hover { color: var(--danger); }
.folder-export-btn {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0 0.25rem;
    font-size: 0.9rem;
    transition: color 0.15s, opacity 0.15s;
}
.folder-item:hover .folder-export-btn { opacity: 1; }
.folder-export-btn:hover { color: var(--accent-light); }
.folder-year-hint {
    display: block;
    padding: 0 0.75rem 0.4rem 1.9rem;
    font-size: 0.68rem;
    color: var(--danger);
    line-height: 1.3;
}
.mail-trash-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1rem;
    padding: 0.25rem 0.4rem;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
}
.mail-item:hover .mail-trash-btn { opacity: 1; }
.mail-trash-btn:hover { background: var(--bg-input); color: var(--danger); }
.mail-item-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.5rem;
}

/* Mail list */
.mail-list-wrap {
    width: 400px;
    border-right: 1px solid var(--border);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}
.mail-list-toolbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-card);
    gap: 0.5rem;
}
.mail-list-toolbar .btn-icon {
    font-size: 0.78rem;
    color: var(--text-secondary);
    padding: 0.25rem 0.55rem;
    border: 1px solid var(--border);
    border-radius: 5px;
}
.mail-list-toolbar .btn-icon:hover { color: var(--text-primary); background: var(--bg-hover); }
.mail-list-toolbar #selectModeBtn { font-weight: 700; letter-spacing: 0.05em; }
.mail-list-toolbar #selectModeBtn.active {
    color: #fff;
    background: var(--accent);
    border-color: var(--accent-light);
}
.mail-list-toolbar .bulk-delete-btn {
    color: #fff;
    background: var(--danger);
    border-color: var(--danger);
}
.mail-list-toolbar .bulk-delete-btn:hover { background: #c0392b; color: #fff; }
.mail-list-toolbar .bulk-delete-btn[hidden] { display: none; }
.mail-list {
    flex: 1;
    overflow-y: auto;
}
.mail-row {
    position: relative;
    overflow: hidden;
    border-bottom: 1px solid var(--border);
    /* prevent text-selection while swiping */
    user-select: none;
    -webkit-user-select: none;
}
.swipe-action {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 80px;
    background: var(--danger);
    color: #fff;
    font-size: 1.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s;
}
.swipe-action.left { left: 0; }
.swipe-action.right { right: 0; }
.mail-row.swiped-right .swipe-action.left,
.mail-row.swiped-left  .swipe-action.right {
    opacity: 1;
    pointer-events: auto;
}
.mail-item {
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background 0.15s;
    background: var(--bg-main);
    position: relative;
    z-index: 1;
}
.mail-item:hover { background: var(--bg-hover); }
.mail-item.selected {
    outline: 1.5px solid var(--danger);
    outline-offset: -1.5px;
}
.mail-list.select-mode .mail-item { cursor: pointer; }
.mail-list.select-mode .mail-trash-btn { display: none; }
.mail-item.active { background: var(--accent); color: #fff; }
.mail-item.active .mail-meta { color: rgba(255,255,255,0.7); }
.mail-item.unread .mail-sender { font-weight: 700; }
.mail-sender {
    font-size: 0.9rem;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.mail-subject {
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.mail-meta {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 2px;
    display: flex;
    justify-content: space-between;
}
.mail-account {
    font-size: 0.7rem;
    color: var(--accent-light);
    margin-top: 2px;
}

/* Mail detail */
.mail-detail {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
}
.mail-detail.empty {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}
.detail-header { margin-bottom: 1.5rem; }
.detail-subject { font-size: 1.3rem; font-weight: 600; margin-bottom: 0.5rem; }
.detail-from { font-size: 0.9rem; color: var(--text-secondary); }
.detail-to { font-size: 0.85rem; color: var(--text-secondary); margin-top: 0.25rem; }
.detail-date { font-size: 0.8rem; color: var(--text-secondary); margin-top: 0.25rem; }
.detail-body {
    margin-top: 1rem;
    line-height: 1.6;
}
.detail-body iframe {
    width: 100%;
    border: none;
    border-radius: 8px;
    background: #fff;
    min-height: 400px;
}
.detail-attachments {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}
.attachment-chip {
    display: inline-block;
    padding: 0.3rem 0.6rem;
    background: var(--bg-input);
    border-radius: 6px;
    font-size: 0.8rem;
    margin-right: 0.5rem;
    margin-bottom: 0.25rem;
    color: var(--text-primary);
    text-decoration: none;
    transition: background 0.15s, color 0.15s;
}
a.attachment-chip:hover { background: var(--accent); color: #fff; }

.attachment-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-top: 0.4rem;
}
.attachment-list .compose-attachment {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.55rem;
    background: var(--bg-input);
    border-radius: 6px;
    font-size: 0.8rem;
}
.attachment-list .remove-attachment {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 0 0.1rem;
}
.attachment-list .remove-attachment:hover { color: var(--danger); }

/* Loading */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3rem;
    color: var(--text-secondary);
}
.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-right: 0.75rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    width: min(520px, 92vw);
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.9rem 1.1rem;
    border-bottom: 1px solid var(--border);
}
.modal-title { font-weight: 600; font-size: 1rem; }
.modal-body { padding: 1.1rem; }
.modal-hint {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: 1rem;
}
.modal-footer {
    margin-top: 1rem;
    display: flex;
    gap: 0.75rem;
    align-items: center;
}
.form-row {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    margin-bottom: 1rem;
}
.form-row .form-label {
    font-size: 0.85rem;
    color: var(--text-primary);
}
.form-row input[type="number"],
.form-row input[type="text"],
.form-row textarea,
.form-row select {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem 0.65rem;
    font-size: 0.95rem;
    font-family: inherit;
}
.form-row.form-row-check {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}
.form-row.form-row-check input[type="checkbox"] {
    width: 1.05rem;
    height: 1.05rem;
    accent-color: var(--accent-light);
    cursor: pointer;
}
.form-row.form-row-check .form-label { cursor: pointer; }
.form-row.form-row-check .form-hint { flex-basis: 100%; }
.form-sep { border: none; border-top: 1px solid var(--border); margin: 0.25rem 0 1rem; }
.form-row input[type="number"] { max-width: 12rem; }
.form-row textarea { resize: vertical; min-height: 8rem; }
.form-row-grow textarea { min-height: 14rem; }
.modal.modal-wide { width: min(720px, 95vw); }

/* Dashboard */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.dashboard-card {
    background: var(--bg-input);
    border-radius: 8px;
    padding: 1rem;
    border-left: 3px solid var(--accent);
}
.dashboard-card-title {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    margin-bottom: 0.6rem;
}
.dashboard-card-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 0.3rem;
    font-size: 0.88rem;
}
.dashboard-card-row .v { color: var(--accent-light); font-weight: 600; }
.dashboard-section { margin-bottom: 1.5rem; }
.dashboard-section h3 {
    font-size: 0.9rem;
    margin-bottom: 0.6rem;
    color: var(--text-primary);
}
.dashboard-meta {
    color: var(--text-secondary);
    font-size: 0.8rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
}

/* Reply/Forward toolbar in Mail-Detail */
.detail-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}
.detail-actions .btn { padding: 0.4rem 1rem; }
.form-hint {
    font-size: 0.78rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

/* iOS „Zum Home-Bildschirm"-Hinweis (Safari kann kein Auto-Prompt) */
.ios-install-hint {
    position: fixed;
    left: 0.6rem;
    right: 0.6rem;
    bottom: calc(0.6rem + env(safe-area-inset-bottom));
    z-index: 60;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.7rem 0.9rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    font-size: 0.85rem;
    color: var(--text-primary);
}
.ios-install-hint[hidden] { display: none; }
.ios-install-hint span { flex: 1; line-height: 1.35; }

/* Sidebar-Backdrop (nur auf Mobile sichtbar wenn Drawer offen) */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 52px 0 0 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 20;
}

/* Desktop default: Sidebar normal, mobile-only-Buttons aus */
.mobile-only { display: none; }

/* Responsive */
@media (max-width: 768px) {
    .header {
        padding: 0.5rem 0.75rem;
        gap: 0.4rem;
        flex-wrap: wrap;
    }
    .header h1 { font-size: 0.8rem; }  /* zwischen Burger und Logout */
    .mobile-only { display: inline-flex; }

    /* Zeile 1: Burger | h1 (flex:1) | Logout */
    .header-logout { order: 2; margin-left: auto; }

    /* Zeile 2+ : header-actions vollbreit */
    .header-actions {
        order: 99;
        flex-basis: 100%;
        flex-wrap: wrap;
        gap: 0.35rem;
        justify-content: flex-start;
    }
    .header-actions select {
        max-width: 9rem;
        font-size: 0.75rem;
        padding: 0.3rem 0.4rem;
    }
    .header-actions .btn {
        padding: 0.35rem 0.55rem;
        font-size: 0.78rem;
    }
    .header-actions .btn-icon { font-size: 1rem; padding: 0.3rem; }

    /* Suche in eigene Zeile, voller Platz */
    .search-wrap {
        order: 99;
        flex-basis: 100%;
        display: flex;
        align-items: center;
        gap: 0.4rem;
    }
    .header-actions input[type="search"] {
        flex: 1;
        min-width: 0;
        max-width: none;
        font-size: 0.9rem;
        padding: 0.45rem 0.6rem;
    }

    /* Sidebar als Drawer */
    .folder-sidebar {
        position: fixed;
        top: 52px;
        left: 0;
        bottom: 0;
        width: 80vw;
        max-width: 280px;
        z-index: 30;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.4);
    }
    .folder-sidebar.open { transform: translateX(0); }
    .sidebar-backdrop.open { display: block; }

    /* Mail-Liste + Detail: Single-Pane */
    .mail-list-wrap { width: 100%; border-right: none; }
    .mail-list { width: 100%; border-right: none; }
    .main-layout.detail-open .mail-list-wrap { display: none; }
    .mail-detail { display: none; }
    .mail-detail.visible { display: block; }
    .main-layout.detail-open .mail-list { display: none; }
    .main-layout.detail-open .mail-detail { display: block; }

    /* Modals: fullscreen */
    .modal,
    .modal.modal-wide {
        width: 100vw;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
    }
    .modal-body { padding: 0.9rem; }
    .form-row textarea { min-height: 10rem; }
    .form-row-grow textarea { min-height: 12rem; }

    /* Detail-Actions buttons enger */
    .detail-actions .btn { padding: 0.35rem 0.6rem; font-size: 0.8rem; }
    .detail-body iframe { min-height: 250px; }

    /* Mobile: Mülleimer-Icon in Mail-Item ausblenden — Swipe ist die Geste */
    .mail-trash-btn { display: none; }
}

@media (max-width: 480px) {
    .header h1 { font-size: 0.7rem; }  /* Branding bleibt auch auf dem Handy sichtbar */
    .header-actions select { flex: 1; max-width: none; }
    .btn.btn-primary .refresh-label { display: none; }
    .btn.btn-primary { padding: 0.45rem 0.7rem; }
    .btn.btn-primary .refresh-icon { font-size: 1.25rem; }
}
