:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #d9dee7;
  --text: #1f2933;
  --muted: #697586;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --green: #0f766e;
  --red: #b42318;
  --shadow: 0 8px 24px rgba(31, 41, 55, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.column-resizing {
  cursor: col-resize;
  user-select: none;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
}

button:hover {
  border-color: #9aa4b2;
}

button.primary {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

button.primary:hover {
  background: var(--blue-dark);
}

.hidden {
  display: none !important;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1,
.topbar h1 {
  margin: 0;
  font-size: 24px;
  letter-spacing: 0;
}

.login-panel p {
  margin: 6px 0 24px;
  color: var(--muted);
}

form label {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  min-width: 0;
  background: #fff;
  color: var(--text);
}

.login-panel button {
  width: 100%;
}

.message {
  min-height: 20px;
  margin-top: 12px;
  color: var(--red);
  white-space: pre-wrap;
}

.app-shell {
  padding: 18px 20px 28px;
}

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

.subtle {
  color: var(--muted);
  margin-top: 4px;
  font-size: 13px;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  font-size: 12px;
  gap: 8px;
  justify-content: flex-end;
  align-items: end;
}

.action-control {
  display: grid;
  gap: 4px;
}

.action-control span {
  color: var(--muted);
}

.date-control {
  display: grid;
  gap: 4px;
  color: var(--muted);
}

.top-actions button,
.top-actions input {
  font-size: 12px;
}

.date-control input {
  width: 146px;
  padding: 7px 8px;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.status-grid div {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
}

.status-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.status-grid strong {
  display: block;
  margin-top: 4px;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  margin: 8px 0 12px;
  overflow-x: auto;
}

.tab {
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  background: transparent;
  padding: 10px 8px 8px;
  white-space: nowrap;
}

.tab.active {
  color: var(--blue);
  border-bottom-color: var(--blue);
}

.toolbar {
  display: flex;
  align-items: end;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.file-upload {
  color: var(--muted);
  display: grid;
  font-size: 12px;
  gap: 4px;
}

.file-upload input {
  max-width: 220px;
}

.breakeven-view,
.quality-view,
.dimension-view {
  display: grid;
  gap: 12px;
}

.maintenance-head {
  align-items: end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: flex;
  gap: 14px;
  justify-content: space-between;
  padding: 12px 14px;
}

.maintenance-head h2 {
  font-size: 16px;
  margin: 0 0 4px;
}

.maintenance-head p {
  color: var(--muted);
  font-size: 13px;
  margin: 0;
}

.maintenance-actions {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.maintenance-actions label {
  color: var(--muted);
  display: grid;
  font-size: 12px;
  gap: 4px;
  margin: 0;
}

.maintenance-shell {
  max-height: calc(100vh - 330px);
}

.user-create-grid {
  align-items: end;
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(160px, 1fr) minmax(180px, 1fr) auto;
  margin: 12px 0;
}

.user-create-grid label {
  color: var(--muted);
  display: grid;
  font-size: 12px;
  gap: 4px;
}

.user-shell {
  max-height: 420px;
}

#users-table {
  min-width: 1120px;
}

.user-actions {
  display: flex;
  gap: 6px;
  white-space: nowrap;
}

.inline-id-input {
  min-width: 220px;
  padding: 6px 8px;
}

.quality-summary {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.quality-summary div,
.quality-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
}

.quality-summary span {
  color: var(--muted);
  display: block;
  font-size: 12px;
}

.quality-summary strong {
  display: block;
  font-size: 14px;
  margin-top: 4px;
}

.quality-list {
  display: grid;
  gap: 10px;
}

.quality-card {
  display: grid;
  gap: 8px;
}

.quality-card.error {
  border-left: 4px solid #b42318;
}

.quality-card.warning {
  border-left: 4px solid #ca8a04;
}

.quality-card.info {
  border-left: 4px solid #2563eb;
}

.quality-card.ok {
  border-left: 4px solid #0f766e;
}

.quality-card-head {
  align-items: center;
  display: flex;
  gap: 8px;
}

.quality-card-head h3 {
  flex: 1;
  font-size: 15px;
  margin: 0;
}

.quality-card p {
  color: var(--text);
  margin: 0;
}

.quality-card .quality-action {
  color: var(--muted);
  font-size: 13px;
}

.quality-badge {
  background: #eef2ff;
  border-radius: 999px;
  color: #1d4ed8;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
}

.quality-card.error .quality-badge {
  background: #fef2f2;
  color: #b42318;
}

.quality-card.warning .quality-badge {
  background: #fffbeb;
  color: #92400e;
}

.quality-card.ok .quality-badge {
  background: #ecfdf5;
  color: #0f766e;
}

.quality-samples {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.quality-samples code {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 12px;
  padding: 3px 5px;
}

.quality-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.quality-actions span {
  color: var(--muted);
  font-size: 12px;
}

#breakeven-table {
  min-width: 1360px;
}

#dimension-table {
  min-width: 1360px;
}

#dimension-table th,
#dimension-table td {
  min-width: 96px;
}

#dimension-table .select-col {
  min-width: 44px;
  text-align: center;
  width: 44px;
}

.dimension-cell {
  min-width: 126px;
  padding: 5px 7px;
  width: 100%;
}

.dirty-row td {
  background: #fff7ed;
}

.import-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  gap: 10px;
  padding: 12px;
}

.import-panel textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
  font: 12px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  min-height: 150px;
  padding: 10px;
  resize: vertical;
  width: 100%;
}

