/* ═══════════════════════════════════════════════════════════════
   CHAT WIDGET  (style-30-chat-widget.css)   prefix: .chat-*
   ══════════════════════════════════════════════════════════════ */

.chat-page {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 60px);
    background: #1a1f2e;
    color: #e8eaf0;
    overflow: hidden;
}

/* ── Header ──────────────────────────────────────────────────── */
.chat-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 24px;
    border-bottom: 1px solid #2d3548;
    background: #252b3b;
    flex-shrink: 0;
}

.chat-title {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 600;
    color: #4a9eff;
}

.chat-subtitle {
    font-size: 0.78rem;
    color: #7a8097;
}

/* ── Conversation title bar (FEAT-081) ───────────────────────── */
.chat-conv-title-bar {
    padding: 4px 24px;
    font-size: 0.78rem;
    color: #7a8097;
    background: #252b3b;
    border-bottom: 1px solid #2d3548;
    min-height: 22px;
    flex-shrink: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

body[data-theme="light"] .chat-conv-title-bar { background: #f8f9fc; border-bottom-color: #dde1eb; color: #9aa0b0; }

/* ── Body ────────────────────────────────────────────────────── */
.chat-body {
    display: flex;
    flex: 1;
    overflow: hidden;
    min-height: 0;
}

/* ── Left sidebar ────────────────────────────────────────────── */
.chat-sidebar {
    width: 230px;
    flex-shrink: 0;
    background: #1e2537;
    border-right: 1px solid #2d3548;
    padding: 14px 10px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.chat-sidebar-label {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #7a8097;
    padding: 0 4px;
}

.chat-conv-list {
    display: flex;
    flex-direction: column;
    gap: 3px;
    flex: 1;
}

/* ── Conversation row (FEAT-078) ─────────────────────────────── */
.chat-conv-row {
    display: flex;
    align-items: center;
    border-radius: 6px;
    border: 1px solid transparent;
    transition: background 0.12s;
}

.chat-conv-row:hover {
    background: #252b3b;
}

.chat-conv-row.chat-conv-active {
    background: #1e3a5f !important;
    border-color: #2d5a8e !important;
}

.chat-conv-item-name {
    flex: 1;
    padding: 8px 4px 8px 10px;
    font-size: 0.855rem;
    color: #b0b8d0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    user-select: none;
    min-width: 0;
}

.chat-conv-row:hover .chat-conv-item-name { color: #e8eaf0; }
.chat-conv-row.chat-conv-active .chat-conv-item-name { color: #4a9eff !important; }

.chat-conv-delete-btn {
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: #5a6478;
    font-size: 0.82rem;
    padding: 4px 7px;
    cursor: pointer;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
    line-height: 1;
}

.chat-conv-row:hover .chat-conv-delete-btn { opacity: 1; }
.chat-conv-delete-btn:hover { color: #e05252; background: rgba(224, 82, 82, 0.12); }

/* legacy single-item class kept for empty-state usage */
.chat-conv-item {
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.855rem;
    color: #b0b8d0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border: 1px solid transparent;
    transition: background 0.12s;
    user-select: none;
}

.chat-conv-item:hover {
    background: #252b3b;
    color: #e8eaf0;
}

.chat-conv-active {
    background: #1e3a5f !important;
    color: #4a9eff !important;
    border-color: #2d5a8e !important;
}

.chat-conv-empty {
    font-size: 0.82rem;
    color: #7a8097;
    text-align: center;
    padding: 20px 8px;
}

/* ── Right chat panel ────────────────────────────────────────── */
.chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: #1a1f2e;
}

.chat-log {
    flex: 1;
    overflow-y: auto;
    padding: 20px 24px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-height: 0;
}

/* BUG-168: anchor messages to the bottom so a short conversation doesn't leave a
   big empty gap above the input. margin-top:auto collapses to 0 once content
   overflows, so long conversations scroll normally (scroll-to-top still works). */
.chat-log > :first-child {
    margin-top: auto;
}

/* ── Empty state ─────────────────────────────────────────────── */
.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    opacity: 0.5;
}

.chat-empty-icon {
    font-size: 2.5rem;
    margin-bottom: 10px;
}

.chat-empty-text {
    font-size: 0.9rem;
    color: #7a8097;
    text-align: center;
}

/* ── Message bubbles ─────────────────────────────────────────── */
.chat-row {
    display: flex;
}

.chat-row-user      { justify-content: flex-end; }
.chat-row-assistant { justify-content: flex-start; }

.chat-bubble {
    max-width: 75%;
    padding: 10px 16px;
    border-radius: 12px;
    font-size: 0.88rem;
    line-height: 1.6;
}

.chat-bubble-user {
    background: #1e3a5f;
    color: #e8eaf0;
    border: 1px solid #2d5a8e;
    border-bottom-right-radius: 4px;
}

.chat-bubble-assistant {
    background: #252b3b;
    color: #e8eaf0;
    border: 1px solid #2d3548;
    border-bottom-left-radius: 4px;
}

.chat-bubble-text {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
}

/* ── Pending / thinking bubble (FEAT-079) ────────────────────── */
.chat-bubble-pending {
    color: #7a8097;
    font-style: italic;
    opacity: 0.85;
}

body[data-theme="light"] .chat-bubble-pending { color: #9aa0b0; }

/* ── SQL debug toggle ────────────────────────────────────────── */
.chat-debug-bar {
    padding: 5px 24px;
    border-top: 1px solid #2d3548;
    background: #1e2537;
    flex-shrink: 0;
}

.chat-sql-details {
    margin-top: 8px;
    border-top: 1px solid #2d3548;
    padding-top: 8px;
}

.chat-sql-summary {
    cursor: pointer;
    font-size: 0.78rem;
    color: #7a8097;
    user-select: none;
}

.chat-sql-code {
    background: #151924;
    border: 1px solid #2d3548;
    border-radius: 4px;
    padding: 8px 12px;
    font-size: 0.79rem;
    color: #a8d5ff;
    overflow-x: auto;
    margin-top: 6px;
    white-space: pre-wrap;
    word-break: break-word;
}

/* ── Sources ─────────────────────────────────────────────────── */
.chat-sources {
    margin-top: 8px;
    border-top: 1px solid #2d3548;
    padding-top: 6px;
}

.chat-sources-label {
    font-size: 0.76rem;
    color: #7a8097;
    font-weight: 500;
}

.chat-sources-list {
    margin: 3px 0 0 0;
    padding-left: 18px;
    font-size: 0.76rem;
    color: #7a8097;
}

/* ── Status ──────────────────────────────────────────────────── */
.chat-status {
    padding: 3px 24px;
    font-size: 0.78rem;
    color: #e8854a;
    min-height: 22px;
    flex-shrink: 0;
}

/* ── Input row ───────────────────────────────────────────────── */
.chat-input-row {
    display: flex;
    gap: 10px;
    padding: 14px 24px;
    border-top: 1px solid #2d3548;
    background: #1e2537;
    flex-shrink: 0;
    align-items: flex-end;
}

.chat-input-box {
    flex: 1;
    min-height: 68px;
    max-height: 150px;
    resize: vertical;
    background: #252b3b !important;
    border-color: #2d3548 !important;
    color: #e8eaf0 !important;
    font-size: 0.9rem;
}

.chat-input-box:focus {
    background: #252b3b !important;
    border-color: #4a9eff !important;
    color: #e8eaf0 !important;
    box-shadow: 0 0 0 2px rgba(74, 158, 255, 0.18) !important;
    outline: none !important;
}

.chat-input-box::placeholder {
    color: #5a6478 !important;
}

.chat-send-btn {
    height: 38px;
    padding: 0 22px;
    white-space: nowrap;
    flex-shrink: 0;
}

/* ── Light theme ─────────────────────────────────────────────── */
body[data-theme="light"] .chat-page             { background: #f0f2f5; color: #1a1f2e; }
body[data-theme="light"] .chat-header           { background: #ffffff; border-bottom-color: #dde1eb; }
body[data-theme="light"] .chat-title            { color: #0056b3; }
body[data-theme="light"] .chat-sidebar          { background: #f8f9fc; border-right-color: #dde1eb; }
body[data-theme="light"] .chat-conv-item        { color: #4a5568; }
body[data-theme="light"] .chat-conv-item:hover  { background: #eef0f5; color: #1a1f2e; }
body[data-theme="light"] .chat-conv-active      { background: #dbeafe !important; color: #1e40af !important; border-color: #93c5fd !important; }
body[data-theme="light"] .chat-conv-row:hover   { background: #eef0f5; }
body[data-theme="light"] .chat-conv-row.chat-conv-active { background: #dbeafe !important; border-color: #93c5fd !important; }
body[data-theme="light"] .chat-conv-item-name   { color: #4a5568; }
body[data-theme="light"] .chat-conv-row:hover .chat-conv-item-name { color: #1a1f2e; }
body[data-theme="light"] .chat-conv-row.chat-conv-active .chat-conv-item-name { color: #1e40af !important; }
body[data-theme="light"] .chat-conv-delete-btn  { color: #9aa0b0; }
body[data-theme="light"] .chat-conv-delete-btn:hover { color: #c0392b; background: rgba(192, 57, 43, 0.1); }
body[data-theme="light"] .chat-panel            { background: #f0f2f5; }
body[data-theme="light"] .chat-bubble-user      { background: #2563eb; color: #ffffff; border-color: #1d4ed8; }
body[data-theme="light"] .chat-bubble-assistant { background: #ffffff; color: #1a1f2e; border-color: #dde1eb; }
body[data-theme="light"] .chat-debug-bar        { background: #f8f9fc; border-top-color: #dde1eb; }
body[data-theme="light"] .chat-sql-code         { background: #f0f4ff; border-color: #c5d3f0; color: #1e3a8a; }
body[data-theme="light"] .chat-sources-label,
body[data-theme="light"] .chat-sources-list     { color: #5a6478; }
body[data-theme="light"] .chat-input-row        { background: #f8f9fc; border-top-color: #dde1eb; }
body[data-theme="light"] .chat-input-box        { background: #ffffff !important; border-color: #c5cad8 !important; color: #1a1f2e !important; }
body[data-theme="light"] .chat-input-box:focus  { border-color: #2563eb !important; box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2) !important; }
body[data-theme="light"] .chat-input-box::placeholder { color: #9aa0b0 !important; }
body[data-theme="light"] .chat-conv-empty       { color: #9aa0b0; }
body[data-theme="light"] .chat-empty-text       { color: #9aa0b0; }
body[data-theme="light"] .chat-status           { color: #d9480f; }

/* ── Results table (BUG-151) ────────────────────────────────── */
.chat-results-wrap {
    margin-top: 8px;
    border-top: 1px solid #2d3548;
    padding-top: 8px;
    overflow-x: auto;
}

.chat-results-table {
    border-collapse: collapse;
    font-size: 0.78rem;
    min-width: max-content;
}

.chat-results-th {
    background: #1e2537;
    color: #b0b8d0;
    padding: 5px 10px;
    border: 1px solid #2d3548;
    text-align: left;
    white-space: nowrap;
    font-weight: 600;
}

.chat-results-td {
    padding: 4px 10px;
    border: 1px solid #2d3548;
    color: #e8eaf0;
    white-space: nowrap;
}

.chat-results-note {
    margin-top: 4px;
    font-size: 0.74rem;
    color: #7a8097;
    font-style: italic;
}

body[data-theme="light"] .chat-results-wrap  { border-top-color: #dde1eb; }
body[data-theme="light"] .chat-results-th    { background: #f0f2f5; color: #4a5568; border-color: #dde1eb; }
body[data-theme="light"] .chat-results-td    { border-color: #dde1eb; color: #1a1f2e; }
body[data-theme="light"] .chat-results-note  { color: #9aa0b0; }

/* ── Async row-loading placeholder (BUG-159) ─────────────────── */
.chat-rows-loading {
    margin-top: 8px;
    border-top: 1px solid #2d3548;
    padding-top: 8px;
    font-size: 0.78rem;
    color: #7a8097;
    font-style: italic;
}

body[data-theme="light"] .chat-rows-loading { border-top-color: #dde1eb; color: #9aa0b0; }

/* ── Timing line (FEAT-076) ──────────────────────────────────── */
.chat-timing-line {
    margin-top: 7px;
    font-size: 0.74rem;
    color: #7a8097;
    border-top: 1px solid #2d3548;
    padding-top: 5px;
}

body[data-theme="light"] .chat-timing-line { color: #9aa0b0; border-top-color: #dde1eb; }

/* ── Inline chart (FEAT-084) ─────────────────────────────────── */
.chat-inline-chart {
    margin-top: 8px;
    border-top: 1px solid #2d3548;
    padding-top: 4px;
}

body[data-theme="light"] .chat-inline-chart { border-top-color: #dde1eb; }

/* ── Inline image thumbnails (FEAT-085) ──────────────────────── */
.chat-thumb-img {
    max-height: 80px;
    cursor: pointer;
    object-fit: contain;
    border-radius: 3px;
    display: block;
    transition: opacity 0.15s;
}

.chat-thumb-img:hover { opacity: 0.85; }

.chat-img-fallback {
    font-size: 0.74rem;
    color: #5a6478;
    font-style: italic;
}

/* ── Chat lightbox overlay (FEAT-085) ────────────────────────── */
.chat-lightbox-hidden { display: none; }

.chat-lightbox-open {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.82);
    z-index: 9999;
}

.chat-lightbox-dialog {
    position: relative;
    max-width: 90vw;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    background: #1e2537;
    border: 1px solid #2d3548;
    border-radius: 8px;
    overflow: hidden;
}

.chat-lightbox-close {
    position: absolute;
    top: 8px;
    right: 10px;
    background: rgba(0,0,0,0.4);
    border: none;
    color: #e8eaf0;
    font-size: 1rem;
    line-height: 1;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-lightbox-close:hover { background: rgba(224,82,82,0.7); }

.chat-lightbox-body {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 36px 20px 20px;
    min-width: 220px;
    min-height: 120px;
}

.chat-lightbox-main-img {
    max-width: 85vw;
    max-height: 80vh;
    object-fit: contain;
    border-radius: 4px;
    display: block;
}

.chat-lightbox-no-img {
    font-size: 0.88rem;
    color: #7a8097;
    font-style: italic;
    padding: 20px;
}

body[data-theme="light"] .chat-img-fallback { color: #9aa0b0; }
body[data-theme="light"] .chat-lightbox-dialog { background: #ffffff; border-color: #dde1eb; }
body[data-theme="light"] .chat-lightbox-close { color: #1a1f2e; }
body[data-theme="light"] .chat-lightbox-no-img { color: #9aa0b0; }

/* ── Mobile (≤ 640 px) ───────────────────────────────────────── */
/* BUG-169: stack sidebar above panel so conversations stay reachable;
   no horizontal overflow on 393px (iPhone 14). */
@media (max-width: 640px) {
    .chat-page {
        width: 100%;
        overflow-x: hidden;
    }
    .chat-body {
        flex-direction: column;
        overflow-x: hidden;
    }
    .chat-sidebar {
        width: 100%;
        max-height: 130px;
        border-right: none;
        border-bottom: 1px solid #2d3548;
        flex-shrink: 0;
        padding: 8px;
        gap: 3px;
        overflow-y: auto;
    }
    body[data-theme="light"] .chat-sidebar {
        border-bottom-color: #dde1eb;
    }
    .chat-panel  { width: 100%; min-width: 0; }
    .chat-bubble    { max-width: 92%; }
    .chat-input-row { padding: 10px 12px; }
    .chat-log       { padding: 12px; }
    .chat-header    { padding: 10px 14px; }
}
