:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel-soft: #f8fafc;
  --text: #253345;
  --heading: #142033;
  --muted: #64748b;
  --line: #d8e0ea;
  --line-soft: #e7edf4;
  --accent: #0f766e;
  --accent-2: #3157a4;
  --amber: #b7791f;
  --danger: #b42318;
  --shadow: 0 10px 22px rgba(28, 38, 54, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.modal-open {
  overflow: hidden;
}

.app {
  width: min(1560px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 16px 0 28px;
}

.topbar,
.fc-querybar,
.dataset-panel,
.comparison-panel,
.controls,
.matrix-panel,
.results-panel,
.morph-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 13px 16px;
}

h1,
h2,
h3 {
  margin: 0;
  letter-spacing: 0;
  color: var(--heading);
}

h1 {
  font-size: 22px;
  font-weight: 740;
}

h2 {
  font-size: 15px;
  font-weight: 720;
}

h3 {
  font-size: 13px;
  font-weight: 720;
}

.muted {
  color: var(--muted);
  margin: 2px 0 0;
}

.top-actions,
.controls,
.inline-actions,
.lightbox-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.tabs {
  display: flex;
  gap: 6px;
  margin-top: 12px;
}

.tabs button {
  background: #fff;
}

.tabs button.active {
  background: var(--heading);
  border-color: var(--heading);
  color: #fff;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

label,
.view-field {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.view-field {
  border: 0;
  margin: 0;
  padding: 0;
}

.view-field legend {
  padding: 0;
}

.query-field {
  min-width: min(430px, 100%);
  flex: 1;
}

select,
input,
button {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

select,
input {
  padding: 0 10px;
}

button {
  padding: 0 12px;
  cursor: pointer;
  font-weight: 660;
}

button:hover,
button.active {
  border-color: var(--accent);
  color: var(--accent);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  min-width: 96px;
}

.pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 8px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.warn {
  color: var(--amber);
}

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

.panel-title.compact {
  margin-top: 12px;
  margin-bottom: 8px;
}

.fc-querybar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 148px auto auto;
  gap: 10px;
  align-items: end;
  margin-top: 12px;
  padding: 12px;
}

.recent-row {
  grid-column: 1 / -1;
  display: flex;
  gap: 6px;
  min-height: 30px;
  align-items: center;
  overflow-x: auto;
}

.recent-row button {
  height: 28px;
  border-color: var(--line-soft);
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.view-tabs {
  display: flex;
  gap: 4px;
}

.view-tabs button {
  height: 32px;
  min-width: 44px;
  padding: 0 9px;
  font-size: 12px;
}

.fc-review-grid {
  display: grid;
  grid-template-columns: minmax(340px, 430px) 1fr;
  gap: 12px;
  margin-top: 12px;
}

.dataset-panel,
.comparison-panel,
.matrix-panel,
.results-panel {
  min-width: 0;
  padding: 12px;
}

.dataset-panel {
  align-self: start;
  position: sticky;
  top: 112px;
  max-height: calc(100vh - 124px);
  overflow: auto;
}

.dataset-rail {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.dataset-tab {
  position: relative;
  display: grid;
  gap: 3px;
  align-content: start;
  height: auto;
  min-height: 82px;
  padding: 9px 10px 9px 34px;
  text-align: left;
  background: var(--panel-soft);
}

.dataset-tab strong,
.dataset-tab span {
  display: block;
}

.dataset-tab strong {
  color: var(--heading);
  font-size: 14px;
}

.dataset-tab span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
}

.dataset-tab .dataset-key {
  position: absolute;
  left: 9px;
  top: 9px;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #e8eef7;
  color: var(--accent-2);
  font-size: 11px;
}

.dataset-tab.active {
  background: color-mix(in srgb, var(--accent-2) 8%, var(--panel));
  border-color: color-mix(in srgb, var(--accent-2) 38%, var(--line));
  box-shadow: inset 3px 0 0 var(--accent-2);
}

.active-hit-list {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.hit-list {
  display: grid;
}

.hit-row {
  display: grid;
  grid-template-columns: 34px 68px minmax(0, 1fr) 64px 12px;
  gap: 8px;
  align-items: center;
  width: 100%;
  min-height: 76px;
  height: auto;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  color: var(--text);
  text-align: left;
}

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

.hit-row:hover {
  background: rgba(15, 118, 110, 0.06);
}

.hit-row.selected {
  background: color-mix(in srgb, var(--accent-2) 8%, var(--panel));
}

.hit-row.empty {
  grid-template-columns: 1fr;
  min-height: 44px;
  color: var(--muted);
  cursor: default;
}

.rank-chip {
  color: var(--muted);
  font-size: 12px;
  font-weight: 780;
}

.hit-thumb {
  width: 68px;
  height: 54px;
  object-fit: contain;
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: #fff;
}

.hit-thumb.missing {
  display: none;
}

.hit-main,
.hit-scores {
  min-width: 0;
}

.hit-id,
.hit-type,
.hit-method,
.hit-scores span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hit-id {
  display: block;
  font-weight: 700;
}

.hit-type {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.hit-method {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.hit-scores {
  display: grid;
  gap: 1px;
  justify-items: end;
  font-size: 11px;
}

.hit-scores strong {
  color: var(--heading);
  font-size: 13px;
}

.hit-scores span {
  color: var(--muted);
}

.morph-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--amber);
}

.morph-dot.ready {
  background: var(--accent);
}

.morph-comparison,
.compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.morph-card {
  padding: 10px;
  min-width: 0;
}

.morph-card.large {
  padding: 12px;
}

.morph-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 32px;
}

.image-button {
  position: relative;
  display: block;
  width: 100%;
  height: auto;
  padding: 0;
  margin-top: 8px;
  border: 0;
  background: transparent;
}

.image-button img,
.morph-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  display: block;
  background: #fdfdfc;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.image-button img:not([src]),
.morph-card img:not([src]) {
  visibility: hidden;
}

.morph-card.large .image-button img {
  aspect-ratio: 16 / 11;
  min-height: 330px;
}

img.missing {
  min-height: 180px;
  background:
    linear-gradient(135deg, rgba(100, 116, 139, 0.06), rgba(183, 121, 31, 0.08));
}

.image-status {
  position: absolute;
  left: 10px;
  bottom: 10px;
  max-width: calc(100% - 20px);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
}

.image-status:empty {
  display: none;
}

dl {
  display: grid;
  grid-template-columns: 82px 1fr;
  gap: 3px 8px;
  margin: 9px 0 0;
  font-size: 12px;
}

.info-grid {
  grid-template-columns: 78px 1fr;
}

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

dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.inline-actions {
  justify-content: flex-end;
  margin-top: 10px;
}

.inline-actions button {
  height: 32px;
  font-size: 12px;
}

.controls {
  align-items: end;
  margin-top: 12px;
  padding: 12px;
}

.grid {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 12px;
  margin-top: 12px;
}

.matrix {
  display: grid;
  grid-template-columns: 82px repeat(6, minmax(38px, 1fr));
  gap: 4px;
  overflow-x: auto;
}

.matrix-cell {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  place-items: center;
  padding: 4px;
  font-size: 11px;
  color: var(--muted);
  background: #fbfcfc;
}

.matrix-cell.ready {
  color: var(--accent);
  border-color: rgba(15, 118, 110, 0.28);
  background: rgba(15, 118, 110, 0.07);
  cursor: pointer;
}

.matrix-cell.head {
  background: transparent;
  border-color: transparent;
  color: var(--text);
  font-weight: 700;
}

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

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

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

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

tbody tr {
  cursor: pointer;
}

tbody tr:hover,
tbody tr.selected {
  background: rgba(49, 87, 164, 0.08);
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  grid-template-rows: auto 1fr;
  background: rgba(16, 24, 40, 0.92);
  color: #eef4fb;
}

.lightbox.open {
  display: grid;
}

.lightbox-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.lightbox-actions button {
  height: 32px;
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.lightbox-body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  min-height: 0;
  padding: 12px;
}

.lightbox-pane {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 8px;
  min-width: 0;
  min-height: 0;
  margin: 0;
}

.lightbox-pane figcaption {
  color: #cbd5e1;
  font-size: 13px;
  font-weight: 700;
}

.lightbox-image-frame {
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: #fff;
  cursor: grab;
}

.lightbox-image-frame:active {
  cursor: grabbing;
}

.lightbox-image-frame img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transform-origin: center center;
  will-change: transform;
}

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

  .dataset-panel {
    position: static;
    max-height: none;
  }

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

@media (max-width: 760px) {
  .app {
    width: min(100vw - 16px, 1560px);
    padding-top: 8px;
  }

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

  .fc-querybar {
    position: static;
    grid-template-columns: 1fr;
  }

  .dataset-rail,
  .morph-comparison,
  .compare,
  .lightbox-body {
    grid-template-columns: 1fr;
  }

  .hit-row {
    grid-template-columns: 28px 56px minmax(0, 1fr) 54px 10px;
    gap: 6px;
    padding: 8px;
  }

  .hit-thumb {
    width: 56px;
    height: 46px;
  }

  .morph-card.large .image-button img {
    min-height: 220px;
  }

  .lightbox-head {
    align-items: stretch;
    flex-direction: column;
  }
}
