: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; }
.topbar a { color: var(--accent); text-decoration: 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;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}
.sidebar-header h2 { margin: 0; font-size: 1rem; }
.sidebar-close { display: none; margin-left: auto; }

/* 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; }

/* Conversation list */
.conversation-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    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; }
.conversation-item .meta { display: flex; gap: 0.5rem; color: var(--muted); font-size: 0.75rem; }

/* 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.is-on { background: #e8f6e8; color: #1c6b2c; }
.ai-mode.is-off { background: #ffe8e8; color: #b22222; }

.activity-feed {
    flex: 1 1 auto;
    overflow-y: auto;
    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-chat img { max-width: 200px; max-height: 400px; border-radius: 6px; }
.message-event.message-tool time { margin-left: 0.4rem; }

/* ===== 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;
}
.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;
}
.chat-reply button {
    padding: 0.5rem 1rem;
    background: var(--accent);
    color: white;
    border: 0;
    border-radius: 6px;
    cursor: pointer;
    align-self: flex-end;
}

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

.contact-panel {
    grid-column: 3;
    background: var(--surface);
    border-left: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
    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-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; }

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

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

    .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; }
}
