/* ─── Reset ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ─── Tokens ─────────────────────────────────────────── */
:root {
  --bg:           #f7f7f8;
  --bg-card:      #ffffff;
  --bg-code:      #f3f4f6;
  --text:         #111827;
  --text-muted:   #6b7280;
  --text-link:    #2563eb;
  --border:       #e5e7eb;
  --accent:       #2563eb;
  --accent-hover: #1d4ed8;
  --accent-fg:    #ffffff;
  --confirm-bg:   #eff6ff;
  --confirm-border: #bfdbfe;
  --warn-bg:      #fffbeb;
  --warn-border:  #fbbf24;
  --warn-btn:     #d97706;
  --warn-btn-hover: #b45309;
  --error-bg:     #fef2f2;
  --error-border: #fca5a5;
  --success:      #059669;
  --shadow-sm:    0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --radius:       10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:           #0f0f10;
    --bg-card:      #1c1c1e;
    --bg-code:      #2c2c2e;
    --text:         #f2f2f7;
    --text-muted:   #8e8e93;
    --text-link:    #60a5fa;
    --border:       #3a3a3c;
    --accent:       #3b82f6;
    --accent-hover: #2563eb;
    --accent-fg:    #ffffff;
    --confirm-bg:   #172554;
    --confirm-border: #1e40af;
    --warn-bg:      #292009;
    --warn-border:  #92400e;
    --warn-btn:     #f59e0b;
    --warn-btn-hover: #fbbf24;
    --error-bg:     #1f0808;
    --error-border: #991b1b;
    --success:      #34d399;
    --shadow-sm:    0 1px 3px rgba(0,0,0,.4);
  }
}

/* ─── Base ────────────────────────────────────────────── */
html { font-size: 16px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--text-link); }
a:hover { text-decoration: underline; }

/* ─── Header ──────────────────────────────────────────── */
header {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 0 2rem;
}

.header-inner {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 2rem;
  padding: 1rem 0;
  flex-wrap: wrap;
}

h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -.02em;
  white-space: nowrap;
}

.model-toggle {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.model-toggle label {
  display: flex;
  align-items: center;
  gap: .35rem;
  font-size: .875rem;
  cursor: pointer;
  white-space: nowrap;
}

.model-note {
  font-size: .75rem;
  color: var(--text-muted);
  font-style: italic;
}

/* ─── Main ───────────────────────────────────────────── */
main {
  flex: 1;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ─── Cards ──────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow-sm);
}

.card-confirm {
  background: var(--confirm-bg);
  border-color: var(--confirm-border);
}

.card-warning {
  background: var(--warn-bg);
  border-color: var(--warn-border);
}

.card-error {
  background: var(--error-bg);
  border-color: var(--error-border);
}

.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1rem;
}

/* ─── Form elements ───────────────────────────────────── */
.field-label {
  display: block;
  font-size: .875rem;
  font-weight: 500;
  margin-bottom: .5rem;
}

textarea {
  display: block;
  width: 100%;
  padding: .625rem .75rem;
  font-family: var(--font);
  font-size: .9375rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  line-height: 1.5;
}

textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ─── Buttons ─────────────────────────────────────────── */
.btn-primary, .btn-secondary, .btn-warning {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  padding: .5rem 1.1rem;
  font-size: .875rem;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-warning {
  background: var(--warn-btn);
  color: #fff;
}
.btn-warning:hover { background: var(--warn-btn-hover); }

.btn-sm { padding: .375rem .8rem; font-size: .8125rem; }

.card-actions {
  display: flex;
  gap: .75rem;
  margin-top: 1.25rem;
  flex-wrap: wrap;
}

/* ─── Clarification section ───────────────────────────── */
#conversation {
  display: flex;
  flex-direction: column;
  gap: .75rem;
  margin-bottom: 1rem;
}

.msg {
  padding: .625rem .875rem;
  border-radius: 8px;
  font-size: .9375rem;
  line-height: 1.55;
  max-width: 90%;
}

.msg-user {
  background: var(--accent);
  color: var(--accent-fg);
  align-self: flex-end;
}

.msg-assistant {
  background: var(--bg-code);
  color: var(--text);
  align-self: flex-start;
}

.inline-input {
  display: flex;
  gap: .75rem;
  align-items: flex-end;
}

.inline-input textarea { flex: 1; }

/* ─── Confirmation card body ─────────────────────────── */
#confirm-body {
  font-size: .9375rem;
  line-height: 1.65;
}

#confirm-body strong { font-weight: 600; }

#confirm-body .plan-row {
  display: grid;
  grid-template-columns: 9rem 1fr;
  gap: .25rem .75rem;
  padding: .25rem 0;
  border-bottom: 1px solid var(--confirm-border);
}

