:root {
  --sidebar: #0c1222;
  --sidebar-hover: #1a2338;
  --sidebar-text: #94a3b8;
  --sidebar-active: #f1f5f9;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --ok: #059669;
  --ok-bg: #ecfdf5;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --danger: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html { font-size: 15px; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

.app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 1.5rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.5rem;
  text-decoration: none;
  color: #fff;
}

.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.85rem;
  color: #fff;
}

.brand-text { font-weight: 600; font-size: 0.95rem; line-height: 1.3; }
.brand-sub { font-size: 0.7rem; color: var(--sidebar-text); font-weight: 400; }

.side-nav { display: flex; flex-direction: column; gap: 0.25rem; }

.side-nav a {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.side-nav a:hover { background: var(--sidebar-hover); color: var(--sidebar-active); }
.side-nav a.active { background: var(--sidebar-hover); color: #fff; }

.main-wrap {
  flex: 1;
  min-width: 0;
  padding: 1.75rem 2rem 3rem;
}

.page-header {
  margin-bottom: 1.5rem;
}

.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

.breadcrumb a { color: var(--muted); }
.breadcrumb a:hover { color: var(--accent); }
.breadcrumb-sep { opacity: 0.4; }

.page-title {
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0 0 0.35rem;
  line-height: 1.25;
}

.page-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.page-subtitle code {
  font-size: 0.85em;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 0.1rem 0.45rem;
  border-radius: 4px;
}

.header-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

.card-flush { padding: 0; overflow: hidden; }

.section-title {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 1rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.stat {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 1rem 1.1rem;
}

.stat-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 0.25rem; }
.stat-value { font-size: 1rem; font-weight: 600; }

.table-wrap { overflow-x: auto; }

table.data {
  width: 100%;
  border-collapse: collapse;
}

table.data th {
  text-align: left;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 0.85rem 1.25rem;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}

table.data td {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover { background: #fafbfc; }

.campaign-name { font-weight: 600; color: var(--text); }
.campaign-name:hover { color: var(--accent); }
.campaign-slug { font-size: 0.8rem; color: var(--muted); font-family: ui-monospace, monospace; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: #f1f5f9;
  color: var(--muted);
  white-space: nowrap;
}

.badge-active { background: var(--ok-bg); color: var(--ok); }
.badge-testing { background: #eff6ff; color: #2563eb; }
.badge-planned { background: #f5f3ff; color: #7c3aed; }
.badge-deferred { background: #f8fafc; color: #64748b; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }

.tier-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 8px;
  font-weight: 700;
  font-size: 0.8rem;
  background: var(--accent-soft);
  color: var(--accent);
}

.segmented {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
}

.segmented a {
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  transition: all 0.15s;
}

.segmented a:hover { color: var(--text); }
.segmented a.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.btn:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
  color: var(--text);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.25);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: #fff;
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover { background: var(--bg); color: var(--text); }

.form-section {
  margin-bottom: 1.75rem;
  padding-bottom: 1.75rem;
  border-bottom: 1px solid var(--border);
}

.form-section:last-of-type { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

.form-section h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0 0 1rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem 1.25rem;
}

.form-grid .full { grid-column: 1 / -1; }

label {
  display: block;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.35rem;
}

.hint { font-size: 0.78rem; color: var(--muted); margin-top: 0.3rem; }

input[type=text],
input[type=number],
input[type=password],
select,
textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 0.9rem;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

textarea { min-height: 6rem; resize: vertical; }

.prose h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 1.5rem 0 0.5rem;
  color: var(--text);
}

.prose h2:first-child { margin-top: 0; }

.prose p { margin: 0.5rem 0; color: #334155; }

.prose ul {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
  color: #334155;
}

.prose li { margin: 0.35rem 0; }

.empty {
  text-align: center;
  padding: 3rem 1.5rem;
  color: var(--muted);
}

.empty-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  opacity: 0.5;
}

.chat-layout {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 12rem);
  max-height: calc(100vh - 8rem);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: #f8fafc;
}

.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 28rem;
}

.chat-bubble {
  max-width: 85%;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  line-height: 1.5;
}

.chat-bubble-user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-bubble-assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.chat-bubble-label {
  font-size: 0.72rem;
  font-weight: 600;
  opacity: 0.75;
  margin-bottom: 0.25rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.chat-bubble-text {
  white-space: pre-wrap;
  font-size: 0.92rem;
}

.chat-compose {
  border-top: 1px solid var(--border);
  padding: 1rem 1.25rem;
  background: var(--surface);
}

.chat-compose textarea {
  min-height: 4.5rem;
  margin-bottom: 0.75rem;
}

.chat-compose-actions {
  display: flex;
  justify-content: flex-end;
}

.setup-layout {
  display: grid;
  grid-template-columns: 1fr min(340px, 38%);
  gap: 1.25rem;
  align-items: start;
}

@media (max-width: 960px) {
  .setup-layout {
    grid-template-columns: 1fr;
  }
}

.setup-intro {
  background: linear-gradient(135deg, #eff6ff 0%, #f8fafc 100%);
  border-color: #bfdbfe;
}

.setup-steps-inline {
  margin: 0;
  padding-left: 1.25rem;
  color: var(--muted);
  line-height: 1.65;
  max-width: none;
}

.field-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.sources-panel {
  padding: 1.25rem;
  position: sticky;
  top: 1rem;
}

.sources-title {
  margin: 0 0 0.25rem;
  font-size: 1rem;
}

.sources-hint {
  margin: 0 0 1rem;
  font-size: 0.85rem;
}

.dropzone-form {
  margin-bottom: 1rem;
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 1.25rem 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: #f8fafc;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone:hover {
  border-color: var(--accent);
  background: #f1f5f9;
}

.dropzone-input {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  overflow: hidden;
}

.dropzone-label {
  font-weight: 600;
  color: var(--accent);
}

.dropzone-hint {
  font-size: 0.8rem;
  color: var(--muted);
}

.dropzone-meta {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.dropzone-select,
.dropzone-title {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
}

.btn-block {
  width: 100%;
  justify-content: center;
}

.paste-details {
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.paste-details summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 500;
  margin-bottom: 0.5rem;
}

.paste-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.paste-form textarea {
  min-height: 5rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  resize: vertical;
}

.sources-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.sources-empty {
  margin: 0;
  text-align: center;
  padding: 1rem 0;
}

.source-card {
  display: block;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s, background 0.15s;
}

.source-card:hover {
  border-color: var(--accent);
  background: #f8fafc;
}

.source-card-active {
  border-color: var(--accent);
  background: #eff6ff;
}

.source-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.2rem;
}

.source-card-title {
  font-weight: 600;
  font-size: 0.9rem;
}

.source-card-meta {
  font-size: 0.78rem;
}

.badge-ok {
  background: var(--ok-bg);
  color: var(--ok);
}

.badge-warn {
  background: #fef3c7;
  color: #92400e;
}

.setup-welcome {
  text-align: center;
  padding: 2.5rem 1.5rem;
}

.setup-steps {
  text-align: left;
  max-width: 22rem;
  margin: 0 auto;
  padding-left: 1.25rem;
  color: var(--muted);
  line-height: 1.7;
}

.setup-excerpt {
  padding: 0.75rem 1rem;
  background: #f8fafc;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.setup-success {
  background: var(--ok-bg);
  border-color: var(--ok);
}

.setup-error {
  background: #fef2f2;
  border-color: #fecaca;
}

.setup-ok {
  background: var(--ok-bg);
  border-color: var(--ok);
}

.setup-banner {
  background: linear-gradient(135deg, #eff6ff 0%, #f8fafc 100%);
  border-color: #bfdbfe;
}

.preview-heading {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.preview-item {
  padding: 0.65rem 0;
  border-top: 1px solid var(--border);
}

.preview-item:first-of-type {
  border-top: none;
  padding-top: 0;
}

@media (max-width: 768px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 1rem;
  }
  .side-nav { flex-direction: row; }
  .main-wrap { padding: 1.25rem 1rem 2rem; }
}

.sidebar-foot {
  margin-top: auto;
  padding-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.82rem;
}

.sidebar-foot .hint { color: var(--sidebar-text); }

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.82rem;
}

.side-link-cabinet {
  font-size: 0.85rem;
  opacity: 0.85;
}

body.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, #0c1222 0%, #1e293b 50%, #f1f5f9 50%);
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.login-form label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
  margin-top: 0.35rem;
}

.login-form input {
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
}

.login-error {
  color: var(--danger);
  background: #fef2f2;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-sm);
  margin: 0 0 1rem;
  font-size: 0.9rem;
}

.btn-block { width: 100%; justify-content: center; }

.metric-glossary {
  margin-bottom: 1.25rem;
  padding: 0.75rem 1rem;
  background: var(--surface-2, #f8fafc);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
}

.metric-glossary summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

.metric-glossary-list {
  margin: 0.75rem 0 0;
}

.metric-glossary-list dt {
  font-weight: 600;
  margin-top: 0.5rem;
  color: var(--text);
}

.metric-glossary-list dd {
  margin: 0.15rem 0 0;
  color: var(--muted);
  line-height: 1.5;
}

.metric-tip {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  cursor: help;
  border-bottom: 1px dotted var(--muted);
}

.metric-tip-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 0.95rem;
  height: 0.95rem;
  border-radius: 50%;
  background: var(--surface-2, #e2e8f0);
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1;
  flex-shrink: 0;
}

.metric-tip-popup {
  display: none;
  position: absolute;
  z-index: 20;
  left: 0;
  top: calc(100% + 0.35rem);
  min-width: 14rem;
  max-width: 18rem;
  padding: 0.5rem 0.65rem;
  background: var(--text, #0f172a);
  color: #f8fafc;
  font-size: 0.78rem;
  font-weight: 400;
  line-height: 1.45;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.18);
  white-space: normal;
}

.metric-tip:hover .metric-tip-popup,
.metric-tip:focus .metric-tip-popup,
.metric-tip:focus-within .metric-tip-popup {
  display: block;
}

th .metric-tip {
  border-bottom: none;
}

.badge-muted {
  background: var(--surface-2, #f1f5f9);
  color: var(--muted);
}
