:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-sunken: #f9fafb;
  --border: #e3e5ea;
  --border-strong: #cdd0d8;
  --text: #16181d;
  --text-muted: #6b7080;
  --text-faint: #9297a6;
  --accent: #4338ca;
  --accent-hover: #362cb0;
  --accent-soft: #eeecfd;
  --accent-contrast: #ffffff;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --warn: #a5730a;

  --mentioned-ranked: #0f7a4d;
  --mentioned-ranked-bg: #e2f6ec;
  --mentioned-unranked: #1b6fa8;
  --mentioned-unranked-bg: #e3f2fa;
  --not-mentioned-bg: #eef0f3;
  --not-mentioned-text: #6b7080;
  --error-bg: #fdecea;
  --error-text: #a3291f;
  --corrected: #7c3aed;
  --corrected-bg: #f3ecfe;

  --shadow-sm: 0 1px 2px rgba(20, 21, 26, 0.05);
  --shadow-md: 0 4px 16px rgba(20, 21, 26, 0.08);
  --shadow-lg: 0 16px 48px rgba(20, 21, 26, 0.18);
  --radius: 12px;
  --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101114;
    --surface: #1a1b1f;
    --surface-sunken: #16171b;
    --border: #2b2d33;
    --border-strong: #3c3f47;
    --text: #f1f2f4;
    --text-muted: #9a9da8;
    --text-faint: #6d7078;
    --accent: #8b85f5;
    --accent-hover: #a29cf9;
    --accent-soft: #262247;
    --accent-contrast: #14131f;
    --danger: #ff8a80;
    --danger-bg: #3a1f1d;
    --warn: #e0ac4f;

    --mentioned-ranked: #4fd6a0;
    --mentioned-ranked-bg: #103526;
    --mentioned-unranked: #6cc7f0;
    --mentioned-unranked-bg: #10293a;
    --not-mentioned-bg: #212328;
    --not-mentioned-text: #9a9da8;
    --error-bg: #3a1f1d;
    --error-text: #ff9c92;
    --corrected: #b9a3fb;
    --corrected-bg: #241d3d;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

button, input, textarea, select { font-family: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 30;
}
.brand {
  font-weight: 700;
  text-decoration: none;
  color: var(--text);
  font-size: 0.98rem;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand::before {
  content: "";
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 3px;
  background: var(--accent);
}
.brand.has-logo::before { content: none; }
.brand-logo { height: 26px; width: auto; border-radius: 4px; }
.login-logo { height: 44px; width: auto; display: block; margin: 0 0 16px; border-radius: 6px; }
.logout-form { margin: 0; }

.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 40px 28px 96px;
}

