:root {
  color-scheme: light;
  --bg: #f5f7f6;
  --panel: #ffffff;
  --panel-soft: #eef4f1;
  --ink: #16201f;
  --muted: #60706c;
  --line: #d8e0dc;
  --line-strong: #aebbb6;
  --teal: #087f7a;
  --teal-strong: #04615d;
  --red: #d44b37;
  --blue: #2869b8;
  --amber: #b58016;
  --green: #277845;
  --shadow: 0 18px 45px rgba(31, 44, 41, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    linear-gradient(180deg, #fbfcfc 0, var(--bg) 340px),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.shell {
  min-height: 100vh;
  padding: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 18px;
  max-width: 1560px;
  margin: 0 auto 12px;
}

.top-actions,
.chart-toolbar,
.section-title,
.tabs,
.page-tabs,
.settings-tabs {
  display: flex;
  align-items: center;
}

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

.hidden {
  display: none !important;
}

#fileInput,
#folderInput {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.button,
.icon-button,
.text-button,
.tab,
.page-tab,
.settings-tab {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  min-height: 36px;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  padding: 0 13px;
  font-weight: 720;
  text-decoration: none;
}

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

.button:hover,
.icon-button:hover,
.tab:hover,
.page-tab:hover,
.settings-tab:hover {
  border-color: var(--line-strong);
  box-shadow: 0 8px 20px rgba(16, 35, 32, 0.08);
}

.button.primary:hover {
  border-color: var(--teal-strong);
  background: var(--teal-strong);
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 36px;
  border-radius: 6px;
  font-weight: 800;
}

.text-button {
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--teal);
  font-weight: 740;
}

.page-tabs {
  gap: 6px;
  max-width: 1560px;
  margin: 0 auto 18px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  width: max-content;
}

.settings-tabs {
  gap: 6px;
  max-width: 1560px;
  margin: 0 0 18px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  width: max-content;
}

.page-tab,
.settings-tab {
  min-height: 33px;
  border-radius: 6px;
  padding: 0 15px;
  font-weight: 760;
}

.page-tab.active,
.settings-tab.active {
  border-color: var(--teal);
  background: var(--teal);
  color: white;
}

.app-page {
  display: none;
}

.app-page.active {
  display: grid;
}

.settings-section {
  display: none;
}

.settings-section.active {
  display: grid;
}

.workspace {
  grid-template-columns: minmax(315px, 385px) minmax(0, 1fr);
  gap: 18px;
  max-width: 1560px;
  margin: 0 auto;
}

.control-panel,
.analysis-panel {
  min-width: 0;
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.control-section,
.stats-strip,
.chart-wrap,
.data-grid {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.control-section {
  border-radius: 8px;
  padding: 14px;
}

.section-title {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.section-title h2 {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.1;
}

.section-title span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 720;
}

select,
input[type="number"] {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfdfc;
  color: var(--ink);
  padding: 0 9px;
}

select:focus,
input:focus,
button:focus-visible,
.button:focus-visible {
  outline: 2px solid rgba(8, 127, 122, 0.25);
  outline-offset: 2px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 11px;
  color: var(--ink);
  font-size: 0.82rem;
}

.check-row input {
  width: 17px;
  height: 17px;
  accent-color: var(--teal);
}

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

.lap-summary {
  margin: -2px 0 12px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 650;
}

.lap-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
}

.lap-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfdfc;
  color: var(--ink);
  font-size: 0.82rem;
}

.lap-row.selected {
  border-color: rgba(8, 127, 122, 0.45);
  background: var(--panel-soft);
}

.lap-row input {
  width: 17px;
  height: 17px;
  accent-color: var(--teal);
}

.lap-copy {
  min-width: 0;
}

.lap-copy strong,
.lap-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lap-copy small {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 650;
}

.lap-badge {
  min-width: 42px;
  border-radius: 999px;
  background: rgba(8, 127, 122, 0.11);
  color: var(--teal-strong);
  padding: 3px 7px;
  text-align: center;
  font-size: 0.67rem;
  font-weight: 820;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.lap-badge.muted {
  background: #eef0ee;
  color: var(--muted);
}

.lap-badge.manual {
  background: rgba(181, 128, 22, 0.14);
  color: #8a6415;
}

.run-item {
  display: grid;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfdfc;
  padding: 7px;
}

.run-item.active {
  border-color: rgba(8, 127, 122, 0.7);
  background: var(--panel-soft);
}

.run-item.hidden-run {
  opacity: 0.62;
}

.run-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.run-select {
  width: 100%;
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  padding: 4px 3px;
  text-align: left;
}

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

.run-action {
  min-height: 29px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--ink);
  font-size: 0.74rem;
  font-weight: 760;
}

.run-action:hover {
  border-color: var(--line-strong);
}

.run-action.danger {
  color: #9f2f21;
}

.run-action.support {
  color: var(--teal);
}

.run-action:disabled {
  cursor: wait;
  opacity: 0.62;
}

.run-name {
  display: block;
  overflow: hidden;
  color: var(--ink);
  font-weight: 760;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-meta {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 650;
}

.color-picker {
  position: relative;
  display: grid;
  place-items: center;
  width: 30px;
  min-height: 42px;
  cursor: pointer;
}

.color-picker input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.color-chip {
  width: 12px;
  height: 30px;
  border-radius: 6px;
  background: var(--run-color);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.color-picker:hover .color-chip,
.color-picker:focus-within .color-chip {
  box-shadow: 0 0 0 3px rgba(8, 127, 122, 0.14);
  transform: scale(1.08);
}

.analysis-panel {
  display: grid;
  align-content: start;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(420px, 58vh) auto;
  gap: 12px;
}

.stats-strip,
.chart-toolbar,
.chart-wrap,
.data-grid,
.table-scroll {
  min-width: 0;
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 1px;
  overflow: hidden;
  border-radius: 8px;
  background: var(--line);
}

.stat {
  min-width: 0;
  background: var(--panel);
  padding: 14px 16px;
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.73rem;
  font-weight: 760;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.stat strong {
  display: block;
  margin-top: 5px;
  font-size: clamp(1rem, 1.55vw, 1.5rem);
  letter-spacing: 0;
  white-space: nowrap;
}

.chart-toolbar {
  justify-content: space-between;
  gap: 10px;
}

.tabs {
  gap: 6px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.tab {
  min-height: 31px;
  border-radius: 6px;
  padding: 0 11px;
  font-size: 0.82rem;
  font-weight: 740;
}

.tab.active {
  border-color: var(--teal);
  background: var(--teal);
  color: white;
}

.chart-wrap {
  position: relative;
  min-height: 420px;
  overflow: hidden;
  border-radius: 8px;
}

#dynoChart {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 420px;
  background: linear-gradient(180deg, #ffffff 0%, #fbfcfb 100%);
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}

.empty-state.hidden {
  display: none;
}

.empty-state h2 {
  margin: 0;
  color: var(--ink);
  font-size: 1.2rem;
  letter-spacing: 0;
}

.empty-state p {
  max-width: 440px;
  margin: 8px auto 0;
  line-height: 1.5;
}

.axis-label,
.tick-label {
  fill: #61716d;
  font-size: 12px;
  font-weight: 650;
}

.grid-line {
  stroke: #e6ece9;
  stroke-width: 1;
}

.axis-line {
  stroke: #a8b6b0;
  stroke-width: 1.3;
}

.power-line {
  fill: none;
  stroke: var(--red);
  stroke-width: 2.8;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.torque-line {
  fill: none;
  stroke: var(--blue);
  stroke-width: 2.4;
  stroke-linejoin: round;
  stroke-linecap: round;
  stroke-dasharray: 8 6;
}

.raw-dot {
  fill: rgba(39, 120, 69, 0.22);
  stroke: rgba(39, 120, 69, 0.58);
  stroke-width: 1;
}

.legend text {
  fill: var(--ink);
  font-size: 12px;
  font-weight: 720;
}

.data-grid {
  border-radius: 8px;
  padding: 14px;
}

.settings-page {
  max-width: 1560px;
  margin: 0 auto;
  align-content: start;
}

.settings-grid {
  display: grid;
  grid-template-columns: minmax(300px, 390px) minmax(0, 1fr);
  gap: 18px;
  width: 100%;
}

.settings-section:not(.active) {
  display: none;
}

.settings-card {
  align-self: start;
}

.settings-card.wide {
  grid-column: 1 / -1;
}

.table-scroll {
  max-height: 280px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  white-space: nowrap;
}

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #eef4f1;
  color: #40504d;
  font-size: 0.73rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

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

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

.message {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

@media (max-width: 1120px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .control-panel {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .analysis-panel {
    grid-template-rows: auto auto minmax(390px, 56vh) auto;
  }

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

@media (max-width: 760px) {
  .shell {
    padding: 12px;
  }

  .topbar,
  .chart-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .tabs,
  .page-tabs,
  .settings-tabs {
    justify-content: stretch;
  }

  .page-tabs,
  .settings-tabs {
    width: 100%;
  }

  .chart-toolbar .button {
    width: 100%;
  }

  .tabs {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
  }

  .button,
  .tab,
  .page-tab,
  .settings-tab {
    flex: 1;
  }

  .tab {
    min-width: 0;
    padding: 0 6px;
    white-space: normal;
    line-height: 1.1;
  }

  .control-panel,
  .field-grid,
  .stats-strip {
    grid-template-columns: 1fr;
  }

  .analysis-panel {
    grid-template-rows: auto auto minmax(340px, 50vh) auto;
  }

  #dynoChart,
  .chart-wrap {
    min-height: 340px;
  }
}

@media print {
  body {
    background: white;
  }

  .shell {
    padding: 0;
  }

  .top-actions,
  .control-panel,
  .chart-toolbar {
    display: none;
  }

  .workspace {
    display: block;
    max-width: none;
  }

  .analysis-panel {
    display: block;
  }

  .stats-strip,
  .chart-wrap,
  .data-grid {
    box-shadow: none;
    break-inside: avoid;
    margin-bottom: 12px;
  }
}
