:root {
  --navy: #003f7d;
  --blue: #004e8a;
  --sky: #009cda;
  --teal: #007f73;
  --green: #1d8b74;
  --red: #e6001a;
  --amber: #c78318;
  --ink: #18212f;
  --muted: #667085;
  --line: #d9e1ea;
  --surface: #ffffff;
  --surface-soft: #f6f8fb;
  --panel: #ffffff;
  --shadow: 0 14px 34px rgba(15, 35, 70, 0.08);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(0, 78, 138, 0.08), rgba(255, 255, 255, 0) 280px),
    var(--surface-soft);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  line-height: 1.4;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1480px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 18px 0 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 92px;
  padding: 16px 18px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 18px;
}

.brand img {
  width: 158px;
  height: auto;
  flex: 0 0 auto;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue);
  font-size: 0.76rem;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  overflow-wrap: anywhere;
}

h1 {
  margin: 0;
  max-width: 940px;
  color: var(--ink);
  font-size: 1.9rem;
  font-weight: 790;
  line-height: 1.12;
  letter-spacing: 0;
}

h2 {
  margin: 0;
  color: var(--ink);
  font-size: 2rem;
  line-height: 1.18;
  letter-spacing: 0;
}

h3 {
  margin: 0;
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.2;
  letter-spacing: 0;
}

.topbar-actions,
.button-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

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

.file-input {
  display: none;
}

.helper-text {
  color: var(--muted);
  font-size: 0.8rem;
}

.primary-action,
.secondary-action,
.icon-button,
.tab-button,
.segment {
  border: 1px solid transparent;
  border-radius: var(--radius);
  min-height: 40px;
  transition:
    background 0.18s ease,
    border-color 0.18s ease,
    color 0.18s ease,
    transform 0.18s ease;
}

.primary-action,
.secondary-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  font-weight: 720;
  white-space: nowrap;
}

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

.primary-action:hover {
  background: #003d6d;
  transform: translateY(-1px);
}

.secondary-action {
  color: var(--blue);
  background: #eef7fc;
  border-color: #cde8f6;
}

.secondary-action:hover {
  border-color: var(--sky);
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  color: var(--blue);
  background: #f4f7fb;
  border-color: var(--line);
}

.icon-button:hover {
  background: #eaf4fa;
  border-color: #bdd8e9;
}

.reset-action,
.optimize-action {
  background: #f7fbff;
  border-color: #cfe0ee;
}

.optimize-action {
  color: var(--teal);
  background: #edf8f7;
  border-color: #c5e5e1;
}

.optimize-action:hover {
  background: #e2f3f1;
  border-color: #8fcfc7;
}

.icon-button svg,
.primary-action svg,
.secondary-action svg,
.tab-button svg {
  width: 18px;
  height: 18px;
  stroke-width: 2.2;
}

.module-tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 18px;
}

.tab-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
  padding: 10px 12px;
  color: var(--ink);
  background: var(--surface);
  border-color: var(--line);
  font-weight: 760;
  box-shadow: 0 8px 18px rgba(20, 39, 70, 0.04);
}

.tab-button.active {
  color: #fff;
  background: var(--navy);
  border-color: var(--navy);
}

.tab-button:not(.active):hover {
  color: var(--blue);
  border-color: #b9d6eb;
}

.module {
  display: none;
}

.module.active {
  display: block;
}

.module-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin: 10px 0 16px;
}

.segmented {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  min-height: 48px;
  background: #eaf0f6;
  border: 1px solid #d7e2eb;
  border-radius: var(--radius);
}

.segment {
  min-height: 38px;
  padding: 0 13px;
  color: #344154;
  background: transparent;
  border-color: transparent;
  font-weight: 720;
  white-space: nowrap;
}

.segment.active {
  color: #fff;
  background: var(--teal);
  box-shadow: 0 8px 16px rgba(0, 127, 115, 0.18);
}

