:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #0b1117;
  --panel: #111923;
  --panel-2: #151f2b;
  --line: #283544;
  --line-strong: #3b4b5d;
  --text: #edf3f8;
  --muted: #96a6b7;
  --accent: #f0b34f;
  --accent-2: #6fcf97;
  --danger: #e56b6f;
  --info: #75a7e6;
  --canvas: #0f141d;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  margin: 0;
}

button, input, select {
  font: inherit;
}

.topbar {
  align-items: center;
  background: #0e151e;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 12px;
  height: 72px;
  justify-content: space-between;
  padding: 0 24px;
}

.brand,
.topbar-actions,
.language-control,
.panel-heading,
.primary-button,
.secondary-link,
.toggle,
.history-item header {
  align-items: center;
  display: flex;
}

.brand {
  min-width: 0;
}

.topbar-actions {
  gap: 10px;
}

.language-control {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 5px;
  gap: 8px;
  min-height: 38px;
  padding: 0 9px;
}

.language-control span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 750;
  text-transform: uppercase;
}

.language-control select {
  background: transparent;
  border: 0;
  color: var(--text);
  min-height: 30px;
  padding: 0 2px;
  width: auto;
}

.brand > div {
  min-width: 0;
}

.brand-mark {
  background: var(--accent);
  border-radius: 4px 1px 4px 1px;
  box-shadow: 0 0 0 4px rgba(240, 179, 79, .12);
  height: 18px;
  margin-right: 14px;
  transform: rotate(45deg);
  width: 18px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 17px; font-weight: 700; }
.brand p,
.panel-heading p {
  color: var(--muted);
  font-size: 11px;
  margin-top: 4px;
}

.brand p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 5px;
  color: #cfd9e4;
  cursor: pointer;
  display: inline-flex;
  height: 38px;
  justify-content: center;
  width: 38px;
}

svg {
  fill: none;
  height: 16px;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
  width: 16px;
}

.app-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: 360px minmax(0, 1fr) 300px;
  min-height: calc(100vh - 72px);
  padding: 14px;
  width: 100%;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  min-width: 0;
  width: 100%;
}

.controls-panel,
.result-panel,
.history-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.result-panel {
  overflow-y: auto;
}

.panel-heading {
  border-bottom: 1px solid var(--line);
  justify-content: space-between;
  min-height: 58px;
  padding: 14px 16px;
}

h2 {
  color: #dce6ef;
  font-size: 13px;
  font-weight: 750;
}

.state-pill,
.muted-count {
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  padding: 5px 8px;
  text-transform: uppercase;
  white-space: nowrap;
}

