@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,300;0,400;0,500;0,600;1,300;1,400&display=swap');

:root {
    --bg: #0a0d14;
    --bg-surface: #10141c;
    --bg-hover: #161b26;
    --border: #1e2433;
    --text: #d4cfc8;
    --text-dim: #7a7670;
    --text-faint: #4a4640;
    --accent: #C0A0B5;
    --accent-dim: #8a7080;
    --blue: #2A3A4A;
    --wine: #4A2A2E;
    --wine-glow: #6a3a40;
    --chen: #C0A0B5;
    --nian: #d4cfc8;
    --font-serif: 'Cormorant Garamond', 'Noto Serif TC', Georgia, serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

[data-theme="light"] {
    --bg: #f5f2ee;
    --bg-surface: #ebe7e1;
    --bg-hover: #e0dbd4;
    --border: #d4cec6;
    --text: #2a2520;
    --text-dim: #6a6560;
    --text-faint: #9a9590;
    --accent: #8a5070;
    --accent-dim: #a07090;
    --blue: #c8d4e0;
    --wine: #c8a0a8;
    --wine-glow: #b88898;
    --chen: #8a5070;
    --nian: #4a4540;
}

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

body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 16px;
    height: 100dvh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Auth screen */
.auth-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100dvh;
    gap: 24px;
}

.auth-screen h1 {
    font-family: var(--font-serif);
    font-weight: 300;
    font-size: 2rem;
    color: var(--accent);
    letter-spacing: 0.1em;
}

.auth-screen input {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 14px;
    padding: 12px 20px;
    border-radius: 8px;
    width: 280px;
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
}

.auth-screen input:focus {
    border-color: var(--accent-dim);
}

.auth-screen button {
    background: var(--wine);
    border: none;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 15px;
    padding: 10px 32px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.auth-screen button:hover {
    background: var(--wine-glow);
}

/* Layout */
.app {
    display: flex;
    height: 100dvh;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 20px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-header h2 {
    font-family: var(--font-serif);
    font-weight: 300;
    font-size: 1.1rem;
    color: var(--accent);
    letter-spacing: 0.08em;
}

.new-chat-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s;
}

.new-chat-btn:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conv-item {
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-dim);
    transition: all 0.15s;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2px;
}

.conv-item:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.conv-item.active {
    background: var(--bg-hover);
    color: var(--accent);
}

.conv-item {
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.conv-item.pressing {
    background: var(--bg-hover);
}

.conv-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Context menu (long-press / right-click on conv items) */
.ctx-menu {
    position: fixed;
    z-index: 200;
    min-width: 180px;
    max-width: 260px;
    background: var(--bg-panel, var(--bg));
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    padding: 4px;
    font-size: 13px;
}

.ctx-title {
    padding: 6px 10px 4px;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-faint);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ctx-item {
    padding: 9px 10px;
    border-radius: 5px;
    color: var(--text-dim);
    cursor: pointer;
}

.ctx-item:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.ctx-item.danger {
    color: var(--wine-glow, #c06);
}

.ctx-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 6px;
}

.conv-project {
    margin-bottom: 2px;
}

.conv-project > summary {
    padding: 8px 12px;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--accent-dim);
    letter-spacing: 0.06em;
    cursor: pointer;
    user-select: none;
    border-radius: 6px;
}

.conv-project > summary:hover {
    background: var(--bg-hover);
}

.conv-project-count {
    color: var(--text-faint);
    margin-left: 4px;
}

.conv-project .conv-item {
    margin-left: 10px;
}

/* Memory panel */
.memory-panel {
    flex-direction: column;
    position: absolute;
    inset: 0;
    background: var(--bg-surface);
    z-index: 10;
}

.memory-header {
    padding: 20px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--accent);
    letter-spacing: 0.08em;
}

.memory-add {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 8px;
}

.memory-add input,
.memory-add textarea {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 13px;
    padding: 8px 10px;
    border-radius: 6px;
    outline: none;
}