.forecast-grid,
.inventory-grid,
.lot-grid,
.program-grid {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.side-stack,
.main-stack {
  display: grid;
  gap: 14px;
  align-content: start;
  min-width: 0;
}

.side-stack {
  flex: 0 0 clamp(320px, 31%, 440px);
}

.main-stack {
  flex: 1 1 0;
}

.panel,
.metric-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel {
  padding: 16px;
}

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

.panel-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.controls-panel {
  display: grid;
  gap: 12px;
}

.chart-panel,
.table-panel {
  min-width: 0;
}

.metric-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  min-width: 0;
}

.metric-card {
  min-height: 96px;
  padding: 14px;
  border-top: 4px solid var(--sky);
}

.metric-card:nth-child(2) {
  border-top-color: var(--teal);
}

.metric-card:nth-child(3) {
  border-top-color: var(--red);
}

.metric-card:nth-child(4) {
  border-top-color: var(--amber);
}

.metric-label {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 730;
}

.metric-value {
  display: block;
  color: var(--ink);
  font-size: 2rem;
  font-weight: 820;
  line-height: 1.05;
}

.metric-note {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.field,
.slider-field {
  display: grid;
  gap: 7px;
}

.field span,
.slider-field span {
  color: #344154;
  font-size: 0.86rem;
  font-weight: 720;
}

textarea,
input[type="number"],
input[type="text"],
select {
  width: 100%;
  min-width: 0;
  color: var(--ink);
  background: #fbfdff;
  border: 1px solid #cfdbe7;
  border-radius: var(--radius);
  outline: none;
  font-size: 1rem;
}

textarea {
  resize: vertical;
  padding: 10px 12px;
  line-height: 1.5;
}

input[type="number"],
input[type="text"],
select {
  min-height: 40px;
  padding: 0 10px;
}

.slider-field input[type="number"] {
  margin-top: 6px;
}

textarea:focus,
input:focus,
select:focus {
  border-color: var(--sky);
  box-shadow: 0 0 0 3px rgba(0, 156, 218, 0.14);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--blue);
}

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

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 5px 9px;
  color: #24515c;
  background: #e7f4f5;
  border: 1px solid #c6e3e4;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 760;
  white-space: nowrap;
}

.chart-frame {
  width: 100%;
  min-height: 320px;
  overflow: hidden;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
  background: #fff;
}

.chart-frame.compact {
  min-height: 230px;
}

.chart-frame svg {
  display: block;
  width: 100%;
  height: 100%;
  min-height: inherit;
}

.split-panel {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(220px, 0.8fr);
  gap: 14px;
  align-items: stretch;
}

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

.insight {
  padding: 11px 12px;
  background: #f8fafc;
  border: 1px solid #e4ebf3;
  border-left: 4px solid var(--blue);
  border-radius: var(--radius);
}

.insight.warning {
  background: #fff8ed;
  border-color: #f5d6a2;
  border-left-color: #c77900;
}

.insight strong {
  display: block;
  margin-bottom: 2px;
  color: var(--ink);
}

.insight.warning strong {
  color: #7c3d00;
}

.insight span {
  color: var(--muted);
  font-size: 0.9rem;
}

