:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --border: #e2e5ea;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --high: #2b8a3e;
  --medium: #b7791f;
  --low: #868e96;
  --error: #c92a2a;
  --chip: #eef1f6;
  /* Chart series colour (validated with the dataviz palette checker against --card in each mode). */
  --series: #3b5bdb;
  --grid: #eceef2;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --card: #1a1d23;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2c3038;
    --accent: #6c8cff;
    --accent-text: #0b0d12;
    --high: #51cf66;
    --medium: #fcc419;
    --low: #868e96;
    --error: #ff6b6b;
    --chip: #252a33;
    --series: #5b7cf0;
    --grid: #262a31;
  }
}

* { box-sizing: border-box; }
/* `hidden` must win over component display rules like `.app { display: flex }`. */
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.wrap { max-width: 1180px; margin: 0 auto; padding: 32px 24px 64px; }

/* App shell: side panel + content */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex: none; position: sticky; top: 0; height: 100vh;
  background: var(--card); border-right: 1px solid var(--border);
  padding: 20px 12px; display: flex; flex-direction: column; gap: 16px;
}
.brand { font-weight: 700; font-size: 16px; padding: 0 10px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav-item span:not(.nav-badge) { white-space: nowrap; }
.nav-item:hover { background: var(--chip); color: var(--text); }
.nav-item.active { background: var(--chip); color: var(--accent); font-weight: 600; }
.nav-item:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.nav-badge {
  margin-left: auto; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px;
  background: var(--bg); color: var(--muted); border: 1px solid var(--border);
}
.content { flex: 1; min-width: 0; }
.account { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 10px; border-top: 1px solid var(--border); }
.account .muted { overflow-wrap: anywhere; }
.account button { align-self: flex-start; }

.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; margin: 0; padding: 28px; }
.login-card h1 { font-size: 22px; margin: 0; }
.login-card p { margin: -8px 0 4px; }
.login-card input { width: 100%; }
.login-card .status { margin: 0; }
.mobile-bar { display: none; }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 20; }

/* Keyword Analysis page */
.subtabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.subtab {
  background: transparent; border: none; border-bottom: 2px solid transparent; border-radius: 0;
  padding: 10px 14px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.subtab.active { color: var(--accent); border-bottom-color: var(--accent); }
.subtab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.form-card { display: flex; flex-direction: column; gap: 12px; margin-top: 0; }
.form-card textarea {
  width: 100%; font: inherit; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); resize: vertical;
}
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > input[type="url"], .row > select:first-child { flex: 1 1 260px; }
.grow { flex: 1; min-width: 120px; }
.seg { display: flex; gap: 16px; flex-wrap: wrap; }
.seg label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-weight: 600; }
.file-btn {
  display: inline-block; margin-top: 8px; padding: 6px 12px; border-radius: 8px; border: 1px solid var(--border);
  cursor: pointer; font-size: 13px;
}
.file-btn input { display: none; }
.small { font-size: 12px; }
.small-note { font-size: 12px; margin: 6px 0 0; }
.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.num-input { width: 90px; padding: 4px 8px; margin-left: 6px; }

