:root {
    --bg: #f7f7f9;
    --card: #ffffff;
    --text: #1f2330;
    --muted: #6b7280;
    --border: #e5e7eb;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --error: #b91c1c;
    --error-bg: #fee2e2;
    --success: #166534;
    --success-bg: #dcfce7;
    --warn: #92400e;
    --warn-bg: #fef3c7;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}
main { max-width: 1100px; margin: 0 auto; padding: 24px; }

.topbar {
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    display: flex;
    align-items: center;
    gap: 16px;
}
.topbar .brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--text); }
.topbar nav { margin-left: auto; display: flex; gap: 16px; align-items: center; }
.topbar nav a { text-decoration: none; color: var(--text); }
.topbar nav a:hover { color: var(--primary); }
.topbar .who { color: var(--muted); font-size: 0.9rem; }
.inline { display: inline; }
button.link { background: none; border: none; color: inherit; cursor: pointer; padding: 0; font: inherit; }
button.link:hover { color: var(--primary); }

h1 { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { background: var(--error-bg); color: var(--error); padding: 10px 14px; border-radius: 6px; margin: 12px 0; }
.success { background: var(--success-bg); color: var(--success); padding: 10px 14px; border-radius: 6px; margin: 12px 0; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 20px;
    margin: 16px 0;
}
.card.stat { text-align: center; flex: 1 1 0; min-width: 140px; }
.card.stat h2 { margin: 0; font-size: 2rem; color: var(--primary); }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.row .grow { flex: 1; }

label { display: block; margin: 12px 0; font-size: 0.95rem; }
label > input:not([type="radio"]):not([type="checkbox"]):not([type="file"]),
label > select,
label > textarea {
    display: block;
    width: 100%;
    margin-top: 4px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
}
label > input:focus, label > select:focus, label > textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; }

