:root {
  color-scheme: light;
  --bg: #f3f5f2;
  --panel: #ffffff;
  --panel-soft: #f9faf8;
  --text: #111827;
  --muted: #626b76;
  --line: #d9ddd7;
  --green: #2f9b5b;
  --teal: #117c7a;
  --amber: #d98722;
  --red: #c24135;
  --blue: #315f9d;
  --shadow: 0 18px 60px rgba(37, 48, 42, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 20% 0%, #ffffff 0, #f3f5f2 34%, #ecefed 100%);
  color: var(--text);
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1880px, calc(100vw - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 14px 0;
  display: grid;
  grid-template-columns: minmax(920px, 1.18fr) minmax(620px, 0.82fr);
  grid-template-areas:
    "topbar topbar"
    "kpis kpis"
    "controls timeline"
    "report report";
  gap: 8px 10px;
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--teal);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
  font-size: 12px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 36px;
  line-height: 1;
  letter-spacing: 0;
}

h2 {
  font-size: 16px;
}

.view-tabs {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.view-tabs button {
  height: 30px;
  padding: 0 12px;
}

.view-tabs button.active {
  border-color: #1f7a4d;
  background: #e9f5ee;
  color: #17683e;
}

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

.scenario-picker {
  min-width: 170px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.74);
}

.scenario-picker select {
  height: 28px;
  padding: 0;
  border: 0;
  background: transparent;
  font-weight: 800;
}

.month-pill {
  display: grid;
  gap: 2px;
  min-width: 190px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.74);
}

.month-pill span,
.kpi span,
label span,
.panel-head p,
.meter-row span,
.mini-grid span {
  color: var(--muted);
}

.simulation-view {
  display: contents;
}

.formula-view {
  grid-column: 1 / -1;
  grid-row: 2 / 5;
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(420px, 0.85fr);
  gap: 10px;
  min-height: 0;
}

.formula-view[hidden] {
  display: none;
}

.app-shell.formula-mode .kpi-grid,
.app-shell.formula-mode .control-layout,
.app-shell.formula-mode .timeline-panel,
.app-shell.formula-mode .report-bar {
  display: none;
}

.formula-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  max-height: calc(100vh - 155px);
  overflow: auto;
  padding-right: 2px;
}

.factor-row {
  gap: 3px;
  padding: 6px;
  border: 1px solid #e8ebe6;
  border-radius: 8px;
  background: var(--panel-soft);
}

.factor-row small {
  color: var(--muted);
  font-size: 11px;
}

.formula-list {
  display: grid;
  gap: 8px;
}

.formula-card {
  padding: 9px;
  border: 1px solid #e8ebe6;
  border-radius: 8px;
  background: var(--panel-soft);
}

.formula-card h3 {
  margin: 0 0 5px;
  font-size: 13px;
}

.formula-card code {
  white-space: normal;
  color: #26313d;
}

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

.kpi,
.panel,
.report-bar {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.kpi {
  min-height: 74px;
  padding: 10px 12px;
  display: grid;
  align-content: space-between;
}

.kpi strong {
  font-size: 24px;
  line-height: 1.05;
}

.kpi small {
  color: var(--muted);
}

.value-card {
  gap: 8px;
}

meter {
  width: 100%;
  height: 9px;
}

meter::-webkit-meter-bar {
  background: #e4e8e2;
  border: 0;
  border-radius: 999px;
}

meter::-webkit-meter-optimum-value {
  background: linear-gradient(90deg, var(--green), var(--teal));
  border-radius: 999px;
}

meter::-moz-meter-bar {
  background: linear-gradient(90deg, var(--green), var(--teal));
}

.control-layout {
  grid-area: controls;
  display: grid;
  grid-template-columns: minmax(430px, 1.32fr) minmax(200px, 0.68fr) minmax(210px, 0.7fr);
  gap: 8px;
  align-items: stretch;
}

.panel {
  padding: 7px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 12px;
  margin-bottom: 8px;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--text);
  cursor: pointer;
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

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

label {
  display: grid;
  gap: 5px;
  min-width: 0;
}

input,
select {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: var(--panel-soft);
  color: var(--text);
}

input:focus,
select:focus,
button:focus-visible {
  outline: 3px solid rgba(49, 95, 157, 0.18);
  border-color: var(--blue);
}

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

.automation-panel {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.automation-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 7px;
}

.automation-head h3 {
  margin: 0;
  font-size: 14px;
}

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

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

.automation-row {
  display: grid;
  grid-template-columns: minmax(92px, 1fr) minmax(86px, 0.8fr) minmax(104px, 0.8fr);
  gap: 6px;
  align-items: center;
  padding: 5px;
  border: 1px solid #e8ebe6;
  border-radius: 8px;
  background: var(--panel-soft);
}

.automation-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
}

.automation-toggle input {
  width: 16px;
  height: 16px;
}

.automation-row input,
.automation-row select {
  height: 28px;
}

button {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 15px;
  background: var(--panel-soft);
  cursor: pointer;
  font-weight: 700;
  touch-action: manipulation;
}

button:hover {
  border-color: #b7c0b5;
}

.primary {
  border-color: #1f7a4d;
  background: var(--green);
  color: #fff;
}

.company-panel,
.closing-panel {
  display: grid;
  align-content: start;
  gap: 8px;
}

.meter-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
}

.meter-row meter {
  grid-column: 1 / -1;
}

.mini-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding-top: 4px;
}

.mini-grid div {
  display: grid;
  gap: 2px;
}

