:root {
  --bg: #eef3f7;
  --surface: #ffffff;
  --ink: #111827;
  --muted: #667085;
  --line: #d8e1ea;
  --soft: #f4f7fa;
  --accent: #16736f;
  --accent-strong: #0f5f5b;
  --blue: #3f6fd8;
  --violet: #6f5bd6;
  --warning: #bb5a2a;
  --shadow: 0 18px 44px rgba(35, 52, 77, 0.12);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background:
    radial-gradient(circle at 20% 0%, rgba(22, 115, 111, 0.13), transparent 32%),
    radial-gradient(circle at 78% 8%, rgba(63, 111, 216, 0.13), transparent 26%),
    var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

.app-shell {
  display: grid;
  grid-template-columns: 248px 1fr;
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: linear-gradient(180deg, #0f172a, #111827 58%, #14201f);
  color: #f4f6fb;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  border-right: 1px solid rgba(255,255,255,0.08);
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  min-width: 0;
}

.mark {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: linear-gradient(135deg, #64d6c9, #9fb7ff);
  color: #0d1824;
  display: grid;
  place-items: center;
  font-weight: 900;
}

.brand h1 {
  font-size: 18px;
  margin: 0;
  letter-spacing: 0;
}

.brand p {
  margin: 3px 0 0;
  color: #a8b3c5;
  font-size: 12px;
}

nav {
  display: grid;
  gap: 6px;
}

.nav-item {
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: #c8d1df;
  text-align: left;
  padding: 12px 12px;
  cursor: pointer;
}

.nav-item.active, .nav-item:hover {
  background: rgba(255,255,255,0.1);
  color: #ffffff;
}

.sidebar-foot {
  margin-top: auto;
  display: flex;
  gap: 8px;
  align-items: center;
  color: #aeb8c8;
  font-size: 13px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #50d494;
}

main {
  padding: 28px;
  min-width: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 13px;
}

h2, h3 {
  margin: 0;
  letter-spacing: 0;
}

h2 { font-size: 28px; }
h3 { font-size: 17px; }

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.primary-btn, .icon-btn {
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 42px;
  cursor: pointer;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
  font-weight: 700;
}

.primary-btn:hover { background: var(--accent-strong); }
.primary-btn:disabled { opacity: 0.65; cursor: progress; }

.icon-btn {
  width: 42px;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
  font-size: 20px;
}

.view { display: none; }
.view.active { display: block; }

.offline-banner {
  display: none;
  margin-bottom: 14px;
  border: 1px solid #f0c98d;
  background: #fff7e8;
  color: #6f4714;
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 14px;
}

body.preview-mode .offline-banner {
  display: block;
}

body.readonly-mode .offline-banner {
  display: block;
  border-color: #b7c8dd;
  background: #f1f6fb;
  color: #334155;
}

body.readonly-mode #runBtn,
body.readonly-mode #runBtnSecondary,
body.readonly-mode #knowledgeForm button {
  opacity: 0.58;
  cursor: not-allowed;
}

.hero-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 0.45fr);
  gap: 22px;
  align-items: center;
  background:
    linear-gradient(120deg, rgba(15, 95, 91, 0.95), rgba(23, 32, 47, 0.96)),
    #17202f;
  color: #fff;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 10px;
  padding: 26px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.hero-panel h3 {
  max-width: 860px;
  font-size: 28px;
  line-height: 1.32;
}

.hero-panel .eyebrow {
  color: rgba(255,255,255,0.72);
}

.hero-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(92px, 1fr));
  gap: 10px;
}

.hero-stats div {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.16);
  border-radius: 8px;
  padding: 15px;
  min-width: 108px;
}

.hero-stats b {
  display: block;
  font-size: 30px;
}

.hero-stats span {
  display: block;
  color: rgba(255,255,255,0.72);
  font-size: 12px;
  margin-bottom: 6px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.brand-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.brand-tabs button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(35, 52, 77, 0.05);
}

.brand-tabs button.active {
  border-color: var(--accent);
  background: #e6f3f2;
  color: var(--accent-strong);
  font-weight: 750;
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 17px;
  min-height: 124px;
  box-shadow: 0 8px 20px rgba(25, 40, 65, 0.06);
}

