/**
 * KalpVruksh - Chat Module Styles
 * Theme-aware (light/dark) using existing --kv-* tokens from kalpvruksh.css
 * Screens: chat.html (conversation list) + chatroom.html (message thread)
 */

/* ===== Chat-specific tokens ===== */
:root {
    --chat-in-bg: #ffffff;
    --chat-in-text: #1a1a1a;
    --chat-out-bg: #1B5E20;
    --chat-out-text: #ffffff;
    --chat-thread-bg: #EFEEF3;
    --chat-bar-bg: #ffffff;
    --chat-meta: #9aa0a6;
    --chat-divider: #e3e3ea;
    --chat-online: #4CAF50;
}
.theme-dark {
    --chat-in-bg: #23233a;
    --chat-in-text: #f1f1f4;
    --chat-out-bg: #2E7D32;
    --chat-out-text: #ffffff;
    --chat-thread-bg: #0f1117;
    --chat-bar-bg: #1a1a2e;
    --chat-meta: #7c828c;
    --chat-divider: #2b2b3d;
    --chat-online: #66BB6A;
}

/* ============================================================
   CONVERSATION LIST  (chat.html)
   ============================================================ */
.chat-search {
    position: relative;
    margin-bottom: 14px;
}
.chat-search input {
    width: 100%;
    height: 44px;
    border: none;
    border-radius: 14px;
    background: var(--kv-input-bg);
    color: var(--kv-text);
    padding: 0 40px 0 42px;
    font-size: 14px;
    outline: none;
}
.chat-search .search-icon {
    position: absolute;
    left: 15px; top: 50%;
    transform: translateY(-50%);
    color: var(--chat-meta);
    font-size: 16px;
}

/* Filter chips */
.chat-filter {
    display: flex;
    gap: 8px;
    margin-bottom: 14px;
}
.chat-filter .chip {
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    background: var(--kv-input-bg);
    color: var(--kv-text-muted);
    cursor: pointer;
    border: none;
    transition: all .2s;
}
.chat-filter .chip.active {
    background: linear-gradient(135deg, var(--kv-primary), var(--kv-primary-light));
    color: #fff;
}

/* Conversation row */
.conv-list { margin: 0 -4px; }
.conv-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 8px;
    border-radius: 14px;
    text-decoration: none;
    transition: background .15s;
}
.conv-item:active { background: var(--kv-input-bg); }
.conv-item + .conv-item { border-top: 1px solid var(--chat-divider); border-radius: 0; }

.conv-avatar {
    position: relative;
    width: 52px; height: 52px;
    border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-weight: 700; font-size: 18px;
    flex-shrink: 0;
}
.conv-avatar .dot {
    position: absolute;
    right: -1px; bottom: -1px;
    width: 14px; height: 14px;
    border-radius: 50%;
    background: var(--chat-meta);
    border: 2.5px solid var(--kv-card-bg);
}
.conv-avatar .dot.online { background: var(--chat-online); }