dl {
  display: grid;
  gap: 6px;
  margin: 0;
}

.closing-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 6px;
  border-bottom: 1px solid #e8ebe6;
}

dt {
  color: var(--text);
}

dd {
  margin: 0;
  font-weight: 800;
  text-align: right;
}

.timeline-panel {
  grid-area: timeline;
  margin-top: 0;
  min-height: 0;
}

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

.chart-card {
  min-height: 76px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  padding: 7px;
  display: grid;
  grid-template-rows: auto 1fr;
}

.chart-title {
  display: flex;
  font-size: 12px;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
}

.chart-title strong {
  color: var(--text);
  white-space: nowrap;
}

.chart-value {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}

.trend-badge {
  min-width: 48px;
  padding: 1px 5px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
}

.trend-badge.up {
  background: #e9f5ee;
  color: #17683e;
}

.trend-badge.down {
  background: #fdebe9;
  color: #9c2f25;
}

.trend-badge.flat {
  background: #eef0ed;
  color: var(--muted);
}

.sparkline {
  width: 100%;
  height: 38px;
  align-self: end;
}

.sparkline path.line {
  fill: none;
  stroke: var(--green);
  stroke-width: 3;
  vector-effect: non-scaling-stroke;
}

.sparkline.up path.line {
  stroke: var(--green);
}

.sparkline.down path.line {
  stroke: var(--red);
}

.sparkline.flat path.line {
  stroke: var(--muted);
}

.sparkline.up path.area {
  fill: rgba(47, 155, 91, 0.08);
}

.sparkline.down path.area {
  fill: rgba(194, 65, 53, 0.08);
}

.sparkline.flat path.area {
  fill: rgba(98, 107, 118, 0.08);
}

.sparkline line {
  stroke: #dde2dc;
  stroke-width: 1;
}

.report-bar {
  grid-area: report;
  display: flex;
  gap: 14px;
  align-items: center;
  min-height: 42px;
  margin-top: 0;
  padding: 8px 10px;
}

.report-bar strong {
  padding: 6px 10px;
  border-radius: 8px;
  background: #e9f5ee;
  color: #17683e;
}

.report-bar.warning strong {
  background: #fff3dd;
  color: #8c5309;
}

.report-bar.danger strong {
  background: #fdebe9;
  color: #9c2f25;
}

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

  .control-layout {
    grid-template-columns: 1fr;
  }

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

@media (max-width: 620px) {
  .app-shell {
    width: min(100vw - 20px, 1280px);
    padding-top: 14px;
  }

  .topbar,
  .actions,
  .report-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .kpi-grid,
  .field-grid,
  .chart-grid,
  .mini-grid,
  .automation-grid,
  .automation-row {
    grid-template-columns: 1fr;
  }

  .top-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .formula-grid,
  .scenario-picker,
  .month-pill {
    min-width: 0;
  }
}

@media (max-width: 1599px) {
  .app-shell {
    width: min(1280px, calc(100vw - 28px));
    min-height: auto;
    display: block;
  }

  .kpi-grid,
  .control-layout,
  .timeline-panel,
  .report-bar {
    margin-top: 10px;
  }
}

@media (max-width: 620px) {
  body {
    background: #f3f5f2;
    font-size: 13px;
  }

  .app-shell {
    width: min(100vw - 16px, 430px);
    padding: 10px 0 max(16px, env(safe-area-inset-bottom));
  }

  .topbar {
    display: grid;
    gap: 10px;
  }

  h1 {
    font-size: 28px;
    line-height: 1.04;
  }

  h2 {
    font-size: 15px;
  }

  .view-tabs,
  .top-actions,
  .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .view-tabs button,
  button,
  input,
  select {
    min-height: 40px;
  }

  .scenario-picker,
  .month-pill,
  .panel,
  .report-bar {
    box-shadow: none;
  }

  .scenario-picker,
  .month-pill {
    padding: 9px 10px;
  }

  .kpi-grid,
  .mini-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .kpi {
    min-height: 82px;
    padding: 9px;
  }

  .kpi strong {
    font-size: 19px;
    overflow-wrap: anywhere;
  }

  .field-grid,
  .automation-grid,
  .chart-grid,
  .formula-grid,
  .formula-view {
    grid-template-columns: 1fr;
  }

  .field-grid {
    gap: 7px;
  }

  .actions button {
    width: 100%;
    padding: 0 8px;
    white-space: normal;
  }

  .automation-head,
  .panel-head {
    gap: 8px;
  }

  .automation-head {
    display: grid;
  }

  .automation-row {
    grid-template-columns: minmax(88px, 1fr) minmax(76px, 0.78fr) minmax(98px, 0.86fr);
    padding: 6px;
  }

  .automation-row input,
  .automation-row select {
    min-height: 36px;
  }

  .chart-card {
    min-height: 70px;
  }

  .chart-title {
    gap: 8px;
  }

  .chart-value {
    justify-content: end;
  }

  .closing-row {
    gap: 10px;
  }

  .report-bar {
    display: grid;
    gap: 8px;
    line-height: 1.35;
  }
}

@media (max-width: 430px) {
  .app-shell {
    width: min(100vw - 12px, 390px);
  }

  .top-actions {
    grid-template-columns: 1fr;
  }

  .panel {
    padding: 8px;
  }

  .automation-row {
    grid-template-columns: 1fr 92px;
  }

  .automation-row select {
    grid-column: 1 / -1;
  }
}
