/* ChatDash — Main Styles */
:root {
    --primary: #4a6cf7;
    --primary-hover: #3a5ce5;
    --primary-light: rgba(74, 108, 247, 0.1);
    --danger: #e74c3c;
    --success: #27ae60;
    --warning: #f39c12;
    --gray-50: #f8f9fa;
    --gray-100: #f0f2f5;
    --gray-200: #e4e6ea;
    --gray-300: #ccc;
    --gray-500: #666;
    --gray-700: #333;
    --gray-900: #1a1a2e;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.12);
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

html { height: 100%; }

body {
    font-family: var(--font);
    background: var(--gray-100);
    color: var(--gray-700);
    min-height: 100vh;
    font-size: 15px;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Login page */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.login-card {
    background: #fff;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 40px;
    max-width: 400px;
    width: 100%;
}

.login-card h1 {
    font-size: 26px;
    color: var(--gray-900);
    margin-bottom: 4px;
}

.login-card .subtitle {
    color: var(--gray-500);
    font-size: 14px;
    margin-bottom: 28px;
}

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

.form-group label {
    display: block;
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 4px;
    color: var(--gray-700);
}

.form-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 15px;
    font-family: var(--font);
    transition: border-color 0.2s, box-shadow 0.2s;
    min-height: 44px;
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.checkbox-group {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
}

.checkbox-group input[type="checkbox"] {
    width: 22px;
    height: 22px;
    accent-color: var(--primary);
}

.checkbox-group label {
    font-size: 14px;
    color: var(--gray-500);
    cursor: pointer;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    font-size: 15px;
    font-weight: 600;
    font-family: var(--font);
    cursor: pointer;
    transition: background 0.2s;
    min-height: 44px;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    width: 100%;
}

.btn-primary:hover {
    background: var(--primary-hover);
    text-decoration: none;
}

.btn-secondary {
    background: var(--gray-200);
    color: var(--gray-700);
}

.btn-secondary:hover {
    background: var(--gray-300);
    text-decoration: none;
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #c0392b;
    text-decoration: none;
}

.btn-sm {
    padding: 6px 14px;
    font-size: 13px;
    min-height: 44px;
}

.alert {
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 20px;
    font-size: 14px;
}

.alert-error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.alert-success {
    background: #f0fdf4;
    color: #166534;
    border: 1px solid #bbf7d0;
}

/* App shell */
.app-header {
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    padding: 0 20px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
}

.app-header .logo {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
    flex-shrink: 0;
}

.app-header .logo span {
    color: var(--primary);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.header-actions a {
    color: var(--gray-500);
    font-size: 14px;
    min-height: 44px;
    display: flex;
    align-items: center;
    white-space: nowrap;
}

.header-user {
    font-weight: 600;
    color: var(--gray-700);
    font-size: 14px;
    white-space: nowrap;
}

.notification-bell {
    position: relative;
    font-size: 20px;
    cursor: pointer;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.notification-bell.notif-active {
    color: var(--primary);
}

.notification-bell.notif-off {
    opacity: 0.6;
}

.notification-badge {
    position: absolute;
    top: 6px;
    right: 2px;
    background: var(--danger);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

.notification-badge:empty,
.notification-badge.hidden {
    display: none;
}

/* Tab filters */
.tab-filters {
    display: flex;
    gap: 0;
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    padding: 0 20px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.tab-filters button {
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 500;
    color: var(--gray-500);
    cursor: pointer;
    white-space: nowrap;
    min-height: 44px;
    font-family: var(--font);
    -webkit-tap-highlight-color: transparent;
}

.tab-filters button.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Conversation list */
.conversation-list {
    list-style: none;
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    background: #fff;
    border-bottom: 1px solid var(--gray-100);
    cursor: pointer;
    transition: background 0.15s;
    min-height: 72px;
    text-decoration: none;
    color: inherit;
    -webkit-tap-highlight-color: transparent;
}

.conversation-item:hover {
    background: var(--gray-50);
    text-decoration: none;
}

.conversation-item.has-unread {
    background: #f0f4ff;
}

.conversation-item.has-unread:hover {
    background: #e8eeff;
}

.conv-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    flex-shrink: 0;
    margin-right: 12px;
}

.conv-content {
    flex: 1;
    min-width: 0;
}

.conv-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2px;
}

.conv-name {
    font-weight: 600;
    font-size: 15px;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-time {
    font-size: 12px;
    color: var(--gray-500);
    white-space: nowrap;
    margin-left: 8px;
}

.conv-preview {
    font-size: 13px;
    color: var(--gray-500);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: 12px;
    flex-shrink: 0;
}

.mode-badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.mode-badge.ai { background: #dbeafe; color: #1d4ed8; }
.mode-badge.human { background: #ffedd5; color: #c2410c; }
.mode-badge.augmented { background: #f3e8ff; color: #7c3aed; }

.unread-dot {
    background: var(--danger);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    min-width: 22px;
    height: 22px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

.unread-dot:empty,
.unread-dot.hidden {
    display: none;
}

.site-label {
    font-size: 11px;
    color: var(--gray-500);
    background: var(--gray-100);
    padding: 1px 6px;
    border-radius: 4px;
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--gray-500);
}

.empty-state .icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.empty-state h2 {
    font-size: 18px;
    color: var(--gray-700);
    margin-bottom: 8px;
}

.empty-state p {
    font-size: 14px;
    max-width: 300px;
    margin: 0 auto;
}

/* Utility */
.hidden { display: none !important; }
.text-center { text-align: center; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mb-2 { margin-bottom: 16px; }

/* ============================================
   Settings page
   ============================================ */
.settings-page {
    padding: 20px;
    max-width: 640px;
    margin: 0 auto;
}

.settings-container h1 {
    font-size: 22px;
    color: var(--gray-900);
    margin-bottom: 4px;
}

.settings-subtitle {
    color: var(--gray-500);
    font-size: 14px;
    margin-bottom: 24px;
}

.settings-card {
    background: #fff;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
}

.settings-card h2 {
    font-size: 16px;
    color: var(--gray-900);
    margin-bottom: 16px;
}

.site-form .form-group,
.site-edit-form .form-group {
    margin-bottom: 12px;
}

.form-help {
    display: block;
    font-size: 12px;
    color: var(--gray-500);
    margin-top: 4px;
}

.form-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.site-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.site-card-header strong {
    display: block;
    font-size: 15px;
    color: var(--gray-900);
}

.site-url {
    font-size: 12px;
    color: var(--gray-500);
}

.site-card-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.site-edit-form {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--gray-200);
}

.site-test-result {
    margin-top: 12px;
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13px;
}

.site-test-result.test-ok {
    background: #f0fdf4;
    color: #166534;
    border: 1px solid #bbf7d0;
}

.site-test-result.test-fail {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

/* API key reveal box */
.api-key-reveal {
    background: #fffbeb;
    border: 1px solid #fcd34d;
}

.api-key-box {
    display: flex;
    gap: 8px;
    align-items: center;
}

.api-key-box .form-input {
    font-family: 'SF Mono', SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 13px;
    background: #fff;
    letter-spacing: 0.3px;
}

/* Quick reply textarea */
.qr-textarea {
    resize: vertical;
    min-height: 80px;
}

.qr-preview {
    font-size: 13px;
    color: var(--gray-500);
    margin-top: 2px;
}

.qr-category-heading {
    font-size: 14px;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ============================================
   Chat view
   ============================================ */
.chat-body {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.chat-header {
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    padding: 0 16px;
    height: 56px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.chat-back {
    font-size: 22px;
    color: var(--gray-700);
    text-decoration: none;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.chat-back:hover {
    text-decoration: none;
    color: var(--primary);
}

.chat-header-info {
    flex: 1;
    min-width: 0;
}

.chat-header-name {
    font-weight: 700;
    font-size: 15px;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-header-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
}

.chat-status {
    color: var(--gray-500);
    text-transform: capitalize;
}

.chat-mode-switcher {
    flex-shrink: 0;
}

.mode-select {
    padding: 6px 10px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: var(--font);
    background: #fff;
    color: var(--gray-700);
    min-height: 44px;
    cursor: pointer;
}

.mode-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

/* Messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
}

.chat-loading,
.chat-empty,
.chat-error {
    text-align: center;
    padding: 40px 20px;
    color: var(--gray-500);
    font-size: 14px;
}

.chat-error {
    color: var(--danger);
}

/* Chat bubbles */
.chat-bubble {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.45;
    word-wrap: break-word;
    position: relative;
}

.bubble-sender {
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 2px;
    opacity: 0.7;
}

.bubble-text {
    white-space: pre-wrap;
}

.bubble-time {
    font-size: 10px;
    opacity: 0.5;
    margin-top: 4px;
    text-align: right;
}

/* User messages — left */
.bubble-user {
    align-self: flex-start;
    background: #fff;
    color: var(--gray-900);
    border: 1px solid var(--gray-200);
    border-bottom-left-radius: 4px;
}

/* Operator messages — right */
.bubble-operator {
    align-self: flex-end;
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.bubble-operator .bubble-sender {
    color: rgba(255,255,255,0.8);
}

.bubble-operator .bubble-time {
    color: rgba(255,255,255,0.6);
}

/* AI bot messages — left, distinct */
.bubble-ai {
    align-self: flex-start;
    background: #eef2ff;
    color: var(--gray-900);
    border: 1px solid #c7d2fe;
    border-bottom-left-radius: 4px;
}

/* AI Draft area */
.ai-draft-area {
    background: #faf5ff;
    border-top: 1px solid #e9d5ff;
    padding: 12px 16px;
    flex-shrink: 0;
}

.ai-draft-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    font-size: 12px;
    font-weight: 600;
    color: #7c3aed;
}

.ai-draft-content {
    font-size: 13px;
    color: var(--gray-700);
    line-height: 1.5;
    max-height: 80px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid #e9d5ff;
    border-radius: var(--radius);
    padding: 8px 12px;
}

/* Chat input area */
.chat-input-area {
    background: #fff;
    border-top: 1px solid var(--gray-200);
    padding: 12px 16px;
    flex-shrink: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.chat-input-form {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chat-textarea {
    flex: 1;
    min-width: 0;
    width: 100%;
    resize: none;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 10px 14px;
    font-size: 15px;
    font-family: var(--font);
    line-height: 1.4;
    max-height: 120px;
    min-height: 44px;
    box-sizing: border-box;
}

.chat-textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.chat-textarea:disabled {
    background: var(--gray-50);
    color: var(--gray-500);
    cursor: not-allowed;
}

.chat-send-btn {
    flex-shrink: 0;
    min-width: 64px;
    min-height: 44px;
}

.chat-input-hint {
    font-size: 11px;
    color: var(--gray-500);
    margin-top: 4px;
    text-align: center;
}

/* ============================================
   Quick Replies — Lightning Bolt trigger
   ============================================ */
.qr-trigger-btn {
    background: none;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    min-width: 44px;
    min-height: 44px;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--warning);
    transition: background 0.2s, border-color 0.2s;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

.qr-trigger-btn:hover {
    background: var(--gray-50);
    border-color: var(--warning);
}

/* Quick Replies — Slide-up Panel */
.qr-panel-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 200;
}

.qr-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 201;
    background: #fff;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
    max-height: 60vh;
    display: flex;
    flex-direction: column;
    transform: translateY(0);
    transition: transform 0.25s ease-out;
}

.qr-panel.hidden {
    transform: translateY(100%);
    pointer-events: none;
}

.qr-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 16px 8px;
    border-bottom: 1px solid var(--gray-200);
    flex-shrink: 0;
}

.qr-panel-title {
    font-weight: 700;
    font-size: 16px;
    color: var(--gray-900);
}

.qr-panel-close {
    background: none;
    border: none;
    font-size: 18px;
    color: var(--gray-500);
    cursor: pointer;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.qr-panel-search {
    padding: 8px 16px;
    flex-shrink: 0;
}

.qr-panel-search .form-input {
    min-height: 44px;
}

.qr-panel-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.qr-panel-loading,
.qr-panel-empty {
    padding: 24px 16px;
    text-align: center;
    color: var(--gray-500);
    font-size: 14px;
}

.qr-category {
    padding: 10px 16px 4px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--gray-500);
    background: var(--gray-50);
}

.qr-item {
    display: flex;
    flex-direction: column;
    width: 100%;
    padding: 12px 16px;
    border: none;
    border-bottom: 1px solid var(--gray-100);
    background: #fff;
    cursor: pointer;
    text-align: left;
    font-family: var(--font);
    min-height: 48px;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s;
}

.qr-item:hover,
.qr-item:active {
    background: var(--primary-light);
}

.qr-item-title {
    font-weight: 600;
    font-size: 14px;
    color: var(--gray-900);
}

.qr-item-preview {
    font-size: 12px;
    color: var(--gray-500);
    margin-top: 2px;
}

/* ============================================
   Responsive — Mobile Optimization
   ============================================ */

/* 375px+ (iPhone SE and above) */
@media (max-width: 480px) {
    .login-card { padding: 28px 20px; }

    .app-header {
        padding: 0 12px;
        gap: 8px;
    }

    .header-actions {
        gap: 8px;
    }

    .header-actions a {
        font-size: 13px;
        padding: 0 2px;
    }

    .header-user {
        display: none;
    }

    .conversation-item { padding: 12px; }

    .conv-avatar {
        width: 40px;
        height: 40px;
        margin-right: 10px;
        font-size: 14px;
    }

    .conv-name { font-size: 14px; }
    .conv-preview { font-size: 12px; }
    .conv-meta { margin-left: 8px; gap: 4px; }

    .tab-filters { padding: 0 12px; }

    .settings-page { padding: 12px; }
    .settings-card { padding: 16px; }

    .chat-header { padding: 0 12px; gap: 8px; }
    .chat-messages { padding: 12px; }
    .chat-bubble { max-width: 88%; }
    .chat-input-area { padding: 10px 12px; }

    .site-card-header { flex-direction: column; align-items: flex-start; }

    .mode-select { font-size: 12px; padding: 4px 8px; }

    .ai-draft-area { padding: 10px 12px; }

    .qr-panel { max-height: 70vh; }
}

/* Ultra-narrow (375px) safety net */
@media (max-width: 375px) {
    .app-header { padding: 0 8px; }
    .header-actions { gap: 6px; }
    .header-actions a { font-size: 12px; }
    .conversation-item { padding: 10px 8px; }
    .conv-avatar { width: 36px; height: 36px; margin-right: 8px; font-size: 13px; }
    .conv-meta { margin-left: 6px; flex-direction: column; gap: 2px; }
    .tab-filters { padding: 0 8px; }
    .tab-filters button { padding: 10px 12px; font-size: 13px; }
    .settings-page { padding: 8px; }
    .chat-header { padding: 0 8px; gap: 6px; }
    .chat-messages { padding: 8px; gap: 6px; }
    .chat-bubble { max-width: 92%; font-size: 13px; padding: 8px 12px; }
    .chat-input-area { padding: 8px; }
    .chat-input-form { gap: 6px; }
    .chat-send-btn { min-width: 56px; font-size: 14px; }
}

/* Ensure all interactive elements meet 44px touch target */
@media (pointer: coarse) {
    .btn { min-height: 44px; }
    .btn-sm { min-height: 44px; }
    .form-input { min-height: 44px; }
    .chat-textarea { min-height: 44px; }
    .mode-select { min-height: 44px; }
    .tab-filters button { min-height: 44px; }
    .conversation-item { min-height: 64px; }
    .qr-item { min-height: 48px; }
    select { min-height: 44px; }
}
