:root {
    --bg: #0f1117;
    --sidebar: #161922;
    --panel: #1e2230;
    --panel-2: #262b3a;
    --border: #2e3346;
    --text: #e6e8ef;
    --muted: #8b93a8;
    --accent: #5b9dff;
    --accent-soft: rgba(91, 157, 255, 0.15);
    --warn: #f5a524;
    --ok: #22c55e;
    --new: #ff5d73;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
        "Helvetica Neue", Arial, "Microsoft JhengHei", sans-serif;
    font-size: 14px;
    line-height: 1.6;
}

#app { display: flex; height: 100vh; }

/* ---- sidebar ---- */
#sidebar {
    width: 30%;
    min-width: 280px;
    max-width: 400px;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.sb-header {
    padding: 20px 20px 14px;
    border-bottom: 1px solid var(--border);
}
.sb-header h1 {
    margin: 0;
    font-size: 20px;
    letter-spacing: 0.5px;
    color: var(--text);
}
.sb-header .subtitle {
    font-size: 12px;
    color: var(--muted);
    margin-top: 4px;
}

#topic-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}
/* ---- category groups ---- */
.category-group {
    margin-bottom: 4px;
}
.category-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px 6px 14px;
    cursor: pointer;
    user-select: none;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.5px;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    transition: color 0.12s ease;
}
.category-header:hover {
    color: #fff;
}
.category-header .category-emoji { font-size: 15px; }
.category-header .category-name { flex: 1; }
.category-header .category-count {
    font-size: 11px;
    color: var(--muted);
    opacity: 0.8;
}
.category-header .category-toggle {
    font-size: 10px;
    width: 12px;
    text-align: center;
    color: var(--muted);
}
.category-group.collapsed .topic-item {
    display: none;
}

.topic-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 18px 8px 32px;
    cursor: pointer;
    border-left: 3px solid transparent;
    user-select: none;
    transition: background 0.12s ease;
}
.topic-item:hover { background: rgba(255,255,255,0.04); }
.topic-item.active {
    background: var(--accent-soft);
    border-left-color: var(--accent);
}
.topic-item .emoji { font-size: 16px; }
.topic-item .name { flex: 1; }
.topic-item .count {
    font-size: 11px;
    background: var(--panel-2);
    color: var(--muted);
    padding: 2px 8px;
    border-radius: 999px;
}
.topic-item .new-badge {
    font-size: 11px;
    background: var(--new);
    color: #fff;
    padding: 2px 8px;
    border-radius: 999px;
    margin-left: 4px;
    animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.55; }
}

.sb-footer {
    border-top: 1px solid var(--border);
    padding: 12px 18px;
    font-size: 11px;
    color: var(--muted);
}
.legend { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.legend .emoji { margin-right: 2px; }
.refresh-hint { color: var(--muted); font-size: 10px; margin-top: 4px; }

/* ---- content ---- */
#content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    overflow: hidden;
}
#content-header {
    padding: 20px 28px 0;
    border-bottom: 1px solid var(--border);
}
.title-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 20px;
}
#current-title {
    margin: 0 0 6px;
    font-size: 18px;
    font-weight: 600;
}
#meta-badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
    font-size: 11px;
    padding: 3px 8px;
    background: var(--panel-2);
    color: var(--muted);
    border-radius: 4px;
    border: 1px solid var(--border);
}
.badge.strong { color: var(--text); }
.badge.accent { color: var(--accent); border-color: var(--accent); }

.history-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0 12px;
    font-size: 12px;
    color: var(--muted);
}
.history-row select {
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 12px;
    min-width: 280px;
}

.tabs { display: flex; gap: 2px; }
.tab {
    background: transparent;
    border: none;
    color: var(--muted);
    padding: 10px 18px;
    cursor: pointer;
    font-size: 13px;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    font-family: inherit;
}
.tab:hover { color: var(--text); }
.tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-pane { display: none; flex: 1; overflow-y: auto; }
.tab-pane.active { display: block; }
.pane-body { padding: 24px 28px 48px; }

.placeholder {
    color: var(--muted);
    font-style: italic;
    padding: 60px 20px;
    text-align: center;
}

/* ---- article body ---- */
.article-title {
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
    margin: 0 0 8px;
    line-height: 1.4;
}
.article-subtitle {
    font-size: 14px;
    color: var(--muted);
    margin: 4px 0;
}
.article-meta {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin: 14px 0 20px;
    padding: 10px 14px;
    background: var(--panel-2);
    border-radius: 6px;
    font-size: 12px;
}
.article-meta .kv { color: var(--muted); }
.article-meta .kv b { color: var(--text); margin-left: 4px; }
.article-body {
    font-size: 15px;
    line-height: 1.85;
    color: #d8dae4;
    white-space: pre-wrap;
    word-wrap: break-word;
}
.article-body strong { color: var(--text); }

