:root {
    --bg: #f7f7f8;
    --surface: #ffffff;
    --border: #e3e3e8;
    --muted: #888;
    --accent: #2b6cff;
    --bubble-in: #f1f0ee;
    --bubble-out: #d6e7ff;
    --topbar-h: 50px;
    --sidebar-w: 320px;
    --panel-w: 320px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: #222;
    overflow: hidden;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.5rem 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    height: var(--topbar-h);
}

.topbar .user { color: var(--muted); margin-left: auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar a { color: var(--accent); text-decoration: none; }
.topbar .nav-link { flex: 0 0 auto; }
/* Icons are mobile-only; desktop shows the text label. */
.topbar .nav-icon { display: none; }

/* Login */
.login-page {
    display: grid;
    place-items: center;
    min-height: calc(100dvh - var(--topbar-h));
}

.login-form {
    background: var(--surface);
    padding: 2rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: min(320px, 100% - 2rem);
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.login-form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; }
.login-form input { padding: 0.5rem; border: 1px solid var(--border); border-radius: 4px; }
.login-form button { padding: 0.6rem; background: var(--accent); color: white; border: 0; border-radius: 4px; cursor: pointer; }
.login-form .error { background: #ffe8e8; padding: 0.5rem; border-radius: 4px; color: #a00; font-size: 0.85rem; }

/* ===== Chat layout — desktop ===== */

.chat-layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    height: calc(100dvh - var(--topbar-h));
    min-height: 0;
}
.chat-layout.has-active {
    grid-template-columns: var(--sidebar-w) 1fr var(--panel-w);
}

/* ===== Sidebar ===== */

.chat-sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    grid-column: 1;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}
.sidebar-header h2 { margin: 0; font-size: 1rem; flex: 0 0 auto; }
.sidebar-close { display: none; margin-left: auto; }