h1 { font-size: 1.6rem; margin: 0 0 4px; font-weight: 700; letter-spacing: -0.015em; }
h2 { font-size: 1.05rem; margin: 32px 0 14px; font-weight: 650; letter-spacing: -0.01em; }
h3 { font-size: 0.9rem; margin: 0 0 8px; font-weight: 650; }
.muted { color: var(--text-muted); }
.small { font-size: 0.82rem; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 24px;
}
.breadcrumb { margin: 0 0 6px; font-size: 0.83rem; }
.breadcrumb a { color: var(--text-muted); text-decoration: none; font-weight: 500; }
.breadcrumb a:hover { color: var(--accent); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.2;
  transition: background 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease, transform 0.05s ease;
}
.btn:hover { border-color: var(--text-faint); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn-primary {
  background: var(--accent); color: var(--accent-contrast); border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-secondary { background: var(--surface); }
.btn-small { padding: 6px 12px; font-size: 0.8rem; }
.link-button {
  background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 0.85rem; padding: 0;
  font-weight: 500;
}
.link-button:hover { color: var(--text); }

.empty-state {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 36px 28px;
  text-align: center;
  background: var(--surface);
}
.empty-state p { max-width: 480px; margin: 0 auto 18px; color: var(--text-muted); font-size: 0.94rem; }
.empty-state-positive { border-style: solid; border-color: var(--mentioned-ranked); background: var(--mentioned-ranked-bg); }
.empty-state-positive p { color: var(--text); margin-bottom: 0; }

.table {
  width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--border); box-shadow: var(--shadow-sm);
}
.table th, .table td { text-align: left; padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 0.89rem; }
.table th { color: var(--text-muted); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; background: var(--surface-sunken); }
.table tbody tr { transition: background 0.1s ease; }
.table tbody tr:hover { background: var(--surface-sunken); }
.table tr:last-child td { border-bottom: none; }
.table-actions { text-align: right; }
.table-actions a { font-weight: 600; font-size: 0.85rem; text-decoration: none; }
.table-actions a:hover { text-decoration: underline; }

.status-tag {
  padding: 3px 10px; border-radius: 999px; font-size: 0.75rem; font-weight: 600;
  background: var(--not-mentioned-bg); color: var(--text-muted); display: inline-block;
}
.status-running { background: var(--mentioned-unranked-bg); color: var(--mentioned-unranked); }
.status-completed { background: var(--mentioned-ranked-bg); color: var(--mentioned-ranked); }

.stack { display: flex; flex-direction: column; gap: 18px; max-width: 480px; }
.form-wide { max-width: 660px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.86rem; font-weight: 600; color: var(--text); }
input[type=text], input[type=password], input[type=number], textarea {
  font-family: inherit;
  font-size: 0.92rem;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-weight: 400;
  resize: vertical;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
input[type=text]:focus, input[type=password]:focus, input[type=number]:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}

.login-wrap { display: flex; justify-content: center; padding-top: 100px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 36px;
  width: 100%; max-width: 380px; box-shadow: var(--shadow-md);
}
.login-card h1 { font-size: 1.25rem; }
.login-card .muted { margin-bottom: 26px; }

.alert { padding: 11px 14px; border-radius: var(--radius-sm); font-size: 0.87rem; font-weight: 500; }
.alert-error { background: var(--danger-bg); color: var(--danger); }

.field-group { border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin: 0; background: var(--surface); }
.field-group legend { padding: 0 6px; font-weight: 650; font-size: 0.86rem; }
.field-group + .field-group { margin-top: 18px; }

.repeatable-list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.repeatable-row { display: flex; gap: 8px; align-items: flex-start; }
.repeatable-row input, .repeatable-row textarea { flex: 1; }
.prompt-row.duplicate textarea { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, var(--surface)); }
.btn-remove {
  border: none; background: var(--not-mentioned-bg); color: var(--text-muted);
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 1rem; line-height: 1;
  flex-shrink: 0; transition: background 0.12s ease, color 0.12s ease;
}
.btn-remove:hover { background: var(--error-bg); color: var(--error-text); }

.prompt-import-tabs { display: flex; flex-direction: column; gap: 10px; margin-bottom: 8px; }
.import-panel { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 14px; background: var(--surface-sunken); }
.import-panel summary { cursor: pointer; font-size: 0.85rem; font-weight: 600; }
.import-panel summary::marker { color: var(--text-faint); }
.import-panel textarea, .import-panel input[type=file] { width: 100%; margin-top: 10px; }
.import-panel .btn { margin-top: 10px; }

.prompt-count-line { font-size: 0.85rem; color: var(--text-muted); margin: 6px 0 0; font-weight: 500; }
.duplicate-note { color: var(--warn); font-weight: 600; }

.competitor-section { margin-top: 32px; }
.competitor-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.competitor-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 14px 16px; box-shadow: var(--shadow-sm);
}
.competitor-card-header { display: flex; align-items: center; gap: 10px; }
.competitor-card-header strong { font-size: 0.94rem; }
.competitor-card-header form { margin-left: auto; }
.competitor-rating { font-size: 0.82rem; color: var(--warn); font-weight: 600; }
.competitor-notes-text { font-size: 0.87rem; margin: 8px 0 0; white-space: pre-wrap; }
.competitor-add-panel { max-width: 640px; }
.competitor-form { display: flex; flex-direction: column; gap: 12px; margin-top: 10px; }
.competitor-form-row { display: flex; gap: 10px; flex-wrap: wrap; }
.competitor-form-row label { flex: 1; min-width: 110px; }
.competitor-form label { font-weight: 600; font-size: 0.82rem; }