.memory-add textarea {
    resize: none;
    max-height: 200px;
    overflow-y: auto;
    line-height: 1.5;
}

.memory-add input:focus,
.memory-add textarea:focus {
    border-color: var(--accent-dim);
}

.memory-add button {
    background: var(--wine);
    border: none;
    color: var(--text);
    width: 32px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
}

.memory-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.memory-item {
    padding: 8px 10px;
    border-radius: 6px;
    font-size: 12px;
    color: var(--text-dim);
    line-height: 1.5;
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 4px;
    transition: background 0.15s;
}

.memory-main {
    display: flex;
    align-items: flex-start;
    gap: 6px;
}

.memory-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.memory-item:hover {
    background: var(--bg-hover);
}

.memory-text {
    flex: 1;
    word-break: break-word;
}

.memory-badge {
    font-family: var(--font-mono);
    font-size: 9px;
    color: var(--accent-dim);
    background: var(--bg);
    padding: 1px 5px;
    border-radius: 3px;
    white-space: normal;
    word-break: break-word;
}

.memory-item .delete-btn {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-size: 14px;
    padding: 0 4px;
    transition: opacity 0.15s;
    flex-shrink: 0;
}

.memory-item:hover .delete-btn {
    opacity: 1;
}

/* Touch screens have no hover — keep delete reachable */
@media (hover: none) {
    .memory-item .delete-btn {
        opacity: 0.5;
    }
}

/* Tools panel */
.tools-body {
    padding: 12px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.tool-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-dim);
    cursor: pointer;
}

.tool-toggle input {
    accent-color: var(--wine);
}

.search-provider-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.search-provider-row select,
.search-provider-row input {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    padding: 6px 8px;
    border-radius: 5px;
    outline: none;
}

.search-max-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-size: 12px;
    color: var(--text-dim);
}

.search-max-row input {
    width: 64px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    padding: 6px 8px;
    border-radius: 5px;
    outline: none;
    text-align: center;
}

.tools-section-title {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--accent-dim);
    letter-spacing: 0.08em;
    margin-top: 4px;
}

.mcp-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    margin-bottom: 8px;
}

.mcp-row-head {
    display: flex;
    align-items: center;
    gap: 6px;
}

.mcp-row input[type="text"],
.mcp-row input[type="password"] {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    padding: 6px 8px;
    border-radius: 5px;
    outline: none;
    width: 100%;
}

.mcp-row .mcp-name {
    flex: 1;
    width: auto;
}

.mcp-row .delete-btn {
    opacity: 0.6;
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-size: 14px;
}

.skill-row .skill-summary {
    font-size: 11px;
    color: var(--text-faint);
    cursor: pointer;
    user-select: none;
}

.skill-row .skill-content,
#custom-css-editor {
    width: 100%;
    min-height: 140px;
    margin-top: 6px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.5;
    padding: 6px 8px;
    border-radius: 5px;
    outline: none;
    resize: vertical;
}

#custom-css-editor {
    min-height: 320px;
    padding: 8px 10px;
    tab-size: 4;
}

.tools-actions {
    display: flex;
    gap: 8px;
}

.tools-actions button {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-size: 12px;
    padding: 6px 12px;
    border-radius: 5px;
    cursor: pointer;
}

.tools-actions .tools-save {
    background: var(--wine);
    border: none;
    color: var(--text);
}

.tools-hint {
    font-size: 11px;
    color: var(--text-faint);
    line-height: 1.5;
}

.tool-chip {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--accent-dim);
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 2px 8px;
    border-radius: 10px;
    margin: 2px 0 6px;
}

.tool-chip.notice {
    color: #c9a06a;
    border-color: #5a4a30;
}

.tool-result {
    margin: 4px 0 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
}

.tool-result summary {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--accent-dim);
    padding: 5px 10px;
    cursor: pointer;
    user-select: none;
}