#confirm-body .plan-row:last-child { border-bottom: none; }

#confirm-body .plan-label { color: var(--text-muted); font-size: .8125rem; }

/* ─── Loading ─────────────────────────────────────────── */
.loading {
  display: flex;
  align-items: center;
  gap: .875rem;
  padding: 1rem 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: .9375rem;
}

.spinner {
  width: 20px; height: 20px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Results ─────────────────────────────────────────── */
.results-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.results-header h2 { font-size: 1rem; font-weight: 600; }

.results-actions { display: flex; gap: .5rem; }

.result-group {
  margin-bottom: 1.75rem;
}

.result-group-title {
  font-size: .8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: .625rem;
  padding-bottom: .375rem;
  border-bottom: 1px solid var(--border);
}

.results-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .875rem;
}

.results-table th {
  text-align: left;
  padding: .5rem .75rem;
  background: var(--bg);
  border-bottom: 2px solid var(--border);
  font-weight: 600;
  font-size: .8125rem;
  white-space: nowrap;
}

.results-table td {
  padding: .5rem .75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.results-table tr:hover td { background: var(--bg); }

.col-ml { width: 9rem; }
.col-date { width: 7rem; white-space: nowrap; }
.col-type { width: 16rem; }

/* ─── Phase 2 analysis ────────────────────────────────── */
.token-estimate, .token-info {
  font-size: .8125rem;
  color: var(--text-muted);
  margin-top: .625rem;
  font-style: italic;
}

.progress-block {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  margin-bottom: 1.25rem;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: .625rem;
  font-size: .875rem;
  padding: .375rem 0;
}

.progress-item .pi-icon {
  width: 18px; height: 18px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: .6875rem;
}

.pi-pending .pi-icon { background: var(--border); }
.pi-running .pi-icon { border: 2px solid var(--accent); border-top-color: transparent; animation: spin .7s linear infinite; }
.pi-done .pi-icon    { background: var(--success); color: #fff; }
.pi-scanned .pi-icon { background: var(--warn-border); color: #fff; }
.pi-error .pi-icon   { background: var(--error-border); color: #fff; }

.pi-label { flex: 1; color: var(--text-muted); }
.pi-done .pi-label, .pi-running .pi-label { color: var(--text); }

#analysis-synthesis {
  white-space: pre-wrap;
  font-size: .9375rem;
  line-height: 1.7;
}

/* ─── Modal ───────────────────────────────────────────── */
dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  max-width: 640px;
  width: calc(100% - 2rem);
  padding: 0;
  max-height: 85vh;
  overflow-y: auto;
}

dialog::backdrop { background: rgba(0,0,0,.45); }

.modal-inner {
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.modal-inner h2 { font-size: 1.125rem; font-weight: 700; }
.modal-inner h3 { font-size: .9375rem; font-weight: 600; margin-top: .5rem; }
.modal-inner p, .modal-inner li { font-size: .9375rem; line-height: 1.6; }
.modal-inner dl { display: flex; flex-direction: column; gap: .75rem; }
.modal-inner dt { font-weight: 600; }
.modal-inner dd { margin-left: 1rem; color: var(--text-muted); }
.modal-inner ul { padding-left: 1.25rem; display: flex; flex-direction: column; gap: .25rem; }

.modal-close {
  position: absolute;
  top: 1rem; right: 1.25rem;
  font-size: 1.25rem;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: .25rem;
}
.modal-close:hover { color: var(--text); }

/* ─── Footer ──────────────────────────────────────────── */
footer {
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  padding: .75rem 2rem;
  font-size: .75rem;
  color: var(--text-muted);
  display: flex;
  gap: 1.5rem;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

footer a { color: var(--text-muted); }
footer a:hover { color: var(--text); }

.version { font-family: monospace; }

/* ─── Feed layout ─────────────────────────────────────── */
#feed {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* User message bubble — right-aligned, blue */
.feed-bubble-user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-fg);
  padding: .625rem .875rem;
  border-radius: 16px 16px 4px 16px;
  font-size: .9375rem;
  line-height: 1.55;
  max-width: 80%;
  word-break: break-word;
}

/* Assistant question text inside a clarification card */
.feed-assistant-text {
  font-size: .9375rem;
  line-height: 1.6;
  color: var(--text);
}

/* Bucket list inside confirm card */
.plan-row-buckets {
  align-items: flex-start;
}

.bucket-list {
  display: flex;
  flex-direction: column;
  gap: .375rem;
  width: 100%;
}

.bucket-item {
  display: grid;
  grid-template-columns: 1.25rem 1fr;
  gap: 0 .5rem;
  align-items: baseline;
  font-size: .875rem;
}

.bucket-num {
  color: var(--text-muted);
  font-size: .75rem;
  font-weight: 600;
  text-align: right;
}

.bucket-label {
  font-weight: 500;
}

.bucket-detail {
  grid-column: 2;
  font-size: .8rem;
  color: var(--text-muted);
  font-family: monospace;
}

/* Confirm card body — dynamic version (class-based, no id) */
.confirm-body {
  font-size: .9375rem;
  line-height: 1.65;
}

.confirm-body .plan-row {
  display: grid;
  grid-template-columns: 9rem 1fr;
  gap: .25rem .75rem;
  padding: .25rem 0;
  border-bottom: 1px solid var(--confirm-border);
}

.confirm-body .plan-row:last-of-type { border-bottom: none; }
.confirm-body .plan-label { color: var(--text-muted); font-size: .8125rem; }

.confirm-note {
  margin-top: .875rem;
  font-style: italic;
  color: var(--text-muted);
  font-size: .875rem;
}

/* Synthesis text */
.synthesis-text {
  white-space: pre-wrap;
  font-size: .9375rem;
  line-height: 1.7;
}

/* Analysis question input inside results card */
.analysis-input-section {
  border-top: 1px solid var(--border);
  padding-top: 1.25rem;
  margin-top: .5rem;
}

.analysis-q {
  display: block;
  width: 100%;
  padding: .625rem .75rem;
  font-family: var(--font);
  font-size: .9375rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  line-height: 1.5;
}

.analysis-q:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* results-header flex fix when export buttons are inside */
.results-header {
  flex-direction: column;
  align-items: flex-start;
  gap: .625rem;
}

/* ─── Agentic feed: assistant cards + markdown ─────────── */
.assistant-card { width: 100%; }

.md-body { font-size: .9375rem; line-height: 1.65; color: var(--text); }
.md-body > *:first-child { margin-top: 0; }
.md-body > *:last-child { margin-bottom: 0; }
.md-body h1 { font-size: 1.25rem; font-weight: 700; margin: 1.1rem 0 .6rem; }
.md-body h2 { font-size: 1.1rem;  font-weight: 700; margin: 1rem 0 .55rem; }
.md-body h3 { font-size: 1rem;    font-weight: 600; margin: .9rem 0 .5rem; }
.md-body h4, .md-body h5, .md-body h6 { font-size: .9375rem; font-weight: 600; margin: .8rem 0 .4rem; }
.md-body p { margin: .55rem 0; }
.md-body ul, .md-body ol { margin: .55rem 0; padding-left: 1.4rem; }
.md-body li { margin: .25rem 0; }
.md-body strong { font-weight: 600; }
.md-body code {
  font-family: ui-monospace, "Cascadia Code", "Liberation Mono", monospace;
  font-size: .85em;
  background: var(--bg-code);
  padding: .1em .35em;
  border-radius: 4px;
}
.md-body blockquote {
  margin: .6rem 0;
  padding: .35rem .9rem;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.md-body hr { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }

.md-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
  margin: .7rem 0;
}
.md-table th, .md-table td {
  text-align: left;
  padding: .4rem .6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.md-table th {
  background: var(--bg);
  font-weight: 600;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
.md-table tr:hover td { background: var(--bg); }

/* Tool-call lines (between assistant messages) */
.tool-calls {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  padding: .25rem 0 .25rem .25rem;
}
.tool-call {
  display: flex;
  align-items: baseline;
  gap: .5rem;
  font-size: .8125rem;
  color: var(--text-muted);
}
.tool-icon { flex-shrink: 0; }
.tool-label { font-family: ui-monospace, "Cascadia Code", "Liberation Mono", monospace; color: var(--text); }
.tool-summary { color: var(--text-muted); font-style: italic; }

/* Reply box (user's turn) */
.reply-box {
  display: flex;
  gap: .6rem;
  align-items: flex-end;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem;
  box-shadow: var(--shadow-sm);
}
.reply-text {
  flex: 1;
  padding: .5rem .65rem;
  font-family: var(--font);
  font-size: .9375rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  line-height: 1.5;
}
.reply-text:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Per-card actions (Copy / Save as PDF) */
.card-tools {
  display: flex;
  gap: .4rem;
  justify-content: flex-end;
  margin-top: .85rem;
  padding-top: .6rem;
  border-top: 1px solid var(--border);
  opacity: .75;
  transition: opacity .15s;
}
.assistant-card:hover .card-tools { opacity: 1; }
.card-tool {
  font-size: .75rem;
  padding: .3rem .7rem;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.card-tool:hover { color: var(--accent); border-color: var(--accent); }

/* Token status in footer */
#token-status { font-family: monospace; font-size: .7rem; color: var(--text-muted); }

/* ─── Utilities ───────────────────────────────────────── */
.hidden { display: none !important; }