.conv-body { flex: 1; min-width: 0; }
.conv-top {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 3px;
}
.conv-name {
    font-size: 15px; font-weight: 700; color: var(--kv-text);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    max-width: 62%;
}
.conv-time { font-size: 11px; color: var(--chat-meta); flex-shrink: 0; }
.conv-bottom {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.conv-preview {
    font-size: 13px; color: var(--kv-text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    flex: 1;
}
.conv-preview .tick { color: var(--kv-text-light); }
.conv-preview .tick.read { color: var(--kv-primary-light); }
.conv-item.unread .conv-name { color: var(--kv-text); }
.conv-item.unread .conv-preview { color: var(--kv-text); font-weight: 600; }

.conv-badge {
    min-width: 20px; height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: var(--kv-primary);
    color: #fff; font-size: 11px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}

/* Empty state */
.chat-empty {
    text-align: center;
    padding: 60px 20px;
    color: var(--kv-text-muted);
}
.chat-empty i { font-size: 44px; opacity: .3; }

/* ============================================================
   CHAT THREAD  (chatroom.html)
   ============================================================ */
body.chat-thread-page { background: var(--chat-thread-bg); }

/* Fixed top bar */
.thread-header {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 100;
    height: 62px;
    display: flex; align-items: center; gap: 10px;
    padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    background: linear-gradient(135deg, var(--kv-primary), var(--kv-primary-light));
    box-shadow: 0 2px 10px rgba(0,0,0,.12);
}
.thread-header .back-btn,
.thread-header .hdr-btn {
    width: 38px; height: 38px;
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-size: 20px;
    background: rgba(255,255,255,.14);
    border-radius: 12px; border: none;
    flex-shrink: 0; text-decoration: none;
}
.thread-header .thread-avatar {
    width: 40px; height: 40px;
    border-radius: 12px;
    background: rgba(255,255,255,.2);
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-weight: 700; font-size: 15px;
    position: relative; flex-shrink: 0;
}
.thread-header .thread-avatar .dot {
    position: absolute; right: -2px; bottom: -2px;
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--chat-meta); border: 2px solid var(--kv-primary);
}
.thread-header .thread-avatar .dot.online { background: #A5D6A7; }
.thread-title { flex: 1; min-width: 0; }
.thread-title h6 {
    color: #fff; font-size: 15px; font-weight: 700; margin: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.thread-title span {
    color: rgba(255,255,255,.8); font-size: 11.5px;
}
.thread-title span.typing { color: #C8E6C9; font-style: italic; }

/* Messages scroll area */
.thread-body {
    padding: 78px 12px 88px;
    min-height: 100vh;
    display: flex; flex-direction: column;
}
.thread-scroll { flex: 1; }

/* Date separator */
.msg-date {
    text-align: center;
    margin: 14px 0 10px;
}
.msg-date span {
    font-size: 11px; font-weight: 600;
    color: var(--chat-meta);
    background: var(--kv-card-bg);
    padding: 4px 12px; border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,.06);
}

/* System chip */
.msg-system {
    text-align: center; margin: 10px 0;
}
.msg-system span {
    font-size: 11px; color: var(--chat-meta);
    background: var(--kv-input-bg);
    padding: 4px 12px; border-radius: 12px;
}

/* Message row + bubble */
.msg-row { display: flex; margin-bottom: 8px; align-items: flex-end; gap: 6px; }
.msg-row.out { flex-direction: row-reverse; }
.msg-mini-avatar {
    width: 28px; height: 28px; border-radius: 9px;
    background: var(--kv-primary-light); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.msg-row.out .msg-mini-avatar { visibility: hidden; }

/* Column wrapper holds the bubble + reactions. The max-width lives HERE
   (a flex child of the row, which has a definite width) so the bubble sizes
   to its content instead of collapsing into a narrow column. */
.msg-col {
    display: flex;
    flex-direction: column;
    max-width: 76%;
    min-width: 0;
}
.msg-row.in  .msg-col { align-items: flex-start; }
.msg-row.out .msg-col { align-items: flex-end; }

.bubble {
    max-width: 100%;
    padding: 8px 12px 6px;
    border-radius: 16px;
    font-size: 14px; line-height: 1.45;
    overflow-wrap: break-word;   /* break only long unbreakable words, not every word */
    position: relative;
    box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.msg-row.in .bubble {
    background: var(--chat-in-bg);
    color: var(--chat-in-text);
    border-bottom-left-radius: 5px;
}
.msg-row.out .bubble {
    background: var(--chat-out-bg);
    color: var(--chat-out-text);
    border-bottom-right-radius: 5px;
}
.bubble .msg-text { white-space: pre-wrap; }

/* meta row inside bubble */
.bubble .meta {
    display: flex; align-items: center; justify-content: flex-end;
    gap: 4px; margin-top: 3px;
}
.bubble .meta .time { font-size: 10px; opacity: .7; }
.msg-row.in .bubble .meta .time { color: var(--chat-meta); }
.bubble .meta .tick { font-size: 13px; opacity: .85; }
.bubble .meta .tick.read { color: #A5D6A7; }

/* reply-to quote inside a bubble */
.bubble .quote {
    border-left: 3px solid rgba(255,255,255,.5);
    padding: 4px 8px; margin-bottom: 5px;
    border-radius: 6px;
    font-size: 12.5px;
    background: rgba(0,0,0,.06);
    opacity: .9;
}
.msg-row.in .bubble .quote { border-left-color: var(--kv-primary-light); background: rgba(0,0,0,.04); }

/* deleted */
.msg-deleted {
    font-style: italic; font-size: 12.5px;
    color: var(--chat-meta) !important;
    background: transparent !important; box-shadow: none !important;
    border: 1px dashed var(--chat-divider);
}

/* Image message (WhatsApp-style thumbnail) */
.bubble.media { padding: 3px; }
.msg-image-wrap {
    position: relative;
    display: block;
    width: 230px; max-width: 65vw;
    border-radius: 13px;
    overflow: hidden;
    cursor: pointer;
    background: rgba(0,0,0,.05);
}
.msg-image {
    display: block; width: 100%;
    max-height: 320px; object-fit: cover;
}
.msg-image-dl {
    position: absolute; top: 8px; right: 8px;
    width: 30px; height: 30px; border-radius: 50%;
    background: rgba(0,0,0,.5); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 15px; backdrop-filter: blur(2px);
}
/* time chip overlaid on the image bottom-right */
.msg-image-time {
    position: absolute; right: 6px; bottom: 6px;
    display: flex; align-items: center; gap: 3px;
    padding: 2px 7px; border-radius: 10px;
    background: rgba(0,0,0,.45); color: #fff;
    font-size: 10px;
}
.msg-image-time .tick.read { color: #A5D6A7; }

/* Document card (WhatsApp-style) */
.msg-doc {
    display: flex; align-items: center; gap: 10px;
    min-width: 210px; max-width: 260px;
    padding: 9px 10px; border-radius: 10px;
    background: rgba(0,0,0,.05);
    cursor: pointer;
}
.msg-row.out .msg-doc { background: rgba(255,255,255,.14); }
.msg-doc .doc-icon {
    width: 42px; height: 42px; border-radius: 10px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; color: #fff;
}
.doc-icon.pdf { background: #E53935; }
.doc-icon.excel { background: #2E7D32; }
.doc-icon.word { background: #1565C0; }
.doc-icon.generic { background: #607D8B; }
.msg-doc .doc-info { flex: 1; min-width: 0; }
.msg-doc .doc-name {
    font-size: 13px; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg-doc .doc-meta { font-size: 11px; opacity: .7; margin-top: 1px; }
.msg-doc .doc-dl {
    width: 34px; height: 34px; flex-shrink: 0;
    border: none; border-radius: 50%;
    background: rgba(0,0,0,.08); color: inherit;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px;
}
.msg-row.out .msg-doc .doc-dl { background: rgba(255,255,255,.22); }

/* Fullscreen image viewer (lightbox) */
.img-lightbox {
    position: fixed; inset: 0; z-index: 3000;
    background: rgba(0,0,0,.94);
    display: none; flex-direction: column;
}
.img-lightbox.show { display: flex; }
.img-lightbox .lb-bar {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top));
    color: #fff;
}
.img-lightbox .lb-name {
    flex: 1; font-size: 14px; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.img-lightbox .lb-btn {
    width: 40px; height: 40px; flex-shrink: 0;
    border: none; border-radius: 12px;
    background: rgba(255,255,255,.15); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px;
}
.img-lightbox .lb-img {
    flex: 1; display: flex; align-items: center; justify-content: center;
    padding: 10px; overflow: auto;
}
.img-lightbox .lb-img img {
    max-width: 100%; max-height: 100%;
    border-radius: 6px; object-fit: contain;
}

/* Scrip card */
.scrip-card {
    min-width: 190px;
    border-radius: 12px;
    padding: 10px 12px;
    border: 1px solid var(--kv-border);
    background: var(--kv-card-bg);
    color: var(--kv-text);
}
.scrip-card.up { border-color: rgba(76,175,80,.5); }
.scrip-card.down { border-color: rgba(239,83,80,.5); }
.scrip-card .sc-top {
    display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px;
}
.scrip-card .sc-sym { font-size: 14px; font-weight: 700; letter-spacing: .3px; }
.scrip-card .sc-exch {
    font-size: 9px; font-weight: 700; padding: 2px 6px;
    border-radius: 6px; background: var(--kv-input-bg); color: var(--kv-text-muted);
}
.scrip-card .sc-price { font-size: 19px; font-weight: 700; }
.scrip-card.up .sc-price, .scrip-card.up .sc-chg { color: var(--kv-success); }
.scrip-card.down .sc-price, .scrip-card.down .sc-chg { color: var(--kv-danger); }
.scrip-card .sc-chg { font-size: 12px; font-weight: 600; margin-left: 6px; }
.scrip-card .sc-foot {
    display: flex; align-items: center; justify-content: space-between;
    margin-top: 6px; font-size: 10px; color: var(--chat-meta);
}

/* Alert card (margin call / sq-off / settlement / expiry) */
.alert-card {
    align-self: center;
    max-width: 90%;
    margin: 8px auto;
    border-radius: 14px;
    padding: 10px 14px;
    border-left: 4px solid var(--kv-warning);
    background: var(--kv-card-bg);
    box-shadow: 0 1px 4px rgba(0,0,0,.08);
    width: 100%;
}
.alert-card.error { border-left-color: var(--kv-danger); }
.alert-card.info  { border-left-color: #29B6F6; }
.alert-card.warning { border-left-color: var(--kv-warning); }
.alert-card .ac-top {
    display: flex; align-items: center; justify-content: space-between; margin-bottom: 3px;
}
.alert-card .ac-label {
    font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px;
    display: flex; align-items: center; gap: 6px;
}
.alert-card.error .ac-label { color: var(--kv-danger); }
.alert-card.warning .ac-label { color: var(--kv-warning); }
.alert-card.info .ac-label { color: #29B6F6; }
.alert-card .ac-time { font-size: 10px; color: var(--chat-meta); }
.alert-card .ac-text { font-size: 13px; color: var(--kv-text); }
.alert-card .ac-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.alert-card .ac-tags .tag {
    font-size: 10px; padding: 2px 8px; border-radius: 8px;
    background: var(--kv-input-bg); color: var(--kv-text-muted);
}

/* Typing indicator */
.typing-row { display: flex; align-items: flex-end; gap: 6px; margin-bottom: 8px; }
.typing-bubble {
    background: var(--chat-in-bg);
    padding: 12px 14px; border-radius: 16px; border-bottom-left-radius: 5px;
    display: flex; gap: 4px;
}
.typing-bubble span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--chat-meta);
    animation: typingBounce 1.2s infinite;
}
.typing-bubble span:nth-child(2) { animation-delay: .2s; }
.typing-bubble span:nth-child(3) { animation-delay: .4s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Reply banner (composer) */
.reply-banner {
    position: fixed;
    left: 0; right: 0; bottom: 66px;
    z-index: 99;
    display: flex; align-items: center; gap: 10px;
    padding: 8px 14px;
    background: var(--kv-input-bg);
    border-top: 1px solid var(--chat-divider);
}
.reply-banner .rb-line { width: 3px; align-self: stretch; border-radius: 2px; background: var(--kv-primary); }
.reply-banner .rb-body { flex: 1; min-width: 0; }
.reply-banner .rb-title { font-size: 11px; font-weight: 700; color: var(--kv-primary-light); }
.reply-banner .rb-text {
    font-size: 12px; color: var(--kv-text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.reply-banner .rb-close { background: none; border: none; color: var(--chat-meta); font-size: 18px; }

/* Scrip hint above composer */
.scrip-hint {
    position: fixed; left: 0; right: 0; bottom: 66px; z-index: 98;
    padding: 7px 14px; font-size: 12px; font-weight: 600;
    background: rgba(27,94,32,.08); color: var(--kv-primary-light);
    border-top: 1px solid rgba(27,94,32,.15);
}

/* Composer / input bar */
.composer {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 100;
    display: flex; align-items: flex-end; gap: 8px;
    padding: 9px 12px;
    padding-bottom: calc(9px + env(safe-area-inset-bottom));
    background: var(--chat-bar-bg);
    border-top: 1px solid var(--chat-divider);
}
.composer .comp-btn {
    width: 40px; height: 40px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    border: none; border-radius: 12px;
    background: var(--kv-input-bg); color: var(--kv-text-muted);
    font-size: 19px;
}
.composer .comp-input {
    flex: 1;
    background: var(--kv-input-bg);
    border-radius: 20px;
    padding: 9px 14px;
    min-height: 40px; max-height: 120px;
}
.composer .comp-input textarea {
    width: 100%; border: none; background: transparent; resize: none;
    color: var(--kv-text); font-size: 14px; line-height: 1.4;
    outline: none; max-height: 100px; font-family: inherit;
}
.composer .send-btn {
    width: 44px; height: 44px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    border: none; border-radius: 14px;
    background: linear-gradient(135deg, var(--kv-primary), var(--kv-primary-light));
    color: #fff; font-size: 19px;
    box-shadow: 0 3px 8px rgba(27,94,32,.35);
}
.composer .send-btn:disabled { opacity: .5; box-shadow: none; }
.composer .send-btn.scrip { background: linear-gradient(135deg, #2E7D32, #4CAF50); }

/* Long-press / action menu (context) */
.msg-actions {
    position: fixed; z-index: 200;
    background: var(--kv-card-bg);
    border-radius: 14px;
    box-shadow: 0 8px 30px rgba(0,0,0,.22);
    padding: 6px;
    display: none;
}
.msg-actions.show { display: block; }
.msg-actions button {
    display: flex; align-items: center; gap: 10px;
    width: 100%; padding: 9px 16px;
    background: none; border: none;
    color: var(--kv-text); font-size: 14px; text-align: left;
    border-radius: 8px;
}
.msg-actions button:active { background: var(--kv-input-bg); }
.msg-actions button.danger { color: var(--kv-danger); }
.msg-actions .react-row {
    display: flex; gap: 4px; padding: 4px 8px 8px;
    border-bottom: 1px solid var(--chat-divider); margin-bottom: 4px;
}
.msg-actions .react-row span { font-size: 22px; padding: 2px 4px; cursor: pointer; }

/* reactions shown under a bubble */
.msg-reactions { display: flex; gap: 4px; margin-top: 3px; }
.msg-row.out .msg-reactions { justify-content: flex-end; }
.msg-reactions .rx {
    font-size: 11px; padding: 1px 7px; border-radius: 10px;
    background: var(--kv-card-bg); border: 1px solid var(--chat-divider);
    box-shadow: 0 1px 2px rgba(0,0,0,.05);
}

/* Connection status banner */
.conn-banner {
    position: fixed; top: 62px; left: 0; right: 0; z-index: 90;
    text-align: center; font-size: 12px; font-weight: 600;
    padding: 5px; color: #fff;
    background: var(--kv-warning);
}
.conn-banner.hide { display: none; }