.tool-result pre {
    margin: 0;
    padding: 8px 12px;
    font-family: var(--font-mono);
    font-size: 11px;
    line-height: 1.6;
    color: var(--text-dim);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 300px;
    overflow-y: auto;
    border-top: 1px solid var(--border);
}

.mcp-test-btn {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-size: 11px;
    padding: 4px 10px;
    border-radius: 5px;
    cursor: pointer;
}

.mcp-test-result {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-dim);
    min-height: 0;
}

.mcp-test-result summary {
    cursor: pointer;
    user-select: none;
}

.mcp-tool-names {
    padding: 6px 2px;
    line-height: 1.7;
    color: var(--text-faint);
    word-break: break-word;
}

.memory-empty {
    text-align: center;
    color: var(--text-faint);
    font-size: 12px;
    font-style: italic;
    padding: 24px 0;
}

/* Main chat area */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-header {
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-header .model-select {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-family: var(--font-mono);
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 4px;
    outline: none;
}

.chat-header .export-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-family: var(--font-mono);
    font-size: 11px;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

.chat-header .export-btn:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    scroll-behavior: smooth;
}

.message {
    margin-bottom: 28px;
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

.message-label {
    font-family: var(--font-serif);
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.action-btn {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-size: 13px;
    padding: 0 4px;
    transition: opacity 0.15s;
}

.message:hover .action-btn {
    opacity: 1;
}

.action-btn:hover {
    color: var(--accent);
}

/* Version navigation */
.version-nav {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    margin-left: 4px;
    font-family: var(--font-mono);
    font-size: 11px;
}

.version-btn {
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-size: 13px;
    padding: 0 3px;
    transition: color 0.15s;
}

.version-btn:hover {
    color: var(--accent);
}

.version-label {
    color: var(--text-faint);
    min-width: 24px;
    text-align: center;
}

.message.user .message-label {
    color: var(--nian);
}

.message.assistant .message-label {
    color: var(--chen);
}

.message-content {
    font-family: var(--font-serif);
    font-size: 16px;
    line-height: 1.7;
    color: var(--text);
    word-break: break-word;
}

.message-content p {
    margin: 0 0 0.8em;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content strong {
    color: var(--accent);
    font-weight: 600;
}

.message-content em {
    font-style: italic;
}

.message-content code {
    font-family: var(--font-mono);
    font-size: 13px;
    background: var(--bg-hover);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--accent);
}

.message-content pre {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px 16px;
    overflow-x: auto;
    margin: 12px 0;
}

.message-content pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
    color: var(--text);
}

.message-content blockquote {
    border-left: 3px solid var(--accent-dim);
    padding-left: 14px;
    margin: 12px 0;
    color: var(--text-dim);
    font-style: italic;
}

.message-content table {
    border-collapse: collapse;
    margin: 12px 0;
    max-width: 100%;
    display: block;
    overflow-x: auto;
    font-size: 14px;
}

.message-content th,
.message-content td {
    border: 1px solid var(--border);
    padding: 6px 12px;
    text-align: left;
    vertical-align: top;
    line-height: 1.5;
}

.message-content th {
    background: var(--bg-surface);
    color: var(--accent);
    font-weight: 500;
    white-space: nowrap;
}

.message-content tr:nth-child(even) td {
    background: var(--bg-surface);
}

.message-content h1, .message-content h2, .message-content h3 {
    font-family: var(--font-serif);
    font-weight: 500;
    color: var(--accent);
    margin: 16px 0 8px;
}

.message-content h1 { font-size: 1.3em; }
.message-content h2 { font-size: 1.15em; }
.message-content h3 { font-size: 1.05em; }

.message-content ul, .message-content ol {
    padding-left: 24px;
    margin: 8px 0;
}

.message-content li {
    margin-bottom: 4px;
}

.message-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 16px 0;
}

.message-content a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid var(--accent-dim);
}

.message-content a:hover {
    border-bottom-color: var(--accent);
}

.message.user .message-content {
    white-space: pre-wrap;
}

