/* ═══════════════════════════════════════════════════════════════
   Hermes Web Chat - Dark Theme CSS
   Compact, Readable, RTL Persian
   ═══════════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800;900&display=swap');

/* ─── CSS Variables ───────────────────────────────────────────── */
:root {
    --bg-primary: #0c0c1d;
    --bg-secondary: #111128;
    --bg-tertiary: #191938;
    --bg-glass: rgba(18, 18, 48, 0.7);
    --bg-glass-hover: rgba(28, 28, 60, 0.8);

    --gradient-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --gradient-secondary: linear-gradient(135deg, #764ba2 0%, #f093fb 100%);
    --gradient-bg: linear-gradient(160deg, #0c0c1d 0%, #151535 50%, #0c0c1d 100%);

    --text-primary: #e4e4f0;
    --text-secondary: #9d9dc0;
    --text-muted: #6565a0;
    --text-accent: #a78bfa;

    --border-color: rgba(100, 110, 230, 0.15);
    --border-glow: rgba(100, 110, 230, 0.35);

    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-glow: 0 0 16px rgba(100, 110, 230, 0.2);

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 22px;

    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;

    --max-chat-width: 720px;
}

/* ─── Reset ───────────────────────────────────────────────────── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: 'Vazirmatn', 'Tahoma', sans-serif;
    background: var(--gradient-bg);
    color: var(--text-primary);
    direction: rtl;
    text-align: right;
    min-height: 100vh;
    overflow-x: hidden;
    line-height: 1.8;
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    background:
        radial-gradient(circle at 25% 20%, rgba(100, 126, 234, 0.06) 0%, transparent 50%),
        radial-gradient(circle at 75% 80%, rgba(118, 75, 162, 0.06) 0%, transparent 50%);
    pointer-events: none;
    z-index: 0;
}

a {
    color: var(--text-accent);
    text-decoration: none;
}
a:hover { color: #c4b5fd; }

/* ─── Scrollbar ───────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(100, 126, 234, 0.3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(100, 126, 234, 0.5); }

/* ═══════════════════════════════════════════════════════════════
   LOGIN PAGE
   ═══════════════════════════════════════════════════════════════ */

.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
    position: relative;
    z-index: 1;
}

.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--bg-glass);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);
    padding: 40px 32px;
    box-shadow: var(--shadow-md), var(--shadow-glow);
    animation: cardIn 0.5s ease;
}

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

.login-logo {
    text-align: center;
    margin-bottom: 32px;
}

