:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --surface-strong: #f1f5f9;
  --border: #dbe3ef;
  --border-strong: #b7c4d9;
  --text: #172033;
  --muted: #66758f;
  --soft: #eef3f9;
  --primary: #2456d6;
  --primary-dark: #1742a6;
  --primary-soft: #e8eefc;
  --success: #18794e;
  --warning: #9a5b00;
  --danger: #b42318;
  --key-accent: #7c2d12;
  --key-accent-soft: #fff4df;
  --key-accent-border: #ffd79a;
  --shadow: 0 20px 60px rgba(23, 32, 51, 0.12);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --font: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --topbar-height: 70px;
  --filters-height: 142px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }

.app-shell {
  min-height: 100vh;
  height: 100vh;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 10px;
  padding: 12px;
}

.topbar {
  min-height: var(--topbar-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  padding: 12px 16px;
  box-shadow: 0 6px 20px rgba(23, 32, 51, 0.05);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.11em;
  font-size: 10px;
  font-weight: 800;
}

h1, h2, h3 { margin: 0; }
h1 { font-size: clamp(20px, 2.4vw, 28px); }
h2 { font-size: 18px; }
h3 { font-size: 16px; margin-bottom: 12px; }

.session-card {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  border-radius: var(--radius-md);
  padding: 9px 10px;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 4px rgba(24, 121, 78, 0.13);
}

.session-card__title { font-weight: 750; font-size: 13px; }
.session-card__caption { color: var(--muted); font-size: 12px; }

.auth-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: 0 6px 20px rgba(23, 32, 51, 0.05);
}

.auth-form {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(180px, 0.8fr) auto minmax(160px, 0.8fr);
  gap: 14px;
  align-items: end;
}

.auth-actions {
  padding-top: 0;
  align-self: end;
}

.auth-status {
  margin: 0;
  padding: 10px 12px;
  text-align: left;
  align-self: end;
}

.filters-panel {
  display: grid;
  grid-template-columns: 150px minmax(220px, 1fr) minmax(220px, 1fr) auto;
  gap: 14px;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: 0 6px 20px rgba(23, 32, 51, 0.05);
}

.filter-group { display: grid; gap: 6px; min-width: 0; align-content: start; height: 100%; }
.period-filter-stack { display: grid; gap: 8px; align-content: start; height: 100%; }
.filters-panel > .filter-group,
.filters-panel > .period-filter-stack,
.filters-panel > .filter-actions { min-height: 118px; }
.filter-group label { font-weight: 750; color: var(--text); font-size: 12px; }

input, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  min-height: 36px;
  padding: 7px 10px;
  color: var(--text);
}