.metric .label {
  color: var(--muted);
  font-size: 13px;
}

.metric .value {
  margin-top: 8px;
  font-size: 28px;
  font-weight: 850;
}

.metric .delta {
  margin-top: 10px;
  font-size: 13px;
  color: var(--accent-strong);
}

.layout-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(360px, 0.65fr);
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px;
  box-shadow: 0 8px 22px rgba(25, 40, 65, 0.07);
}

.account-growth-panel {
  grid-column: 1 / -1;
}

.chart-panel {
  min-height: 480px;
  grid-row: span 2;
}

.chart-panel.wide-chart {
  grid-column: 1 / -1;
  grid-row: auto;
}

.chart-panel.small-chart {
  min-height: 380px;
  grid-row: span 1;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.compact-head {
  margin-bottom: 8px;
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
}

.legend i {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  display: inline-block;
}

canvas {
  width: 100%;
  height: 380px;
  display: block;
}

.small-chart canvas {
  height: 300px;
}

.ai-brief {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(380px, 0.7fr);
  gap: 18px;
  align-items: stretch;
  margin-bottom: 16px;
  border-color: #a8ccc8;
  background:
    linear-gradient(120deg, rgba(22, 115, 111, 0.1), rgba(255,255,255,0.9)),
    var(--surface);
}

.analysis-text {
  line-height: 1.65;
  margin: 0;
  font-size: 17px;
  color: #1f2937;
}

.brief-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.brief-stats div {
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.78);
  border-radius: 9px;
  padding: 13px;
  min-width: 0;
}

.brief-stats b, .brief-stats span, .brief-stats strong {
  display: block;
}

.brief-stats b {
  font-size: 13px;
  overflow-wrap: anywhere;
}

.brief-stats span {
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
}

.brief-stats strong {
  color: var(--accent-strong);
  font-size: 24px;
  margin-top: 8px;
}

.signal-list, .run-list, .report-list, .knowledge-list, .source-status-list {
  display: grid;
  gap: 10px;
}

.account-growth-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 2px;
}

.account-growth-meta span {
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--muted);
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 12px;
}

.account-growth-head,
.account-growth-row {
  display: grid;
  grid-template-columns: minmax(190px, 1.35fr) repeat(4, minmax(86px, 0.7fr));
  gap: 10px;
  align-items: center;
}

.account-growth-head {
  color: var(--muted);
  font-size: 12px;
  padding: 0 12px;
}

.account-growth-head span:not(:first-child) {
  text-align: right;
}

.account-growth-row {
  background: var(--soft);
  border: 1px solid #dce4ef;
  border-radius: 9px;
  padding: 12px;
}

.account-growth-row > div {
  min-width: 0;
}

.account-growth-row b,
.account-growth-row span,
.account-growth-row strong {
  display: block;
}

.account-growth-account b {
  margin-bottom: 4px;
  overflow-wrap: anywhere;
}

.account-growth-row > div:not(.account-growth-account) {
  text-align: right;
}

.account-growth-row > div:not(.account-growth-account) span {
  color: var(--muted);
  font-size: 11px;
  margin-bottom: 3px;
}

.account-growth-row strong {
  font-size: 17px;
  line-height: 1.15;
  color: #17202f;
}

.brand-compare {
  display: grid;
  gap: 10px;
}

.compare-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: var(--soft);
  border: 1px solid #dce4ef;
  border-radius: 9px;
  padding: 13px;
}

.compare-row b, .compare-row span {
  display: block;
}

.compare-row strong {
  font-size: 20px;
  color: var(--accent-strong);
}

.account-matrix {
  display: grid;
  gap: 18px;
}

.brand-account-block {
  display: grid;
  gap: 12px;
}

.brand-row-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-row-head h3 {
  margin-bottom: 2px;
}

.brand-avatar {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: #17202f;
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 900;
}

.account-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 12px;
}

.account-card {
  display: grid;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  background: #fbfcff;
  border: 1px solid var(--line);
  border-top: 3px solid var(--accent);
  border-radius: 9px;
  padding: 14px;
}

.account-card:hover {
  border-color: #c7d3e3;
  box-shadow: 0 8px 20px rgba(25, 40, 65, 0.08);
}

.account-card span, .account-card small {
  color: var(--muted);
}

