/* ═══════════════════════════════════════════════════════════════════════════════
   Helix Chat — Styles v2 (Glass Edition)
   ═══════════════════════════════════════════════════════════════════════════════ */
:root {
    --bg:          #0a0a0f;
    --bg-card:     #12121a;
    --bg-hover:    #1a1a28;
    --bg-input:    #16161f;
    --bg-msg-user: #1c1c2e;
    --bg-msg-ai:   transparent;
    --border:      #1e1e2e;
    --text:        #e4e4ef;
    --text-dim:    #888;
    --text-muted:  #555;
    --accent:      #7c6aef;
    --accent-hover:#9b8df2;
    --accent-glow: rgba(124,106,239,0.15);
    --green:       #22c55e;
    --red:         #ef4444;
    --yellow:      #eab308;
    --radius:      8px;
    --radius-lg:   12px;
    --radius-xl:   16px;
    --sidebar-w:   280px;
    --font-mono:   'SF Mono','Cascadia Code','JetBrains Mono','Fira Code','Consolas',monospace;
}

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

/* ── Login Overlay ─────────────────────────────────────────────────────────── */
.login-overlay {
    position: fixed; inset: 0; z-index: 9999;
    display: flex; align-items: center; justify-content: center;
    background: var(--bg);
}
.login-overlay .login-card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius-xl); padding: 40px 36px;
    width: 340px; text-align: center;
}
.login-overlay .login-logo { color: var(--accent); margin-bottom: 12px; }
.login-overlay h1 { font-size: 22px; color: var(--text); margin-bottom: 4px; }
.login-overlay .login-sub { color: var(--text-dim); font-size: 14px; margin-bottom: 24px; }
.login-overlay form { display: flex; flex-direction: column; gap: 12px; }
.login-overlay input {
    padding: 10px 14px; background: var(--bg-input); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 14px; outline: none;
}
.login-overlay input:focus { border-color: var(--accent); }
.login-overlay button[type=submit] {
    padding: 10px; background: var(--accent); color: #fff; border: none;
    border-radius: var(--radius); font-size: 14px; font-weight: 600; cursor: pointer;
    transition: background 200ms;
}
.login-overlay button[type=submit]:hover { background: var(--accent-hover); }
.login-overlay button[type=submit]:disabled { opacity: 0.5; cursor: wait; }
.login-overlay .login-error { color: var(--red); font-size: 13px; min-height: 18px; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
    height: 100vh;
    -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */
.app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

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

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--accent);
    font-weight: 700;
    font-size: 15px;
}
.logo svg { flex-shrink: 0; }

.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    color: var(--text);
    transition: background 0.15s;
    position: relative;
    user-select: none;
    -webkit-user-select: none;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--accent-glow); color: var(--accent); }

.conv-icon { font-size: 14px; flex-shrink: 0; opacity: 0.6; }
.conv-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.conv-count {
    font-size: 10px;
    color: var(--text-muted);
    background: var(--bg-hover);
    padding: 2px 6px;
    border-radius: 10px;
    flex-shrink: 0;
}

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

.model-selector label {
    display: block;
    font-size: 11px;
    color: var(--text-dim);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.model-selector select {
    width: 100%;
    padding: 8px 10px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 13px;
    outline: none;
    cursor: pointer;
}
.model-selector select:focus { border-color: var(--accent); }

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 100;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: 8px;
    cursor: pointer;
}

.mobile-home-link {
    display: none;
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 100;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: 8px;
    text-decoration: none;
    line-height: 0;
}
.mobile-home-link:active { border-color: var(--accent); color: var(--accent); }

/* ── Chat Main ──────────────────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

/* ── Welcome Screen ─────────────────────────────────────────────────────────── */
.welcome {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 20px;
    gap: 16px;
}
.welcome-icon { font-size: 56px; margin-bottom: 8px; }
.welcome h1 {
    font-size: 28px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), #a78bfa, #60a5fa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.welcome p { color: var(--text-dim); font-size: 15px; max-width: 400px; }