.legend {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.84rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.legend-swatch {
  width: 22px;
  height: 3px;
  border-radius: 999px;
  background: var(--blue);
}

.legend-swatch.dashed {
  background: repeating-linear-gradient(90deg, currentColor 0 6px, transparent 6px 10px);
}

.math-label {
  white-space: nowrap;
  font-weight: 800;
}

.math-label sub {
  font-size: 0.72em;
  line-height: 0;
}

.calculation-notes {
  margin-top: 2px;
}

.calculation-notes .insight {
  padding: 9px 10px;
}

.calculation-notes .insight span {
  font-size: 0.82rem;
}

.table-scroll {
  width: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
}

table {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
  background: #fff;
}

.forecast-table {
  min-width: 470px;
  table-layout: fixed;
}

.forecast-table th,
.forecast-table td {
  padding: 9px 8px;
}

.forecast-table th:nth-child(1),
.forecast-table td:nth-child(1) {
  width: 74px;
}

.forecast-table th:nth-child(2),
.forecast-table td:nth-child(2),
.forecast-table th:nth-child(4),
.forecast-table td:nth-child(4),
.forecast-table th:nth-child(5),
.forecast-table td:nth-child(5) {
  width: 78px;
}

.forecast-table th:nth-child(3),
.forecast-table td:nth-child(3) {
  width: 112px;
}

th,
td {
  padding: 10px 11px;
  border-bottom: 1px solid #e6edf4;
  text-align: right;
  vertical-align: middle;
  white-space: nowrap;
}

th:first-child,
td:first-child {
  text-align: left;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: #344154;
  background: #f4f7fb;
  font-size: 0.78rem;
  font-weight: 790;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover td {
  background: #fbfdff;
}

.forecast-grid .chart-panel {
  min-height: 420px;
}

.forecast-grid .metric-strip,
.inventory-grid .metric-strip,
.lot-grid .metric-strip,
.program-grid .metric-strip,
.main-stack .metric-strip {
  grid-column: auto;
}

.inventory-grid .chart-panel {
  min-height: 390px;
}

.lot-grid .chart-panel {
  min-height: 390px;
}

.product-editor {
  min-height: 260px;
}

.product-editor input {
  width: 110px;
  min-height: 34px;
}

.product-name {
  width: 140px;
}

.danger-button {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  color: #a2232f;
  background: #fff5f5;
  border: 1px solid #f2ccd0;
  border-radius: var(--radius);
}

.danger-button svg {
  width: 17px;
  height: 17px;
}

.copy-toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 10;
  padding: 12px 14px;
  color: #fff;
  background: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-weight: 720;
}

.export-preview {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(24, 33, 47, 0.58);
}

.export-preview-dialog {
  display: grid;
  gap: 14px;
  width: min(1120px, 96vw);
  max-height: 92vh;
  padding: 16px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.24);
}

.export-preview-header,
.export-preview-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.export-preview img {
  width: 100%;
  height: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.info-modal {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(24, 33, 47, 0.58);
}

.info-dialog {
  display: grid;
  gap: 16px;
  width: min(980px, 96vw);
  max-height: 92vh;
  padding: 18px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.24);
}

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

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

.formula-card {
  display: grid;
  gap: 8px;
  padding: 14px;
  background: #fbfdff;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
}

.formula-card strong {
  color: var(--ink);
}

.formula-card code {
  padding: 9px 10px;
  white-space: normal;
  color: #12315a;
  background: #eef7fc;
  border-radius: var(--radius);
  font-family: "Segoe UI", Arial, sans-serif;
  font-weight: 760;
}

.formula-card span,
.info-current {
  color: var(--muted);
  line-height: 1.5;
}

.info-current {
  padding: 12px 14px;
  background: #f8fafc;
  border: 1px solid #e4ebf3;
  border-left: 4px solid var(--teal);
  border-radius: var(--radius);
}

.info-current.info-warning {
  background: #fff8ed;
  border-color: #f5d6a2;
  border-left-color: #c77900;
}

.calculation-block {
  display: grid;
  gap: 10px;
}

.calculation-block h4 {
  margin: 2px 0 0;
  color: var(--ink);
  font-size: 1rem;
}

.calculation-table {
  display: grid;
  overflow: hidden;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
}

.calculation-row {
  display: grid;
  grid-template-columns: minmax(150px, 0.32fr) minmax(0, 0.68fr);
  gap: 12px;
  padding: 10px 12px;
  background: #fff;
  border-bottom: 1px solid #e6edf4;
}

.calculation-row:last-child {
  border-bottom: 0;
}

.calculation-row strong {
  color: var(--ink);
}

.calculation-row span {
  color: var(--muted);
  line-height: 1.45;
}

.hidden {
  display: none !important;
}

@media (max-width: 1180px) {
  .forecast-grid,
  .inventory-grid,
  .lot-grid,
  .program-grid {
    flex-direction: column;
  }

  .side-stack,
  .main-stack {
    width: 100%;
    flex: 1 1 auto;
  }

  .forecast-grid .controls-panel,
  .inventory-grid .controls-panel,
  .lot-grid .controls-panel,
  .program-grid .controls-panel {
    grid-row: auto;
  }

  .forecast-grid .metric-strip,
  .inventory-grid .metric-strip,
  .lot-grid .metric-strip,
  .program-grid .metric-strip {
    grid-column: auto;
  }
}