.form-actions { display: flex; gap: 10px; padding-top: 4px; }
.page-header-actions { display: flex; gap: 10px; }

.manual-list { display: flex; flex-direction: column; gap: 14px; }
.manual-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow-sm);
}
.manual-prompt { font-weight: 650; font-size: 0.95rem; margin: 0 0 12px; }
.manual-form { display: flex; flex-direction: column; gap: 12px; }
.manual-form-row { display: flex; gap: 14px; flex-wrap: wrap; align-items: flex-end; }
.manual-source { flex: 2; min-width: 200px; }
.manual-mentioned { flex: 0 0 auto; padding-bottom: 9px; font-weight: 500; }
.manual-position { flex: 1; min-width: 100px; }
.manual-form label { font-weight: 600; font-size: 0.82rem; }
.manual-form-actions { display: flex; align-items: center; gap: 12px; }
.manual-orphans { margin-top: 32px; }
.manual-orphans .manual-card { opacity: 0.85; }
.manual-orphans form { margin-top: 8px; }

.checkbox-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.checkbox-pill {
  display: flex; align-items: center; gap: 7px; border: 1px solid var(--border-strong); border-radius: 999px;
  padding: 8px 16px; font-size: 0.85rem; font-weight: 600; cursor: pointer; background: var(--surface);
  transition: border-color 0.12s ease, background 0.12s ease, color 0.12s ease;
}
.checkbox-pill:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.checkbox-pill input { accent-color: var(--accent); }
.checkbox-inline { flex-direction: row; align-items: center; font-weight: 500; }
.run-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow-sm); }

.run-header-actions { display: flex; align-items: flex-start; gap: 10px; }

.run-cost-box {
  text-align: right; font-size: 0.85rem; display: flex; flex-direction: column; gap: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px 16px; box-shadow: var(--shadow-sm);
}
.run-cost-box .muted { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; margin-right: 6px; }

.provider-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 20px; }
.provider-summary-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 14px 16px; box-shadow: var(--shadow-sm); text-align: center;
}
.provider-summary-name { font-size: 0.78rem; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin: 0 0 6px; }
.provider-summary-pct { font-size: 1.6rem; font-weight: 700; margin: 0; letter-spacing: -0.02em; }
.provider-summary-card .small { display: block; margin-top: 4px; }

.progress-banner { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 20px; margin-bottom: 20px; box-shadow: var(--shadow-sm); }
.progress-bar { height: 8px; border-radius: 999px; background: var(--not-mentioned-bg); overflow: hidden; margin-bottom: 10px; }
.progress-fill { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.progress-banner p { margin: 0; font-size: 0.85rem; color: var(--text-muted); font-weight: 500; }

.grid-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow-sm); }
.results-grid { border-collapse: collapse; width: 100%; min-width: 640px; table-layout: fixed; }
.results-grid th, .results-grid td { border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); padding: 0; }
.results-grid th {
  padding: 12px 16px; text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-muted); background: var(--surface-sunken); font-weight: 650;
}
.results-grid th:last-child, .results-grid td:last-child { border-right: none; }
.results-grid tr:last-child td { border-bottom: none; }
.prompt-col {
  width: 34%; padding: 0 !important; font-size: 0.87rem; vertical-align: top;
}
.prompt-col-inner {
  max-height: 140px; overflow-y: auto; padding: 14px 16px;
}

.grid-cell-td { text-align: center; vertical-align: stretch; }
.grid-cell {
  width: 100%; height: 100%; min-height: 64px; border: none; background: var(--not-mentioned-bg);
  color: var(--not-mentioned-text); cursor: pointer; font-size: 0.86rem; font-weight: 650;
  display: flex; align-items: center; justify-content: center; gap: 6px; position: relative; padding: 10px;
  transition: filter 0.1s ease;
}
.grid-cell:hover { filter: brightness(0.97); }
.cell-mentioned-ranked { background: var(--mentioned-ranked-bg); color: var(--mentioned-ranked); font-size: 1rem; }
.cell-mentioned-unranked { background: var(--mentioned-unranked-bg); color: var(--mentioned-unranked); }
.cell-not-mentioned { background: var(--surface); color: var(--text-faint); font-weight: 400; }
.cell-generation_error, .cell-extraction_error { background: var(--error-bg); color: var(--error-text); }
.cell-pending, .cell-running { background: var(--surface-sunken); }