/* Thinking block */
.thinking-block {
    margin-bottom: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

.thinking-summary {
    padding: 8px 14px;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-faint);
    cursor: pointer;
    user-select: none;
    letter-spacing: 0.05em;
}

.thinking-summary:hover {
    color: var(--text-dim);
}

.thinking-content {
    padding: 12px 14px;
    font-family: var(--font-serif);
    font-size: 13px;
    line-height: 1.6;
    color: var(--text-dim);
    white-space: pre-wrap;
    word-break: break-word;
    border-top: 1px solid var(--border);
    max-height: 400px;
    overflow-y: auto;
}

/* Edit mode */
.edit-textarea {
    width: 100%;
    background: var(--bg-surface);
    border: 1px solid var(--accent-dim);
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 15px;
    line-height: 1.5;
    padding: 10px 14px;
    border-radius: 8px;
    resize: none;
    outline: none;
    min-height: 48px;
    max-height: 200px;
}

.edit-buttons {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.edit-buttons button {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-family: var(--font-mono);
    font-size: 11px;
    padding: 4px 12px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

.edit-save {
    border-color: var(--accent-dim) !important;
    color: var(--accent) !important;
}

.edit-save:hover {
    background: var(--wine) !important;
}

.edit-buttons button:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

/* Regenerate */
.regen-container {
    display: flex;
    justify-content: center;
    max-width: 800px;
    margin: -16px auto 16px;
}

.regen-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-faint);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.regen-btn:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

/* Token usage */
.token-usage {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-faint);
    margin-top: 8px;
    letter-spacing: 0.03em;
}

.typing-indicator {
    display: inline-block;
    color: var(--accent-dim);
    font-style: italic;
    font-size: 14px;
}

.typing-indicator::after {
    content: '';
    animation: dots 1.5s infinite;
}

@keyframes dots {
    0%, 20% { content: ''; }
    40% { content: '.'; }
    60% { content: '..'; }
    80%, 100% { content: '...'; }
}

/* Input area */
.input-area {
    padding: 16px 24px 24px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-wrapper {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

.input-wrapper textarea {
    flex: 1;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 15px;
    line-height: 1.5;
    padding: 12px 16px;
    border-radius: 12px;
    resize: none;
    outline: none;
    min-height: 48px;
    max-height: 200px;
    transition: border-color 0.2s;
}

.input-wrapper textarea:focus {
    border-color: var(--accent-dim);
}

.input-wrapper textarea::placeholder {
    color: var(--text-faint);
}

.send-btn {
    background: var(--wine);
    border: none;
    color: var(--accent);
    width: 48px;
    height: 48px;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: background 0.2s;
    flex-shrink: 0;
}

.send-btn:hover {
    background: var(--wine-glow);
}

.send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.send-btn.stop {
    background: none;
    border: 1px solid var(--wine-glow, #c06);
    color: var(--wine-glow, #c06);
    animation: stop-breathe 1.6s ease-in-out infinite;
}

@keyframes stop-breathe {
    50% { opacity: 0.55; }
}

.action-btn.msg-del:hover {
    color: var(--wine-glow, #c06);
}

@media (hover: none) {
    .message .action-btn {
        opacity: 0.4;
    }
}

/* Empty state */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-faint);
    gap: 12px;
}

.empty-state h2 {
    font-family: var(--font-serif);
    font-weight: 300;
    font-size: 1.6rem;
    color: var(--accent-dim);
}

.empty-state p {
    font-size: 14px;
    font-style: italic;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-faint);
}

/* Mobile */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        width: min(92vw, 400px);
        left: -100vw;
        top: 0;
        bottom: 0;
        z-index: 100;
        transition: left 0.25s ease;
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 99;
    }

    .sidebar-overlay.open {
        display: block;
    }

    .menu-btn {
        display: flex !important;
    }

    .messages {
        padding: 16px;
    }

    .input-area {
        padding: 12px 16px 16px;
    }

    .chat-header {
        padding: 10px 12px;
    }

    .header-actions {
        gap: 6px;
    }
}