.welcome-suggestions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 24px;
    max-width: 500px;
    width: 100%;
}

.suggestion {
    padding: 14px 16px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    color: var(--text);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    transition: all 0.2s;
}
.suggestion:hover {
    border-color: var(--accent);
    background: var(--accent-glow);
}

/* ── Messages ───────────────────────────────────────────────────────────────── */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
    display: none; /* shown when conversation active */
}
.messages.active { display: block; }

.message {
    padding: 16px 24px;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
    animation: msgIn 0.2s ease;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}
.message.user .message-avatar { background: var(--accent); color: #fff; }
.message.assistant .message-avatar { background: linear-gradient(135deg, #22c55e, #10b981); color: #fff; }

.message-role {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.message.user .message-role { color: var(--accent); }
.message.assistant .message-role { color: var(--green); }

.message-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-left: auto;
}

.message-content {
    font-size: 14px;
    line-height: 1.7;
    color: var(--text);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Markdown styling in messages */
.message-content p { margin-bottom: 10px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; color: var(--text-dim); }

.message-content code {
    background: var(--bg-hover);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 13px;
}

.message-content pre {
    background: #111118;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    overflow-x: auto;
    margin: 10px 0;
    position: relative;
}
.message-content pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}

.code-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-dim);
    padding: 4px 8px;
    font-size: 11px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
}
.message-content pre:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { color: var(--text); background: var(--accent-glow); }

.message-content ul, .message-content ol {
    margin: 8px 0;
    padding-left: 24px;
}
.message-content li { margin-bottom: 4px; }

.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding: 8px 16px;
    margin: 10px 0;
    color: var(--text-dim);
    background: var(--accent-glow);
    border-radius: 0 var(--radius) var(--radius) 0;
}

.message-content a {
    color: var(--accent);
    text-decoration: none;
}
.message-content a:hover { text-decoration: underline; }

/* Headings */
.message-content h1, .message-content h2, .message-content h3 { margin: 16px 0 8px; font-weight: 600; line-height: 1.3; }
.message-content h1 { font-size: 1.4em; }
.message-content h2 { font-size: 1.2em; }
.message-content h3 { font-size: 1.1em; }

/* KaTeX Math Rendering */
.message-content .katex { color: var(--text); font-size: 1.05em; }
.message-content .katex-display { overflow-x: auto; overflow-y: hidden; padding: 12px 0; margin: 12px 0; text-align: center; }
.message-content .katex-display > .katex { font-size: 1.21em; }
.message-content .katex-display::-webkit-scrollbar { height: 4px; }
.message-content .katex-display::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }
.math-error { color: var(--red); font-family: var(--font-mono); font-size: 13px; background: rgba(239,68,68,0.1); padding: 2px 6px; border-radius: 4px; }
.math-fallback { font-family: var(--font-mono); color: var(--text-dim); }

.message-content table {
    border-collapse: collapse;
    margin: 10px 0;
    width: 100%;
}
.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
    font-size: 13px;
}
.message-content th { background: var(--bg-hover); font-weight: 600; }

/* Message images */
.message-images {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}
.message-images img {
    max-width: 200px;
    max-height: 200px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    object-fit: cover;
}