.spinner {
  width: 15px; height: 15px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.corrected-dot {
  position: absolute; top: 7px; right: 7px; width: 7px; height: 7px; border-radius: 50%; background: var(--corrected);
  box-shadow: 0 0 0 2px var(--surface);
}
.corrected-dot-static { position: static; display: inline-block; box-shadow: none; }
.corrected-badge {
  font-size: 0.72rem; font-weight: 650; color: var(--corrected); background: var(--corrected-bg);
  border-radius: 999px; padding: 3px 10px; margin-left: 8px; vertical-align: middle;
}

.grid-legend { display: flex; flex-wrap: wrap; gap: 18px; margin-top: 16px; font-size: 0.8rem; color: var(--text-muted); align-items: center; font-weight: 500; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: middle; }
.swatch-mentioned-ranked { background: var(--mentioned-ranked); }
.swatch-mentioned-unranked { background: var(--mentioned-unranked); }
.swatch-not-mentioned { background: var(--text-faint); }
.swatch-error { background: var(--error-text); }

.section-heading { font-size: 1.15rem; font-weight: 650; margin: 40px 0 14px; }

.raw-list { display: flex; flex-direction: column; }
.raw-prompt-block { padding: 22px 0; border-top: 1px solid var(--border); }
.raw-prompt-block:first-child { border-top: none; padding-top: 0; }
.raw-prompt-text { font-weight: 650; font-size: 0.95rem; margin: 0 0 14px; }
.raw-answer { margin-bottom: 16px; }
.raw-answer:last-child { margin-bottom: 0; }
.raw-answer-label { font-size: 0.75rem; font-weight: 650; text-transform: uppercase; letter-spacing: 0.03em; margin: 0 0 6px; }
.raw-answer-label.label-mentioned-ranked { color: var(--mentioned-ranked); }
.raw-answer-label.label-mentioned-unranked { color: var(--mentioned-unranked); }
.raw-answer-label.label-not-mentioned { color: var(--not-mentioned-text); }
.raw-answer-label.label-error { color: var(--error-text); }
.raw-answer-text {
  white-space: pre-wrap; word-break: break-word; margin: 0; background: var(--surface-sunken);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px;
  font-size: 0.83rem; line-height: 1.55; font-family: inherit; color: var(--text);
}
.raw-answer-error { margin: 0; font-size: 0.85rem; color: var(--error-text); }

.detail-backdrop { position: fixed; inset: 0; background: rgba(15, 16, 20, 0.4); z-index: 20; backdrop-filter: blur(1px); }
.detail-panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); background: var(--surface);
  border-left: 1px solid var(--border); z-index: 21; display: flex; flex-direction: column; overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.detail-panel-header { display: flex; justify-content: space-between; align-items: flex-start; padding: 20px 22px; border-bottom: 1px solid var(--border); gap: 10px; }
.detail-prompt { font-weight: 650; margin: 5px 0 0; font-size: 0.98rem; }
.detail-panel-body { display: flex; flex-direction: column; gap: 22px; padding: 22px; }
.detail-raw pre {
  white-space: pre-wrap; word-break: break-word; background: var(--surface-sunken); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 15px; font-size: 0.85rem; max-height: 40vh; overflow-y: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.detail-fields { display: flex; flex-direction: column; gap: 16px; }
.detail-actions { display: flex; gap: 10px; margin-top: 4px; }
#detail-error-banner { margin: 0 22px; }

@media (max-width: 700px) {
  .page { padding: 24px 16px 72px; }
  .page-header { flex-direction: column; }
  .detail-panel { width: 100vw; }
}