.account-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.account-card-head b,
.account-card-head span {
  display: block;
}

.account-card-head small {
  border: 1px solid #cfd9e6;
  background: #ffffff;
  border-radius: 999px;
  padding: 3px 8px;
  white-space: nowrap;
  font-size: 12px;
}

.capture-date {
  color: var(--muted);
  font-size: 12px;
}

.account-kpis {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.account-kpis div {
  min-width: 0;
  border: 1px solid #e1e8f1;
  background: #ffffff;
  border-radius: 8px;
  padding: 8px;
}

.account-kpis span,
.video-links span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.account-kpis strong {
  display: block;
  font-size: 18px;
  line-height: 1.1;
}

.video-links {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.video-links a {
  color: var(--accent-strong);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.video-links a:hover {
  text-decoration: underline;
}

.video-links em {
  color: var(--muted);
  font-style: normal;
  font-size: 13px;
}

.signal, .run-item, .report-item, .kb-item {
  background: var(--soft);
  border: 1px solid #dce4ef;
  border-radius: 9px;
  padding: 13px;
}

.signal b, .kb-item b {
  display: block;
  margin-bottom: 4px;
}

.source-status {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 86px;
  gap: 6px 12px;
  align-items: center;
  background: var(--soft);
  border: 1px solid #dce4ef;
  border-left: 4px solid #9aa4b2;
  border-radius: 9px;
  padding: 13px;
}

.source-status b,
.source-status span,
.source-status small {
  display: block;
  min-width: 0;
}

.source-status strong {
  justify-self: end;
  border-radius: 999px;
  padding: 5px 9px;
  background: #eef2f6;
  color: #42526b;
  font-size: 12px;
}

.source-status small {
  grid-column: 1 / -1;
  color: var(--muted);
}

.source-status.fresh {
  border-left-color: #28a36a;
}

.source-status.fresh strong {
  background: #e8f7ef;
  color: #167348;
}

.source-status.carried_forward {
  border-left-color: #c98922;
}

.source-status.carried_forward strong {
  background: #fff4dd;
  color: #87560a;
}

.source-status.missing {
  border-left-color: #c84c4c;
}

.source-status.missing strong {
  background: #ffecec;
  color: #9f2f2f;
}

.pipeline {
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.stage {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fbfcff;
  min-height: 90px;
}

.stage b, .stage span {
  display: block;
}

.stage span {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.wide-btn {
  width: 100%;
}

.kb-form {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) 170px 130px;
  gap: 10px;
  margin-bottom: 16px;
}

.kb-form textarea {
  grid-column: 1 / -1;
  min-height: 96px;
  resize: vertical;
}

input, textarea, select {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 11px;
  background: #fff;
  color: var(--ink);
}

.source-table {
  width: 100%;
  border-collapse: collapse;
}

.source-table th, .source-table td {
  text-align: left;
  border-bottom: 1px solid var(--line);
  padding: 12px 10px;
  vertical-align: top;
}

.source-table th {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  background: #152033;
  color: #fff;
  border-radius: 7px;
  padding: 12px 14px;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1100px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; }
  .metric-grid { grid-template-columns: repeat(2, minmax(150px, 1fr)); }
  .layout-grid { grid-template-columns: 1fr; }
  .pipeline { grid-template-columns: repeat(2, minmax(120px, 1fr)); }
  .hero-panel { grid-template-columns: 1fr; }
  .ai-brief { grid-template-columns: 1fr; }
  .account-growth-head { display: none; }
  .account-growth-row { grid-template-columns: minmax(180px, 1.2fr) repeat(2, minmax(96px, 1fr)); }
  .account-growth-row > div:not(.account-growth-account) { text-align: left; }
}

@media (max-width: 680px) {
  main { padding: 16px; }
  .topbar { align-items: stretch; flex-direction: column; }
  .top-actions { width: 100%; }
  .primary-btn { flex: 1; }
  .metric-grid { grid-template-columns: 1fr; }
  .kb-form { grid-template-columns: 1fr; }
  .hero-stats { grid-template-columns: 1fr; }
  .brief-stats { grid-template-columns: 1fr; }
  .account-growth-row { grid-template-columns: 1fr 1fr; }
  .account-growth-account { grid-column: 1 / -1; }
}