.warn-banner {
    background: rgba(245, 165, 36, 0.15);
    border: 1px solid var(--warn);
    color: var(--warn);
    padding: 10px 14px;
    border-radius: 6px;
    margin-bottom: 16px;
    font-size: 13px;
}

/* ---- code / json ---- */
pre[class*="language-"] {
    background: #0b0d14 !important;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12.5px;
    max-height: none;
}
code { font-family: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace; }

/* marked prompt */
.prompt-md { font-size: 14px; }
.prompt-md h1, .prompt-md h2, .prompt-md h3, .prompt-md h4 {
    color: var(--text); margin-top: 1.4em;
}
.prompt-md h1 { font-size: 22px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.prompt-md h2 { font-size: 18px; }
.prompt-md h3 { font-size: 16px; color: var(--accent); }
.prompt-md code {
    background: var(--panel-2);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 12.5px;
}
.prompt-md pre code { background: transparent; padding: 0; }
.prompt-md pre {
    background: #0b0d14;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 12px;
    overflow-x: auto;
    font-size: 12.5px;
}
.prompt-md ul, .prompt-md ol { padding-left: 24px; }
.prompt-md li { margin: 3px 0; }
.prompt-md blockquote {
    border-left: 3px solid var(--accent);
    color: var(--muted);
    margin: 10px 0;
    padding: 4px 12px;
}
.prompt-md table {
    border-collapse: collapse;
    margin: 10px 0;
}
.prompt-md th, .prompt-md td {
    border: 1px solid var(--border);
    padding: 6px 10px;
}
.prompt-md hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }

/* ---- trigger button ---- */
.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}
.trigger-btn {
    background: linear-gradient(135deg, #f5a524 0%, #f58424 100%);
    color: #1a1405;
    border: none;
    padding: 7px 14px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: transform 0.1s ease, box-shadow 0.15s ease;
    box-shadow: 0 2px 6px rgba(245, 165, 36, 0.25);
}
.trigger-btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(245, 165, 36, 0.4);
}
.trigger-btn:active:not(:disabled) { transform: translateY(0); }
.trigger-btn:disabled {
    background: var(--panel-2);
    color: var(--muted);
    cursor: not-allowed;
    box-shadow: none;
}
.trigger-btn.running {
    background: linear-gradient(135deg, #f5a524 0%, #f58424 100%);
    animation: trigger-pulse 1.4s ease-in-out infinite;
    cursor: progress;
}
.trigger-btn .bolt { filter: drop-shadow(0 0 2px rgba(255,200,60,0.5)); }

@keyframes trigger-pulse {
    0%, 100% { box-shadow: 0 2px 6px rgba(245, 165, 36, 0.35); }
    50% { box-shadow: 0 2px 18px rgba(245, 165, 36, 0.9); }
}

/* ---- trigger modal (bottom-right slide-in) ---- */
.trigger-modal {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: 460px;
    max-width: calc(100vw - 40px);
    max-height: calc(100vh - 40px);
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 100;
    transform: translateY(0);
    transition: transform 0.22s ease, opacity 0.22s ease;
}
.trigger-modal.hidden {
    transform: translateY(40px);
    opacity: 0;
    pointer-events: none;
}
.tm-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--sidebar);
    border-bottom: 1px solid var(--border);
}
.tm-title { flex: 1; font-weight: 600; font-size: 14px; }
.tm-elapsed {
    font-size: 11px;
    color: var(--muted);
    font-family: "SF Mono", Menlo, monospace;
}
.tm-close {
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 20px;
    cursor: pointer;
    padding: 0 6px;
    line-height: 1;
    font-family: inherit;
}
.tm-close:hover { color: var(--text); }

.tm-body {
    padding: 12px 14px 14px;
    overflow-y: auto;
}
.tm-stages {
    list-style: none;
    padding: 0;
    margin: 0 0 10px;
    font-size: 12.5px;
}
.tm-stages li {
    padding: 5px 0;
    display: flex;
    gap: 8px;
    align-items: center;
    color: var(--muted);
    border-bottom: 1px dashed var(--border);
}
.tm-stages li:last-child { border-bottom: none; }
.tm-stages li.running { color: var(--text); }
.tm-stages li.done { color: var(--ok); }
.tm-stages li.error { color: #ef4444; }
.tm-stages .stg-icon { width: 18px; text-align: center; }
.tm-stages .stg-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-family: "SF Mono", Menlo, monospace;
    font-size: 11.5px;
}
.tm-stages li.running .stg-icon { animation: trigger-pulse 1.4s ease-in-out infinite; }