select[multiple] { min-height: 88px; padding-top: 6px; padding-bottom: 6px; }
.field-hint { color: var(--muted); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.filter-actions { display: flex; gap: 10px; align-self: stretch; align-items: start; padding-top: 18px; }

.searchable-multiselect {
  min-height: 118px;
  display: grid;
  grid-template-rows: auto minmax(48px, 1fr) auto auto;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  padding: 7px;
}

.searchable-multiselect__search {
  min-height: 32px;
  border-radius: 8px;
  background: var(--surface-muted);
}

.searchable-multiselect__options {
  min-height: 48px;
  max-height: 66px;
  overflow: auto;
  display: grid;
  gap: 4px;
  padding-right: 2px;
}

.searchable-multiselect__option {
  min-height: 26px;
  display: flex;
  align-items: center;
  gap: 7px;
  border-radius: 7px;
  padding: 3px 5px;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}

.searchable-multiselect__option:hover {
  background: var(--surface-muted);
}

.searchable-multiselect__option-input {
  width: 14px;
  height: 14px;
  min-height: 14px;
  flex: 0 0 auto;
  padding: 0;
}

.searchable-multiselect__option-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.searchable-multiselect__empty {
  color: var(--muted);
  font-size: 11px;
  padding: 2px 5px;
}

.searchable-multiselect__selected {
  min-height: 15px;
  color: var(--muted);
  font-size: 10.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.primary-button, .secondary-button, .ghost-button, .icon-button {
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 9px 12px;
  font-weight: 750;
}

.primary-button { background: var(--primary); color: #fff; }
.primary-button:hover { background: var(--primary-dark); }
.secondary-button { background: var(--soft); color: var(--text); border-color: var(--border); }
.ghost-button { background: transparent; color: var(--muted); border-color: var(--border); padding: 7px 9px; }
.icon-button {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: var(--soft);
  font-size: 24px;
  line-height: 1;
}

.funnel-card {
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px;
  box-shadow: 0 8px 30px rgba(23, 32, 51, 0.06);
}

.funnel-card__header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 8px;
}

.funnel-help {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  color: var(--muted);
  font-size: 11px;
}

.funnel-help span {
  border: 1px solid var(--border);
  background: var(--surface-muted);
  border-radius: 999px;
  padding: 4px 7px;
}

.state {
  border-radius: var(--radius-md);
  background: var(--surface-muted);
  border: 1px dashed var(--border-strong);
  padding: 18px;
  text-align: center;
  color: var(--muted);
  margin-bottom: 10px;
}

.state--hidden { display: none; }

.funnel-scroll {
  min-height: 0;
  overflow: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
}

.funnel-table {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
  font-size: 11.5px;
}

.funnel-table th,
.funnel-table td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 0;
  vertical-align: middle;
  background: var(--surface);
}

.funnel-table thead th {
  position: sticky;
  z-index: 2;
  background: var(--surface-strong);
  font-weight: 800;
}

.funnel-table thead tr:first-child th { top: 0; height: 36px; }
.funnel-table thead tr:nth-child(2) th { top: 36px; height: 30px; }
.funnel-table thead tr:nth-child(3) th { top: 66px; height: 32px; }

.position-head,
.position-data-cell {
  position: sticky;
  left: 0;
  z-index: 4;
  min-width: 210px;
  max-width: 240px;
  box-shadow: 1px 0 0 var(--border);
}

.funnel-table thead .position-head { z-index: 6; padding: 6px 8px; }
.position-data-cell { background: var(--surface) !important; padding: 6px 8px !important; }
.row-selected .position-data-cell { background: #f3f7ff !important; }

.position-cell { display: grid; gap: 3px; }
.position-cell strong { font-size: 12px; }
.position-cell span { color: var(--muted); font-size: 11px; }

.stage-head { min-width: 68px; color: #111827; }
.stage-head__inner {
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 4px 6px;
}
.stage-head__title { font-size: 13px; font-weight: 850; white-space: nowrap; }
.stage-head__actions { display: inline-flex; gap: 3px; align-items: center; }
.stage-action-button {
  min-width: 22px;
  height: 22px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  font-weight: 900;
  padding: 0 5px;
}
.stage-action-button--text {
  width: auto;
  font-size: 10px;
  font-weight: 750;
  color: var(--primary-dark);
}

.stage-collapsed-head {
  min-width: 34px;
  width: 34px;
  max-width: 34px;
  background: repeating-linear-gradient(
    90deg,
    #f8fafc,
    #f8fafc 8px,
    #e7edf7 8px,
    #e7edf7 10px
  ) !important;
}
.stage-collapsed-button {
  width: 100%;
  height: 98px;
  display: grid;
  grid-template-rows: 24px 1fr;
  place-items: center;
  gap: 4px;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 4px 2px;
}
.stage-collapsed-button__icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: #3f4646;
  color: #fff;
  font-size: 18px;
  line-height: 1;
}
.stage-collapsed-button__name {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 10px;
  color: var(--muted);
  line-height: 1.1;
  white-space: nowrap;
}
.stage-collapsed-cell {
  width: 34px;
  background: repeating-linear-gradient(
    90deg,
    #ffffff,
    #ffffff 8px,
    #edf2f8 8px,
    #edf2f8 10px
  ) !important;
  text-align: center;
}
.stage-collapsed-cell__button {
  width: 100%;
  min-height: 38px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 900;
}

.metric-head {
  min-width: clamp(40px, 3.6vw, 46px);
  width: clamp(40px, 3.6vw, 46px);
  text-align: center;
  color: #111827;
  font-size: 16px;
  font-weight: 500 !important;
  padding: 3px !important;
}
.metric-total {
  min-width: clamp(40px, 3.6vw, 46px);
  width: clamp(40px, 3.6vw, 46px);
  text-align: center;
  color: #111827;
  background: #ecfdf3 !important;
  font-size: 16px;
  font-weight: 500 !important;
  padding: 3px !important;
}
.metric-cell { min-width: clamp(40px, 3.6vw, 46px); width: clamp(40px, 3.6vw, 46px); padding: 2px !important; }

.metric-button {
  width: 100%;
  min-height: 28px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-size: 15px;
  font-weight: 500;
}
.metric-button:hover { border-color: #a9bdf5; background: #dbe6ff; }
.metric--key {
  color: var(--key-accent) !important;
  background: var(--key-accent-soft) !important;
  font-weight: 850 !important;
}
.metric-button--key {
  color: var(--key-accent);
  background: var(--key-accent-soft);
  border-color: var(--key-accent-border);
  font-weight: 850;
}
.metric-button--key:hover { background: #ffe9bd; border-color: #ffc76f; }
.metric-button--zero { background: var(--surface-muted); color: var(--muted); }
.metric-button--zero.metric-button--key {
  color: var(--key-accent);
  background: var(--key-accent-soft);
}

.has-tooltip {
  position: relative;
}

.has-tooltip:hover,
.has-tooltip:focus-visible {
  z-index: 90;
}

.has-tooltip::after,
.has-tooltip::before {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition-property: opacity, transform, visibility;
  transition-duration: 120ms, 120ms, 0s;
  transition-timing-function: ease, ease, linear;
  transition-delay: 0s, 0s, 120ms;
}

.has-tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  top: calc(100% + 9px);
  transform: translate(-50%, -2px);
  width: max-content;
  max-width: min(300px, 72vw);
  z-index: 80;
  border-radius: 10px;
  padding: 9px 11px;
  background: #111827;
  color: #ffffff;
  box-shadow: 0 12px 30px rgba(17, 24, 39, 0.22);
  font-size: 13.5px;
  font-weight: 650;
  line-height: 1.35;
  text-align: left;
  white-space: normal;
}

.has-tooltip::before {
  content: "";
  position: absolute;
  left: 50%;
  top: calc(100% + 3px);
  transform: translate(-50%, -2px);
  z-index: 81;
  border: 6px solid transparent;
  border-bottom-color: #111827;
}

.has-tooltip:hover::after,
.has-tooltip:hover::before,
.has-tooltip:focus-visible::after,
.has-tooltip:focus-visible::before {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
  transition-delay: 500ms, 500ms, 500ms;
}

.row-selected td:not(.position-data-cell) { background: #fbfdff; }

.drawer { position: fixed; inset: 0; display: none; z-index: 30; }
.drawer[aria-hidden="false"] { display: block; }
.drawer__overlay { position: absolute; inset: 0; background: rgba(16, 24, 39, 0.44); }
.drawer__panel {
  position: absolute;
  right: 0;
  top: 0;
  height: 100%;
  width: min(900px, 94vw);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 22px;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 14px;
  overflow: auto;
}
.drawer__header { display: flex; justify-content: space-between; align-items: start; gap: 16px; }
.drawer__meta { display: flex; flex-wrap: wrap; gap: 8px; }
.meta-chip {
  border: 1px solid var(--border);
  background: var(--surface-muted);
  border-radius: 999px;
  padding: 7px 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.drawer__body { display: grid; grid-template-columns: minmax(360px, 1fr) minmax(300px, 0.85fr); gap: 18px; min-height: 0; }
.drilldown-list { display: grid; gap: 10px; }
.drilldown-card {
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-muted);
  padding: 14px;
}
.drilldown-card:hover,
.drilldown-card--active { border-color: #9bb3f7; background: #f4f7ff; }
.drilldown-card__head { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.drilldown-card__name { font-weight: 850; }
.drilldown-card__date { color: var(--muted); font-size: 12px; }
.drilldown-card__meta { color: var(--muted); font-size: 12px; display: grid; gap: 3px; }
.timeline-panel { border-left: 1px solid var(--border); padding-left: 18px; }
.timeline-empty { color: var(--muted); border: 1px dashed var(--border-strong); border-radius: var(--radius-md); padding: 18px; }
.timeline { display: grid; gap: 12px; }
.timeline-item {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 12px 12px 38px;
  background: var(--surface-muted);
}
.timeline-item::before {
  content: "";
  position: absolute;
  left: 14px;
  top: 16px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
}
.timeline-item--corrected::before { background: var(--warning); }
.timeline-item__stage { font-weight: 850; margin-bottom: 4px; }
.timeline-item__status { color: var(--text); margin-bottom: 6px; }
.timeline-item__time { color: var(--muted); font-size: 12px; display: grid; gap: 2px; }
.corrected-badge {
  display: inline-flex;
  width: max-content;
  margin-top: 8px;
  background: #fff4e0;
  color: #7a4d00;
  border: 1px solid #ffd68a;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 800;
}

@media (max-width: 1180px) {
  .app-shell { height: auto; min-height: 100vh; }
  .topbar, .funnel-card__header { flex-direction: column; align-items: stretch; }
  .auth-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filter-actions { align-self: stretch; }
  .drawer__body { grid-template-columns: 1fr; }
  .timeline-panel { border-left: 0; padding-left: 0; }
}

@media (max-width: 720px) {
  .app-shell { padding: 8px; }
  .auth-form { grid-template-columns: 1fr; }
  .filters-panel { grid-template-columns: 1fr; }
  .filter-actions, .funnel-help { flex-direction: column; align-items: stretch; }
  .session-card { align-items: flex-start; flex-wrap: wrap; }
  .metric-head, .metric-total, .metric-cell { min-width: 42px; width: 42px; }
}