.menu-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
}

.theme-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all 0.2s;
}

.theme-btn:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

/* Header right side + ⋯ overflow menu */
.header-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.overflow-wrap {
    position: relative;
}

/* Rides on .ctx-menu for the shared look; anchored under ⋯ instead of fixed. */
.overflow-menu {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    left: auto;
    min-width: 160px;
}

/* Dreaming glow relay: when the 🌙 knob panel is open the button already
   glows; nothing extra needed here — 🌙 stays in the header by design. */

/* Image attachments */
.attach-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 18px;
    cursor: pointer;
    padding: 8px 4px;
    align-self: flex-end;
    transition: color 0.2s;
}

.attach-btn:hover {
    color: var(--accent);
}

.pending-images {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    padding: 8px 12px 0;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
}

.pending-thumb {
    position: relative;
}

.pending-thumb img {
    width: 64px;
    height: 64px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.pending-thumb button {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: none;
    background: var(--wine);
    color: var(--text);
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
}

.msg-images {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin: 6px 0;
}

.msg-images img {
    max-width: 240px;
    max-height: 240px;
    border-radius: 10px;
    border: 1px solid var(--border);
    cursor: pointer;
    object-fit: cover;
}

/* Search */
.search-box {
    padding: 0 12px 8px;
}

.search-box input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 7px 10px;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 14px;
    outline: none;
}

.search-box input:focus {
    border-color: var(--accent-dim);
}

.search-back {
    color: var(--text-dim);
    font-style: italic;
}

.search-result {
    display: block !important;
    padding: 8px 10px;
}

.search-result-title {
    font-size: 12px;
    color: var(--accent-dim);
    margin-bottom: 2px;
}

.search-result-snippet {
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.4;
    word-break: break-all;
}

.message.search-hit {
    animation: hitGlow 2.5s ease-out;
}

@keyframes hitGlow {
    0% { background: var(--bg-hover); box-shadow: 0 0 0 2px var(--accent-dim); }
    100% { background: transparent; box-shadow: none; }
}

/* SP editor */
.conv-search {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-surface);
}

.conv-search input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 13px;
    padding: 6px 10px;
    border-radius: 6px;
    outline: none;
    min-width: 0;
}

.conv-search button {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    border-radius: 5px;
    padding: 4px 10px;
    cursor: pointer;
    font-size: 13px;
}

.conv-search button:hover {
    color: var(--text);
    border-color: var(--accent-dim);
}

.conv-search-count {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-faint);
    white-space: nowrap;
}

mark.search-hit {
    background: var(--wine);
    color: var(--text);
    border-radius: 2px;
    padding: 0 1px;
}

mark.search-hit.active {
    background: var(--accent);
    color: var(--bg);
}

.ctx-stats {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-faint);
    white-space: nowrap;
}

.memory-count {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--accent-dim);
}

.sp-row {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 8px 12px 4px;
}

.sp-row button {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 5px 10px;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 12px;
    cursor: pointer;
}

.sp-details {
    padding: 0 12px 4px;
}

.sp-details > summary {
    font-size: 11px;
    color: var(--text-faint);
    cursor: pointer;
    user-select: none;
}

.sp-details .sp-editor {
    width: 100%;
    min-height: 200px;
    margin: 6px 0;
}

.sp-details .tools-actions {
    padding-bottom: 4px;
}

.memory-item .mem-edit {
    opacity: 0.5;
}

.memory-item:hover .mem-edit,
.memory-item .mem-edit:hover {
    opacity: 1;
    color: var(--accent);
}

.memory-edit-text,
.memory-edit-kw {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    padding: 6px 8px;
    border-radius: 5px;
    outline: none;
    margin-bottom: 6px;
}

.memory-edit-text {
    resize: vertical;
    min-height: 60px;
}

.pending-file {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text-dim);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
}

.pending-file button {
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
}

.file-block {
    margin: 6px 0;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
}

.file-block > summary {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text-dim);
    cursor: pointer;
    user-select: none;
}