.tm-log-label {
    font-size: 10.5px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin: 4px 0 2px;
}
.tm-log {
    background: #0b0d14;
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 8px 10px;
    font-size: 11.5px;
    line-height: 1.45;
    color: #c8cbd6;
    max-height: 180px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
    margin: 0;
    font-family: "SF Mono", Menlo, monospace;
}
.tm-result { margin-top: 10px; font-size: 13px; font-weight: 600; }
.tm-result.ok { color: var(--ok); }
.tm-result.err { color: #ef4444; }
.tm-result .detail { font-weight: 400; font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---- history tab ---- */
.history-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
}
.history-toolbar .hint { color: var(--muted); font-size: 12px; flex: 1; }
.compare-btn {
    background: var(--accent);
    color: #0b0d14;
    border: none;
    padding: 6px 14px;
    border-radius: 5px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}
.compare-btn:disabled {
    background: var(--panel-2);
    color: var(--muted);
    cursor: not-allowed;
}
.compare-btn:not(:disabled):hover {
    background: #6fadff;
}
.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12.5px;
}
.history-table th, .history-table td {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.history-table th {
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 10.5px;
    letter-spacing: 0.5px;
    background: var(--panel-2);
    position: sticky;
    top: 0;
    cursor: pointer;
    user-select: none;
}
.history-table th .sort-arrow {
    color: var(--accent);
    margin-left: 4px;
}
.history-table tr.row-clickable { cursor: pointer; }
.history-table tr.row-clickable:hover { background: rgba(255,255,255,0.04); }
.history-table tr.is-current { background: var(--accent-soft); }
.history-table td.fname {
    font-family: "SF Mono", Menlo, monospace;
    font-size: 11.5px;
    color: var(--text);
    word-break: break-all;
    max-width: 360px;
}
.history-table td.muted { color: var(--muted); }
.history-table td.snap {
    font-family: "SF Mono", Menlo, monospace;
    font-size: 11px;
    color: var(--accent);
}
.history-table td.snap.missing { color: var(--muted); }
.history-table input[type="checkbox"] {
    width: 14px;
    height: 14px;
    cursor: pointer;
    accent-color: var(--accent);
}

/* ---- compare view ---- */
.compare-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding-bottom: 10px;
    margin-bottom: 14px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--panel);
    z-index: 5;
}
.back-btn {
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 5px 12px;
    border-radius: 5px;
    cursor: pointer;
    font-family: inherit;
    font-size: 12.5px;
}
.back-btn:hover { border-color: var(--accent); }
.compare-files {
    flex: 1;
    display: flex;
    gap: 12px;
    font-family: "SF Mono", Menlo, monospace;
    font-size: 11.5px;
    color: var(--muted);
}
.compare-files .vs { color: var(--accent); font-weight: 700; }
.compare-grid {
    display: flex;
    gap: 4%;
}
.compare-col {
    width: 48%;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 14px 16px;
}
.compare-col h3.col-label {
    margin: 0 0 10px;
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 6px;
}
.compare-col .article-title {
    font-size: 18px;
    margin-bottom: 6px;
}
.compare-col .article-meta { margin: 10px 0 14px; }
.compare-col .article-body { font-size: 13.5px; line-height: 1.7; }

/* ---- prompt diff ---- */
.diff-section {
    margin-top: 24px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--panel-2);
    overflow: hidden;
}
.diff-summary {
    padding: 10px 14px;
    cursor: pointer;
    font-weight: 600;
    font-size: 13px;
    color: var(--text);
    user-select: none;
    background: var(--sidebar);
}
.diff-summary:hover { background: rgba(255,255,255,0.04); }
.diff-section[open] .diff-summary { border-bottom: 1px solid var(--border); }
.diff-meta {
    padding: 10px 14px;
    font-size: 12px;
    color: var(--muted);
    border-bottom: 1px solid var(--border);
    background: var(--panel-2);
}
.diff-content {
    background: #0b0d14;
    padding: 12px 14px;
    font-family: "SF Mono", Menlo, Consolas, monospace;
    font-size: 12px;
    line-height: 1.55;
    overflow-x: auto;
    white-space: pre;
}
.diff-line { display: block; }
.diff-line.add { color: #4ade80; background: rgba(34,197,94,0.08); }
.diff-line.del { color: #f87171; background: rgba(239,68,68,0.08); }
.diff-line.hunk { color: var(--accent); background: rgba(91,157,255,0.08); }
.diff-line.ctx { color: #c8cbd6; }
.diff-empty {
    padding: 20px;
    text-align: center;
    color: var(--muted);
    font-style: italic;
}