@media (max-width: 880px) {
  .app-shell {
    width: min(100vw - 16px, 1480px);
    padding-top: 10px;
  }

  .topbar,
  .module-header {
    align-items: stretch;
    flex-direction: column;
  }

  .brand {
    align-items: flex-start;
    flex-direction: column;
  }

  .brand img {
    width: 132px;
  }

  h1 {
    font-size: 1.55rem;
  }

  h2 {
    font-size: 1.65rem;
  }

  .topbar-actions,
  .segmented {
    width: 100%;
  }

  .primary-action,
  .secondary-action,
  .segment {
    flex: 1 1 auto;
  }

  .module-tabs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tab-button {
    justify-content: flex-start;
  }

  .metric-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .forecast-grid .main-stack,
  .inventory-grid .main-stack,
  .lot-grid .main-stack,
  .program-grid .main-stack {
    order: 1 !important;
  }

  .forecast-grid .side-stack,
  .inventory-grid .side-stack,
  .lot-grid .side-stack,
  .program-grid .side-stack {
    order: 2 !important;
  }

  .metric-value {
    font-size: 1.8rem;
  }

  .split-panel {
    grid-template-columns: 1fr;
  }

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

  .calculation-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }
}

@media (max-width: 580px) {
  body {
    background:
      linear-gradient(180deg, rgba(0, 78, 138, 0.08), rgba(255, 255, 255, 0) 210px),
      var(--surface-soft);
  }

  .app-shell {
    width: calc(100vw - 12px);
    padding: 6px 0 28px;
  }

  .topbar {
    gap: 12px;
    min-height: 0;
    padding: 12px;
  }

  .brand {
    gap: 10px;
  }

  .brand img {
    width: 116px;
  }

  h1 {
    font-size: 1.22rem;
    line-height: 1.16;
  }

  h2 {
    font-size: 1.42rem;
  }

  h3 {
    font-size: 0.98rem;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .topbar-actions .icon-button {
    width: 44px;
    height: 44px;
  }

  .topbar-actions .primary-action {
    min-width: 0;
    min-height: 44px;
    white-space: normal;
  }

  .module-tabs,
  .metric-strip,
  .two-col {
    grid-template-columns: 1fr;
  }

  .module-tabs {
    gap: 6px;
    margin: 10px 0 14px;
  }

  .tab-button {
    justify-content: center;
    min-height: 44px;
    padding: 9px 10px;
  }

  .module-header {
    gap: 10px;
    margin: 8px 0 12px;
  }

  .segmented {
    justify-content: flex-start;
    overflow-x: auto;
  }

  .segment {
    flex: 0 0 auto;
    min-height: 42px;
  }

  .panel {
    padding: 12px;
  }

  .panel-title {
    align-items: flex-start;
    flex-wrap: wrap;
    min-height: 0;
  }

  .panel-actions {
    margin-left: auto;
  }

  .status-pill {
    white-space: normal;
  }

  .metric-card {
    min-height: 86px;
    padding: 12px;
  }

  .metric-value {
    font-size: 1.65rem;
  }

  .chart-frame {
    min-height: 300px;
    overflow: hidden;
  }

  .chart-frame svg {
    min-width: 0;
    width: 100%;
  }

  .chart-frame.compact {
    min-height: 250px;
  }

  .chart-frame.compact svg {
    min-width: 0;
    width: 100%;
  }

  textarea {
    min-height: 110px;
    max-height: 150px;
  }

  .table-scroll {
    margin: 0 -2px;
  }

  table {
    min-width: 560px;
    font-size: 0.88rem;
  }

  th,
  td {
    padding: 9px;
  }

  .upload-row,
  .button-row {
    align-items: stretch;
  }

  .upload-row .secondary-action,
  .button-row .secondary-action {
    min-height: 44px;
  }

  .helper-text {
    width: 100%;
  }

  .product-editor input,
  .product-name {
    width: 100%;
    min-width: 96px;
  }

  .export-preview {
    padding: 10px;
  }

  .export-preview-dialog {
    width: 100%;
    max-height: 94vh;
    padding: 12px;
  }

  .info-modal {
    padding: 10px;
  }

  .info-dialog {
    width: 100%;
    max-height: 94vh;
    padding: 12px;
  }
}