.file-block pre {
    margin: 6px 0 0;
    max-height: 320px;
    overflow: auto;
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-all;
}

.sp-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 0 12px 8px;
}

.sp-actions button {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 5px 10px;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 13px;
    cursor: pointer;
}

.sp-actions button:hover {
    border-color: var(--accent-dim);
}

#sp-meta {
    font-size: 11px;
    color: var(--text-faint);
    margin-left: auto;
}

.sp-editor {
    flex: 1;
    margin: 0 12px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.5;
    resize: none;
    outline: none;
}

.memory-badge.kw {
    background: var(--blue);
    color: var(--text-dim);
}

.msg-time {
    font-size: 11px;
    color: var(--text-faint);
    margin-left: 8px;
    font-weight: normal;
}

/* ── 夢態旋鈕 ── */
.dream-panel {
    position: absolute;
    top: 56px;
    right: 12px;
    z-index: 50;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 16px;
    width: min(320px, calc(100vw - 24px));
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

.dream-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.dream-label {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text-dim);
    width: 84px;
}

.dream-row input[type="range"] {
    flex: 1;
    accent-color: var(--accent);
}

.dream-val {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--accent);
    width: 36px;
    text-align: right;
}

.dream-hint {
    font-size: 12px;
    color: var(--text-faint);
    line-height: 1.5;
    margin: 6px 0 10px;
}

.dream-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.dream-actions button {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    border-radius: 6px;
    padding: 4px 12px;
    cursor: pointer;
    font-family: var(--font-serif);
    font-size: 13px;
}

.dream-actions button:hover {
    border-color: var(--accent-dim);
    color: var(--accent);
}

.theme-btn.dreaming {
    border-color: var(--accent);
    color: var(--accent);
    box-shadow: 0 0 8px rgba(192, 160, 181, 0.35);
}

/* ── Hypnagogic ── */
.hypna {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    padding: max(24px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}

.hypna-close {
    position: absolute;
    top: max(14px, env(safe-area-inset-top));
    right: 16px;
    background: none;
    border: none;
    color: var(--text-faint);
    font-size: 26px;
    cursor: pointer;
    line-height: 1;
    padding: 6px;
}

.hypna-close:hover {
    color: var(--accent);
}

.hypna-reply {
    flex: 1;
    overflow-y: auto;
    font-size: 21px;
    line-height: 1.75;
    color: var(--text);
    padding: 28px 4px 20px;
    max-width: 680px;
    margin: 0 auto;
    width: 100%;
}

.hypna-reply:empty::before {
    content: "…";
    color: var(--text-faint);
}

.hypna textarea {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 22px;
    line-height: 1.6;
    padding: 18px 20px;
    min-height: 120px;
    max-width: 680px;
    margin: 0 auto;
    width: 100%;
    resize: none;
    outline: none;
}

.hypna textarea:focus {
    border-color: var(--accent-dim);
}

/* Display names row */
.name-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.name-row input {
    flex: 1;
    min-width: 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 14px;
    padding: 6px 10px;
    outline: none;
}

.name-row input:focus {
    border-color: var(--accent-dim);
}

.name-x {
    color: var(--text-faint);
    font-size: 12px;
}

/* ── Gallery ── */
.gallery {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg);
    display: flex;
    flex-direction: column;
}

.gallery-head {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 14px 6px;
}

.gallery-count {
    color: var(--text-faint);
    font-size: 13px;
}

.gallery-close {
    position: static;
}

.gallery-grid {
    flex: 1;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 8px;
    padding: 4px 14px max(14px, env(safe-area-inset-bottom));
    align-content: start;
}

.gallery-cell {
    position: relative;
    aspect-ratio: 1;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    border: 1px solid var(--border);
}

