/* UTM Tracker — dashboard styles. Palette follows a validated,
   colorblind-safe scheme with selected light and dark modes. */

:root {
  color-scheme: light;
  --plane: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f4f3f0;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --series-1: #2a78d6;
  --series-1-soft: rgba(42, 120, 214, 0.14);
  --good: #006300;
  --danger: #d03b3b;
  --danger-soft: rgba(208, 59, 59, 0.1);
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 4px 16px rgba(11, 11, 11, 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --plane: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --series-1: #3987e5;
    --series-1-soft: rgba(57, 135, 229, 0.2);
    --good: #0ca30c;
    --danger: #e66767;
    --danger-soft: rgba(230, 103, 103, 0.14);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --plane: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --series-1: #3987e5;
  --series-1-soft: rgba(57, 135, 229, 0.2);
  --good: #0ca30c;
  --danger: #e66767;
  --danger-soft: rgba(230, 103, 103, 0.14);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--plane);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 15px; margin: 0; }
h3 { font-size: 17px; margin: 0 0 6px; }
.muted { color: var(--muted); }

/* ------------------------------- top bar -------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 20px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; }
.brand-mark { font-size: 20px; }
.brand-name { letter-spacing: -0.01em; }
.tabs { display: flex; gap: 4px; margin-left: 8px; }
.tab {
  border: 0;
  background: transparent;
  color: var(--ink-2);
  font: inherit;
  font-weight: 550;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.tab:hover { background: var(--surface-2); color: var(--ink); }
.tab.is-active { background: var(--series-1-soft); color: var(--series-1); }
.icon-btn {
  margin-left: auto;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-2);
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 16px;
}
.icon-btn:hover { background: var(--surface-2); }

/* ------------------------------- layout --------------------------------- */
.layout { display: block; }
.view { display: none; }
.view.is-active { display: flex; }
#view-links { align-items: flex-start; }

.sidebar {
  width: 260px;
  flex: 0 0 260px;
  padding: 18px 14px;
  border-right: 1px solid var(--border);
  min-height: calc(100vh - 56px);
  background: var(--surface);
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.biz-list { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.biz-item {
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border: 1px solid transparent;
}
.biz-item:hover { background: var(--surface-2); }
.biz-item.is-active { background: var(--series-1-soft); border-color: color-mix(in srgb, var(--series-1) 30%, transparent); }
.biz-item .biz-name { font-weight: 600; }
.biz-item .biz-meta { font-size: 12px; color: var(--muted); }

.content { flex: 1; padding: 26px 30px; max-width: 1080px; min-width: 0; }
.content.wide { max-width: 1180px; margin: 0 auto; width: 100%; }

/* ------------------------------- panels --------------------------------- */
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.panel-actions, .panel-head .filters { display: flex; gap: 8px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }

/* ------------------------------- forms ---------------------------------- */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field > span { font-size: 12.5px; font-weight: 550; color: var(--ink-2); }
.field em { color: var(--muted); font-style: normal; font-weight: 400; }
input[type="text"], select {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--baseline);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  width: 100%;
  outline: none;
}
input[type="text"]:focus, select:focus { border-color: var(--series-1); box-shadow: 0 0 0 3px var(--series-1-soft); }
input::placeholder { color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 16px; }
.form-actions { margin-top: 4px; }
.link-form .field { margin-bottom: 14px; }

.inline-form { display: flex; flex-direction: column; gap: 8px; padding: 10px; background: var(--surface-2); border-radius: var(--radius-sm); margin-bottom: 10px; }
.inline-actions { display: flex; gap: 6px; }
.row-form { display: flex; gap: 6px; }
.row-form input { width: 200px; }
.check { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ink-2); }
.check input { width: auto; }
.filter-inline { display: flex; align-items: center; gap: 8px; }
.filter-inline select { width: auto; }
.filters select { width: auto; }