.choice-list { display: flex; flex-wrap: wrap; gap: 8px; }
.choice {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 10px; border: 1px solid var(--border);
  border-radius: 8px; cursor: pointer; max-width: 100%;
}
.choice img { width: 20px; height: 20px; border-radius: 5px; }
.choice span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.choice:has(input:disabled) { opacity: .5; cursor: not-allowed; }
.choice-list:has(.suggestion) { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.choice.suggestion { align-items: flex-start; }
.choice.suggestion img, .icon-ph { width: 32px; height: 32px; border-radius: 8px; flex: none; }
.icon-ph { background: var(--chip); }
.suggestion-text { display: flex; flex-direction: column; min-width: 0; white-space: normal !important; }
.suggestion-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.src { display: inline-block; font-size: 11px; font-weight: 600; padding: 0 6px; border-radius: 999px; margin-right: 4px; background: var(--chip); color: var(--text); }
.src-ai { color: var(--accent); }

.result-head h2 { margin: 0; }
table.data th.sortable { cursor: pointer; user-select: none; }
table.data th.sortable:hover { color: var(--text); }
.insights { margin: 4px 0 14px; padding-left: 18px; }
.insights li { margin: 2px 0; }
.group-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.kw-group { border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.kw-group p { margin: 6px 0 8px; }
.kw-group-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin: 14px 0 6px; }
.stat { display: flex; flex-direction: column; gap: 2px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; }
.stat strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.chart-title { margin: 16px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }

.trend { position: relative; }
.trend-svg { width: 100%; height: auto; display: block; touch-action: pan-y; }
.trend-svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.trend-svg .grid { stroke: var(--grid); stroke-width: 1; }
.trend-svg .axis { fill: var(--muted); font-size: 11px; }
.trend-svg .crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-tip {
  position: absolute; transform: translate(-50%, calc(-100% - 10px)); pointer-events: none;
  background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
  box-shadow: 0 4px 14px rgb(0 0 0 / .12); display: flex; flex-direction: column; align-items: center; white-space: nowrap;
}
.chart-tip strong { font-size: 14px; font-variant-numeric: tabular-nums; }
.chart-tip span { font-size: 12px; color: var(--muted); }
.table-view { margin-top: 8px; font-size: 13px; }
.table-view summary { cursor: pointer; color: var(--muted); }
.table-view table { max-width: 320px; margin-top: 6px; }
.spark { display: block; }

.store-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.store-grid .card { margin-top: 16px; }
.store-list { list-style: none; margin: 0; padding: 0; }
.store-list li { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-top: 1px solid var(--border); }
.store-list li:first-child { border-top: none; }
.store-list li.yours { background: var(--chip); border-radius: 8px; padding-left: 6px; padding-right: 6px; }
.store-list .rank { width: 22px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.store-list img { width: 32px; height: 32px; border-radius: 8px; flex: none; }
.store-app { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.store-app strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.yours-chip { color: var(--accent); font-weight: 600; }
.gap-tabs { margin-top: 16px; }
.gap-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; margin-top: 10px; }
.gap-filters label { display: inline-flex; align-items: center; gap: 6px; }
.brand-filter { flex: 1 1 320px; }
.brand-filter input[type="text"] { flex: 1; min-width: 160px; padding: 4px 8px; font-size: 13px; }

@media (max-width: 800px) {
  .sidebar {
    position: fixed; left: 0; top: 0; z-index: 30; transform: translateX(-100%);
    transition: transform .2s ease;
  }
  .sidebar.open { transform: none; box-shadow: 0 0 24px rgb(0 0 0 / .2); }
  .mobile-bar {
    display: flex; align-items: center; gap: 8px; padding: 10px 12px;
    border-bottom: 1px solid var(--border); background: var(--card); position: sticky; top: 0; z-index: 10;
  }
  .mobile-title { font-weight: 600; }
  .icon-btn { padding: 6px; display: inline-flex; }
  .wrap { padding: 20px 16px 48px; }
}
h1 { margin: 0 0 4px; font-size: 26px; }
h2 { margin: 0 0 12px; font-size: 17px; }
h3 { margin: 0; font-size: 15px; }
.muted { color: var(--muted); }
.top { margin-bottom: 20px; }

.search { display: flex; gap: 8px; flex-wrap: wrap; }
.search input { flex: 1 1 420px; }
input, select, button {
  font: inherit;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}
button { cursor: pointer; }
#go, .primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .6; cursor: progress; }
.ghost { background: transparent; }
.small { padding: 4px 10px; font-size: 12px; }

.status { margin: 16px 0; padding: 12px 14px; border-radius: 8px; background: var(--card); border: 1px solid var(--border); }
.status.error { border-color: var(--error); color: var(--error); }
.spinner {
  display: inline-block; width: 12px; height: 12px; margin-right: 8px; vertical-align: -1px;
  border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 18px; margin-top: 16px; }
.app { display: flex; gap: 16px; align-items: flex-start; }
.app img { width: 72px; height: 72px; border-radius: 16px; flex: none; }
.app .meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { display: inline-block; background: var(--chip); border-radius: 999px; padding: 2px 10px; font-size: 12px; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px 24px; }
.grid h4 { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.grid ul { margin: 0; padding-left: 18px; }

.tabs { display: flex; gap: 6px; margin-top: 24px; align-items: center; }
.tab { background: transparent; border-color: transparent; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--card); border-color: var(--border); color: var(--text); }
.spacer { flex: 1; }

.notes { margin-top: 12px; }
.group { margin-top: 16px; }
.group-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.group-head .muted { flex: 1; min-width: 200px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 7px 8px; border-top: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; border-top: none; white-space: nowrap; }
td.kw { font-weight: 600; white-space: nowrap; }
td.num { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
td.why { color: var(--muted); min-width: 260px; }
.prio { font-weight: 600; font-size: 12px; text-transform: capitalize; }
.prio.high { color: var(--high); }
.prio.medium { color: var(--medium); }
.prio.low { color: var(--low); }
.negatives { display: flex; flex-wrap: wrap; gap: 6px; }
th.num { text-align: right; cursor: help; }
td.num { white-space: nowrap; }
.metrics-info { margin-top: 24px; }
.metrics-info .group-head { margin-bottom: 0; }
.notes-list { margin: 10px 0 0; padding-left: 18px; font-size: 13px; }
.notes-list.errors { color: var(--error); }
.metrics-info h3 .source { font-weight: 400; font-size: 13px; margin-left: 6px; }
.metric-stage { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.metric-stage strong { min-width: 90px; }
.metric-stage .muted { flex: 1; min-width: 200px; }
.fetched { margin-top: 8px; font-size: 13px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.history { margin-top: 16px; }
.history h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.history-list { display: flex; gap: 8px; flex-wrap: wrap; }
.history-item { display: flex; align-items: center; gap: 10px; text-align: left; padding: 8px 12px; max-width: 100%; }
.history-item img { width: 32px; height: 32px; border-radius: 8px; flex: none; }
.history-item span { display: flex; flex-direction: column; min-width: 0; }
.history-item strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-item .muted { font-size: 12px; }

@media (max-width: 600px) {
  .app { flex-direction: column; }
  .search select, .search button { flex: 1 1 auto; }
}