.gallery-cell img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.gallery-cap {
    position: absolute;
    inset: auto 0 0 0;
    padding: 14px 8px 6px;
    font-size: 11px;
    line-height: 1.3;
    color: #fff;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
    opacity: 0;
    transition: opacity 0.15s;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.gallery-cell:hover .gallery-cap {
    opacity: 1;
}

.gallery-empty {
    grid-column: 1 / -1;
    color: var(--text-faint);
    text-align: center;
    padding: 40px 0;
}

@media (max-width: 600px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
        gap: 6px;
    }
}

/* ── Canvas (對頁) ── */
.canvas {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg);
    display: flex;
    flex-direction: column;
}

.canvas-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 16px 6px;
}

.canvas-title {
    font-family: var(--font-serif);
    color: var(--text-faint);
    font-size: 14px;
    letter-spacing: 0.15em;
}

.canvas-meta {
    color: var(--text-faint);
    font-size: 12px;
    margin-left: auto;
}

.canvas-close {
    position: static;
}

.canvas-body {
    flex: 1;
    display: flex;
    gap: 0;
    padding: 4px 16px max(16px, env(safe-area-inset-bottom));
    overflow: hidden;
}

.canvas-pane {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.canvas-pane-label {
    font-family: var(--font-serif);
    font-size: 12px;
    padding: 4px 6px;
    opacity: 0.75;
}

.canvas-editor {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-family: var(--font-serif);
    font-size: 18px;
    line-height: 1.8;
    padding: 8px 20px;
    resize: none;
    outline: none;
    width: 100%;
}

.canvas-spine {
    width: 1px;
    background: var(--border);
    margin: 28px 0 8px;
}

@media (max-width: 600px) {
    .canvas-body {
        flex-direction: column;
        overflow-y: auto;
    }
    .canvas-spine {
        width: auto;
        height: 1px;
        margin: 10px 0;
    }
    .canvas-editor {
        min-height: 38vh;
        font-size: 17px;
    }
}

/* Loading — senderos que se encuentran */
.loading {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--bg);
    transition: opacity 0.7s ease;
}

.loading.fade {
    opacity: 0;
    pointer-events: none;
}

.loading-svg {
    width: min(320px, 78vw);
    overflow: visible;
}

.path-chen, .path-nian {
    fill: none;
    stroke-width: 1.4;
    stroke-linecap: round;
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: path-draw 1.1s cubic-bezier(0.45, 0, 0.25, 1) forwards;
}

.path-chen { stroke: var(--chen); }
.path-nian { stroke: var(--nian); animation-delay: 0.12s; }

@keyframes path-draw {
    to { stroke-dashoffset: 0; }
}

.meet {
    fill: var(--accent);
    opacity: 0;
    animation: meet-in 0.5s ease 1.15s forwards;
}

.meet-glow {
    fill: var(--accent);
    opacity: 0;
    transform-box: fill-box;
    transform-origin: center;
    animation: meet-pulse 2.4s ease 1.15s infinite;
}

@keyframes meet-in {
    to { opacity: 1; }
}

@keyframes meet-pulse {
    0% { opacity: 0; transform: scale(0.35); }
    35% { opacity: 0.22; }
    100% { opacity: 0; transform: scale(1.35); }
}

.loading-word {
    font-family: var(--font-serif);
    font-weight: 300;
    font-size: 1.6rem;
    letter-spacing: 0.18em;
    color: var(--accent);
    margin-top: -10px;
    opacity: 0;
    animation: loading-word-in 0.9s ease 1.3s forwards;
}

.loading-sub {
    font-family: var(--font-serif);
    font-style: italic;
    font-size: 0.85rem;
    color: var(--text-faint);
    opacity: 0;
    animation: loading-word-in 0.9s ease 1.55s forwards;
}

@keyframes loading-word-in {
    to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .path-chen, .path-nian, .meet, .loading-word, .loading-sub {
        animation-duration: 0.01s;
        animation-delay: 0s;
    }
    .meet-glow { animation: none; }
}

/* KaTeX — a display formula is wider than a phone long before it is wide
   than a desk, so let it scroll sideways instead of being clipped. */
.katex-display {
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 0;
}
