:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-soft: #f9faf7;
  --line: #d9ddd2;
  --line-strong: #bdc5b5;
  --text: #20231f;
  --muted: #646b60;
  --primary: #2f6f5e;
  --primary-soft: #e1eee9;
  --danger: #a33d4f;
  --danger-soft: #f4e2e6;
  --warn: #9d622b;
  --warn-soft: #f1e8dc;
  --good: #426b36;
  --shadow: 0 2px 8px rgba(32, 35, 31, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: "Avenir Next", "Helvetica Neue", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

button,
input,
select,
textarea {
  font: inherit;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 20px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  line-height: 1.2;
  font-weight: 650;
}

h2 {
  font-size: 15px;
  line-height: 1.25;
  font-weight: 650;
}

.topbar p {
  margin-top: 4px;
  color: var(--muted);
  overflow-wrap: anywhere;
}

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

button,
.file-button {
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  padding: 7px 11px;
  cursor: pointer;
}

button:hover,
.file-button:hover {
  border-color: var(--primary);
}

.file-button input {
  display: none;
}

.share-status {
  min-width: 74px;
  color: var(--muted);
  font-size: 12px;
}

main {
  padding: 24px 28px 32px;
}

.controls {
  display: grid;
  grid-template-columns: minmax(130px, 0.65fr) minmax(130px, 0.65fr) minmax(210px, 1.1fr) minmax(120px, 0.6fr) minmax(120px, 0.6fr) minmax(120px, 0.6fr) minmax(140px, 0.75fr);
  gap: 12px;
  align-items: end;
  margin-bottom: 16px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

input,
select,
textarea {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  padding: 7px 9px;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--primary) 22%, transparent);
  border-color: var(--primary);
}

[hidden] {
  display: none !important;
}

.group-controls {
  display: grid;
  grid-template-columns: minmax(190px, 0.35fr) minmax(0, 1fr);
  gap: 12px;
  align-items: start;
  margin: -4px 0 16px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.group-head {
  display: grid;
  gap: 8px;
}

textarea {
  min-height: 82px;
  resize: vertical;
  line-height: 1.4;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 12px;
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border: 1px solid var(--line);
  background: var(--surface);
  margin-bottom: 16px;
}

.summary-strip div {
  padding: 13px 16px;
  border-right: 1px solid var(--line);
}

.summary-strip div:last-child {
  border-right: 0;
}

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

.summary-strip strong {
  display: block;
  margin-top: 4px;
  font-size: 22px;
  line-height: 1.2;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(760px, 1.7fr) minmax(340px, 0.75fr);
  gap: 16px;
  align-items: start;
}

.table-panel,
.detail-panel,
.scatter-panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.panel-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-heading.tight {
  padding: 14px 0 8px;
  border-bottom: 0;
}

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

.table-wrap {
  width: 100%;
  overflow: auto;
  max-height: 590px;
}

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

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

td {
  font-size: 13px;
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover {
  background: var(--surface-soft);
}

tbody tr.active {
  background: var(--primary-soft);
}

.segment-name {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 650;
}

.segment-meta {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

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

.delta-high {
  color: var(--danger);
  font-weight: 650;
}

.delta-low {
  color: var(--good);
  font-weight: 650;
}

.delta-flat {
  color: var(--muted);
}

.detail-panel {
  padding-bottom: 16px;
}

.bar-chart {
  display: grid;
  gap: 9px;
  padding: 14px 16px 18px;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(90px, 120px) minmax(0, 1fr) 52px;
  gap: 10px;
  align-items: center;
}

.bar-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.bar-track {
  height: 12px;
  background: #eef0eb;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 0;
  background: var(--primary);
}

.bar-fill.warn {
  background: var(--warn);
}

.bar-fill.danger {
  background: var(--danger);
}

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

.prediction-panel {
  margin: 0 16px 16px;
  padding: 12px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
}

.prediction-head,
.prediction-main,
.prediction-grid {
  display: grid;
  gap: 4px;
}

.prediction-head {
  grid-template-columns: 1fr auto;
  align-items: baseline;
  margin-bottom: 10px;
}

.prediction-head span,
.prediction-grid span,
.prediction-main span {
  color: var(--muted);
  font-size: 12px;
}

.prediction-main strong {
  font-size: 28px;
  line-height: 1.15;
}

.prediction-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.prediction-grid strong {
  display: block;
  margin-top: 2px;
  font-size: 13px;
}

.curve-block {
  padding: 0 16px;
}

#curveChart {
  width: 100%;
  height: auto;
  border: 1px solid var(--line);
  background: var(--surface-soft);
}

.scatter-panel {
  margin-top: 16px;
}

.scatter-chart {
  position: relative;
  height: 320px;
  margin: 0 16px 16px;
  border: 1px solid var(--line);
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    var(--surface-soft);
  background-size: 100% 25%, 25% 100%;
}

.dot {
  position: absolute;
  width: 8px;
  height: 8px;
  transform: translate(-50%, 50%);
  border-radius: 50%;
  background: var(--primary);
  border: 1px solid var(--surface);
}

.dot.target {
  width: 12px;
  height: 12px;
  background: var(--danger);
}

.axis-label {
  position: absolute;
  color: var(--muted);
  font-size: 12px;
  background: color-mix(in srgb, var(--surface-soft) 90%, transparent);
  padding: 2px 4px;
}

.empty {
  padding: 28px 16px;
  color: var(--muted);
}

@media (max-width: 1160px) {
  .controls,
  .group-controls,
  .workspace {
    grid-template-columns: 1fr;
  }

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

  .summary-strip div:nth-child(2) {
    border-right: 0;
  }

  .summary-strip div:nth-child(-n + 2) {
    border-bottom: 1px solid var(--line);
  }
}

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

  main {
    padding: 16px;
  }

  .header-actions {
    justify-content: flex-start;
  }

  .summary-strip {
    grid-template-columns: 1fr;
  }

  .summary-strip div {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .summary-strip div:last-child {
    border-bottom: 0;
  }
}
