:root {
  color-scheme: light;
  --bg: #f5f6f2;
  --surface: #ffffff;
  --surface-soft: #f8faf7;
  --line: #d9ded6;
  --line-strong: #b8c1b4;
  --text: #1f2933;
  --muted: #65717a;
  --teal: #0f766e;
  --teal-dark: #115e59;
  --amber: #b45309;
  --red: #b42318;
  --red-soft: #fff0ee;
  --shadow: 0 18px 45px rgba(28, 37, 30, 0.11);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button,
input,
textarea {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: background 0.16s ease, border-color 0.16s ease, transform 0.16s ease;
}

button:hover {
  background: #eef2ed;
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: wait;
  opacity: 0.64;
}

button.primary {
  background: var(--teal);
  border-color: var(--teal);
  color: #ffffff;
}

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

button.ghost {
  background: transparent;
}

button.danger {
  border-color: #f2b6ae;
  color: var(--red);
  background: var(--red-soft);
}

button svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.wide {
  width: 100%;
}

input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0 11px;
  color: var(--text);
  background: #ffffff;
}

input::placeholder,
textarea::placeholder {
  color: #8b97a1;
}

textarea {
  width: 100%;
  min-height: 84px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 10px 11px;
  color: var(--text);
  background: #ffffff;
  resize: vertical;
  line-height: 1.45;
}

input:focus,
textarea:focus {
  outline: 3px solid rgba(15, 118, 110, 0.18);
  border-color: var(--teal);
}

label span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
}

.auth-shell {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 28px;
  background:
    linear-gradient(135deg, rgba(15, 118, 110, 0.14), transparent 42%),
    linear-gradient(315deg, rgba(180, 83, 9, 0.11), transparent 38%),
    var(--bg);
}

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

.auth-form {
  margin-top: 28px;
  display: grid;
  gap: 16px;
}

.form-error {
  min-height: 20px;
  margin: 0;
  color: var(--red);
}

.app-shell {
  min-height: 100%;
  display: grid;
  grid-template-rows: 72px 1fr;
}

.topbar {
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  display: grid;
  grid-template-columns: auto minmax(220px, 1fr) auto;
  align-items: center;
  gap: 18px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 13px;
}

.brand.compact {
  align-items: flex-start;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #1f2933;
  color: #ffffff;
  font-weight: 800;
  letter-spacing: 0;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.05;
}

.brand span,
.account span {
  color: var(--muted);
  font-size: 13px;
}

.account {
  display: flex;
  align-items: center;
  gap: 12px;
}

.main-nav {
  justify-self: center;
  display: inline-flex;
  padding: 3px;
  background: #e6ebe4;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.main-tab {
  border: 0;
  min-height: 34px;
  min-width: 112px;
  background: transparent;
  color: var(--muted);
}

.main-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08);
}

.app-view {
  display: none;
}

.app-view.active {
  display: block;
}

.work-layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: 0;
}

.scenario-pane {
  border-right: 1px solid var(--line);
  background: #eef1eb;
  padding: 16px;
  min-height: calc(100vh - 72px);
}

.pane-toolbar,
.toolbar,
.action-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.pane-toolbar {
  margin-bottom: 14px;
}

.session-toolbar {
  margin-top: -6px;
}

.pane-toolbar button {
  flex: 1;
}

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