/* ------------------------------- buttons -------------------------------- */
.btn {
  font: inherit;
  font-weight: 550;
  border: 1px solid var(--baseline);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); background: var(--series-1); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-danger:hover { background: var(--danger-soft); }
.link-btn { border: 0; background: none; color: var(--series-1); cursor: pointer; font: inherit; padding: 2px 4px; }
.link-btn:hover { text-decoration: underline; }

/* ------------------------------- chips ---------------------------------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
}
.chip .chip-count { color: var(--muted); font-size: 12px; }
.chip .chip-x {
  border: 0; background: none; color: var(--muted); cursor: pointer;
  width: 20px; height: 20px; border-radius: 50%; font-size: 14px; line-height: 1;
}
.chip .chip-x:hover { background: var(--danger-soft); color: var(--danger); }
.chips-empty { color: var(--muted); font-size: 13px; }

/* --------------------------- url preview -------------------------------- */
.preview { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; margin-bottom: 14px; }
.preview-label { display: block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 4px; }
.url-preview { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: var(--ink-2); word-break: break-all; }

/* ------------------------------- tables --------------------------------- */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left; font-weight: 600; color: var(--muted);
  font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.03em;
  padding: 8px 10px; border-bottom: 1px solid var(--border);
}
.data-table td { padding: 11px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .link-title { font-weight: 600; }
.data-table .link-dest { color: var(--muted); font-size: 12px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.short-cell { display: flex; align-items: center; gap: 6px; }
.short-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: var(--series-1); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--series-1-soft); color: var(--series-1); font-size: 12px; font-weight: 550; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.empty-inline { color: var(--muted); padding: 18px 4px; text-align: center; font-size: 13px; }

/* ------------------------------- empty ---------------------------------- */
.empty-state { text-align: center; padding: 80px 20px; color: var(--ink-2); }
.empty-emoji { font-size: 44px; margin-bottom: 10px; }
.empty-state p { max-width: 380px; margin: 0 auto; color: var(--muted); }

/* -------------------------------- KPIs ---------------------------------- */
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.kpi-value { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.kpi-label { color: var(--muted); font-size: 12.5px; margin-top: 3px; }

/* ------------------------- timeseries (bars) ---------------------------- */
.timeseries { display: flex; align-items: flex-end; gap: 2px; height: 160px; padding-top: 8px; overflow-x: auto; }
.ts-bar { flex: 1 1 0; min-width: 2px; background: var(--series-1); border-radius: 3px 3px 0 0; align-self: flex-end; min-height: 2px; transition: filter 0.1s; cursor: default; }
.ts-bar:hover { filter: brightness(1.15); }
.ts-bar.is-zero { background: var(--grid); }

/* --------------------------- horizontal bars ---------------------------- */
.bar-list { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 120px 1fr auto; align-items: center; gap: 10px; }
.bar-label { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-label small { color: var(--muted); }
.bar-track { background: var(--surface-2); border-radius: 5px; height: 20px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--series-1); border-radius: 5px; min-width: 3px; }
.bar-value { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 13px; min-width: 28px; text-align: right; }

/* ------------------------------- toast ---------------------------------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--plane);
  padding: 10px 18px; border-radius: 999px; font-size: 13px; font-weight: 550;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); z-index: 50;
}
.toast.is-error { background: var(--danger); color: #fff; }

.chart-tip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--ink); color: var(--plane);
  padding: 5px 9px; border-radius: 6px; font-size: 12px; font-weight: 550;
  transform: translate(-50%, -130%); white-space: nowrap;
}

/* ----------------------------- responsive ------------------------------- */
@media (max-width: 860px) {
  #view-links { flex-direction: column; }
  .sidebar { width: 100%; flex-basis: auto; min-height: 0; border-right: 0; border-bottom: 1px solid var(--border); }
  .content { padding: 20px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .kpi-row { grid-template-columns: 1fr 1fr; }
  .bar-row { grid-template-columns: 100px 1fr auto; }
}
