/* ── CSS-Variablen ──────────────────────────────────────────────────────────── */
:root {
    --blue          : #007AFF;
    --bg            : #F2F2F7;
    --surface       : #FFFFFF;
    --border        : rgba(60,60,67,0.18);
    --text          : #1C1C1E;
    --text-2        : #8E8E93;
    --bubble-out    : #007AFF;
    --bubble-in     : #E5E5EA;
    --bubble-out-fg : #FFFFFF;
    --bubble-in-fg  : #1C1C1E;
    --header-bg     : rgba(242,242,247,0.85);
    --safe-t        : env(safe-area-inset-top,    0px);
    --safe-b        : env(safe-area-inset-bottom, 0px);
    --safe-l        : env(safe-area-inset-left,   0px);
    --safe-r        : env(safe-area-inset-right,  0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --blue          : #0A84FF;
        --bg            : #000000;
        --surface       : #1C1C1E;
        --border        : rgba(84,84,88,0.65);
        --text          : #FFFFFF;
        --text-2        : #8E8E93;
        --bubble-out    : #0A84FF;
        --bubble-in     : #2C2C2E;
        --bubble-out-fg : #FFFFFF;
        --bubble-in-fg  : #FFFFFF;
        --header-bg     : rgba(28,28,30,0.85);
    }
}

/* ── Reset & Base ────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body            { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
    font-family  : -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, sans-serif;
    background   : var(--bg);
    color        : var(--text);
    font-size    : 16px;
    line-height  : 1.4;
    -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; }
input, button { font-family: inherit; }

/* ── Views ───────────────────────────────────────────────────────────────────── */
.view {
    position        : fixed;
    inset           : 0;
    display         : flex;
    flex-direction  : column;
    background      : var(--bg);
    padding-left    : var(--safe-l);
    padding-right   : var(--safe-r);
}
.view.hidden { display: none; }

/* ── Header ──────────────────────────────────────────────────────────────────── */
.nav-header {
    display         : flex;
    align-items     : center;
    justify-content : space-between;
    padding         : calc(var(--safe-t) + 12px) 16px 12px;
    background      : var(--header-bg);
    backdrop-filter : blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom   : 0.5px solid var(--border);
    flex-shrink     : 0;
    min-height      : calc(var(--safe-t) + 54px);
}
.nav-header h2 { font-size: 20px; font-weight: 700; }

.icon-btn {
    background : none;
    border     : none;
    color      : var(--blue);
    padding    : 6px;
    display    : flex;
    align-items: center;
    border-radius: 8px;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active { opacity: .6; }

.chat-header { gap: 4px; justify-content: flex-start; }
.chat-partner-name { font-size: 17px; font-weight: 600; margin-left: 4px; }

/* ── Auth ────────────────────────────────────────────────────────────────────── */
#view-auth {
    justify-content : center;
    align-items     : center;
    padding         : calc(var(--safe-t) + 24px) 24px calc(var(--safe-b) + 24px);
    overflow-y      : auto;
    -webkit-overflow-scrolling: touch;
}

.auth-inner {
    width       : 100%;
    max-width   : 340px;
    display     : flex;
    flex-direction: column;
    align-items : center;
    gap         : 20px;
}

.auth-logo svg {
    width        : 80px;
    height       : 80px;
    border-radius: 20px;
    box-shadow   : 0 6px 24px rgba(0,122,255,.35);
    display      : block;
}
.auth-logo + h1 { font-size: 28px; font-weight: 700; }
.auth-sub       { color: var(--text-2); font-size: 15px; text-align: center; }

.tabs {
    display       : flex;
    width         : 100%;
    border        : 1px solid var(--border);
    border-radius : 12px;
    overflow      : hidden;
}
.tab {
    flex       : 1;
    padding    : 11px;
    background : none;
    border     : none;
    color      : var(--text-2);
    font-size  : 15px;
    transition : all .2s;
}
.tab.active { background: var(--blue); color: #fff; font-weight: 600; }

form { width: 100%; display: flex; flex-direction: column; gap: 12px; }

.field input {
    width         : 100%;
    padding       : 14px 16px;
    border        : 1px solid var(--border);
    border-radius : 12px;
    background    : var(--surface);
    color         : var(--text);
    font-size     : 16px;
    outline       : none;
    transition    : border-color .2s;
    -webkit-appearance: none;
}
.field input:focus { border-color: var(--blue); }

.btn-primary {
    padding       : 15px;
    background    : var(--blue);
    color         : #fff;
    border        : none;
    border-radius : 12px;
    font-size     : 17px;
    font-weight   : 600;
    transition    : opacity .2s;
}
.btn-primary:active   { opacity: .8; }
.btn-primary:disabled { opacity: .45; }

.auth-error {
    background    : #FF3B30;
    color         : #fff;
    padding       : 12px 16px;
    border-radius : 12px;
    font-size     : 14px;
    width         : 100%;
    text-align    : center;
}
.auth-error.hidden { display: none; }

/* ── Gesprächsliste ──────────────────────────────────────────────────────────── */
#conversation-list {
    flex                   : 1;
    overflow-y             : auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior    : contain;
}

