[hidden] { display: none !important; }

:root {
  --bg: #0f172a;
  --bg-alt: #111c34;
  --surface: rgba(30, 41, 59, 0.7);
  --surface-solid: #1e293b;
  --border: rgba(148, 163, 184, 0.16);
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --text-faint: #64748b;
  --accent: #6366f1;
  --accent-soft: rgba(99, 102, 241, 0.16);
  --danger: #f97316;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --ease-out: cubic-bezier(0, 0, 0.2, 1);
  --ease-in: cubic-bezier(0.4, 0, 1, 1);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(circle at 15% 10%, rgba(99, 102, 241, 0.10), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(56, 189, 248, 0.08), transparent 45%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button, input, select { font-family: inherit; }

*:focus-visible {
  outline: 2px solid #818cf8;
  outline-offset: 2px;
}

.glass {
  background: var(--surface);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

button, .band-rows input, .kpi-card, .ranking-item, .btn, .icon-btn {
  transition: all 0.25s var(--ease);
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 22px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.btn-primary {
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  color: white;
  box-shadow: 0 8px 20px rgba(79, 70, 229, 0.35);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 12px 26px rgba(79, 70, 229, 0.45); }
.btn-ghost {
  background: rgba(148, 163, 184, 0.08);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: rgba(148, 163, 184, 0.14); color: var(--text); }
.small-btn { margin-top: 16px; align-self: flex-start; padding: 8px 16px; font-size: 0.85rem; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(148, 163, 184, 0.08);
  color: var(--text-dim);
  display: grid; place-items: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--accent-soft); color: #a5b4fc; transform: translateY(-1px); }
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; }

/* ---------- Toasts / Banner ---------- */
.toast-container {
  position: fixed; top: 20px; right: 20px; z-index: 100;
  display: flex; flex-direction: column; gap: 10px;
}
.toast {
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-left: 3px solid #fbbf24;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  max-width: 320px;
  animation: toastIn 0.3s var(--ease-out);
}
.toast.exit { animation: toastOut 0.25s var(--ease-in) forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateX(20px); } }
@keyframes toastOut { to { opacity: 0; transform: translateX(20px); } }

#fallbackBanner { display: none; }
#fallbackBanner.show { display: flex; }
.fallback-banner {
  position: sticky; top: 0; z-index: 90;
  display: flex; align-items: center; gap: 10px;
  background: rgba(249, 115, 22, 0.12);
  border-bottom: 1px solid rgba(249, 115, 22, 0.35);
  color: #fdba74;
  padding: 10px 20px;
  font-size: 0.85rem;
}
.banner-dismiss {
  margin-left: auto;
  background: none; border: none; color: inherit;
  cursor: pointer; font-size: 0.9rem; padding: 2px 6px;
  border-radius: 4px;
}
.banner-dismiss:hover { background: rgba(249,115,22,0.2); }

/* ---------- Phase 1: Setup ---------- */
.phase {
  position: fixed;
  inset: 0;
  z-index: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

#phase1 {
  z-index: 2;
  clip-path: inset(0 0 0 0); /* fully visible */
}
#phase1.exiting {
  clip-path: inset(0 100% 0 0); /* clipped: left edge at 100% = off-screen left */
  pointer-events: none;
  transition: clip-path 420ms var(--ease-out);
}
#phase1.hidden {
  display: none;
}

#phase2 {
  z-index: 1;
  clip-path: inset(0 0 0 100%); /* fully clipped: left edge at 100% = off-screen left */
  pointer-events: none;
}
#phase2.entering {
  z-index: 3;
  display: block;
  clip-path: inset(0 0 0 0); /* fully visible */
  pointer-events: auto;
  transition: clip-path 420ms var(--ease-out);
}
#phase2.visible {
  z-index: 3;
  display: block;
  clip-path: inset(0 0 0 0); /* fully visible */
  pointer-events: auto;
}
#phase2.exiting {
  clip-path: inset(0 0 0 100%);
  pointer-events: none;
  transition: clip-path 420ms var(--ease-out);
}
#phase2.hidden {
  display: none;
}

.setup-shell {
  width: calc(100% - 64px);
  max-width: 960px;
  margin: 32px auto;
  padding: 32px;
  box-sizing: border-box;
}
.setup-header { margin-bottom: 28px; }
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark { width: 36px; height: 36px; fill: var(--accent); flex-shrink: 0; }
.brand h1 { margin: 0; font-size: 1.6rem; letter-spacing: -0.02em; }
.subtitle { margin: 2px 0 0; color: var(--text-dim); font-size: 0.9rem; }

.setup-grid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 28px; align-items: start; }
.setup-col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }

