:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3347;
  --accent: #4f7ef7;
  --accent2: #34d399;
  --accent3: #f59e0b;
  --text: #e2e8f0;
  --muted: #8892a4;
  --danger: #f87171;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* ── Layout ── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px 28px;
  display: flex;
  align-items: center;
  gap: 14px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.app-header .subtitle {
  color: var(--muted);
  font-size: 13px;
  margin-left: auto;
}

.logo-badge {
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 6px;
  letter-spacing: 0.5px;
}

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 28px 24px 60px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px 24px;
}

.card-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  margin-bottom: 16px;
}

/* ── KPI Row ── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 16px;
}

@media (max-width: 1100px) { .kpi-grid { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 650px)  { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 20px 18px;
}

.kpi-card .label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--muted);
  margin-bottom: 10px;
}

.kpi-card .value {
  font-size: 32px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -1px;
  color: var(--text);
}

.kpi-card .value.accent  { color: var(--accent); }
.kpi-card .value.green   { color: var(--accent2); }
.kpi-card .value.amber   { color: var(--accent3); }

.kpi-card .sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 6px;
}

.types-back-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--accent);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.types-back-btn:hover { background: rgba(79,126,247,0.1); }

.type-row { cursor: pointer; }
.type-row:hover td { background: rgba(79,126,247,0.07); }

.kpi-clickable {
  cursor: pointer;
  border-color: var(--accent);
  transition: background 0.15s, border-color 0.15s;
}
.kpi-clickable:hover { background: rgba(79,126,247,0.08); }

.modal-wide { max-width: 820px; }

/* ── Chart Grids ── */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
}

@media (max-width: 1000px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

.chart-wrap {
  position: relative;
  height: 280px;
}

.chart-wrap.tall {
  height: 340px;
}

/* ── Tables ── */
.table-wrap {
  overflow-x: auto;
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead th[data-col] {
  cursor: pointer;
  user-select: none;
}

thead th[data-col]:hover { color: var(--text); }

thead th[data-col]::after {
  content: ' ↕';
  opacity: 0.3;
  font-size: 10px;
}

thead th[data-col].sort-asc::after  { content: ' ↑'; opacity: 1; color: var(--accent); }
thead th[data-col].sort-desc::after { content: ' ↓'; opacity: 1; color: var(--accent); }

thead th {
  background: var(--surface2);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  padding: 10px 14px;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

tbody tr:last-child { border-bottom: none; }

tbody tr:hover { background: var(--surface2); }

tbody td {
  padding: 10px 14px;
  color: var(--text);
  white-space: nowrap;
}

.tag {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--surface2);
  color: var(--muted);
  letter-spacing: 0.3px;
}

.discount-pill {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 20px;
  background: rgba(52, 211, 153, 0.15);
  color: var(--accent2);
}

.price-strike {
  color: var(--muted);
  margin-right: 4px;
}

.price-sale {
  font-weight: 600;
  color: var(--accent2);
}

/* ── Section dividers ── */
.section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

/* ── Card title row (title + search side by side) ── */
.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 0;
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

.search-field {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 13px;
  padding: 7px 30px 7px 12px;
  width: 260px;
  outline: none;
  transition: border-color 0.15s;
}

.search-input::placeholder { color: var(--muted); }
.search-input:focus { border-color: var(--accent); }

.search-clear {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  transition: color 0.15s;
}
.search-clear:hover { color: var(--text); }
.search-clear[hidden] { display: none; }

.search-count {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

/* ── Catalog Explorer ── */
.year-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.year-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  padding: 7px 12px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 72px;
}

.year-btn:hover { border-color: var(--accent); color: var(--text); }

.year-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.year-btn .yr-count {
  font-size: 10px;
  font-weight: 400;
  opacity: 0.75;
}

.year-btn.active .yr-count { opacity: 0.85; }

.catalog-stats {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 16px 0 0;
  flex-wrap: wrap;
}

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

.catalog-stat strong { color: var(--text); }

.catalog-filter-tabs {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.filter-tab {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 12px;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.filter-tab:hover { color: var(--text); border-color: var(--accent); }

.filter-tab.active {
  background: var(--surface2);
  border-color: var(--accent);
  color: var(--accent);
}

/* ── SKU Modal ── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  z-index: 500;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
}

.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 860px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 24px 28px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-sku {
  font-family: monospace;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
  letter-spacing: 0.5px;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
}

.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 24px;
  cursor: pointer;
  line-height: 1;
  padding: 0 0 0 16px;
  flex-shrink: 0;
  transition: color 0.15s;
}
.modal-close:hover { color: var(--text); }

.modal-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
}

.pill {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.pill.green  { border-color: var(--accent2); color: var(--accent2); background: rgba(52,211,153,0.1); }
.pill.amber  { border-color: var(--accent3); color: var(--accent3); background: rgba(245,158,11,0.1); }
.pill.blue   { border-color: var(--accent);  color: var(--accent);  background: rgba(79,126,247,0.1); }
.pill.red    { border-color: var(--danger);  color: var(--danger);  background: rgba(248,113,113,0.1); }

.modal-body {
  padding: 0 28px 28px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.modal-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  padding-top: 20px;
}

@media (max-width: 600px) { .modal-stats { grid-template-columns: repeat(2, 1fr); } }

.modal-stat {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}

.modal-stat .s-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--muted);
  margin-bottom: 6px;
}

.modal-stat .s-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.5px;
}

.modal-stat .s-value.green { color: var(--accent2); }
.modal-stat .s-value.amber { color: var(--accent3); }

.modal-stat-image {
  grid-column: 4;
  grid-row: 1 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}
.modal-stat-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 6px;
}

.modal-section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

.modal-chart-wrap {
  position: relative;
  height: 220px;
}

.recon-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

@media (max-width: 500px) { .recon-grid { grid-template-columns: 1fr; } }

.recon-row {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.recon-row .r-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
}

.recon-row .r-value {
  font-size: 13px;
  color: var(--text);
  word-break: break-word;
}

.recon-row .r-value a {
  color: var(--accent);
  text-decoration: none;
  word-break: break-all;
}
.recon-row .r-value a:hover { text-decoration: underline; }

.type-link {
  cursor: pointer;
}
.type-link:hover {
  opacity: 0.8;
}

.help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--border);
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  cursor: default;
  position: relative;
  vertical-align: middle;
  margin-left: 2px;
}
.help-icon::after {
  content: attr(data-tip);
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 400;
  color: var(--text);
  white-space: nowrap;
  z-index: 200;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.help-icon:hover::after {
  display: block;
}

.sku-link {
  cursor: pointer;
  color: var(--accent);
  text-decoration: none;
  font-family: monospace;
}
.sku-link:hover { text-decoration: underline; }

#products-grouped,
#catalog-explorer,
#discount-distribution {
  scroll-margin-top: 80px;
}

/* ── Skeleton loader ── */
.skeleton {
  background: linear-gradient(90deg, var(--surface2) 25%, var(--border) 50%, var(--surface2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: 6px;
  height: 100%;
  width: 100%;
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.app-footer {
  text-align: center;
  padding: 16px 0 12px;
  font-size: 13px;
}
.app-footer a {
  color: var(--muted);
  text-decoration: none;
}
.app-footer a:hover {
  color: var(--text);
  text-decoration: underline;
}