.conv-item {
    display      : flex;
    align-items  : center;
    gap          : 12px;
    padding      : 14px 16px;
    background   : var(--surface);
    border-bottom: 0.5px solid var(--border);
    cursor       : pointer;
    transition   : background .1s;
}
.conv-item:active { background: var(--bg); }

.avatar {
    width          : 48px;
    height         : 48px;
    border-radius  : 50%;
    background     : var(--blue);
    display        : flex;
    align-items    : center;
    justify-content: center;
    color          : #fff;
    font-size      : 20px;
    font-weight    : 700;
    flex-shrink    : 0;
    user-select    : none;
}
.avatar-sm { width: 40px; height: 40px; font-size: 16px; }

.conv-info { flex: 1; min-width: 0; }
.conv-name    { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: 14px; color: var(--text-2); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time    { font-size: 12px; color: var(--text-2); flex-shrink: 0; }

.empty-state {
    flex           : 1;
    display        : flex;
    align-items    : center;
    justify-content: center;
    text-align     : center;
    color          : var(--text-2);
    padding        : 40px 24px;
    font-size      : 15px;
    line-height    : 1.7;
}

/* ── Chat ────────────────────────────────────────────────────────────────────── */
.message-list {
    flex                   : 1;
    overflow-y             : auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior    : contain;
    padding                : 12px 16px;
    display                : flex;
    flex-direction         : column;
    gap                    : 4px;
}

.msg-bubble {
    max-width    : 72%;
    padding      : 10px 14px;
    border-radius: 18px;
    font-size    : 16px;
    word-break   : break-word;
    hyphens      : auto;
}
.msg-out {
    align-self          : flex-end;
    background          : var(--bubble-out);
    color               : var(--bubble-out-fg);
    border-bottom-right-radius: 4px;
}
.msg-in {
    align-self         : flex-start;
    background         : var(--bubble-in);
    color              : var(--bubble-in-fg);
    border-bottom-left-radius: 4px;
}
.msg-time {
    display    : block;
    font-size  : 11px;
    opacity    : .6;
    margin-top : 4px;
    text-align : right;
}
.msg-error { font-style: italic; opacity: .65; }

/* Eingabebereich */
.input-area {
    display         : flex;
    align-items     : center;
    gap             : 8px;
    padding         : 8px 16px calc(var(--safe-b) + 8px);
    background      : var(--header-bg);
    backdrop-filter : blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top      : 0.5px solid var(--border);
    flex-shrink     : 0;
}

#msg-input {
    flex          : 1;
    padding       : 10px 16px;
    border        : 1px solid var(--border);
    border-radius : 22px;
    background    : var(--surface);
    color         : var(--text);
    font-size     : 16px;
    outline       : none;
    transition    : border-color .2s;
    -webkit-appearance: none;
    resize        : none;
}
#msg-input:focus { border-color: var(--blue); }

.send-btn {
    width          : 38px;
    height         : 38px;
    border-radius  : 50%;
    background     : var(--blue);
    border         : none;
    display        : flex;
    align-items    : center;
    justify-content: center;
    color          : #fff;
    flex-shrink    : 0;
    transition     : opacity .2s;
}
.send-btn:active { opacity: .75; }
.send-btn svg    { width: 18px; height: 18px; }

/* ── Suchmodal ───────────────────────────────────────────────────────────────── */
#view-search {
    position   : fixed;
    inset      : 0;
    background : rgba(0,0,0,.45);
    display    : flex;
    align-items: flex-end;
    z-index    : 100;
    padding    : 0;          /* überschreibt .view-Padding */
}
#view-search.hidden { display: none; }

.modal-sheet {
    background     : var(--surface);
    border-radius  : 16px 16px 0 0;
    width          : 100%;
    max-height     : 75vh;
    display        : flex;
    flex-direction : column;
    padding-bottom : var(--safe-b);
}

