:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #19202d;
  --muted: #647084;
  --line: #d7dde7;
  --line-strong: #aeb8c8;
  --accent: #1f5f8b;
  --accent-soft: #e1effc;
  --danger: #a94732;
  --ok: #2f855a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px 1fr;
}

.sidebar {
  background: #101827;
  color: #eef4fb;
  padding: 22px 18px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
}

.brand {
  font-weight: 700;
  font-size: 18px;
  margin-bottom: 18px;
}

.side-link {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: #d7e3f2;
  padding: 9px 10px;
  border-radius: 6px;
  margin: 2px 0;
}

.side-link:hover,
.side-link.active {
  background: #1d2a3d;
  text-decoration: none;
}

.side-link span:last-child {
  color: #95a7bd;
  white-space: nowrap;
}

.side-section {
  margin: 14px 8px 6px;
  color: #95a7bd;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.content {
  padding: 24px;
  min-width: 0;
}

.content.analysis-page {
  padding: 16px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 18px;
}

.analysis-page .topbar {
  margin-bottom: 10px;
}

h1 {
  margin: 0 0 6px;
  font-size: 28px;
}

.analysis-page h1 {
  font-size: 24px;
}

h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.muted {
  color: var(--muted);
}

.grid {
  display: grid;
  gap: 16px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 13px 14px;
}

.stat .value {
  font-weight: 700;
  font-size: 22px;
  margin-top: 4px;
}

.compact-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.compact-stats .stat {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  border-radius: 999px;
  padding: 4px 9px;
}

.compact-stats .stat .value {
  font-size: 13px;
  margin-top: 0;
}

.analysis-page .compact-stats {
  display: none;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  min-width: 0;
}

.dataset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
}

.dataset-card {
  display: block;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  color: var(--ink);
}

.dataset-card:hover {
  border-color: var(--accent);
  text-decoration: none;
}

.dataset-card .title {
  font-weight: 700;
  margin-bottom: 6px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  color: #334155;
  background: #f8fafc;
}

.warning {
  color: var(--danger);
}

.load-error {
  max-width: 760px;
}

.load-error p {
  margin: 8px 0;
}

.load-error pre {
  overflow: auto;
  margin: 10px 0 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f3f5f8;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

button,
select,
input[type="search"] {
  font: inherit;
}

button {
  border: 1px solid var(--line-strong);
  background: #ffffff;
  color: var(--ink);
  border-radius: 6px;
  padding: 7px 11px;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.primary-button {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

input[type="search"] {
  min-width: 260px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 8px 10px;
}

.chart-wrap {
  position: relative;
  height: 520px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  overflow: hidden;
}

canvas.chart {
  width: 100%;
  height: 100%;
  display: block;
}

.tooltip {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(12px, 12px);
  display: none;
  pointer-events: none;
  max-width: min(420px, calc(100% - 28px));
  background: rgba(15, 23, 42, 0.94);
  color: #ffffff;
  border-radius: 7px;
  padding: 9px 10px;
  font-size: 12px;
  line-height: 1.42;
  z-index: 5;
  box-shadow: 0 8px 20px rgba(0,0,0,.18);
}

.tooltip .tip-title {
  font-weight: 700;
  margin-bottom: 4px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin-top: 10px;
  font-size: 13px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 22px;
  height: 3px;
  border-radius: 3px;
  display: inline-block;
}

.table-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: 0 0 10px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  max-height: 560px;
  background: #ffffff;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  border-bottom: 1px solid #e4e8ef;
  padding: 7px 9px;
  text-align: left;
  white-space: nowrap;
}

th {
  background: #eef2f7;
  position: sticky;
  top: 0;
  z-index: 2;
}

td.num,
th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.series-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 8px;
  max-height: 280px;
  overflow: auto;
  padding-right: 4px;
}

.series-control {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px;
  background: #fbfcfe;
}

.series-control input {
  margin: 0;
}

.series-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.matrix-table table {
  width: max-content;
  min-width: 0;
}

.matrix-table th,
.matrix-table td {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.matrix-table th:first-child,
.matrix-table td:first-child {
  min-width: 142px;
  max-width: 142px;
}

.matrix-table th.num,
.matrix-table td.num {
  min-width: 92px;
  max-width: 124px;
}

.analysis-workspace {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.analysis-main {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.analysis-main .panel {
  padding: 10px;
}

.analysis-main .grid {
  gap: 8px;
}

.analysis-page .chart-wrap {
  height: min(50vh, 520px);
  min-height: 400px;
}

.measurement-panel {
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
}

.measurement-panel .button-row {
  align-items: flex-start;
}

.measurement-panel .series-controls {
  grid-template-columns: 1fr;
  max-height: none;
  min-height: 0;
  overflow: auto;
}

.measurement-panel .series-control {
  grid-template-columns: auto minmax(0, 1fr) auto;
}

.measurement-panel .series-control .series-name + .chip {
  display: none;
}

.measurement-panel .series-name {
  font-size: 14px;
}

.image-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.image-stage {
  position: relative;
  height: min(68vh, 720px);
  min-height: 420px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}

.image-stage.grabbing {
  cursor: grabbing;
}

.image-stage img {
  position: absolute;
  top: 0;
  left: 0;
  max-width: none;
  transform-origin: 0 0;
  user-select: none;
  pointer-events: none;
}

.zoom-readout {
  min-width: 58px;
  text-align: center;
}

.empty-state {
  padding: 24px;
  color: var(--muted);
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: static;
    height: auto;
  }
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .topbar,
  .table-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .analysis-workspace {
    grid-template-columns: 1fr;
  }
  .measurement-panel {
    position: static;
    max-height: 360px;
  }
  .measurement-panel .series-controls {
    max-height: 230px;
  }
}