.field-group { display: flex; flex-direction: column; gap: 6px; }
.field-group label { font-size: 0.82rem; font-weight: 600; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.field-group input[type="text"],
.field-group select {
  background: rgba(148, 163, 184, 0.06);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text);
  font-size: 0.92rem;
  outline: none;
}
.field-group input[type="text"]:focus,
.field-group select:focus { border-color: var(--accent); }
.band-rows { display: flex; flex-direction: column; gap: 8px; }
.band-row {
  display: grid;
  grid-template-columns: 44px 1fr 70px 44px;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 6px 8px;
  background: rgba(148, 163, 184, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
}
.band-row .band-color-cell { grid-column: 1; }
.band-row .band-name-cell { grid-column: 2; }
.band-row .band-pct-cell { grid-column: 3; }
.band-row .band-lock-cell { grid-column: 4; }
.band-row .band-slider-cell { grid-column: 1 / -1; }
.band-row input[type="color"] { width: 36px; height: 36px; padding: 2px; border-radius: var(--radius-sm); cursor: pointer; background: none; border: 1px solid var(--border); }
.band-row .band-name-input {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  background: rgba(148, 163, 184, 0.06);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  width: 100%;
  box-sizing: border-box;
}
.band-row .pct-wrap { display: flex; align-items: center; gap: 2px; }
.band-row .pct-wrap input {
  width: 56px; padding: 4px 6px; font-size: 0.82rem;
  background: rgba(148, 163, 184, 0.06); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); text-align: center;
}
.band-row .pct-wrap span { font-size: 0.78rem; color: var(--text-faint); }
.band-row .lock-btn { width: 36px; height: 36px; background: none; border: 1px solid var(--border); border-radius: var(--radius-sm); color: var(--text-faint); cursor: pointer; display: grid; place-items: center; padding: 0; }
.band-row .lock-btn:hover { background: rgba(148, 163, 184, 0.12); color: var(--text); }
.band-row .lock-btn svg { width: 18px; height: 18px; fill: currentColor; }
/* Lock state: aria-pressed=true means locked — show closed lock, hide open lock */
.band-row .lock-btn[aria-pressed="true"] .lock-open { display: none; }
.band-row .lock-btn[aria-pressed="true"] .lock-closed { display: block; }
/* Unlocked: show open lock, hide closed lock */
.band-row .lock-btn[aria-pressed="false"] .lock-open { display: block; }
.band-row .lock-btn[aria-pressed="false"] .lock-closed { display: none; }
.band-row .band-slider { width: 100%; accent-color: var(--accent); cursor: pointer; }
.band-row[disabled] { opacity: 0.5; pointer-events: none; }