.modal-header {
    display         : flex;
    align-items     : center;
    justify-content : space-between;
    padding         : 16px 16px 8px;
    border-bottom   : 0.5px solid var(--border);
}
.modal-header h3 { font-size: 18px; font-weight: 700; }

#search-input {
    margin        : 12px 16px;
    padding       : 12px 16px;
    border        : 1px solid var(--border);
    border-radius : 12px;
    background    : var(--bg);
    color         : var(--text);
    font-size     : 16px;
    outline       : none;
    -webkit-appearance: none;
    flex-shrink   : 0;
}
#search-input:focus { border-color: var(--blue); }

#search-results {
    overflow-y             : auto;
    -webkit-overflow-scrolling: touch;
    flex                   : 1;
}

.search-item {
    display      : flex;
    align-items  : center;
    gap          : 12px;
    padding      : 12px 16px;
    cursor       : pointer;
    border-bottom: 0.5px solid var(--border);
    transition   : background .1s;
}
.search-item:active { background: var(--bg); }
.search-item span  { font-size: 16px; font-weight: 500; }

.search-empty {
    padding    : 24px;
    text-align : center;
    color      : var(--text-2);
    font-size  : 15px;
}

/* ── Toast ───────────────────────────────────────────────────────────────────── */
.toast {
    position   : fixed;
    bottom     : calc(var(--safe-b) + 90px);
    left       : 50%;
    transform  : translateX(-50%);
    background : rgba(0,0,0,.8);
    color      : #fff;
    padding    : 10px 20px;
    border-radius: 20px;
    font-size  : 14px;
    z-index    : 200;
    white-space: nowrap;
    max-width  : 80vw;
    overflow   : hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}
.toast.hidden { display: none; }

/* ── Nachrichten-Aktionsmenü ─────────────────────────────────────────────────── */
.action-sheet {
    position  : fixed;
    inset     : 0;
    background: rgba(0,0,0,.45);
    z-index   : 300;
    display   : flex;
    align-items: flex-end;
}
.action-sheet.hidden { display: none; }
.action-sheet-inner {
    width         : 100%;
    padding       : 8px 16px calc(var(--safe-b) + 16px);
    background    : var(--surface);
    border-radius : 14px 14px 0 0;
    display       : flex;
    flex-direction: column;
    gap           : 8px;
}
.action-btn {
    width         : 100%;
    padding       : 15px;
    border        : none;
    border-radius : 12px;
    background    : var(--bg);
    color         : var(--text);
    font-size     : 17px;
    text-align    : center;
    transition    : opacity .15s;
}
.action-btn:active { opacity: .6; }
.action-btn.action-danger { color: #FF3B30; }
.action-btn.hidden { display: none; }

/* ── PWA-Versionsanzeige ─────────────────────────────────────────────────────── */
.pwa-version {
    margin-top : 20px;
    font-size  : 12px;
    color      : var(--text-2);
    text-align : center;
}

/* ── WebRTC-Anrufmodal ─────────────────────────────────────────────────────── */
.call-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 24px;
    backdrop-filter: blur(6px);
}
.call-modal.hidden { display: none; }

.call-card {
    background: var(--surface);
    border-radius: 24px;
    padding: 40px 24px 32px;
    max-width: 360px;
    width: 100%;
    text-align: center;
    box-shadow: 0 12px 60px rgba(0, 0, 0, 0.6);
}
.call-avatar {
    width: 96px;
    height: 96px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    font-size: 40px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
}
.call-name {
    font-size: 22px;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text);
}
.call-status {
    font-size: 15px;
    color: var(--text-2);
    margin-bottom: 4px;
}
.call-timer {
    font-size: 14px;
    color: var(--text-2);
    font-variant-numeric: tabular-nums;
    min-height: 18px;
    margin-bottom: 24px;
}
.call-actions {
    display: flex;
    gap: 24px;
    justify-content: center;
}
.call-btn {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: none;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform .12s, opacity .12s;
}
.call-btn:active { transform: scale(0.92); opacity: 0.85; }
.call-btn svg { width: 28px; height: 28px; }
.call-btn-accept { background: #34C759; }
.call-btn-decline { background: #FF3B30; }
.call-btn-mute { background: #555; }
.call-btn-mute.is-muted { background: var(--primary); }
.call-btn.hidden { display: none; }

/* Call-Button im Chat-Header */
.chat-header #btn-call { margin-left: auto; }
