/* ============================================================
   CipherChat – Mobile-First Premium Design
   Dark Theme, Touch-Optimized, PWA-Ready
   ============================================================ */

/* ── CSS Variables & Reset ───────────────────────────────── */
:root {
    /* Colors */
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-tertiary: #1a1a2e;
    --bg-hover: #252540;
    --bg-active: #2a2a4a;
    --bg-message-sent: linear-gradient(135deg, #6C5CE7, #a855f7);
    --bg-message-received: #1e1e2e;
    
    --text-primary: #f0f0f5;
    --text-secondary: #8888a8;
    --text-muted: #555570;
    --text-on-accent: #ffffff;
    
    --accent: #6C5CE7;
    --accent-hover: #7b6ce8;
    --accent-light: #a855f7;
    --success: #00d68f;
    --error: #ff3d71;
    --warning: #ffaa33;
    
    --border: rgba(255,255,255,0.08);
    --border-hover: rgba(255,255,255,0.15);
    --glass: rgba(255,255,255,0.04);
    
    /* Shadows & Effects */
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.3);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
    --blur: blur(12px);
    
    /* Spacing & Sizes */
    --radius-sm: 10px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 9999px;
    
    --header-height: 64px;
    --input-height: 48px;
    --touch-target: 44px;
    
    /* Animation */
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    touch-action: manipulation;
}

/* ── Screens & Layout ───────────────────────────────────── */
.screen {
    display: none;
    height: 100vh;
    width: 100vw;
    position: relative;
}

.screen.active {
    display: flex;
}

/* ── Auth Screen ────────────────────────────────────────── */
#auth-screen {
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
}

.auth-container {
    width: 100%;
    max-width: 420px;
    padding: 32px 24px;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
}

.auth-logo {
    display: flex;
    justify-content: center;
    margin-bottom: 24px;
}

.auth-logo svg {
    filter: drop-shadow(0 4px 12px rgba(108,92,231,0.3));
}

.auth-container h1 {
    font-size: 1.75rem;
    font-weight: 700;
    text-align: center;
    background: linear-gradient(135deg, var(--accent), var(--accent-light));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 8px;
}

.auth-subtitle {
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 24px;
}

.auth-tabs {
    display: flex;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    padding: 4px;
    margin-bottom: 24px;
}

.auth-tab {
    flex: 1;
    padding: 12px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 8px;
    transition: var(--transition);
    min-height: var(--touch-target);
}

.auth-tab.active {
    background: var(--accent);
    color: var(--text-on-accent);
}

.auth-form {
    display: none;
    animation: fadeIn 0.3s ease;
}

.auth-form.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.input-group {
    margin-bottom: 16px;
}

.input-group input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 1rem;
    outline: none;
    transition: var(--transition);
    min-height: var(--touch-target);
}

.input-group input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(108,92,231,0.2);
}

.input-group input::placeholder {
    color: var(--text-muted);
}

.btn-primary {
    width: 100%;
    padding: 14px 20px;
    background: linear-gradient(135deg, var(--accent), var(--accent-light));
    color: var(--text-on-accent);
    border: none;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    min-height: var(--touch-target);
    margin-top: 8px;
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 20px rgba(108,92,231,0.4);
}

.btn-primary:active:not(:disabled) {
    transform: translateY(0);
}

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.error-msg {
    color: var(--error);
    font-size: 0.85rem;
    margin-top: 16px;
    text-align: center;
    min-height: 1.2em;
}

/* ── Main App Layout ────────────────────────────────────── */
#app-screen {
    flex-direction: row;
}

/* Sidebar */
.sidebar {
    width: 360px;
    min-width: 360px;
    max-width: 100vw;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
    transition: transform var(--transition-slow);
}

.sidebar.hidden {
    transform: translateX(-100%);
    position: absolute;
    z-index: 5;
}

.sidebar-header {
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    min-height: var(--header-height);
}

.user-badge {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.user-info .username {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-info .status {
    font-size: 0.75rem;
    color: var(--success);
}

.btn-icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    border: none;
    background: var(--glass);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
}

.btn-icon:hover {
    background: var(--bg-hover);
}

.btn-icon:active {
    transform: scale(0.98);
}

/* Search Box */
.search-box {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}

.search-box svg {
    flex-shrink: 0;
}

.search-box input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 0.9rem;
    outline: none;
    min-height: var(--touch-target);
}

.search-box input::placeholder {
    color: var(--text-muted);
}

/* Conversation List */
.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    background: transparent;
    border: none;
    color: inherit;
    text-align: left;
    width: 100%;
    min-height: var(--touch-target);
}

.conv-item:hover {
    background: var(--bg-hover);
}

.conv-item.active {
    background: var(--bg-active);
}

.conv-item:active {
    transform: scale(0.99);
}