button, .cta {
    display: inline-block;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 6px;
    padding: 10px 16px;
    font: inherit;
    cursor: pointer;
    text-decoration: none;
    line-height: 1.2;
}
button:hover, .cta:hover { background: var(--primary-hover); color: white; }
button:disabled { background: #9ca3af; cursor: not-allowed; }

/* Outlined secondary button — same shape as .cta but quiet */
.cta-secondary {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}
.cta-secondary:hover { background: #eff6ff; color: var(--primary); }

.header-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.m-0 { margin: 0; }

.hero { text-align: center; padding: 60px 0; }
.hero h1 { font-size: 2.4rem; margin-bottom: 8px; }

.page-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 16px; }

.data { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 8px; overflow: hidden; }
.data th, .data td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.data th { background: #f0f2f5; font-weight: 600; font-size: 0.9rem; }

.status {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.85rem;
    background: #eef2ff;
    color: #3730a3;
}
.status-uploaded { background: var(--warn-bg); color: var(--warn); }
.status-transcribing, .status-running, .status-pending { background: #e0f2fe; color: #075985; }
.status-transcribed, .status-completed { background: var(--success-bg); color: var(--success); }
.status-failed { background: var(--error-bg); color: var(--error); }

.recorder { margin: 24px 0; padding: 20px; border: 2px dashed var(--border); border-radius: 10px; text-align: center; }
.rec-state { font-size: 1.1rem; margin-bottom: 12px; color: var(--muted); }
.rec-state.recording { color: var(--error); font-weight: 600; }
.rec-controls { display: flex; gap: 8px; justify-content: center; margin-bottom: 12px; }
.rec-controls button { padding: 8px 16px; }
audio { width: 100%; margin: 12px 0; }

.transcript {
    white-space: pre-wrap;
    background: #f0f2f5;
    padding: 12px;
    border-radius: 6px;
    font-size: 0.95rem;
    max-height: 360px;
    overflow: auto;
}

.analysis-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 12px;
    margin: 16px 0;
}
.analysis-btn {
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    text-align: left;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.analysis-btn:hover { border-color: var(--primary); background: #eff6ff; }
.analysis-btn.personal { border-style: dashed; }
.analysis-btn strong { font-size: 1rem; }
.analysis-btn span { font-size: 0.85rem; color: var(--muted); }
.analysis-btn:disabled { opacity: 0.6; cursor: wait; }
.model-badge { display: inline-block; margin-top: 6px; padding: 1px 6px; border-radius: 4px; background: #f0f2f5; color: var(--muted); font-size: 0.75rem; font-family: ui-monospace, monospace; }
.section-h { margin-top: 16px; margin-bottom: 8px; font-size: 0.95rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.tier-picker { border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; margin: 12px 0; }
.tier-picker legend { padding: 0 6px; font-size: 0.9rem; color: var(--muted); }
.tier-option { display: flex; gap: 10px; align-items: flex-start; padding: 8px 4px; cursor: pointer; border-radius: 6px; margin: 0; }
.tier-option:hover { background: #f5f7fb; }
.tier-option input[type="radio"] { flex: 0 0 auto; margin: 4px 0 0 0; width: 18px; height: 18px; cursor: pointer; }
.tier-option > span { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.tier-option strong { font-weight: 600; }
.tier-option small { font-size: 0.85rem; color: var(--muted); }
.tier-option small.muted { font-family: ui-monospace, monospace; font-size: 0.75rem; }
.custom-card summary { cursor: pointer; padding: 4px 0; }
.custom-card textarea { font-family: inherit; }
.inline-select {
    display: inline-block;
    width: auto;
    margin: 0;
    padding: 2px 6px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font: inherit;
    font-size: 0.9rem;
    background: var(--card);
}
.notes-card summary { cursor: pointer; padding: 4px 0; }
.notes-card textarea {
    width: 100%;
    margin: 8px 0;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    resize: vertical;
}
.text-fallback summary { cursor: pointer; padding: 4px 0; color: var(--muted); }
.text-fallback summary strong { color: var(--text); }
.text-fallback details[open] { padding-top: 8px; }

/* Project document cards (collapsible, like analysis cards) */
.doc-controls { display: flex; gap: 12px; align-items: center; margin: 12px 0 6px; }
.document-card { padding: 0; }
.document-card > summary {
    list-style: none;
    cursor: pointer;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.document-card > summary::-webkit-details-marker { display: none; }
.document-card > summary::before {
    content: "▸";
    color: var(--muted);
    margin-right: 8px;
}
.document-card[open] > summary::before { content: "▾"; }
.document-card .doc-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.document-card .doc-name { font-size: 1rem; }
.document-card .doc-tail { display: flex; align-items: center; gap: 8px; }
.document-card .doc-delete-btn {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 26px;
    height: 26px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    font-size: 1.1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.document-card .doc-delete-btn:hover { background: var(--error-bg); color: var(--error); border-color: var(--error); }
.document-card .doc-body { padding: 0 16px 14px 16px; }
.document-card .doc-text {
    width: 100%;
    margin-top: 4px;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    resize: vertical;
    white-space: pre-wrap;
}

.analysis-card header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.analysis-card { padding: 0; }
.analysis-card > summary {
    list-style: none;
    cursor: pointer;
    padding: 14px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.analysis-card > summary::-webkit-details-marker { display: none; }
.analysis-card > summary::before {
    content: "▸";
    color: var(--muted);
    margin-right: 8px;
    transition: transform 0.15s ease;
}
.analysis-card[open] > summary::before { content: "▾"; }
.analysis-card .ac-title { flex: 1; min-width: 0; }
.analysis-card .ac-tail { display: flex; align-items: center; gap: 8px; }
.analysis-card > .markdown,
.analysis-card > .error,
.analysis-card > footer,
.analysis-card > .instruction { margin-left: 18px; margin-right: 18px; }
.analysis-card > footer { margin-bottom: 14px; }
.analysis-card > .markdown { margin-top: 6px; margin-bottom: 10px; }
.analysis-card > .error { margin-top: 6px; margin-bottom: 14px; }
.analysis-delete {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 26px;
    height: 26px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    font-size: 1.1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.analysis-delete:hover { background: var(--error-bg); color: var(--error); border-color: var(--error); }
.analysis-card .instruction {
    margin: 6px 18px 10px !important;
    background: #f5f7fb;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
}
.analysis-card .instruction summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; padding: 2px 0; }
.analysis-card .instruction summary:hover { color: var(--text); }
.analysis-card .instruction pre {
    margin: 6px 0 0;
    white-space: pre-wrap;
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text);
}
.markdown { white-space: pre-wrap; line-height: 1.6; }
.markdown code { background: #f0f2f5; padding: 1px 5px; border-radius: 3px; font-size: 0.9em; }

.footer { text-align: center; color: var(--muted); padding: 24px; }

/* Debug panel ----------------------------------------------------------- */
.debug-panel {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 9999;
    font-family: ui-monospace, "Cascadia Mono", "Consolas", monospace;
    font-size: 0.82rem;
}
.debug-toggle {
    background: #18181b;
    color: #fafafa;
    border: 1px solid #27272a;
    border-radius: 999px;
    padding: 6px 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}
.debug-toggle:hover { background: #27272a; }
.debug-dot {
    display: inline-block;
    width: 8px; height: 8px; border-radius: 50%;
    background: #f59e0b;
    animation: debugPulse 1.6s ease-in-out infinite;
}
@keyframes debugPulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
.debug-panel .debug-body {
    margin-top: 8px;
    width: min(380px, calc(100vw - 32px));
    max-height: min(70vh, 600px);
    overflow-y: auto;
    background: #18181b;
    color: #e4e4e7;
    border: 1px solid #27272a;
    border-radius: 10px;
    padding: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.debug-panel.collapsed .debug-body { display: none; }
.debug-panel.collapsed .debug-toggle { box-shadow: 0 4px 12px rgba(0,0,0,0.25); }
.debug-header { display: flex; justify-content: space-between; align-items: center; padding-bottom: 6px; border-bottom: 1px solid #27272a; margin-bottom: 8px; }
.debug-close { background: none; border: none; color: #a1a1aa; font-size: 1.4rem; line-height: 1; padding: 0 6px; cursor: pointer; }
.debug-close:hover { color: #fff; }

.debug-section { background: #0f0f10; border: 1px solid #27272a; border-radius: 6px; padding: 6px 10px; margin: 6px 0; }
.debug-section summary { cursor: pointer; user-select: none; color: #d4d4d8; padding: 2px 0; font-weight: 600; }
.debug-section[open] summary { margin-bottom: 6px; }
.debug-section form { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.debug-section label { display: flex; flex-direction: column; gap: 2px; margin: 0; color: #a1a1aa; font-size: 0.78rem; }
.debug-section label > input,
.debug-section label > select {
    background: #18181b;
    color: #fafafa;
    border: 1px solid #3f3f46;
    border-radius: 4px;
    padding: 5px 7px;
    font: inherit;
    width: 100%;
    margin: 0;
}
.debug-section button {
    background: #27272a;
    color: #fafafa;
    border: 1px solid #3f3f46;
    border-radius: 4px;
    padding: 5px 10px;
    cursor: pointer;
    font: inherit;
    align-self: flex-start;
}
.debug-section button:hover { background: #3f3f46; }
.debug-section button.danger { background: #7f1d1d; border-color: #991b1b; }
.debug-section button.danger:hover { background: #991b1b; }
.debug-out {
    background: #0a0a0a;
    color: #d4d4d8;
    border: 1px solid #27272a;
    border-radius: 4px;
    padding: 6px 8px;
    margin: 6px 0 0;
    font-size: 0.75rem;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 200px;
    overflow-y: auto;
}
.debug-out:empty { display: none; }
.debug-out.error { color: #fca5a5; border-color: #7f1d1d; }

/* Project page header — title + description as polished inline-editable text */
.project-header { margin: 8px 0 24px; }
.project-header .title-input {
    display: block;
    width: 100%;
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.2;
    border: none;
    outline: none;
    background: transparent;
    padding: 4px 8px;
    margin: 0 0 4px -8px;
    border-radius: 6px;
    color: var(--text);
    box-shadow: 0 0 0 1px transparent;
    transition: background 0.1s, box-shadow 0.1s;
}
.project-header .title-input:hover { background: rgba(0,0,0,0.03); }
.project-header .title-input:focus { background: var(--card); box-shadow: 0 0 0 2px var(--primary); }
.project-header .desc-input {
    display: block;
    width: 100%;
    font-size: 1rem;
    line-height: 1.5;
    border: none;
    outline: none;
    background: transparent;
    color: var(--muted);
    padding: 4px 8px;
    margin: 0 0 6px -8px;
    border-radius: 6px;
    resize: none;
    overflow: hidden;
    min-height: 1.5em;
    font-family: inherit;
    box-shadow: 0 0 0 1px transparent;
    transition: background 0.1s, box-shadow 0.1s, color 0.1s;
}
.project-header .desc-input:hover { background: rgba(0,0,0,0.03); color: var(--text); }
.project-header .desc-input:focus { background: var(--card); box-shadow: 0 0 0 2px var(--primary); color: var(--text); }

/* Document gallery — tile grid */
.upload-cta {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
}
.doc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 14px;
    margin: 16px 0;
}
.doc-tile {
    display: flex;
    flex-direction: column;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    text-align: left;
    padding: 0;
    color: var(--text);
    transition: border-color 0.12s, box-shadow 0.12s, transform 0.12s;
    font: inherit;
}
.doc-tile:hover {
    border-color: var(--primary);
    box-shadow: 0 6px 16px rgba(0,0,0,0.08);
    transform: translateY(-1px);
    background: var(--card);
}
.doc-tile-thumb {
    aspect-ratio: 4 / 5;
    background: #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}
.doc-tile-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.doc-tile-meta {
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.doc-tile .doc-name {
    font-weight: 600;
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.doc-tile .status { align-self: flex-start; }

/* Document modal */
.doc-dialog {
    border: none;
    border-radius: 12px;
    padding: 0;
    width: min(1100px, 92vw);
    max-width: 92vw;
    max-height: 90vh;
    box-shadow: 0 20px 60px rgba(0,0,0,0.25);
    background: var(--card);
}
.doc-dialog::backdrop { background: rgba(0,0,0,0.45); }
.doc-dialog .dialog-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}
.doc-dialog .dialog-header #dlgLabel {
    flex: 1;
    border: 1px solid transparent;
    background: transparent;
    border-radius: 6px;
    padding: 6px 10px;
    font-weight: 600;
    font-size: 1.05rem;
    margin: 0;
    outline: none;
    width: auto;
}
.doc-dialog .dialog-header #dlgLabel:hover { background: rgba(0,0,0,0.03); }
.doc-dialog .dialog-header #dlgLabel:focus { background: var(--card); box-shadow: 0 0 0 2px var(--primary); }
.dialog-close {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 32px;
    height: 32px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    font-size: 1.4rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.dialog-close:hover { background: var(--bg); color: var(--text); }

.doc-dialog .dialog-body {
    display: grid;
    grid-template-columns: minmax(280px, 1fr) minmax(280px, 1fr);
    gap: 0;
    height: calc(90vh - 60px);
    overflow: hidden;
}
.doc-dialog .dlg-image-wrap {
    background: #0f0f10;
    overflow: auto;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 14px;
}
.doc-dialog .dlg-image-wrap img { max-width: 100%; height: auto; border-radius: 4px; }
.doc-dialog .dlg-text-wrap {
    display: flex;
    flex-direction: column;
    padding: 14px 16px;
    gap: 8px;
    overflow: hidden;
}
.doc-dialog .dlg-text-label { font-size: 0.85rem; color: var(--muted); margin: 0; }
.doc-dialog #dlgText {
    flex: 1;
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px;
    font: inherit;
    resize: none;
    white-space: pre-wrap;
    background: var(--card);
}
.doc-dialog .dlg-actions { gap: 8px; flex-wrap: wrap; align-items: center; }
.doc-dialog button.danger {
    background: transparent;
    color: var(--error);
    border: 1px solid var(--error);
}
.doc-dialog button.danger:hover { background: var(--error-bg); color: var(--error); }

@media (max-width: 720px) {
    .doc-dialog .dialog-body {
        grid-template-columns: 1fr;
        height: auto;
        max-height: calc(90vh - 60px);
        overflow-y: auto;
    }
    .doc-dialog .dlg-image-wrap { max-height: 40vh; }
}