.scenario-card {
  width: 100%;
  display: block;
  text-align: left;
  border-radius: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.scenario-card.active {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.scenario-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-weight: 700;
}

.scenario-meta {
  margin-top: 7px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.status-pill {
  border-radius: 999px;
  padding: 2px 7px;
  background: #e5ebe4;
  color: #334155;
}

.status-pill.ready {
  background: #d7f2e8;
  color: #07594d;
}

.workspace {
  min-width: 0;
  padding: 18px 22px 32px;
}

.report-workspace,
.admin-workspace {
  max-width: 1180px;
  margin: 0 auto;
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

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

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

#resultsPanel {
  margin-top: 18px;
}

.scenario-form,
.report-form {
  display: grid;
  gap: 16px;
}

.form-grid {
  display: grid;
  gap: 14px;
}

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

.form-grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.form-grid.four {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.form-grid.five {
  grid-template-columns: 1.1fr repeat(4, minmax(120px, 1fr));
}

.section-band {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.compact-band {
  padding: 12px 16px;
}

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

.section-head h2,
.legend-panel h2 {
  margin: 0;
  font-size: 16px;
}

.compact-tools button {
  min-height: 34px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 540px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
  vertical-align: middle;
}

th {
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

td input {
  height: 34px;
}

.editable-grid {
  min-width: 540px;
}

.editable-grid-head,
.element-row {
  display: grid;
  grid-template-columns: minmax(160px, 1.15fr) minmax(150px, 1fr) minmax(150px, 0.85fr) minmax(160px, 0.85fr) 46px;
  gap: 8px;
  align-items: center;
}

.editable-grid-head {
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 9px 8px;
  border-bottom: 1px solid var(--line);
}

.element-row {
  padding: 8px;
  border-bottom: 1px solid var(--line);
}

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

.row-remove {
  width: 38px;
  padding: 0;
  color: var(--red);
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.segmented label {
  cursor: pointer;
}

.segmented input {
  display: none;
}

.segmented span {
  margin: 0;
  padding: 8px 13px;
  color: var(--muted);
}

.report-mode-control span {
  min-width: 112px;
  text-align: center;
}

.report-table-options {
  margin: 14px 0;
}

.segmented input:checked + span {
  background: var(--teal);
  color: #ffffff;
}

.checkbox-label {
  min-height: 40px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding-top: 21px;
}

.checkbox-label input,
.source-checks input {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.checkbox-label span,
.source-checks span {
  margin: 0;
}

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

.source-checks label {
  display: flex;
  align-items: center;
  gap: 9px;
}

.source-fields {
  display: grid;
  gap: 12px;
}

.file-chip {
  min-height: 22px;
  color: var(--muted);
  font-size: 13px;
}

.results-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: 16px;
}

.chart-shell,
.legend-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.chart-shell {
  min-height: 460px;
  padding: 12px;
}

#resultsCanvas {
  width: 100%;
  height: 436px;
  display: block;
}

.legend-panel {
  padding: 14px;
}

.chart-legend {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  max-height: 410px;
  overflow: auto;
}

.legend-item {
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}

.legend-swatch {
  width: 14px;
  height: 4px;
  border-radius: 3px;
}

.series-color-0 { background: #0f766e; }
.series-color-1 { background: #b45309; }
.series-color-2 { background: #2563eb; }
.series-color-3 { background: #be123c; }
.series-color-4 { background: #6d28d9; }
.series-color-5 { background: #15803d; }
.series-color-6 { background: #92400e; }
.series-color-7 { background: #475569; }

.results-table-wrap {
  margin-top: 16px;
  background: var(--surface);
}

.results-table {
  min-width: 760px;
}

.admin-form {
  display: grid;
  gap: 14px;
}

.admin-table-wrap {
  margin-top: 16px;
  background: var(--surface);
}

.admin-table {
  min-width: 860px;
}

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

.admin-actions button {
  min-height: 32px;
  padding: 0 10px;
}

.role-pill,
.access-pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 0 8px;
  background: #e5ebe4;
  color: #334155;
  font-size: 12px;
}

.role-pill.admin,
.access-pill.enabled {
  background: #d7f2e8;
  color: #07594d;
}

.access-pill.disabled {
  background: var(--red-soft);
  color: var(--red);
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  max-width: min(420px, calc(100vw - 44px));
  background: #172027;
  color: #ffffff;
  border-radius: 8px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 20;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.empty-state {
  color: var(--muted);
  padding: 18px 12px;
  text-align: center;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-rows: auto 1fr;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: flex-start;
    gap: 14px;
    padding: 14px 16px;
  }

  .main-nav {
    justify-self: stretch;
    width: 100%;
  }

  .main-tab {
    flex: 1;
  }

  .work-layout,
  .results-grid {
    grid-template-columns: 1fr;
  }

  .scenario-pane {
    min-height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .form-grid.two,
  .form-grid.three,
  .form-grid.four,
  .form-grid.five {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 680px) {
  .topbar,
  .section-head,
  .account,
  .panel-title-row {
    flex-direction: column;
    align-items: stretch;
  }

  .workspace {
    padding: 14px;
  }

}