.conv-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.conv-name {
    font-weight: 500;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-username {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-time {
    font-size: 0.75rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.empty-state,
.loading,
.error {
    padding: 24px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.error {
    color: var(--error);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

.btn-text {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.85rem;
    transition: var(--transition);
    padding: 8px 0;
    min-height: var(--touch-target);
    min-width: var(--touch-target);
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-text:hover {
    color: var(--error);
}

/* ── Chat Area ──────────────────────────────────────────── */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    position: relative;
    min-width: 0;
}

/* Chat Placeholder */
.chat-placeholder {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 16px;
    color: var(--text-secondary);
    padding: 24px;
}

.chat-placeholder svg {
    opacity: 0.5;
}

.chat-placeholder h2 {
    color: var(--text-primary);
    font-size: 1.3rem;
    font-weight: 600;
}

.chat-placeholder p {
    max-width: 280px;
    line-height: 1.4;
}

/* Chat Container */
.chat-container {
    display: none;
    flex-direction: column;
    height: 100%;
}

/* Chat Header */
.chat-header {
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: var(--header-height);
}

.chat-header h3 {
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-status {
    font-size: 0.75rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Messages Container */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
    width: 4px;
}

.messages::-webkit-scrollbar-track {
    background: transparent;
}

.messages::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 2px;
}

/* Message Bubbles */
.message {
    display: flex;
    gap: 8px;
    max-width: 85%;
    animation: messageIn 0.25s ease;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message.received {
    align-self: flex-start;
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: 100%;
}

.message-text {
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.4;
    word-break: break-word;
    white-space: pre-wrap;
}

.message.sent .message-text {
    background: var(--bg-message-sent);
    color: var(--text-on-accent);
    border-bottom-right-radius: 6px;
}

.message.received .message-text {
    background: var(--bg-message-received);
    color: var(--text-primary);
    border-bottom-left-radius: 6px;
}

.message-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    padding: 0 4px;
}

.message.sent .message-time {
    text-align: right;
}

/* Message Form */
.message-form {
    padding: 12px 16px 16px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
}

.input-wrapper {
    display: flex;
    align-items: center;
    background: var(--bg-tertiary);
    border-radius: 24px;
    padding: 4px 4px 4px 16px;
    border: 1px solid var(--border);
    transition: var(--transition);
    gap: 8px;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(108,92,231,0.15);
}

.input-wrapper input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 0.95rem;
    outline: none;
    padding: 10px 0;
    min-height: 36px;
}

.input-wrapper input::placeholder {
    color: var(--text-muted);
}

.input-wrapper input:disabled {
    opacity: 0.6;
}

.btn-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--accent), var(--accent-light));
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
}

.btn-send:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 16px rgba(108,92,231,0.4);
}

.btn-send:active:not(:disabled) {
    transform: scale(1);
}

.btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ── Modal ─────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: var(--blur);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 16px;
    animation: fadeIn 0.2s ease;
}

.modal {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 420px;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border);
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 56px;
}

.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.btn-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 8px;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.btn-close:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.search-results {
    max-height: 40vh;
    overflow-y: auto;
    padding: 8px;
}

.search-result-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    background: transparent;
    border: none;
    color: inherit;
    text-align: left;
    width: 100%;
    min-height: var(--touch-target);
}

.search-result-item:hover {
    background: var(--bg-hover);
}

.search-result-item:active {
    transform: scale(0.99);
}

.search-user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.search-user-info .name {
    font-weight: 500;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-user-info .username {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ── Avatar Component ───────────────────────────────────── */
.avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
    color: white;
    flex-shrink: 0;
    user-select: none;
    text-transform: uppercase;
}

.avatar.sm {
    width: 32px;
    height: 32px;
    font-size: 0.8rem;
}

.avatar.lg {
    width: 52px;
    height: 52px;
    font-size: 1.3rem;
}

/* ── Responsive / Mobile ───────────────────────────────── */
@media (max-width: 768px) {
    /* Sidebar wird standardmäßig ausgeblendet */
    .sidebar {
        position: absolute;
        z-index: 20;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
    }
    
    .sidebar:not(.hidden) {
        transform: translateX(0);
    }
    
    /* Back Button im Chat-Header immer sichtbar */
    #btn-back {
        display: flex !important;
    }
    
    /* Chat nimmt volle Breite */
    .chat-area {
        width: 100%;
    }
    
    /* Message-Bubbles etwas schmaler für bessere Lesbarkeit */
    .message {
        max-width: 90%;
    }
    
    /* Größere Touch-Targets */
    .auth-tab,
    .btn-primary,
    .conv-item,
    .search-result-item {
        min-height: 48px;
    }
    
    /* Input-Fields größer für Mobile-Tastatur */
    .input-group input,
    .input-wrapper input {
        font-size: 16px; /* Verhindert Zoom auf iOS */
    }
    
    /* Modal volle Höhe auf kleinen Screens */
    .modal {
        max-height: 90vh;
        margin: 8px;
    }
}

/* ── Tablet / Desktop Enhancements ──────────────────────── */
@media (min-width: 769px) {
    /* Sidebar immer sichtbar */
    .sidebar {
        transform: none !important;
        position: relative;
    }
    
    /* Back Button nur auf Mobile */
    #btn-back {
        display: none !important;
    }
    
    /* Hover-Effekte nur auf Desktop */
    .conv-item:hover,
    .search-result-item:hover,
    .btn-icon:hover {
        transform: translateX(2px);
    }
}

/* ── Reduced Motion ────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    html {
        scroll-behavior: auto;
    }
}

/* ── High Contrast Mode ────────────────────────────────── */
@media (prefers-contrast: high) {
    :root {
        --border: rgba(255,255,255,0.3);
        --text-muted: #aaa;
    }
    
    .btn-primary {
        border: 2px solid var(--text-on-accent);
    }
}

/* ── Utility Classes ───────────────────────────────────── */
.hidden { display: none !important; }
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    white-space: nowrap;
    border: 0;
}

/* ── Print Styles ──────────────────────────────────────── */
@media print {
    body {
        background: white;
        color: black;
    }
    
    .sidebar,
    .message-form,
    .btn-icon {
        display: none !important;
    }
}