.login-logo .logo-icon {
    width: 60px;
    height: 60px;
    background: var(--gradient-primary);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    margin-bottom: 14px;
    box-shadow: var(--shadow-glow);
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

.login-logo h1 {
    font-size: 1.35rem;
    font-weight: 700;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 4px;
}

.login-logo p {
    color: var(--text-secondary);
    font-size: 0.82rem;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-input {
    width: 100%;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: 'Vazirmatn', sans-serif;
    font-size: 0.9rem;
    transition: border-color var(--transition-normal), box-shadow var(--transition-normal);
    direction: rtl;
}

.form-input:focus {
    outline: none;
    border-color: var(--border-glow);
    box-shadow: 0 0 0 3px rgba(100, 126, 234, 0.1);
}

.form-input::placeholder { color: var(--text-muted); }

.btn-primary {
    width: 100%;
    padding: 12px 20px;
    margin-top: 8px;
    background: var(--gradient-primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-family: 'Vazirmatn', sans-serif;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 0 24px rgba(100, 126, 234, 0.3);
}

.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.login-error {
    background: rgba(239, 68, 68, 0.12);
    border: 1px solid rgba(239, 68, 68, 0.25);
    color: #fca5a5;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    margin-bottom: 14px;
    display: none;
    text-align: center;
}

.login-error.show {
    display: block;
    animation: shake 0.3s ease;
}

@keyframes shake {
    25% { transform: translateX(-6px); }
    75% { transform: translateX(6px); }
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ═══════════════════════════════════════════════════════════════
   CHAT PAGE
   ═══════════════════════════════════════════════════════════════ */

.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: relative;
    z-index: 1;
}

/* ─── Header ──────────────────────────────────────────────────── */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--bg-glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border-color);
    z-index: 10;
    flex-shrink: 0;
}

.chat-header .header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-header .logo-sm {
    width: 34px;
    height: 34px;
    background: var(--gradient-primary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

.chat-header h2 {
    font-size: 0.95rem;
    font-weight: 600;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.chat-header .status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    box-shadow: 0 0 6px rgba(74, 222, 128, 0.5);
    animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chat-header .status-text {
    font-size: 0.72rem;
    color: #4ade80;
}

.btn-icon {
    width: 34px;
    height: 34px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-fast);
}

.btn-icon:hover {
    background: var(--bg-glass-hover);
    color: var(--text-primary);
}

/* ─── Messages Area ───────────────────────────────────────────── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.message-wrapper {
    display: flex;
    gap: 8px;
    max-width: 85%;
    min-width: 0;
    animation: msgIn 0.25s ease;
}

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

.message-wrapper.user {
    align-self: flex-start;
    flex-direction: row;
}

.message-wrapper.bot {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    margin-top: 2px;
}

.message-wrapper.user .message-avatar {
    background: var(--gradient-primary);
}

.message-wrapper.bot .message-avatar {
    background: var(--gradient-secondary);
}

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius-lg);
    font-size: 0.88rem;
    line-height: 1.85;
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    direction: auto;
}

.message-wrapper.user .message-bubble {
    background: var(--gradient-primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-wrapper.bot .message-bubble {
    background: var(--bg-glass);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

/* ─── Markdown in bot messages ─────────────────────────────────── */
.message-wrapper.bot .message-bubble pre {
    direction: ltr;
    text-align: left;
    background: rgba(0, 0, 0, 0.35);
    padding: 8px 10px;
    border-radius: 6px;
    overflow-x: auto;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.8rem;
    margin: 6px 0;
    line-height: 1.5;
}

.message-wrapper.bot .message-bubble code {
    background: rgba(0, 0, 0, 0.3);
    padding: 1px 5px;
    border-radius: 4px;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.82rem;
}

.message-time {
    font-size: 0.65rem;
    color: var(--text-muted);
    margin-top: 3px;
}

.message-wrapper.user .message-time {
    text-align: left;
}

/* ─── Typing Indicator ────────────────────────────────────────── */
.typing-indicator {
    display: none;
    align-self: flex-end;
    max-width: 75%;
    flex-direction: row-reverse;
    gap: 8px;
}

.typing-indicator.show {
    display: flex;
}

.typing-bubble {
    padding: 10px 16px;
    background: var(--bg-glass);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 4px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.typing-dot {
    width: 7px;
    height: 7px;
    background: var(--text-accent);
    border-radius: 50%;
    animation: bounce 1.4s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* ─── Welcome Message ─────────────────────────────────────────── */
.welcome-msg {
    text-align: center;
    padding: 48px 16px;
    color: var(--text-secondary);
    max-width: var(--max-chat-width);
    margin: 0 auto;
    width: 100%;
}

.welcome-msg .welcome-icon {
    font-size: 40px;
    margin-bottom: 12px;
    display: block;
}

.welcome-msg h3 {
    font-size: 1.1rem;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.welcome-msg p {
    font-size: 0.85rem;
    color: var(--text-muted);
    max-width: 320px;
    margin: 0 auto;
    line-height: 1.8;
}

/* ─── Input Area ──────────────────────────────────────────────── */
.chat-input-area {
    padding: 12px 16px;
    background: var(--bg-glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.chat-input-wrapper {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    max-width: var(--max-chat-width);
    margin: 0 auto;
    width: 100%;
}

.chat-input {
    flex: 1;
    padding: 11px 16px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    color: var(--text-primary);
    font-family: 'Vazirmatn', sans-serif;
    font-size: 0.9rem;
    resize: none;
    min-height: 42px;
    max-height: 100px;
    transition: border-color var(--transition-normal);
    direction: auto;
    line-height: 1.6;
}

.chat-input:focus {
    outline: none;
    border-color: var(--border-glow);
}

.chat-input::placeholder { color: var(--text-muted); }

.btn-send {
    width: 42px;
    height: 42px;
    background: var(--gradient-primary);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
    flex-shrink: 0;
}

.btn-send:hover {
    transform: scale(1.05);
    box-shadow: 0 0 20px rgba(100, 126, 234, 0.3);
}

.btn-send:active { transform: scale(0.95); }
.btn-send:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

/* ─── Connection Status ───────────────────────────────────────── */
.connection-status {
    display: none;
    text-align: center;
    padding: 8px 12px;
    font-size: 0.8rem;
    border-radius: var(--radius-sm);
    margin: 0 16px 8px;
    max-width: var(--max-chat-width);
    margin-left: auto;
    margin-right: auto;
}

.connection-status.show { display: block; }

.connection-status.disconnected {
    background: rgba(239, 68, 68, 0.12);
    border: 1px solid rgba(239, 68, 68, 0.25);
    color: #fca5a5;
}

.connection-status.reconnecting {
    background: rgba(234, 179, 8, 0.12);
    border: 1px solid rgba(234, 179, 8, 0.25);
    color: #fcd34d;
}

/* ─── Sidebar ─────────────────────────────────────────────────── */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 50;
}
.sidebar-overlay.show { display: block; }

.sidebar {
    position: fixed;
    top: 0;
    right: -260px;
    width: 260px;
    height: 100%;
    background: var(--bg-secondary);
    border-left: 1px solid var(--border-color);
    z-index: 51;
    padding: 20px;
    transition: right var(--transition-normal);
    display: flex;
    flex-direction: column;
}

.sidebar.open { right: 0; }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h3 {
    font-size: 1rem;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
    font-size: 0.85rem;
}

.sidebar-item:hover {
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-primary);
}

.sidebar-item.active {
    background: rgba(100, 126, 234, 0.1);
    color: var(--text-accent);
}

.sidebar-footer {
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
}

/* ─── Responsive ──────────────────────────────────────────────── */
@media (max-width: 768px) {
    .login-card {
        padding: 32px 24px;
        border-radius: var(--radius-lg);
    }

    .chat-header {
        padding: 8px 12px;
    }

    .chat-messages {
        padding: 12px;
        gap: 8px;
    }

    .chat-input-area {
        padding: 10px 12px;
    }

    .message-wrapper {
        max-width: 85%;
    }
}

@media (max-width: 400px) {
    .login-card {
        padding: 24px 18px;
        max-width: 100%;
    }

    .login-logo .logo-icon {
        width: 48px;
        height: 48px;
        font-size: 24px;
    }

    .login-logo h1 {
        font-size: 1.2rem;
    }

    .message-avatar {
        width: 26px;
        height: 26px;
        font-size: 12px;
    }

    .message-wrapper {
        max-width: 90%;
    }

    .chat-input {
        font-size: 0.85rem;
    }

    .btn-send {
        width: 38px;
        height: 38px;
        font-size: 16px;
    }
}

/* ─── Print ───────────────────────────────────────────────────── */
@media print {
    body::before { display: none; }
    .chat-header, .chat-input-area, .sidebar, .sidebar-overlay { display: none !important; }
    .chat-messages { overflow: visible; height: auto; }
    .message-bubble { border: 1px solid #ccc; }
}

/* ─── CRITICAL FIXES ─────────────────────────────────────────── */
/* Prevent any element from exceeding viewport width */
html, body {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
}

.message-bubble {
    max-width: 100% !important;
    min-width: 0;
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
    hyphens: auto;
}

.message-wrapper {
    width: 100%;
    max-width: 85% !important;
    min-width: 0;
}

.message-wrapper.bot {
    align-self: flex-end;
    margin-right: 0;
    margin-left: auto;
}

.message-wrapper.user {
    align-self: flex-start;
    margin-left: 0;
    margin-right: auto;
}

/* Code blocks must not overflow */
.message-bubble pre {
    max-width: calc(100vw - 60px) !important;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
}

.message-bubble code {
    max-width: calc(100vw - 60px) !important;
    overflow-wrap: break-word;
    word-break: break-all;
}

/* One page layout - header + messages + input fit in viewport */
.chat-container {
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
}

.chat-messages {
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1;
    min-height: 0;
}

.chat-header {
    flex-shrink: 0;
    height: auto;
}

.chat-input-area {
    flex-shrink: 0;
    height: auto;
}