/* Streaming cursor */
.streaming-cursor {
    display: inline-block;
    width: 8px;
    height: 18px;
    background: var(--accent);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: blink 0.8s step-end infinite;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

/* Thinking indicator */
.thinking {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    color: var(--text-dim);
    font-size: 13px;
}
.thinking-dots { display: flex; gap: 4px; }
.thinking-dots span {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: 50%;
    animation: dotPulse 1.4s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotPulse { 0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }

/* ── Input Area ─────────────────────────────────────────────────────────────── */
.input-area {
    padding: 0 24px 20px;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.input-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    overflow: hidden;
    transition: border-color 0.2s;
}
.input-container:focus-within { border-color: var(--accent); }

.image-preview-row {
    display: flex;
    gap: 8px;
    padding: 0;
    flex-wrap: wrap;
    overflow: hidden;
    max-height: 0;
    transition: all 0.2s;
}
.image-preview-row.has-images {
    padding: 10px 14px 0;
    max-height: 120px;
}

.image-preview {
    position: relative;
    width: 64px;
    height: 64px;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
}
.image-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.image-preview .remove-image {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 18px;
    height: 18px;
    background: rgba(0,0,0,0.7);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    padding: 6px 8px;
}

.attach-btn {
    color: var(--text-dim);
    padding: 8px;
    border-radius: var(--radius);
    flex-shrink: 0;
}
.attach-btn:hover { color: var(--text); background: var(--bg-hover); }

#chatInput {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    outline: none;
    padding: 8px 4px;
    max-height: 200px;
    line-height: 1.5;
}
#chatInput::placeholder { color: var(--text-muted); }

.send-btn {
    width: 36px;
    height: 36px;
    background: var(--accent);
    border: none;
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
    opacity: 0.5;
}
.send-btn:not(:disabled) { opacity: 1; }
.send-btn:not(:disabled):hover { background: var(--accent-hover); transform: scale(1.05); }
.send-btn:disabled { cursor: default; }

.input-hint {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 14px 8px;
    font-size: 11px;
    color: var(--text-muted);
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
    flex-shrink: 0;
}
.status-dot.busy { background: var(--yellow); animation: dotPulse 1s infinite; }
.status-dot.error { background: var(--red); }

/* ── Context Menu ───────────────────────────────────────────────────────────── */
.context-menu {
    position: fixed;
    z-index: 1000;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    min-width: 160px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.context-item {
    display: block;
    width: 100%;
    padding: 8px 12px;
    background: none;
    border: none;
    color: var(--text);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    border-radius: 4px;
}
.context-item:hover { background: var(--bg-hover); }
.context-item.danger { color: var(--red); }

.hidden { display: none !important; }

/* ── Icon Button ────────────────────────────────────────────────────────────── */
.icon-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-btn:hover { color: var(--text); background: var(--bg-hover); }

/* ── Scrollbar ──────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* ═══════════════════════════════════════════════════════════════════════════════
   Responsive
   ═══════════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 200;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-toggle { display: flex; }
    .mobile-home-link { display: flex; }

    .chat-main { width: 100%; }
    .message { padding: 12px 16px; }
    .input-area { padding: 0 12px 12px; }
    .welcome-suggestions { grid-template-columns: 1fr; }
    .welcome h1 { font-size: 22px; }
}

@media (max-width: 480px) {
    .message-content { font-size: 14px; }
    .message-images img { max-width: 150px; max-height: 150px; }
}

/* Sidebar overlay on mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 150;
    background: rgba(0,0,0,0.5);
}
.sidebar-overlay.active { display: block; }

/* ═══════════════════════════════════════════════════════════════════════════════
   iOS / Mobile Optimizations
   ═══════════════════════════════════════════════════════════════════════════════ */

/* Fix 100vh on iOS */
@supports (height: 100dvh) {
    body, .app { height: 100dvh; }
}

/* Safe area insets */
@supports (padding: env(safe-area-inset-top)) {
    .sidebar { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); padding-left: env(safe-area-inset-left); }
    .sidebar-toggle { top: calc(12px + env(safe-area-inset-top)); left: calc(12px + env(safe-area-inset-left)); }
    .mobile-home-link { top: calc(12px + env(safe-area-inset-top)); right: calc(12px + env(safe-area-inset-right)); }
    .input-area { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
    .chat-main { padding-right: env(safe-area-inset-right); }
}

/* iOS standalone (Add to Home Screen) */
@media (display-mode: standalone) {
    .sidebar { padding-top: env(safe-area-inset-top, 20px); }
    body { overscroll-behavior: none; -webkit-overflow-scrolling: touch; }
}

/* Touch-friendly (44px Apple HIG) */
@media (pointer: coarse) {
    .icon-btn { min-width: 44px; min-height: 44px; }
    .conv-item { min-height: 44px; padding: 12px; }
    .send-btn { width: 44px; height: 44px; }
    .attach-btn { min-width: 44px; min-height: 44px; }
    .suggestion { min-height: 48px; padding: 14px 16px; }
    .context-item { min-height: 44px; padding: 12px 14px; }
    #chatInput { font-size: 16px; min-height: 44px; }
    select { font-size: 16px; min-height: 44px; }
    .conv-item:hover { background: transparent; }
    .conv-item:active { background: var(--bg-hover); }
    .suggestion:hover { border-color: var(--border); background: var(--bg-card); }
    .suggestion:active { border-color: var(--accent); background: var(--accent-glow); }
}

/* Interactive elements — no select */
.conv-item, .icon-btn, .send-btn, .attach-btn, .suggestion, .context-item,
.sidebar-toggle { -webkit-user-select: none; user-select: none; }

/* iOS rubber-band fix */
body { overscroll-behavior-y: contain; }
.messages { -webkit-overflow-scrolling: touch; }
.conv-list { -webkit-overflow-scrolling: touch; }

/* iOS input zoom fix */
@media screen and (max-width: 768px) {
    input[type="text"], input[type="search"], select, textarea {
        font-size: 16px !important;
    }
}


/* --- Glass & Glow Enhancements (v2) --- */

/* Sidebar glass */
.sidebar {
    background: rgba(18, 18, 26, 0.85) !important;
    backdrop-filter: blur(16px) !important;
    -webkit-backdrop-filter: blur(16px) !important;
    border-right-color: rgba(255, 255, 255, 0.06) !important;
}

/* Active conversation glow */
.conv-item.active {
    background: rgba(124, 106, 239, 0.12) !important;
    border-color: rgba(124, 106, 239, 0.2) !important;
    box-shadow: inset 3px 0 0 var(--accent), 0 0 16px rgba(124, 106, 239, 0.05) !important;
}

/* New chat button glow */
.new-chat-btn:hover,
.sidebar .btn-primary:hover {
    box-shadow: 0 4px 20px rgba(124, 106, 239, 0.25) !important;
}

/* Message glass cards */
.msg.ai .msg-content {
    background: rgba(124, 106, 239, 0.04) !important;
    border: 1px solid rgba(124, 106, 239, 0.08) !important;
    border-radius: 12px !important;
}

/* Input area glass */
.input-area,
.chat-input-area {
    background: rgba(18, 18, 26, 0.8) !important;
    backdrop-filter: blur(16px) !important;
    -webkit-backdrop-filter: blur(16px) !important;
    border-top-color: rgba(255, 255, 255, 0.06) !important;
}

/* Send button glow */
.send-btn:hover,
.chat-send:hover {
    box-shadow: 0 4px 16px rgba(124, 106, 239, 0.3) !important;
}

/* Chat header glass */
.chat-header,
.main-header {
    background: rgba(18, 18, 26, 0.75) !important;
    backdrop-filter: blur(20px) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    border-bottom-color: rgba(255, 255, 255, 0.06) !important;
}

/* Status indicator glow */
.status-indicator.online,
.online-dot {
    box-shadow: 0 0 8px rgba(34, 197, 94, 0.5) !important;
    animation: chatStatusPulse 3s ease-in-out infinite;
}

@keyframes chatStatusPulse {
    0%, 100% { box-shadow: 0 0 8px rgba(34, 197, 94, 0.5); }
    50% { box-shadow: 0 0 14px rgba(34, 197, 94, 0.7); }
}

/* Code blocks in messages */
.msg-content pre {
    background: rgba(10, 10, 15, 0.8) !important;
    border: 1px solid rgba(255, 255, 255, 0.06) !important;
    border-radius: 8px !important;
}

/* Noise overlay */
.noise-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 9998;
    opacity: 0.015;
    mix-blend-mode: overlay;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-size: 256px 256px;
}