.preview-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  gap: 12px;
  padding: 12px;
}

.preview-head {
  align-items: start;
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.preview-head h3,
.preview-list h4 {
  margin: 0;
}

.preview-head p {
  color: var(--muted);
  margin: 4px 0 0;
}

.preview-actions {
  display: flex;
  gap: 8px;
}

.preview-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.preview-grid div {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
}

.preview-grid span {
  color: var(--muted);
  display: block;
  font-size: 12px;
}

.preview-grid strong {
  display: block;
  font-size: 18px;
  margin-top: 4px;
}

.preview-grid .preview-danger {
  background: #fef2f2;
  border-color: #fecaca;
}

.preview-list {
  border-top: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 10px;
}

.preview-list h4 {
  flex-basis: 100%;
  font-size: 13px;
}

.preview-list code,
.preview-list p {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 5px;
  font-size: 12px;
  margin: 0;
  padding: 4px 6px;
}

.preview-list strong {
  font-weight: 700;
}

#breakeven-table small {
  color: var(--muted);
  display: block;
  font-size: 11px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.roi-input,
.note-input {
  padding: 5px 7px;
  width: 100%;
}

.roi-input {
  text-align: right;
}

.gap-cell {
  font-weight: 600;
}

.dashboard {
  display: grid;
  gap: 14px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
}

.kpi-card,
.chart-panel,
.watch-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 3px 12px rgba(31, 41, 55, 0.04);
}

.kpi-card {
  padding: 12px 14px;
}

.kpi-card span,
.kpi-card small,
.panel-title span {
  color: var(--muted);
  font-size: 12px;
}

.kpi-card strong {
  display: block;
  margin: 7px 0 5px;
  font-size: 22px;
  line-height: 1.1;
}

.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(360px, 1fr));
  gap: 12px;
}

.chart-panel,
.watch-panel {
  padding: 12px;
  min-width: 0;
}

.chart-panel.wide {
  grid-column: 1 / -1;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  margin-bottom: 10px;
}

.panel-title h2 {
  margin: 0;
  font-size: 15px;
}

.stack-chart,
.bar-chart,
.metric-chart {
  display: grid;
  gap: 10px;
}

.metric-row {
  display: grid;
  grid-template-columns: minmax(110px, 0.35fr) minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.metric-name {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.metric-name span {
  color: var(--muted);
  font-size: 12px;
}

.metric-bars {
  display: grid;
  gap: 5px;
}

.metric-bars div {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) 72px;
  gap: 8px;
  align-items: center;
  font-size: 12px;
}

.metric-bars span {
  color: var(--muted);
}

.metric-bars i {
  height: 10px;
  border-radius: 4px;
  background: var(--blue);
}

.metric-bars i.gmv {
  background: var(--green);
}

.metric-bars i.roi {
  background: #c2410c;
}

.metric-bars em {
  color: #344054;
  font-style: normal;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.stack-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  color: var(--muted);
  font-size: 12px;
}

.stack-legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.stack-legend i {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 2px;
}

.stack-row {
  display: grid;
  gap: 5px;
}

.stack-label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
}

