:root {
  color-scheme: light;
  --red: #b31f2b;
  --red-dark: #861621;
  --ink: #20252b;
  --muted: #66717d;
  --line: #dce1e5;
  --soft: #f4f6f7;
  --paper: #ffffff;
  --teal: #08736d;
  --teal-soft: #e8f5f3;
  --amber: #a25f00;
  --amber-soft: #fff4dd;
  --danger-soft: #fbe9eb;
  --shadow: 0 16px 45px rgba(24, 29, 34, 0.14);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--soft); }

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background: var(--soft);
  letter-spacing: 0;
}

button,
input,
select { font: inherit; }

button,
a { -webkit-tap-highlight-color: transparent; }

button { cursor: pointer; }

.topbar {
  min-height: 64px;
  padding: 10px clamp(16px, 3vw, 42px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  color: #fff;
  background: var(--ink);
  border-bottom: 4px solid var(--red);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 13px;
  color: inherit;
  text-decoration: none;
  white-space: nowrap;
}

.brand strong { font-size: 1.22rem; }
.brand span { color: #d9dde0; font-size: 0.9rem; }

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.sync-status {
  margin-right: 8px;
  padding-left: 12px;
  color: #d9dde0;
  font-size: 0.78rem;
  white-space: nowrap;
  border-left: 1px solid #4b525a;
}

.sync-status.good { color: #93ded4; }
.sync-status.bad { color: #ffc36d; }

.command,
.nav-link,
.quiet,
.primary {
  min-height: 36px;
  padding: 8px 13px;
  border: 1px solid transparent;
  border-radius: 5px;
  text-decoration: none;
  font-weight: 700;
}

.command,
.nav-link {
  color: #fff;
  background: transparent;
  border-color: #525a62;
}

.command:hover,
.nav-link:hover { background: #343a41; }

.tabs {
  min-height: 48px;
  padding: 0 clamp(16px, 3vw, 42px);
  display: flex;
  align-items: stretch;
  gap: 5px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar { display: none; }

.tabs button {
  padding: 0 18px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-bottom: 3px solid transparent;
  font-weight: 700;
}

.tabs button:hover { color: var(--ink); }
.tabs button.active { color: var(--red); border-bottom-color: var(--red); }

main {
  width: min(1700px, 100%);
  margin: 0 auto;
  padding: 20px clamp(14px, 3vw, 38px) 54px;
}

.filter-shell {
  margin-bottom: 18px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.filter-toolbar {
  min-height: 52px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.quick-ranges { display: flex; gap: 5px; flex-wrap: wrap; }

.quick-ranges button {
  min-height: 34px;
  padding: 7px 11px;
  color: var(--muted);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-weight: 700;
}

.quick-ranges button:hover { color: var(--ink); background: var(--soft); }
.quick-ranges button.active { color: #fff; background: var(--ink); border-color: var(--ink); }

.quiet { color: var(--ink); background: #fff; border-color: var(--line); }
.quiet:hover { background: var(--soft); }
.primary { color: #fff; background: var(--red); border-color: var(--red); }
.primary:hover { background: var(--red-dark); }

.filters {
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 12px;
  border-top: 1px solid var(--line);
}

.filters label {
  min-width: 0;
  display: grid;
  gap: 6px;
  color: #505962;
  font-size: 0.74rem;
  font-weight: 800;
  text-transform: uppercase;
}

.filters input,
.filters select {
  width: 100%;
  height: 40px;
  padding: 8px 10px;
  color: var(--ink);
  background: #fff;
  border: 1px solid #cbd2d8;
  border-radius: 4px;
  outline: 0;
  text-transform: none;
}

.filters input:focus,
.filters select:focus { border-color: var(--red); box-shadow: 0 0 0 3px rgba(179, 31, 43, 0.12); }

.filters .advanced { display: none; }
.filters.expanded .advanced { display: grid; }

.filters .check {
  min-height: 40px;
  display: none;
  grid-template-columns: 18px 1fr;
  align-items: center;
  align-self: end;
  gap: 8px;
}

.filters.expanded .check { display: grid; }
.filters .check input { width: 18px; height: 18px; margin: 0; }

.filter-actions { display: flex; align-items: end; gap: 8px; }

.message {
  margin-bottom: 18px;
  padding: 13px 15px;
  color: #6f4500;
  background: var(--amber-soft);
  border: 1px solid #eac478;
  border-radius: 5px;
}

.loading {
  padding: 70px 20px;
  text-align: center;
  color: var(--muted);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.view-head {
  margin: 4px 0 18px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
}

.view-head h1 { margin: 0; font-size: clamp(1.45rem, 2vw, 2rem); }
.view-head p { margin: 6px 0 0; color: var(--muted); }
.head-meta { display: flex; justify-content: flex-end; gap: 7px; flex-wrap: wrap; }

.chip,
.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
}

.chip { color: #4b555e; background: #fff; border: 1px solid var(--line); }
.chip.live { color: #075c57; background: var(--teal-soft); border-color: #b7ddd8; }

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

.kpi {
  min-height: 114px;
  padding: 15px;
  display: grid;
  align-content: space-between;
  gap: 7px;
  background: #fff;
  border: 1px solid var(--line);
  border-top: 3px solid #7b858e;
  border-radius: 6px;
}

.kpi.teal { border-top-color: var(--teal); }
.kpi.red { border-top-color: var(--red); }
.kpi.amber { border-top-color: var(--amber); }
.kpi span { color: var(--muted); font-size: 0.72rem; font-weight: 800; text-transform: uppercase; }
.kpi strong { font-size: 1.55rem; line-height: 1; overflow-wrap: anywhere; }
.kpi small { color: var(--muted); font-size: 0.74rem; }

.flow-band {
  margin-bottom: 18px;
  padding: 16px;
  background: var(--ink);
  border-radius: 7px;
  color: #fff;
}

.scope-band {
  margin-bottom: 18px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  overflow: hidden;
  color: #fff;
  background: var(--ink);
  border-radius: 6px;
}

.scope-band > div { min-width: 0; padding: 13px 16px; border-right: 1px solid #40474e; }
.scope-band > div:last-child { border-right: 0; }
.scope-band strong, .scope-band span { display: block; }
.scope-band strong { font-size: 0.82rem; }
.scope-band span { margin-top: 4px; color: #cbd1d5; font-size: 0.72rem; }
.readiness { margin-bottom: 16px; padding: 14px 16px; display: grid; gap: 4px; background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--teal); border-radius: 5px; }
.readiness.warning { border-left-color: var(--amber); background: var(--amber-soft); }
.readiness span, .method-note { color: var(--muted); font-size: 0.78rem; line-height: 1.5; }
.method-note { margin: 13px 2px 0; }

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

.section-title h2 { margin: 0; font-size: 1rem; }
.section-title p { margin: 3px 0 0; color: var(--muted); font-size: 0.8rem; }
.flow-band .section-title p { color: #bdc4ca; }

.flow-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  border-radius: 5px;
  background: #4c545c;
}

.flow-step {
  min-height: 92px;
  padding: 13px 15px;
  display: grid;
  gap: 6px;
  background: #2d3339;
}

.flow-step span { color: #cbd1d5; font-size: 0.72rem; font-weight: 800; text-transform: uppercase; }
.flow-step strong { font-size: 1.4rem; }
.flow-step small { color: #aeb7bd; }
.flow-step.issue { box-shadow: inset 0 -4px var(--red); }
.flow-step.receipt { box-shadow: inset 0 -4px var(--teal); }

.chart-grid {
  margin-bottom: 18px;
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.75fr);
  gap: 14px;
}

.panel {
  min-width: 0;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.legend { display: flex; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 0.72rem; }
.legend span::before { content: ""; display: inline-block; width: 9px; height: 9px; margin-right: 5px; border-radius: 2px; background: var(--ink); }
.legend .receipt::before { background: var(--teal); }
.legend .issue::before { background: var(--red); }
.chart-controls { display: grid; justify-items: end; gap: 8px; }
.chart-modes { display: inline-flex; padding: 2px; background: #edf0f2; border-radius: 5px; }
.chart-modes button { padding: 6px 9px; color: #59636b; background: transparent; border: 0; border-radius: 3px; font-size: 0.7rem; font-weight: 800; }
.chart-modes button.active { color: #fff; background: var(--ink); }

.trend-chart {
  height: 250px;
  padding-top: 16px;
  display: flex;
  align-items: stretch;
  gap: 8px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
}

.trend-group {
  min-width: 54px;
  flex: 1 0 54px;
  display: grid;
  grid-template-rows: 1fr 30px;
  gap: 7px;
  padding: 0 2px;
  color: inherit;
  background: transparent;
  border: 0;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
}

.trend-group:hover, .trend-group:focus-visible { background: #f2f4f5; outline: 2px solid transparent; }

.bars { display: flex; align-items: end; justify-content: center; gap: 3px; }
.bar { width: min(14px, 24%); min-height: 2px; border-radius: 2px 2px 0 0; background: var(--ink); }
.bar.receipt { background: var(--teal); }
.bar.issue { background: var(--red); }
.bar.issue.amount { width: min(24px, 55%); }
.trend-label { color: var(--muted); font-size: 0.65rem; text-align: center; white-space: nowrap; }

.rank-list { min-width: 0; max-width: 100%; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; overflow: hidden; }
.rank-row { width: 100%; min-width: 0; max-width: 100%; padding: 5px; display: grid; gap: 5px; overflow: hidden; color: inherit; text-align: left; background: transparent; border: 0; border-radius: 4px; cursor: pointer; }
.rank-row:hover, .rank-row:focus-visible { background: #f5f6f7; outline: 2px solid var(--ink); outline-offset: 1px; }
.rank-head { width: 100%; min-width: 0; max-width: 100%; display: flex; justify-content: space-between; gap: 12px; font-size: 0.78rem; }
.rank-head span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-head strong { white-space: nowrap; }
.track { width: 100%; min-width: 0; max-width: 100%; height: 7px; overflow: hidden; background: #e8ecee; border-radius: 3px; }
.track span { display: block; height: 100%; background: var(--red); border-radius: inherit; }

.table-panel {
  padding: 0;
  overflow: hidden;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.table-tools {
  min-height: 60px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
}

.table-tools h2 { margin: 0; font-size: 1rem; }
.table-actions { display: flex; align-items: end; justify-content: flex-end; gap: 9px; }
.sort-control { display: grid; gap: 3px; color: var(--muted); font-size: 0.66rem; font-weight: 800; text-transform: uppercase; }
.sort-control select { height: 38px; min-width: 205px; padding: 6px 30px 6px 9px; color: var(--ink); background: #fff; border: 1px solid #cbd2d8; border-radius: 4px; font-size: 0.76rem; text-transform: none; }
.table-tools .search { width: min(360px, 50vw); height: 38px; padding: 8px 10px; border: 1px solid #cbd2d8; border-radius: 4px; }
.table-wrap { width: 100%; overflow: auto; }

table { width: 100%; border-collapse: collapse; font-size: 0.79rem; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid #e8ebed; vertical-align: middle; }
th { position: sticky; top: 0; z-index: 1; color: #4d565e; background: #f6f7f8; font-size: 0.68rem; text-transform: uppercase; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--red); }
td.numeric, th.numeric { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr[data-project], tbody tr[data-item] { cursor: pointer; }
tbody tr[data-project]:hover, tbody tr[data-item]:hover { background: #fff8f8; }
.main-cell { min-width: 240px; }
.main-cell strong, .main-cell span { display: block; }
.main-cell span { margin-top: 3px; color: var(--muted); }
.date-cell { min-width: 126px; white-space: nowrap; }
.date-cell strong, .date-cell span { display: block; }
.date-cell span { margin-top: 3px; color: var(--muted); text-transform: capitalize; }
.warehouse-cell { max-width: 220px; color: #4f5962; }
.muted { color: var(--muted); }

.status-pill.balanced { color: #075c57; background: var(--teal-soft); }
.status-pill.pending { color: #754700; background: var(--amber-soft); }
.status-pill.over_issued,
.status-pill.no_request { color: #8b1721; background: var(--danger-soft); }
.status-pill.no_activity { color: #59636b; background: #edf0f2; }

.detail-button {
  margin-top: 7px;
  padding: 5px 8px;
  color: var(--red);
  background: #fff;
  border: 1px solid #e0b8bd;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 800;
}

.detail-button:hover { color: #fff; background: var(--red); border-color: var(--red); }

.empty { padding: 46px 20px; text-align: center; color: var(--muted); }

.alert-summary {
  margin-bottom: 16px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.alert-stat { padding: 15px; background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--amber); border-radius: 6px; }
.alert-stat.critical { border-left-color: var(--red); }
.alert-stat strong { display: block; margin-top: 5px; font-size: 1.55rem; }

.method-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.method-block { padding: 18px; background: #fff; border: 1px solid var(--line); border-radius: 7px; }
.method-block h2 { margin: 0 0 8px; font-size: 1rem; }
.method-block p { margin: 0; color: var(--muted); line-height: 1.6; }

.drill {
  position: fixed;
  inset: 0 0 0 auto;
  z-index: 40;
  width: min(850px, 94vw);
  display: grid;
  grid-template-rows: auto auto 1fr;
  background: #fff;
  box-shadow: var(--shadow);
}

.drill[hidden] { display: none; }
.drill-head { padding: 18px 20px; display: flex; justify-content: space-between; align-items: start; gap: 20px; color: #fff; background: var(--ink); border-bottom: 4px solid var(--red); }
.drill-head span { color: #c7cdd2; font-size: 0.72rem; text-transform: uppercase; }
.drill-head h2 { margin: 5px 0 0; font-size: 1.25rem; }
.drill-actions { display: flex; align-items: center; gap: 8px; }
.back-button { min-height: 38px; padding: 8px 11px; color: #fff; background: transparent; border: 1px solid #5c646c; border-radius: 4px; font-size: 0.75rem; font-weight: 800; }
.back-button:hover, .back-button:focus-visible, .close-button:focus-visible { background: #363d43; outline: 2px solid #fff; outline-offset: 1px; }
.close-button { width: 38px; height: 38px; color: #fff; background: transparent; border: 1px solid #5c646c; border-radius: 4px; font-size: 1.5rem; line-height: 1; }
.close-button:hover { background: #363d43; }
.breadcrumb { min-height: 48px; padding: 8px 18px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.breadcrumb button { padding: 6px 8px; color: var(--red); background: transparent; border: 0; font-weight: 800; }
.breadcrumb span { color: var(--muted); }
.drill-content { min-height: 0; padding: 18px; overflow: auto; }
.drill-content .table-panel { box-shadow: none; }
.drawer-shade { position: fixed; inset: 0; z-index: 35; background: rgba(20, 24, 28, 0.4); }
.chart-tooltip { position: fixed; z-index: 70; min-width: 180px; max-width: 280px; padding: 10px 12px; display: grid; gap: 3px; color: #fff; background: #1f252a; border: 1px solid #4a535a; border-radius: 5px; box-shadow: 0 8px 22px rgba(0, 0, 0, 0.24); pointer-events: none; font-size: 0.72rem; }
.chart-tooltip[hidden] { display: none; }
.chart-tooltip strong { margin-bottom: 3px; font-size: 0.78rem; }
.chart-tooltip span { color: #d7dde1; }

@media (max-width: 1180px) {
  .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .filters { grid-template-columns: repeat(3, minmax(150px, 1fr)); }
  .chart-grid { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .top-actions { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
  .sync-status { width: 100%; order: -1; margin: 0; padding: 0; border: 0; }
  .tabs { overflow-x: auto; }
  .tabs button { min-height: 46px; white-space: nowrap; }
  main { padding-top: 14px; }
  .filter-toolbar { align-items: flex-start; flex-direction: column; }
  .filters { grid-template-columns: 1fr 1fr; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .flow-grid { grid-template-columns: 1fr 1fr; gap: 1px; }
  .scope-band { grid-template-columns: 1fr; }
  .scope-band > div { border-right: 0; border-bottom: 1px solid #40474e; }
  .scope-band > div:last-child { border-bottom: 0; }
  .view-head { align-items: flex-start; flex-direction: column; }
  .head-meta { justify-content: flex-start; }
  .table-tools { align-items: stretch; flex-direction: column; }
  .table-actions { align-items: stretch; flex-direction: column; }
  .sort-control select { width: 100%; }
  .table-tools .search { width: 100%; }
  .section-title { align-items: flex-start; flex-direction: column; }
  .chart-controls { width: 100%; justify-items: start; }
  .alert-summary, .method-grid { grid-template-columns: 1fr; }
}

@media (max-width: 470px) {
  .brand { align-items: flex-start; flex-direction: column; gap: 2px; }
  .command, .nav-link { padding-inline: 9px; font-size: 0.78rem; }
  .filters { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: 1fr; }
  .flow-grid { grid-template-columns: 1fr; }
  .drill-head { align-items: stretch; flex-direction: column; }
  .drill-actions { justify-content: space-between; }
}

@media print {
  .top-actions, .tabs, .filter-shell, .drawer-shade, .drill { display: none !important; }
  body { background: #fff; }
  main { width: 100%; padding: 10px; }
  .panel, .table-panel, .kpi, .flow-band { break-inside: avoid; }
}