/* Live search box */
.sidebar-search { position: relative; flex: 1 1 auto; min-width: 0; }
.sidebar-search-input {
    width: 100%;
    box-sizing: border-box;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.3rem 1.7rem 0.3rem 0.7rem;
    font-size: 0.8rem;
    background: var(--bg);
    color: #222;
}
.sidebar-search-input:focus { outline: none; border-color: var(--accent); }
.sidebar-search-clear {
    position: absolute;
    right: 0.35rem;
    top: 50%;
    transform: translateY(-50%);
    border: none;
    background: none;
    cursor: pointer;
    font-size: 1.1rem;
    line-height: 1;
    color: var(--muted);
    padding: 0 0.25rem;
}
.sidebar-search-clear:hover { color: #222; }
.sidebar-search-clear[hidden] { display: none; }

/* Filters bar */
.sidebar-filters {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    position: relative;
    flex: 0 0 auto;
}
.filter-toggle {
    border: 1px solid var(--border);
    background: var(--bg);
    border-radius: 999px;
    padding: 0.25rem 0.65rem;
    font-size: 0.8rem;
    cursor: pointer;
    color: #222;
}
.filter-toggle.has-selection { background: #eef3ff; border-color: var(--accent); color: var(--accent); }
.filter-toggle .caret { font-size: 0.65rem; margin-left: 0.15rem; }

.filter-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    z-index: 30;
    min-width: 180px;
}
.filter-dropdown[hidden] { display: none; }
.filter-dropdown label { font-size: 0.85rem; display: flex; gap: 0.4rem; align-items: center; cursor: pointer; }
.filter-checkbox { font-size: 0.8rem; display: flex; gap: 0.35rem; align-items: center; cursor: pointer; color: #444; }

/* Tri-state status options: neutral (☐) → include (✓) → exclude (✕). */
.filter-dropdown-tri { min-width: 210px; }
.tri-option {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    width: 100%;
    border: none;
    background: none;
    padding: 0.2rem 0.15rem;
    font-size: 0.85rem;
    text-align: left;
    cursor: pointer;
    color: #222;
    border-radius: 4px;
}
.tri-option:hover { background: var(--bg); }
.tri-box {
    flex: 0 0 auto;
    width: 1.1em;
    text-align: center;
    font-size: 0.95rem;
    line-height: 1;
    color: #999;
}
.tri-box::before { content: '☐'; }
.tri-option.is-include .tri-box { color: #1a7f37; }
.tri-option.is-include .tri-box::before { content: '✓'; }
.tri-option.is-exclude .tri-box { color: #b22222; }
.tri-option.is-exclude .tri-box::before { content: '✕'; }
.tri-option.is-exclude .tri-label { color: #999; text-decoration: line-through; }
.tri-legend {
    margin-top: 0.3rem;
    padding-top: 0.35rem;
    border-top: 1px solid var(--border);
    font-size: 0.7rem;
    color: #888;
}
.tri-legend .tri-inc { color: #1a7f37; }
.tri-legend .tri-exc { color: #b22222; }

/* Red-flag filter: icon-only checkbox, bright when active, dim otherwise. */
.filter-red-flag input { display: none; }
.filter-red-flag .red-flag-filter-icon {
    font-size: 1rem;
    line-height: 1;
    filter: grayscale(1);
    opacity: 1;
    transition: filter 0.12s, opacity 0.12s;
}
.filter-red-flag.is-active .red-flag-filter-icon { filter: none; opacity: 1; }
.filter-red-flag.is-active { color: #b22222; }

/* Conversation list */
.conversation-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1 1 auto;
    min-height: 0;
}
.conversation-item.is-filtered-out { display: none; }
.conversation-item a {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    text-decoration: none;
    color: inherit;
}
.conversation-item.is-active a { background: #eef3ff; }
.conversation-item .title { display: flex; align-items: center; gap: 0.35rem; min-width: 0; }
.conversation-item .title .contact-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-item .title small { color: var(--muted); font-weight: normal; flex: 0 0 auto; }
.conversation-item .preview {
    display: flex;
    align-items: baseline;
    gap: 0.25rem;
    min-width: 0;
    font-size: 0.8rem;
    color: var(--muted);
}
.conversation-item .preview .preview-marker { color: var(--accent); flex: 0 0 auto; font-weight: 600; }
.conversation-item .preview .preview-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
    flex: 1 1 auto;
}
.conversation-item .preview.is-incoming .preview-text { color: #333; }
/* Unread: newest message is an incoming one the chatter hasn't opened yet. */
.conversation-item.is-unviewed .preview-text { font-weight: 700; color: #111; }
.conversation-item.is-unviewed .preview-marker { font-weight: 700; }
.conversation-item .meta { display: flex; gap: 0.5rem; color: var(--muted); font-size: 0.75rem; }
.conversation-item .red-flag-badge { flex: 0 0 auto; font-size: 0.9rem; line-height: 1; }

/* Tenant badge */
.tenant-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 22px;
    padding: 0 0.3rem;
    height: 16px;
    border-radius: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    background: #eef3ff;
    color: var(--accent);
    border: 1px solid #d8e2ff;
    flex: 0 0 auto;
    line-height: 1;
    letter-spacing: 0.02em;
    vertical-align: middle;
}

/* Channel icon */
.channel-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    flex: 0 0 auto;
    vertical-align: middle;
}
.channel-icon svg { display: block; width: 16px; height: 16px; }
.channel-fallback {
    width: 16px; height: 16px;
    border-radius: 50%;
    background: var(--muted);
    color: #fff;
    font-size: 0.65rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* ===== Chat detail frame ===== */

.chat-detail-frame {
    display: contents;
}

.chat-main {
    grid-column: 2;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg);
}

.chat-header {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    padding: 0.75rem 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}
.header-identity { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; flex: 1 1 auto; }
.header-identity strong { font-size: 0.95rem; display: flex; align-items: center; gap: 0.35rem; min-width: 0; }
.header-identity strong .contact-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.header-identity strong small { color: var(--muted); font-weight: normal; flex: 0 0 auto; }
.header-chips { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }

.chat-header .badge,
.chat-header .status,
.chat-header .price,
.chat-header .ai-mode {
    font-size: 0.75rem;
    color: var(--muted);
    background: var(--bg);
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
}

/* Editable chips (status / price popovers) */
.chip-editor { position: relative; }
.chip-trigger {
    border: 1px solid var(--border);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.75rem;
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    line-height: 1.2;
}
.chip-trigger:hover { background: #eef3ff; color: var(--accent); border-color: var(--accent); }
.chip-trigger .caret { font-size: 0.6rem; }

.chip-popover {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.10);
    padding: 0.5rem;
    z-index: 40;
    min-width: 220px;
}
.chip-popover[hidden] { display: none; }

.status-options {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}
.status-option {
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    padding: 0.4rem 0.6rem;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
    color: #222;
}
.status-option:hover { background: var(--bg); }
.status-option.is-current { background: #eef3ff; color: var(--accent); font-weight: 600; }

.chip-price-form { display: flex; flex-direction: column; gap: 0.5rem; }
.chip-price-form .price-row {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    font-size: 0.75rem;
    color: var(--muted);
}
.chip-price-form input,
.chip-price-form select {
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font: inherit;
    color: #222;
}
.chip-price-form .price-actions { display: flex; justify-content: flex-end; }
.chip-price-form button.primary {
    padding: 0.35rem 0.75rem;
    background: var(--accent);
    color: #fff;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
}

.header-back, .header-info, .sidebar-close, .panel-close { display: none; }
.icon-btn {
    width: 32px;
    height: 32px;
    border: 0;
    background: transparent;
    border-radius: 6px;
    font-size: 1.2rem;
    cursor: pointer;
    color: #555;
    line-height: 1;
}
.icon-btn:hover { background: var(--bg); }

.ai-toggle-form { flex: 0 0 auto; }
.ai-toggle-form button { border: 0; cursor: pointer; }
.ai-mode { font-size: 1em; }
.ai-mode.is-on { background: #e8f6e8; color: #1c6b2c; }
.ai-mode.is-off { background: #ffe8e8; color: #b22222; }

/* Header red-flag toggle: filled red when raised, dim when clear. */
.red-flag-form { flex: 0 0 auto; }
.red-flag-toggle {
    border: 1px solid var(--border);
    background: var(--bg);
    border-radius: 999px;
    padding: 0.15rem 0.5rem;
    font-size: 0.9rem;
    line-height: 1;
    cursor: pointer;
}
.red-flag-toggle.is-clear { filter: grayscale(1); opacity: 1; }
.red-flag-toggle.is-flagged { background: #ffe8e8; border-color: #b22222; opacity: 1; filter: none; }

.activity-feed {
    flex: 1 1 auto;
    overflow-y: auto;
    /* Feed scrolls vertically only; explicit so overflow-y:auto doesn't
       implicitly promote overflow-x to auto and show a stray h-scrollbar. */
    overflow-x: hidden;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 0;
}

.message { display: flex; flex-direction: column; max-width: 70%; }
.message.is-incoming { align-self: flex-start; }
.message.is-outgoing { align-self: flex-end; }

.message .bubble {
    padding: 0.5rem 0.75rem;
    border-radius: 12px;
    background: var(--bubble-in);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.message.is-outgoing .bubble { background: var(--bubble-out); }

.message time {
    font-size: 0.7rem;
    color: var(--muted);
    margin-top: 0.15rem;
    text-align: center;
}

.message .author { font-size: 0.7rem; color: var(--muted); }
.message.is-outgoing time,
.message.is-outgoing .author { align-self: flex-end; }

.message-event {
    align-self: center;
    color: var(--muted);
    font-size: 0.8rem;
    background: var(--surface);
    padding: 0.25rem 0.75rem;
    border-radius: 999px;
    border: 1px dashed var(--border);
}

.message-event.message-tool {
    font-size: 0.7rem;
    font-style: italic;
    color: #aaa;
    background: transparent;
    border: 0;
    padding: 0.1rem 0.5rem;
}

.message-event.message-red-flag.is-raised {
    color: #b22222;
    background: #ffecec;
    border-color: #e6a3a3;
    border-style: solid;
}
.message-event.message-red-flag.is-cleared { color: #1c6b2c; }

.message-chat img { max-width: 200px; max-height: 400px; border-radius: 6px; }
.message-event.message-tool time { margin-left: 0.4rem; }

/* Outgoing footer: author + time + delivery ticks on one line */
.message .message-meta { display: flex; align-items: center; gap: 0.3rem; }
.message.is-outgoing .message-meta { justify-content: flex-end; }
.message.is-incoming .message-meta { justify-content: flex-start; }
.message .message-meta time { margin-top: 0; }

.msg-ticks { font-size: 0.72rem; line-height: 1; color: var(--muted); letter-spacing: -2px; }
.msg-ticks.is-read { color: var(--accent); }
.msg-ticks.is-failed { color: #b22222; letter-spacing: 0; }

/* Quoted / replied-to message preview */
.reply-quote {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0.25rem 0.5rem;
    margin-bottom: 0.2rem;
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    background: rgba(43, 108, 255, 0.07);
    text-decoration: none;
    color: inherit;
    max-width: 100%;
}
.reply-quote.is-static { cursor: default; }
.reply-quote-author { font-size: 0.7rem; font-weight: 600; color: var(--accent); }
.reply-quote-text {
    font-size: 0.78rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-wrap: auto;
}

/* Forwarded message — compact label inside the bubble, like a reply quote */
.forward-origin {
    display: block;
    font-size: 0.72rem;
    color: var(--muted);
    font-style: italic;
    margin-bottom: 1px;
}
.forward-origin strong { color: var(--accent); font-style: normal; font-weight: 600; }
.message .bubble.is-forwarded { border-left: 3px solid var(--muted); border-top-left-radius: 4px; border-bottom-left-radius: 4px; }

/* Emoji reactions — sit in the bubble's bottom-right corner (the meta row) */
.reactions-corner { display: inline-flex; align-items: center; gap: 0.2rem; position: relative; }
.message.is-incoming .reactions-corner { margin-left: auto; }
.reaction-chip {
    font-size: 0.85rem;
    line-height: 1;
    padding: 0.05rem 0.3rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    cursor: default;
}
.reaction-chip.is-own { cursor: pointer; border-color: var(--accent); background: #eef3ff; }
.reaction-add {
    font-size: 0.95rem;
    line-height: 1;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    opacity: 0.45;
    transition: opacity 0.1s;
}
.reaction-add:hover { opacity: 1; }
.message:hover .reaction-add, .reactions-corner:focus-within .reaction-add { opacity: 0.85; }
.reaction-add-wrap { position: relative; display: inline-flex; }
.reaction-picker {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 0.25rem;
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    gap: 0.15rem;
    padding: 0.25rem;
    max-width: min(320px, 90vw);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    z-index: 5;
}
.reaction-picker[hidden] { display: none; }
.reaction-option {
    font-size: 1.1rem;
    line-height: 1;
    padding: 0.15rem;
    border: 0;
    background: transparent;
    cursor: pointer;
    border-radius: 6px;
}
.reaction-option:hover { background: var(--bg); }
.reaction-option {
    font-size: 1.1rem;
    line-height: 1;
    padding: 0.15rem;
    border: 0;
    background: transparent;
    cursor: pointer;
    border-radius: 6px;
}
.reaction-option:hover { background: var(--bg); }

.message-ad { align-self: center; max-width: 85%; width: 85%; }
.message-ad time { align-self: center; }
.ad-card {
    border: 1px solid var(--border);
    border-left: 3px solid #1877f2;
    border-radius: 10px;
    background: var(--surface);
    padding: 0.6rem 0.75rem;
}
.ad-card-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; margin-bottom: 0.5rem; }
.ad-card-title { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.ad-source-badge {
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: #fff;
    background: #1877f2;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
}
.ad-source-badge.is-tiktok { background: #000; }
.ad-source-badge.is-tg_start { background: #229ed9; }
.ad-card-body { display: flex; gap: 0.6rem; }
.ad-thumb img { width: 80px; height: 80px; object-fit: cover; border-radius: 6px; }
.ad-card-text { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; }
.ad-headline { font-weight: 600; font-size: 0.85rem; }
.ad-body { font-size: 0.8rem; color: var(--muted); white-space: pre-wrap; word-wrap: break-word; }
.ad-link { font-size: 0.78rem; }
.ad-card-meta { display: flex; gap: 0.75rem; margin-top: 0.5rem; font-size: 0.68rem; color: var(--muted); }

.attachment { display: flex; flex-direction: column; gap: 0.3rem; }
.attachment-transcript {
    font-size: 0.8rem;
    color: #555;
    font-style: italic;
    background: rgba(0, 0, 0, 0.04);
    border-left: 2px solid rgba(0, 0, 0, 0.15);
    padding: 0.3rem 0.5rem;
    border-radius: 4px;
    max-width: 360px;
    white-space: pre-wrap;
    word-wrap: break-word;
}
.pdf-preview { display: flex; flex-direction: column; gap: 0.3rem; }
.pdf-embed {
    width: 360px;
    max-width: 100%;
    height: 480px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    background: #fff;
}
.attachment-link {
    font-size: 0.85rem;
    color: #2563eb;
    text-decoration: none;
}
.attachment-link:hover { text-decoration: underline; }

/* ===== Tarot reading bubble ===== */

.message-tarot { max-width: 85%; width: 85%; }
.message-tarot time { align-self: flex-end; }

.tarot-bubble {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    padding: 0.5rem 0.75rem;
    width: 100%;
    box-sizing: border-box;
}
.message-tarot.is-draft .tarot-bubble { cursor: text; }
.message-tarot.is-draft .tarot-bubble.is-editing { cursor: default; }

.tarot-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}
.tarot-title { font-weight: 600; }
.tarot-title small { font-weight: 400; color: var(--muted); }

.tarot-header-right {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}
.tarot-send-form { margin: 0; display: inline-flex; }

.tarot-status-badge {
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    padding: 0.1rem 0.4rem;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.08);
    color: #333;
}
.tarot-status-badge.is-draft { background: #fde68a; color: #7c4a03; }
.tarot-status-badge.is-sent { background: #bbf7d0; color: #14532d; }

.tarot-meta {
    font-size: 0.75rem;
    color: var(--muted);
    line-height: 1.25;
}

.tarot-divider {
    border: 0;
    border-top: 1px solid rgba(0, 0, 0, 0.12);
    margin: 0.1rem 0;
}

.tarot-text { white-space: pre-wrap; word-wrap: break-word; }

.tarot-textarea {
    width: 100%;
    min-height: 140px;
    padding: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font: inherit;
    resize: vertical;
    background: #fff;
    box-sizing: border-box;
}

.tarot-edit-footer { display: contents; }

.tarot-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}
.tarot-actions form { margin: 0; }

/* Visibility driven by .is-editing on the bubble */
.tarot-bubble:not(.is-editing) .tarot-textarea,
.tarot-bubble:not(.is-editing) .tarot-edit-footer { display: none; }
.tarot-bubble.is-editing .tarot-text,
.tarot-bubble.is-editing .tarot-send-form { display: none; }

.tarot-btn {
    padding: 0.35rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: #fff;
    color: #222;
    cursor: pointer;
    font: inherit;
}
.tarot-btn-sm {
    padding: 0.1rem 0.5rem;
    font-size: 0.7rem;
    line-height: 1.2;
}
.tarot-btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}
.tarot-btn[disabled] { opacity: 0.6; cursor: default; }

.is-fading-out {
    opacity: 0;
    transform: scale(0.96);
    transition: opacity 250ms ease, transform 250ms ease;
    pointer-events: none;
}

.chat-empty-wrap { display: flex; align-items: center; justify-content: center; }
.chat-empty { color: var(--muted); padding: 2rem; }

.chat-reply {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex: 0 0 auto;
    align-items: flex-end;
}

/* Hidden native file input */
.chat-reply-file-input {
    display: none;
}

/* Dropzone — wraps preview + textarea */
.chat-reply-dropzone {
    flex: 1 1 auto;
    min-width: 0;
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    border: 2px dashed transparent;
    border-radius: 8px;
    transition: border-color 150ms ease, background 150ms ease;
    padding: 2px;
}
.chat-reply-dropzone.is-drag-over {
    border-color: var(--accent);
    background: #edf3ff;
}

/* Image preview strip */
.chat-reply-preview {
    position: relative;
    display: inline-flex;
    align-items: flex-start;
    align-self: flex-start;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid var(--border);
}
.chat-reply-preview[hidden] { display: none; }
.chat-reply-preview img {
    display: block;
    max-height: 120px;
    max-width: 220px;
    object-fit: cover;
    border-radius: 5px;
}
.chat-reply-preview-remove {
    position: absolute;
    top: 3px;
    right: 3px;
    width: 20px;
    height: 20px;
    line-height: 18px;
    text-align: center;
    border-radius: 50%;
    background: rgba(0,0,0,.55);
    color: #fff;
    border: 0;
    cursor: pointer;
    font-size: 14px;
    padding: 0;
}
.chat-reply-preview-remove:hover {
    background: rgba(0,0,0,.8);
}

.chat-reply textarea {
    flex: 1 1 auto;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    resize: vertical;
    min-height: 44px;
    max-height: 200px;
    font: inherit;
    width: 100%;
}
.chat-reply-dropzone.has-preview textarea {
    min-height: 36px;
}

/* Actions column: icon row on top, Send below */
.chat-reply-actions {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 0 0 auto;
    align-self: stretch;
}

/* Top row of small icon buttons */
.chat-reply-icon-row {
    display: flex;
    flex-direction: row;
    gap: 0.25rem;
    flex: 0 0 auto;
}

/* Shared style for small icon buttons (attach, AI) */
.chat-reply-icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 5px;
    cursor: pointer;
    color: var(--muted);
    transition: color 150ms, border-color 150ms;
    flex: 0 0 auto;
    position: relative;
}
.chat-reply-icon-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}
.chat-reply-icon-btn svg {
    display: block;
    width: 14px;
    height: 14px;
}

/* AI dropdown button caret */
.chat-reply-ai-btn .ai-caret {
    font-size: 9px;
    line-height: 1;
    color: var(--muted);
    position: absolute;
    bottom: 2px;
    right: 3px;
}

/* Wrapper that anchors the dropdown */
.chat-reply-ai-wrap {
    position: relative;
    display: flex;
    align-items: flex-end;
}

/* AI dropdown panel */
.chat-reply-ai-dropdown {
    position: absolute;
    bottom: calc(100% + 6px);
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,.1);
    min-width: 180px;
    padding: 0.25rem 0;
    z-index: 200;
}
.chat-reply-ai-dropdown[hidden] { display: none; }
.chat-reply-ai-dropdown button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 0.45rem 0.75rem;
    background: none;
    border: 0;
    cursor: pointer;
    font: inherit;
    font-size: 0.85rem;
    color: #333;
    white-space: nowrap;
}
.chat-reply-ai-dropdown button:hover {
    background: var(--bg);
    color: var(--accent);
}

/* Emoji picker */
.chat-reply-emoji-wrap {
    position: relative;
    display: flex;
    align-items: flex-end;
}
.chat-reply-emoji-panel {
    position: absolute;
    bottom: calc(100% + 6px);
    left: 0;
    width: 280px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,.1);
    padding: 0.4rem 0.5rem;
    z-index: 200;
}
.chat-reply-emoji-panel[hidden] { display: none; }
.emoji-panel-title {
    font-size: 0.7rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 0.25rem 0.1rem;
}
.emoji-panel-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 0.1rem;
    margin-bottom: 0.25rem;
}
.emoji-cell {
    border: 0;
    background: none;
    cursor: pointer;
    font-size: 1.25rem;
    line-height: 1;
    padding: 0.2rem;
    border-radius: 6px;
}
.emoji-cell:hover { background: var(--bg); }

/* :shortcode: autocomplete */
.chat-reply-emoji-autocomplete {
    position: absolute;
    bottom: calc(100% + 4px);
    left: 0;
    min-width: 200px;
    max-width: 320px;
    max-height: 220px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,.12);
    padding: 0.2rem;
    z-index: 200;
}
.chat-reply-emoji-autocomplete[hidden] { display: none; }
.emoji-ac-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    text-align: left;
    border: 0;
    background: none;
    cursor: pointer;
    font: inherit;
    padding: 0.3rem 0.45rem;
    border-radius: 6px;
}
.emoji-ac-item.is-active { background: #eef3ff; }
.emoji-ac-item:hover { background: var(--bg); }
.emoji-ac-char { font-size: 1.15rem; line-height: 1; }
.emoji-ac-name { font-size: 0.82rem; color: var(--muted); }

.chat-reply-send-btn {
    flex: 1 1 auto;
    padding: 0 0.9rem;
    background: var(--accent);
    color: white;
    border: 0;
    border-radius: 6px;
    cursor: pointer;
    white-space: nowrap;
    font: inherit;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 52px;
}
.chat-reply-send-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}
@keyframes send-spin {
    to { transform: rotate(360deg); }
}
.send-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,.35);
    border-top-color: #fff;
    border-radius: 50%;
    animation: send-spin 0.6s linear infinite;
}

/* ===== Contact panel ===== */

.contact-panel {
    grid-column: 3;
    background: var(--surface);
    border-left: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-height: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.25rem;
}
.panel-header h3 { margin: 0; }

.panel-form {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}
.panel-form label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.8rem;
    color: var(--muted);
}
.panel-form input,
.panel-form select {
    padding: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    color: #222;
    font: inherit;
}
.panel-form button {
    align-self: flex-start;
    padding: 0.4rem 0.75rem;
    background: var(--accent);
    color: white;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
}
.panel-meta {
    margin: 0;
    font-size: 0.8rem;
    color: var(--muted);
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.25rem 0.75rem;
}
.panel-meta dt { font-weight: 600; }
.panel-meta dd { margin: 0; word-break: break-all; }

/* ===== System config page ===== */

.system-config-page {
    height: calc(100dvh - var(--topbar-h));
    overflow-y: auto;
    background: var(--bg);
}
.system-config-container {
    max-width: 960px;
    margin: 0 auto;
    padding: 1.5rem 1rem 3rem;
}
.system-config-header h1 { margin: 0 0 0.25rem; font-size: 1.25rem; }
.system-config-hint { margin: 0 0 1.25rem; color: var(--muted); font-size: 0.85rem; }
.system-config-hint code,
.system-config-key {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.05rem 0.35rem;
    font-size: 0.85em;
}
.system-config-empty { color: var(--muted); }
.system-config-form { display: flex; flex-direction: column; gap: 1rem; }
.system-config-tabs {
    display: flex;
    gap: 0.25rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
}
.system-config-tab {
    appearance: none;
    background: transparent;
    border: 0;
    border-bottom: 2px solid transparent;
    padding: 0.55rem 0.9rem;
    margin-bottom: -1px;
    font: inherit;
    font-size: 0.9rem;
    color: var(--muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}
.system-config-tab:hover { color: inherit; }
.system-config-tab.is-active {
    color: inherit;
    border-bottom-color: var(--accent);
    font-weight: 600;
}
.system-config-tab-count {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0 0.45rem;
    font-size: 0.7rem;
    color: var(--muted);
}
.system-config-panel {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.system-config-panel[hidden] { display: none; }
.system-config-row {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.9rem 1rem;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}
.system-config-row label { font-size: 0.85rem; }
.system-config-row textarea {
    width: 100%;
    font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.4;
    padding: 0.5rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    resize: vertical;
    background: var(--bg);
    color: #222;
}
.system-config-description {
    margin: 0;
    color: var(--muted);
    font-size: 0.8rem;
}
.system-config-meta { margin: 0; color: var(--muted); font-size: 0.7rem; }
.system-config-actions {
    position: sticky;
    bottom: 0;
    background: var(--bg);
    padding: 0.75rem 0;
    display: flex;
    justify-content: flex-end;
}
.system-config-actions button.primary {
    padding: 0.55rem 1.25rem;
    background: var(--accent);
    color: #fff;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
}

.flash {
    padding: 0.55rem 0.75rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    font-size: 0.85rem;
}
.flash-success { background: #e6f7ec; color: #156a2d; border: 1px solid #b9e3c5; }
.flash-error { background: #ffe8e8; color: #a00; border: 1px solid #f4bcbc; }

.system-config-row input[type='text'],
.system-config-row input[type='number'],
.system-config-row select {
    width: 100%;
    box-sizing: border-box;
    font-family: inherit;
    font-size: 0.9rem;
    padding: 0.4rem 0.55rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
    color: #222;
}
.system-config-row input[disabled] { color: var(--muted); background: var(--surface); }

.geo-services { gap: 0.75rem; }
.geo-service {
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.6rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    background: var(--bg);
}
.geo-service-head {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
}
.geo-service-autosend {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.85rem;
    color: var(--muted);
}
.geo-service-autosend input { width: auto; }
.geo-service-remove {
    margin-left: auto;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.25rem 0.55rem;
    font-size: 0.75rem;
    color: var(--muted);
    cursor: pointer;
}
.geo-service-remove:hover { color: #a00; border-color: #f4bcbc; }
.geo-service label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.8rem; color: var(--muted); }
.geo-service-prices {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 0.5rem;
}
.geo-service-add {
    margin-top: 0.25rem;
}
.geo-service-add button {
    background: transparent;
    border: 1px dashed var(--border);
    border-radius: 4px;
    padding: 0.4rem 0.8rem;
    font: inherit;
    font-size: 0.85rem;
    color: var(--muted);
    cursor: pointer;
}
.geo-service-add button:hover { color: inherit; border-style: solid; }

/* ===== Finance pages ===== */

.finance-page {
    height: calc(100dvh - var(--topbar-h));
    overflow-y: auto;
    background: var(--bg);
}
.finance-container {
    padding: 1.5rem 1rem 3rem;
}
.finance-header { margin-bottom: 1rem; }
.finance-header h1 { margin: 0 0 0.25rem; font-size: 1.25rem; }
.finance-hint { margin: 0 0 1rem; color: var(--muted); font-size: 0.85rem; }
.finance-back { display: inline-block; margin-bottom: 0.5rem; color: var(--accent); text-decoration: none; font-size: 0.85rem; }
.finance-back:hover { text-decoration: underline; }
.finance-empty { color: var(--muted); }
.finance-muted { color: var(--muted); }

.finance-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    font-size: 0.9rem;
}
.finance-table th,
.finance-table td {
    text-align: left;
    padding: 0.6rem 0.9rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.finance-table th {
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
}
.finance-table tbody tr:last-child td { border-bottom: 0; }
.finance-row { cursor: pointer; }
.finance-row:hover { background: var(--bg); }
.finance-row.is-empty { cursor: default; }
.finance-row.is-empty:hover { background: transparent; }
.finance-cell-date { white-space: nowrap; min-width: 9rem; }
.finance-cell-date a { color: inherit; text-decoration: none; }
.finance-cell-date a:hover { text-decoration: underline; }
.finance-date-ymd { display: block; }
.finance-date-weekday { display: block; font-size: 0.8rem; color: var(--muted); }
.finance-cell-count, .finance-col-count { text-align: right; color: var(--muted); }
.finance-cell-totals { line-height: 1.35; }
.finance-total-in { font-weight: 500; }
.finance-total-out { font-size: 0.75rem; color: var(--muted); margin-top: 0.15rem; }

/* Date-range picker in the header */
.finance-range { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.finance-presets {
    font-weight: 200;
    font-size: small;
    flex-basis: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem 0.75rem;
}
.finance-presets a { color: var(--accent); text-decoration: none; }
.finance-presets a:hover { text-decoration: underline; }
.finance-date-input {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.2rem 0.4rem;
}
.finance-range-apply {
    font: inherit;
    font-size: 0.8rem;
    cursor: pointer;
    color: var(--accent);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.2rem 0.6rem;
}
.finance-range-apply:hover { background: var(--bg); }

/* Stats table — numeric columns + period totals line */
.finance-table-stats { font-variant-numeric: tabular-nums; }
.finance-table-stats th.finance-num,
.finance-table-stats td.finance-num { text-align: right; white-space: nowrap; }
.finance-cell-sales { line-height: 1.3; }
.finance-sales-usd { font-weight: 600; }
.finance-sales-orig { font-size: 0.75rem; color: var(--muted); margin-top: 0.1rem; }
.finance-total-row td { background: var(--bg); font-weight: 600; border-bottom: 2px solid var(--border); }
.finance-total-row .finance-sales-orig { font-weight: 400; }
.finance-highlight { background: #fffde7 !important; }

.finance-day-totals { margin-top: 0.25rem; display: flex; gap: 1.25rem; flex-wrap: wrap; font-size: 0.9rem; }
.finance-day-in strong, .finance-day-out strong { font-weight: 500; }
.finance-day-out { color: var(--muted); }

.finance-table-detail .finance-cell-amount { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 500; }
.finance-table-detail .finance-cell-amount.is-positive { color: #156a2d; }
.finance-table-detail .finance-cell-amount.is-negative { color: #a00; }
.finance-cell-time { color: var(--muted); white-space: nowrap; }
.finance-actor-ai {
    display: inline-block;
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    background: var(--bg);
    border: 1px solid var(--border);
    font-size: 0.75rem;
    color: var(--muted);
}
.finance-cell-contact a { color: var(--accent); text-decoration: none; }
.finance-cell-contact a:hover { text-decoration: underline; }
.finance-meta { font-size: 0.75rem; color: var(--muted); margin-top: 0.15rem; }
.finance-meta strong { font-weight: 500; color: inherit; }

/* Inline payment-datetime editor (finance day table) */
.pdt-editor { position: relative; display: inline-block; }
.pdt-trigger {
    border: 1px solid transparent;
    background: transparent;
    cursor: pointer;
    font: inherit;
    color: inherit;
    padding: 0.1rem 0.3rem;
    border-radius: 4px;
    border-bottom: 1px dashed var(--border);
}
.pdt-trigger:hover { background: #eef3ff; color: var(--accent); border-color: var(--accent); }

/* Fixed positioning (coords set in JS) so the popover escapes the table's
   overflow:hidden clipping context. */
.pdt-popover {
    position: fixed;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.10);
    padding: 0.4rem;
    z-index: 50;
    width: 300px;
}
.pdt-popover[hidden] { display: none; }
.pdt-head { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); padding: 0.2rem 0.4rem 0.4rem; }
.pdt-list { display: flex; flex-direction: column; gap: 0.1rem; max-height: 320px; overflow-y: auto; }
.pdt-empty { padding: 0.5rem 0.4rem; color: var(--muted); font-size: 0.8rem; }
.pdt-item {
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    padding: 0.4rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
    color: #222;
}
.pdt-item:hover { background: var(--bg); }
.pdt-item:disabled { opacity: 0.5; cursor: default; }
.pdt-when { font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.pdt-text { font-size: 0.75rem; color: var(--muted); margin-top: 0.1rem; white-space: normal; }

/* ===== Mobile (≤ 768px) — single column with overlays ===== */

@media (max-width: 768px) {
    :root { --sidebar-w: 100%; --panel-w: 100%; }

    /* Compact topbar: swap nav text for icons so links never overflow width. */
    .topbar { gap: 0.5rem; padding: 0.5rem 0.6rem; }
    .topbar-brand { flex: 0 0 auto; }
    .topbar .nav-link { font-size: 1.15rem; line-height: 1; }
    .topbar .nav-icon { display: inline; }
    .topbar .nav-label { display: none; }
    .topbar .user { font-size: 0.85rem; }

    .chat-layout,
    .chat-layout.has-active {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr;
    }

    .chat-sidebar,
    .chat-main,
    .contact-panel {
        grid-column: 1;
        grid-row: 1;
    }

    /* Default view = list: show sidebar, hide main + panel */
    .chat-layout .chat-sidebar { display: flex; }
    .chat-layout .chat-main { display: none; }
    .chat-layout .contact-panel { display: none; }

    /* When viewing detail: hide sidebar, show main */
    .chat-layout[data-view="detail"] .chat-sidebar { display: none; }
    .chat-layout[data-view="detail"] .chat-main { display: flex; }

    /* Panel overlay on top of detail */
    .chat-layout[data-panel-open="true"] .contact-panel {
        display: flex;
        position: fixed;
        inset: var(--topbar-h) 0 0 0;
        z-index: 20;
    }

    .header-back, .header-info, .sidebar-close, .panel-close { display: inline-flex; align-items: center; justify-content: center; }
    .header-identity strong { font-size: 0.9rem; }
    .header-identity small { display: none; }
    .chat-header { gap: 0.4rem; padding: 0.5rem 0.5rem; }
    .ai-toggle-form button { padding: 0.15rem 0.45rem; font-size: 0.7rem; }

    .message { max-width: 90%; }

    .filter-dropdown { left: 0.5rem; right: 0.5rem; }
}