.pill-row {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.state-pill.is-ok {
  border-color: rgba(111, 207, 151, .45);
  color: var(--accent-2);
}

.state-pill.is-error {
  border-color: rgba(229, 107, 111, .48);
  color: var(--danger);
}

.compute-summary {
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 12px 16px;
}

.compute-summary-grid {
  display: grid;
  gap: 9px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

.compute-summary span,
.compute-device span {
  color: var(--muted);
  display: block;
  font-size: 10px;
  font-weight: 750;
  text-transform: uppercase;
}

.compute-summary strong,
.compute-device strong {
  color: #e8f0f7;
  display: block;
  font-size: 12px;
  font-weight: 760;
  margin-top: 4px;
  overflow-wrap: anywhere;
}

.compute-summary p {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
}

.compute-devices {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.compute-device {
  background: #0d141d;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 8px 10px;
}

.compute-device.is-usable {
  border-color: rgba(111, 207, 151, .38);
}

.compute-device.is-blocked {
  border-color: rgba(229, 107, 111, .34);
}

form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding: 16px;
}

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

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.field span,
.toggle span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.field-label,
.toggle-label {
  align-items: center;
  display: flex;
  gap: 6px;
  min-width: 0;
}

.help-tip {
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  color: #c9d6e2;
  cursor: help;
  display: inline-flex;
  flex: 0 0 auto;
  height: 15px;
  justify-content: center;
  outline: none;
  position: relative;
  width: 15px;
  z-index: 5;
}

.help-tip::before {
  content: "i";
  font-size: 10px;
  font-weight: 800;
  line-height: 13px;
  text-transform: none;
}

.help-tip::after {
  background: #0a1017;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .32);
  color: #dce7f1;
  content: attr(data-tip);
  font-size: 11px;
  font-weight: 600;
  left: 0;
  line-height: 1.35;
  opacity: 0;
  padding: 8px 9px;
  pointer-events: none;
  position: absolute;
  text-transform: none;
  top: calc(100% + 7px);
  transform: translateY(-2px);
  transition: opacity .12s ease, transform .12s ease;
  visibility: hidden;
  width: 220px;
  white-space: normal;
  z-index: 40;
}

.field-row .field:nth-child(2) .help-tip::after,
.toggle-row .toggle:nth-child(2) .help-tip::after {
  left: auto;
  right: 0;
}

.help-tip:hover,
.help-tip:focus-visible {
  border-color: rgba(240, 179, 79, .85);
  color: var(--accent);
}

.help-tip:hover::after,
.help-tip:focus-visible::after {
  opacity: 1;
  transform: translateY(0);
  visibility: visible;
}

input,
select {
  background: #0d141d;
  border: 1px solid var(--line);
  border-radius: 5px;
  color: var(--text);
  min-height: 36px;
  outline: none;
  padding: 0 10px;
  width: 100%;
}

input:focus,
select:focus {
  border-color: rgba(240, 179, 79, .85);
  box-shadow: 0 0 0 2px rgba(240, 179, 79, .14);
}

.language-control select {
  background: transparent;
  border: 0;
  min-height: 30px;
  padding: 0 2px;
  width: auto;
}

.language-control select:focus {
  box-shadow: none;
}

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

.toggle {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  gap: 8px;
  min-height: 36px;
  padding: 0 10px;
}

.toggle input {
  accent-color: var(--accent);
  height: 14px;
  min-height: 14px;
  padding: 0;
  width: 14px;
}

.primary-button,
.secondary-link {
  border-radius: 5px;
  font-size: 12px;
  font-weight: 750;
  gap: 8px;
  justify-content: center;
  min-height: 40px;
  text-decoration: none;
}

.primary-button {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #20160a;
  cursor: pointer;
}

.primary-button:disabled {
  cursor: wait;
  opacity: .68;
}

.secondary-link {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: #d8e0e9;
  min-width: 110px;
  padding: 0 12px;
}

.secondary-link.is-disabled {
  opacity: .45;
  pointer-events: none;
}

.result-actions,
.history-tools {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.status-box {
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  min-height: 38px;
  padding: 11px 16px;
}

.status-box.is-running { color: var(--info); }
.status-box.is-ok { color: var(--accent-2); }
.status-box.is-error { color: var(--danger); }

.progress-track {
  background: #0b1118;
  border-bottom: 1px solid var(--line);
  height: 8px;
  overflow: hidden;
}

.progress-bar {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  height: 100%;
  transition: width .22s ease;
  width: 0%;
}

.preview-frame {
  align-items: center;
  background: var(--canvas);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: center;
  min-height: 360px;
  overflow: hidden;
  position: relative;
}

#comparisonImage {
  display: none;
  height: 100%;
  max-height: 58vh;
  object-fit: contain;
  width: 100%;
}

.empty-preview {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.terrain-section,
.roi-section {
  border-bottom: 1px solid var(--line);
}

.subsection-heading {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 12px;
  justify-content: space-between;
  min-height: 56px;
  padding: 12px 14px;
}

h3 {
  color: #dce6ef;
  font-size: 12px;
  font-weight: 760;
  margin: 0;
}

.subsection-heading p {
  color: var(--muted);
  font-size: 11px;
  margin-top: 4px;
}

.terrain-controls {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.terrain-controls label {
  align-items: center;
  display: flex;
  gap: 6px;
}

.terrain-controls span,
.terrain-controls output {
  color: var(--muted);
  font-size: 10px;
  font-weight: 750;
  text-transform: uppercase;
}

.terrain-controls select {
  min-height: 30px;
  width: 112px;
}

.terrain-controls input[type="range"] {
  accent-color: var(--accent);
  min-height: 20px;
  padding: 0;
  width: 120px;
}

.terrain-number {
  min-height: 30px;
  padding: 0 7px;
  width: 76px;
}

.terrain-controls output {
  color: var(--accent);
  min-width: 45px;
}

.mini-button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  color: #d8e0e9;
  cursor: pointer;
  font-size: 11px;
  font-weight: 750;
  min-height: 30px;
  padding: 0 10px;
}

.mini-button:hover {
  border-color: rgba(240, 179, 79, .72);
  color: var(--accent);
}

.danger-button {
  border-color: rgba(255, 102, 102, .55);
  color: #ff9d9d;
}

.danger-button:hover {
  border-color: rgba(255, 102, 102, .85);
  color: #ffd0d0;
}

.diagnostic-section {
  border-bottom: 1px solid var(--line);
}

.diagnostic-frame {
  align-items: center;
  background: var(--canvas);
  display: flex;
  justify-content: center;
  min-height: 300px;
  overflow: hidden;
  padding: 10px;
}

#slopeImage {
  background: #ffffff;
  display: block;
  max-height: 58vh;
  object-fit: contain;
  width: 100%;
}

.embedded-terrain {
  background: #0b1018;
  height: min(58vh, 520px);
  min-height: 340px;
  overflow: hidden;
  position: relative;
}

.embedded-terrain canvas,
.roi-terrain canvas {
  display: block;
  height: 100%;
  width: 100%;
}

.terrain-loading,
.terrain-hint,
.roi-rank {
  background: rgba(14, 21, 30, .78);
  border: 1px solid rgba(40, 53, 68, .82);
  border-radius: 4px;
  color: #c8d3df;
  font-size: 10px;
  font-weight: 750;
  letter-spacing: .06em;
  padding: 7px 9px;
  text-transform: uppercase;
}

.terrain-loading {
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
}

.terrain-hint {
  bottom: 12px;
  left: 50%;
  pointer-events: none;
  position: absolute;
  transform: translateX(-50%);
  white-space: nowrap;
}

.roi-table {
  display: grid;
  grid-template-columns: 88px repeat(4, minmax(180px, 1fr));
  overflow-x: auto;
}

.roi-head,
.roi-rank-cell,
.roi-cell {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  min-width: 0;
}

.roi-head {
  background: #0d141d;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .08em;
  padding: 10px;
  text-transform: uppercase;
}

.roi-rank-cell {
  color: var(--muted);
  display: flex;
  flex-direction: column;
  font-size: 10px;
  gap: 8px;
  padding: 10px;
}

.roi-rank-cell strong {
  color: #edf3f8;
  font-size: 13px;
}

.roi-segments {
  color: var(--accent);
}

.roi-cell {
  background: #0b1018;
  min-height: 220px;
  padding: 8px;
  position: relative;
}

.roi-cell img {
  background: #ffffff;
  display: block;
  height: 220px;
  object-fit: contain;
  width: 100%;
}

.roi-terrain {
  background: #101520;
  cursor: grab;
  height: 220px;
  overflow: hidden;
  position: relative;
}

.roi-terrain.is-pending {
  align-items: center;
  cursor: default;
  display: flex;
  justify-content: center;
}

.roi-load-button {
  background: rgba(17, 25, 35, .92);
  border: 1px solid rgba(240, 179, 79, .6);
  border-radius: 5px;
  color: var(--accent);
  cursor: pointer;
  font-size: 11px;
  font-weight: 800;
  min-height: 32px;
  padding: 0 11px;
  text-transform: uppercase;
}

.roi-load-button:disabled {
  border-color: var(--line);
  color: var(--muted);
  cursor: wait;
}

.roi-terrain:active {
  cursor: grabbing;
}

.roi-rank {
  left: 8px;
  pointer-events: none;
  position: absolute;
  top: 8px;
}

.metrics-grid {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 14px;
}

.metric {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  min-height: 72px;
  padding: 11px;
}

.metric span {
  color: var(--muted);
  display: block;
  font-size: 10px;
  font-weight: 750;
  text-transform: uppercase;
}

.metric strong {
  color: #edf3f8;
  display: block;
  font-size: 18px;
  font-weight: 780;
  margin-top: 8px;
}

.history-panel {
  overflow: hidden;
}

.storage-state {
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 10px;
  font-weight: 750;
  margin: 0;
  padding: 0 12px 10px;
  text-transform: uppercase;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding: 12px;
}

.history-item {
  background: #0d141d;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: inherit;
  cursor: pointer;
  padding: 11px;
  text-align: left;
  width: 100%;
}

.history-item:hover,
.history-item.is-selected {
  border-color: rgba(240, 179, 79, .72);
}

.history-item header {
  justify-content: space-between;
}

.history-item b {
  color: #e3ebf3;
  font-size: 12px;
  font-weight: 750;
}

.history-item time,
.history-item p {
  color: var(--muted);
  font-size: 10px;
}

.history-item p {
  margin-top: 8px;
}

@media (max-width: 1180px) {
  .app-grid {
    grid-template-columns: 340px minmax(0, 1fr);
  }
  .history-panel {
    grid-column: 1 / -1;
    max-height: 280px;
  }
}

@media (max-width: 820px) {
  body {
    overflow-x: hidden;
  }
  .topbar {
    height: auto;
    min-height: 64px;
    max-width: 100vw;
    overflow-x: hidden;
    padding: 12px 14px;
  }
  .panel-heading {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-start;
  }
  .app-grid {
    grid-template-columns: 1fr;
    max-width: 100vw;
    overflow-x: hidden;
    padding: 8px;
  }
  .field-row,
  .toggle-row,
  .compute-summary-grid,
  .metrics-grid {
    grid-template-columns: 1fr;
  }
  .subsection-heading,
  .terrain-controls,
  .result-actions,
  .history-tools {
    align-items: flex-start;
    flex-direction: column;
    justify-content: flex-start;
  }
  .terrain-controls label,
  .terrain-controls select,
  .terrain-controls input[type="range"],
  .terrain-number {
    width: 100%;
  }
  .embedded-terrain {
    height: 320px;
    min-height: 280px;
  }
  .terrain-hint {
    max-width: 88%;
    text-align: center;
    white-space: normal;
  }
  .roi-table {
    display: block;
  }
  .roi-head {
    display: none;
  }
  .roi-rank-cell,
  .roi-cell {
    border-right: 0;
  }
  .preview-frame {
    min-height: 260px;
  }
}