.advanced-settings { border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.advanced-settings summary {
  padding: 12px 16px; font-size: 0.85rem; font-weight: 600; color: var(--text-dim);
  cursor: pointer; user-select: none; list-style: none; display: flex; align-items: center; gap: 8px;
}
.advanced-settings summary::-webkit-details-marker { display: none; }
.advanced-settings summary::before { content: '▶'; font-size: 0.7rem; transition: transform 0.2s; }
.advanced-settings[open] summary::before { transform: rotate(90deg); }
.advanced-body { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 14px; }
.slider-group label { display: flex; justify-content: space-between; color: var(--text-dim); }
.slider-group input[type="range"] { width: 100%; accent-color: var(--accent); }


/* ---------- CSV Source Row ---------- */
.csv-source-group { margin-bottom: 4px; }
.csv-source-row { display: flex; align-items: center; gap: 6px; }
.csv-url-input {
  flex: 1;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-raised);
  color: var(--text);
  font-size: 0.875rem;
  font-family: inherit;
  min-width: 0;
  height: 34px;
  box-sizing: border-box;
}
.csv-url-input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.csv-url-input.input-success { border-color: #22c55e; }
.csv-url-input.input-error { border-color: #ef4444; }
.csv-url-input::placeholder { color: var(--text-faint); }
.csv-url-btn, #csvFileBtn, #uploadBtn, #csvClearBtn { height: 34px; padding: 0 10px; box-sizing: border-box; flex-shrink: 0; }

.action-row {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  align-items: center;
}
.action-row-end {
  justify-content: flex-end;
}
.action-row .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.action-row .btn svg { display: block; }

.preview-col .preview-label { font-size: 0.82rem; font-weight: 600; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.chart-card {
  padding: 16px;
  display: flex;
}
.chart-card svg { width: 100%; height: 100%; }

/* ---------- Phase 2 Workspace ---------- */
.phase2 { padding: 24px; min-height: 100vh; }

/* ---- Mobile: swipe card deck ---- */
.workspace-deck {
  display: flex;
  width: 100%;
  overflow-x: scroll;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none; /* Firefox */
  gap: 16px;
  padding-bottom: 8px; /* space for dots */
}
.workspace-deck::-webkit-scrollbar { display: none; }

.workspace-card {
  scroll-snap-align: start;
  flex-shrink: 0;
  width: 100%;
  overflow-y: visible;
  /* Desktop: becomes a grid child, not a flex item */
}
@media (min-width: 901px) {
  .left-panel { align-self: start; }
}

/* Card dots (mobile only) */
.card-dots {
  display: none;
  justify-content: center;
  gap: 8px;
  padding: 10px 0 4px;
}
.card-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--border);
  transition: background 0.2s;
}
.card-dot.active { background: var(--accent); }

/* ---- Desktop: side-by-side panels ---- */
/* Desktop (≥901px): grid layout for workspace-deck */
@media (min-width: 901px) {
  .workspace-deck {
    display: grid;
    grid-template-columns: 0.9fr 1.1fr;
    gap: 24px;
    max-width: 1400px;
    margin: 0 auto;
    align-items: start;
  }
  .workspace-card {
    width: auto;
    flex-shrink: unset;
    overflow-y: visible;
  }
  .card-dots { display: none !important; }
}

/* ---- Shared panel styles ---- */
.panel { padding: 26px; display: flex; flex-direction: column; }
.panel-title { margin: 0 0 20px; font-size: 1.25rem; letter-spacing: -0.01em; }

.kpi-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 22px; }
.kpi-card {
  background: rgba(148, 163, 184, 0.06);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.kpi-label { font-size: 0.75rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.05em; }
.kpi-value { font-size: 1.8rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.legend { display: flex; flex-direction: column; gap: 8px; margin-bottom: 20px; }
.legend-row {
  display: flex; align-items: center; gap: 10px;
  font-size: 0.85rem;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: rgba(148, 163, 184, 0.04);
}
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.legend-name { flex: 1; color: var(--text-dim); }
.legend-target { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

.distribution-chart { padding: 14px; height: 248px; box-sizing: border-box; flex-shrink: 0; align-self: start; width: 100%; }
.distribution-chart svg { width: 100%; height: 220px; }

/* ---------- Right panel: ranking list ---------- */
.right-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.right-actions { display: flex; gap: 10px; }

.ranking-list {
  list-style: none;
  margin: 0; padding: 0;
  display: flex; flex-direction: column; gap: 8px;
  padding-right: 4px;
  touch-action: pan-y; /* allow vertical scroll without deck interference */
}
.ranking-list::-webkit-scrollbar { width: 6px; }
.ranking-list::-webkit-scrollbar-thumb { background: rgba(148,163,184,0.3); border-radius: 3px; }

.ranking-item {
  display: grid;
  grid-template-columns: 32px 4px 1fr auto auto;
  align-items: center;
  gap: 12px;
  background: rgba(148, 163, 184, 0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: grab;
  position: relative;
}
.ranking-item.dragging { opacity: 0.4; }
.ranking-item.drag-over { border-color: #818cf8; box-shadow: 0 0 0 2px rgba(129,140,248,0.3); }
.rank-num { font-weight: 700; color: var(--text-faint); font-variant-numeric: tabular-nums; text-align: center; }
.band-bar { width: 4px; height: 28px; border-radius: 2px; }
.item-name { font-size: 0.92rem; }
.band-chip {
  font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 3px 8px; border-radius: 999px; color: #0f172a;
}
.menu-trigger {
  background: none; border: none; color: var(--text-faint); cursor: pointer;
  width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
}
.menu-trigger:hover { background: rgba(148,163,184,0.15); color: var(--text); }
.menu-trigger svg { width: 16px; height: 16px; fill: currentColor; }

.rank-menu {
  position: fixed;
  z-index: 200;
  padding: 14px;
  width: 200px;
  display: flex; flex-direction: column; gap: 8px;
}
.rank-menu label { font-size: 0.75rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.rank-menu-hint { margin: 0; font-size: 0.72rem; color: var(--text-faint); }

.fab {
  margin-top: 16px;
  align-self: flex-end;
  box-shadow: 0 10px 28px rgba(79, 70, 229, 0.4);
}
.fab .icon { width: 18px; height: 18px; fill: currentColor; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  html, body {
    overflow-x: hidden;
    height: 100%;
  }
  body {
    background-attachment: fixed;
  }
  .setup-grid { grid-template-columns: 1fr; }
  .workspace-deck { display: flex; }
  .card-dots { display: flex; }
}

@media (max-width: 520px) {
  .setup-shell { width: calc(100% - 40px); margin: 32px auto; padding: 20px; }
  .brand h1 { font-size: 1.3rem; }
  .phase2 { padding: 16px; }
  .workspace-deck { gap: 12px; padding-bottom: 12px; }
  .panel { padding: 18px; }
  .kpi-row { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 1.4rem; }

  .fab { align-self: stretch; justify-content: center; }
}

@media (min-width: 901px) {
  .workspace-deck { display: grid; }
  .card-dots { display: none !important; }
}

/* Floating back pill */
.floating-back {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 100;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  transition: background 180ms, box-shadow 180ms;
}
.floating-back:hover {
  background: var(--surface-raised);
  box-shadow: 0 6px 20px rgba(0,0,0,0.35);
}
.floating-back.hidden { display: none; }