.stack-label span {
  color: var(--muted);
}

.stack-bar {
  height: 18px;
  display: flex;
  overflow: hidden;
  border-radius: 4px;
  background: #eef2f7;
}

.stack-bar span {
  min-width: 0;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  overflow: hidden;
  text-shadow: 0 1px 2px rgba(15, 23, 42, 0.3);
  white-space: nowrap;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(110px, 1.1fr) minmax(120px, 1fr) 58px;
  gap: 8px;
  align-items: center;
}

.bar-name {
  min-width: 0;
  display: grid;
  gap: 2px;
  font-size: 12px;
}

.bar-name span {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-track {
  height: 12px;
  background: #eef2f7;
  border-radius: 4px;
  overflow: hidden;
}

.bar-track span {
  display: block;
  height: 100%;
}

.bar-track .bad {
  background: var(--red);
}

.bar-track .good {
  background: var(--green);
}

.bar-track .spend {
  background: var(--blue);
}

.bar-value {
  font-size: 12px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.heatmap {
  overflow: auto;
}

.heatmap-grid {
  display: grid;
  gap: 1px;
  min-width: 920px;
}

.heatmap-head,
.heatmap-cell {
  min-height: 42px;
  padding: 7px 8px;
  background: #f8fafc;
  font-size: 12px;
}

.heatmap-head {
  color: #344054;
  font-weight: 600;
}

.heatmap-head.row {
  position: sticky;
  left: 0;
  z-index: 1;
}

.heatmap-cell {
  color: #102a43;
  display: grid;
  align-content: center;
  gap: 2px;
}

.heatmap-cell span {
  color: rgba(31, 41, 51, 0.72);
}

.watch-panel {
  overflow: auto;
}

#watch-table {
  min-width: 860px;
}

.text-bad {
  color: var(--red);
  font-weight: 600;
}

.search {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
  min-width: min(360px, 100%);
}

.check {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 8px;
}

.table-shell {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: auto;
  max-height: calc(100vh - 260px);
}

.split-table-shell {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  overflow: hidden;
}

.table-frozen-pane {
  border-right: 1px solid var(--line);
  max-height: calc(100vh - 260px);
  overflow: auto;
  position: relative;
  scrollbar-width: none;
  z-index: 4;
}

.table-frozen-pane::-webkit-scrollbar {
  display: none;
}

.table-scroll-pane {
  overflow: auto;
  max-height: calc(100vh - 260px);
}

.split-table-shell table {
  width: max-content;
}

.split-table-shell .table-scroll-pane table {
  min-width: 0;
}

.split-table-shell .table-frozen-pane table {
  min-width: 0;
}

.table-scrollbar {
  background: #f1f5f9;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 6px 6px 0 0;
  cursor: pointer;
  height: 26px;
  margin-top: 4px;
  padding: 7px 10px;
  user-select: none;
}

.table-scrollbar + .table-shell {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.table-scrollbar-track {
  background: #dbe3ee;
  border-radius: 999px;
  height: 10px;
  position: relative;
}

.table-scrollbar-thumb {
  background: #64748b;
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.08);
  cursor: grab;
  height: 10px;
  left: 0;
  min-width: 42px;
  position: absolute;
  top: 0;
  touch-action: none;
}

.table-scrollbar-thumb:active {
  cursor: grabbing;
  background: #334155;
}

table {
  width: 100%;
  min-width: 1320px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
  table-layout: fixed;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #eef2f7;
  border-bottom: 1px solid var(--line);
  color: #344054;
  font-weight: 600;
  text-align: left;
  padding: 8px 10px;
  white-space: nowrap;
  min-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
}

thead th .col-resizer {
  bottom: 0;
  cursor: col-resize;
  position: absolute;
  right: -4px;
  top: 0;
  width: 8px;
  z-index: 3;
}

thead th .col-resizer::after {
  background: transparent;
  bottom: 7px;
  content: "";
  position: absolute;
  right: 3px;
  top: 7px;
  width: 2px;
}

thead th:hover .col-resizer::after,
body.column-resizing thead th .col-resizer::after {
  background: #94a3b8;
}

tbody td {
  border-bottom: 1px solid var(--line);
  padding: 7px 10px;
  white-space: nowrap;
  min-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody tr:nth-child(even) {
  background: #fafbfc;
}

tbody tr.total-row {
  background: #f2f6fb;
  font-weight: 600;
}

tbody tr.brand-total-row {
  background: #ecfdf3;
  color: #064e3b;
  font-weight: 700;
}

th.frozen-col,
td.frozen-col {
  left: var(--frozen-left);
  position: sticky;
}

thead th.frozen-col {
  z-index: 5;
}

tbody td.frozen-col {
  background: var(--panel);
  z-index: 2;
}

tbody tr:nth-child(even) td.frozen-col {
  background: #fafbfc;
}

tbody tr.total-row td.frozen-col {
  background: #f2f6fb;
}

tbody tr.brand-total-row td.frozen-col {
  background: #ecfdf3;
}

th.frozen-edge,
td.frozen-edge {
  box-shadow: 8px 0 10px -10px rgba(15, 23, 42, 0.45);
}

td.number,
th.number {
  text-align: right;
}

td.with-bar {
  position: relative;
  isolation: isolate;
}

td.with-bar::before {
  content: "";
  position: absolute;
  top: 4px;
  bottom: 4px;
  right: 4px;
  width: var(--bar-width, 0%);
  max-width: calc(100% - 8px);
  background: var(--bar-color, rgba(37, 99, 235, 0.12));
  border-radius: 4px;
  z-index: -1;
}

td.with-bar span {
  position: relative;
  z-index: 1;
  font-variant-numeric: tabular-nums;
}

.result {
  margin-top: 12px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.refresh-result-line {
  align-items: center;
  display: flex;
  gap: 8px;
  min-height: 42px;
  overflow: visible;
  white-space: normal;
}

.refresh-detail {
  display: inline-flex;
  position: relative;
}

.refresh-info-icon {
  align-items: center;
  background: #fff7ed;
  border: 1px solid #fb923c;
  border-radius: 50%;
  color: #c2410c;
  display: inline-flex;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 12px;
  font-weight: 700;
  height: 20px;
  justify-content: center;
  line-height: 1;
  padding: 0;
  width: 20px;
}

.refresh-info-popup {
  background: #111827;
  border-radius: 6px;
  box-shadow: var(--shadow);
  color: #fff;
  display: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  left: 28px;
  line-height: 1.55;
  max-height: min(420px, 70vh);
  min-width: 320px;
  overflow: auto;
  padding: 12px;
  position: absolute;
  top: -10px;
  white-space: pre-wrap;
  width: min(620px, calc(100vw - 80px));
  z-index: 20;
}

.refresh-detail:hover .refresh-info-popup,
.refresh-detail:focus-within .refresh-info-popup {
  display: block;
}

dialog {
  width: min(620px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0.28);
}

dialog form {
  padding: 20px;
}

dialog h2 {
  margin: 0 0 16px;
  font-size: 18px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
}

.form-grid .full {
  grid-column: 1 / -1;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

@media (max-width: 900px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .status-grid {
    grid-template-columns: 1fr 1fr;
  }

  .kpi-grid {
    grid-template-columns: 1fr 1fr;
  }

  .chart-grid {
    grid-template-columns: 1fr;
  }

  .chart-panel.wide {
    grid-column: span 1;
  }
}

@media (max-width: 560px) {
  .app-shell {
    padding: 12px;
  }

  .status-grid,
  .kpi-grid,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .bar-row {
    grid-template-columns: 1fr;
  }

  .metric-row {
    grid-template-columns: 1fr;
  }
}
