:root {
  --bg: #0b0d11;
  --panel: #14171d;
  --border: #262b33;
  --text: #e6edf3;
  --muted: #9aa7b5;
  --accent: #4a9eff;
  --accent-awb: #7eb6ff;
  --surface-2: #1c2129;
  --header-control-h: 32px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 18px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex: 0 0 auto;
}

.db-chips {
  flex-basis: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.db-chips__empty {
  font-size: 12px;
  color: var(--muted);
}

.db-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 11px 4px 9px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.db-chip:hover { border-color: var(--muted); }

.db-chip__dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #51606f;
  flex: 0 0 auto;
}

.db-chip--active {
  border-color: var(--db-color, var(--accent));
  background: color-mix(in srgb, var(--db-color, var(--accent)) 16%, transparent);
}
.db-chip--active .db-chip__dot {
  background: var(--db-color, var(--accent));
}
.db-chip--active .db-chip__label { font-weight: 600; }

.db-chip__del {
  display: none;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: 1px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}
.db-chip:hover .db-chip__del,
.db-chip:focus-within .db-chip__del {
  display: inline-flex;
}
.db-chip__del:hover {
  background: #ff6b6b;
  color: #1a0606;
}

.topbar h1 {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
}

.topbar .lang-toggle-btn {
  margin-left: auto;
  flex: 0 0 auto;
  order: 3;
}

.topbar .db-chips {
  order: 4;
}

.lang-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: var(--header-control-h);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  box-sizing: border-box;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.lang-toggle-btn:hover {
  color: var(--text);
  border-color: #4a5568;
  background: var(--surface-2);
}

.lang-toggle-btn:focus-visible {
  outline: 2px solid var(--accent-awb);
  outline-offset: 2px;
}

.lang-toggle-mark {
  position: relative;
  display: block;
  width: 24px;
  height: 22px;
  font-weight: 700;
  line-height: 1;
  user-select: none;
}

.lang-toggle-mark::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26px;
  height: 1.5px;
  background: currentColor;
  opacity: 0.5;
  transform: translate(-50%, -50%) rotate(-38deg);
}

.lang-toggle-mark__zh {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 11px;
}

.lang-toggle-mark__en {
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 9px;
  letter-spacing: 0.02em;
}

html[data-lang="en"] .lang-toggle-mark__en,
html[data-lang="zh"] .lang-toggle-mark__zh {
  color: var(--accent-awb);
}

.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1 1 0;
  min-width: 0;
  flex-wrap: nowrap;
}

.ctrl {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

select {
  background: #0f1115;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 12px;
}

.btn {
  background: #0f1115;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #04121f;
  font-weight: 600;
}
.btn--primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.55; cursor: progress; }

.axis-group {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 12px;
}

.ctrl--path {
  flex: 0 1 clamp(360px, 52vw, 520px);
  min-width: 280px;
  white-space: nowrap;
}
.ctrl--path input {
  flex: 1 1 320px;
  min-width: 0;
  background: #0f1115;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 9px;
  font-size: 12px;
}
.ctrl--path input:focus {
  outline: none;
  border-color: var(--accent);
}

.sep {
  width: 1px;
  align-self: stretch;
  background: var(--border);
  margin: 0 2px;
}

.count {
  flex: 0 1 280px;
  min-width: 0;
  font-size: 12px;
  color: var(--accent);
  white-space: nowrap;
  max-width: min(28vw, 280px);
  overflow: hidden;
  text-overflow: ellipsis;
}
.count:empty { display: none; }
.count--error { color: #ff6b6b; }

@media (max-width: 760px) {
  .topbar h1 {
    flex-basis: 100%;
  }

  .controls {
    flex-basis: 100%;
    flex-wrap: wrap;
  }

  .ctrl--path {
    flex: 1 1 100%;
    min-width: 0;
  }

  .count {
    flex-basis: auto;
    max-width: 100%;
  }
}

.nav-link {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}
.nav-link:hover { text-decoration: underline; }

.layout {
  --plot-w: 1fr;
  --plot-min: 380px;
  --grid-min: 320px;
  --plot-ratio: 4 / 3;
  display: grid;
  grid-template-columns: minmax(var(--plot-min), var(--plot-w)) 7px minmax(var(--grid-min), 1fr);
  gap: 0;
  flex: 1 1 auto;
  min-height: 0;
}

.plot-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.view-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px 4px;
  flex: 0 0 auto;
  flex-wrap: wrap;
}

.view-tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.view-tab:hover { border-color: var(--muted); }
.view-tab--on {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-color: var(--accent);
  color: var(--text);
  font-weight: 600;
}

.matrix-mode-sel {
  height: 26px;
  padding: 0 6px;
  font-size: 12px;
}

.matrix-snap {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
}
.matrix-snap input { accent-color: var(--accent); cursor: pointer; }
.matrix-snap[hidden] { display: none !important; }

.stat-unmatched,
.stat-reducible {
  font: inherit;
  color: var(--accent);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 0 5px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.stat-unmatched:hover,
.stat-reducible:hover { background: rgba(74, 158, 255, 0.14); }
.stat-unmatched.on,
.stat-reducible.on {
  background: rgba(74, 158, 255, 0.22);
  border-color: var(--accent);
  text-decoration: none;
}
.stat-reducible:disabled {
  color: var(--muted);
  cursor: default;
  text-decoration: none;
  opacity: 0.65;
}

.matrix-stat {
  font-size: 12px;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.matrix-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.matrix-wrap[hidden] { display: none; }

.matrix-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden; /* canvas lays out to container width → never scrolls */
}

.matrix-block-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.matrix-block-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}

#matrixCanvas0,
#matrixCanvas1 {
  display: block;
  width: 100%;
  background: #1e1e1e;
  border-radius: 8px;
  cursor: pointer;
}

/* `hidden` must win over author `display:flex` (same class of bug as Gallery E73). */
#axisGroup[hidden],
.matrix-block[hidden],
#pathControl[hidden],
#updateBtn[hidden] {
  display: none !important;
}

.ext-swatch {
  display: inline-block;
  width: 16px;
  text-align: center;
  background: #161719;
  color: #4a4d52;
  border-radius: 3px;
}

.splitter {
  position: relative;
  cursor: col-resize;
  background: var(--border);
  touch-action: none;
  user-select: none;
}
.splitter::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 3px;
  height: 42px;
  border-radius: 2px;
  background: #4a5361;
}
.splitter:hover,
.splitter.is-dragging {
  background: var(--accent);
}
.splitter:hover::before,
.splitter.is-dragging::before {
  background: #cfe2ff;
}

.plot-wrap {
  position: relative;
  flex: 0 1 auto;
  width: 100%;
  aspect-ratio: var(--plot-ratio, 4 / 3);
  max-height: 100%;
  min-height: 0;
  padding: 0;
}

#plot {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.hint {
  margin: 0;
  padding: 8px 14px 12px;
  font-size: 12px;
  color: var(--muted);
  flex: 0 0 auto;
}

.tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(0, 0, 0, 0.85);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 11px;
  line-height: 1.5;
  z-index: 5;
  white-space: nowrap;
}

.grid-pane {
  position: relative;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.grid-toolbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 10px;
  box-sizing: border-box;
}

.scene-btn {
  white-space: nowrap;
  height: 28px;
  padding: 0 12px;
}
.scene-btn--on {
  background: var(--accent);
  border-color: var(--accent);
  color: #04121f;
  font-weight: 600;
}
.scene-stat {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.grid-zoom {
  margin-left: auto;
  display: inline-flex;
  align-items: stretch;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
  height: 28px;
}

.grid-zoom__btn {
  width: 34px;
  height: 100%;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.grid-zoom__btn:first-child {
  border-right: 1px solid var(--border);
}
.grid-zoom__btn:hover:not(:disabled) {
  background: #1a1e26;
}
.grid-zoom__btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, var(--grid-cell, 120px));
  grid-auto-rows: auto;
  align-content: start;
  justify-content: start;
  gap: 6px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  box-sizing: border-box;
}

.cell {
  position: relative;
  width: var(--grid-cell, 120px);
  height: calc(var(--grid-cell, 120px) * 4 / 3);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  cursor: pointer;
}
.cell:hover { border-color: var(--accent); }

.cell--tagged {
  border-color: var(--db-color);
  box-shadow: inset 0 3px 0 var(--db-color);
}

.cell--discarded {
  filter: grayscale(1);
  opacity: 0.4;
}
.cell--discarded:hover {
  opacity: 0.8;
}

.cell--replacement {
  opacity: 0.68;
  cursor: pointer;
}
.cell--replacement:hover {
  opacity: 1;
  border-color: #72b5ff;
  box-shadow: 0 0 0 2px rgba(74, 158, 255, 0.24);
}

.cell--reduction-selected {
  border-color: #72b5ff;
  box-shadow:
    0 0 0 2px var(--accent),
    0 0 18px rgba(74, 158, 255, 0.45);
}

.cell__badge {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 3;
  min-width: 23px;
  height: 23px;
  padding: 0 6px;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.9);
  background: var(--accent);
  color: #04121f;
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 2px 7px rgba(0, 0, 0, 0.55);
}

.cell__act {
  position: absolute;
  top: 5px;
  right: 5px;
  z-index: 2;
  width: 22px;
  height: 22px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 15px;
  line-height: 1;
  border: 1px solid rgba(255, 255, 255, 0.35);
  cursor: pointer;
}
.cell:hover .cell__act {
  display: inline-flex;
}
.cell__act--x:hover {
  background: #ff6b6b;
  color: #1a0606;
}
.cell__act--restore:hover {
  background: var(--accent);
  color: #04121f;
}

.grid__divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 2px 2px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.grid__divider::before,
.grid__divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}
.grid__divider--reduction {
  margin-top: 14px;
  color: #aeb9c8;
}

.grid--reduction .cell--replacement {
  cursor: pointer;
}

.cell img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.cell__cap {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 3px 5px;
  font-size: 10px;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  text-align: left;
}

.grid.labels-hidden .cell__cap {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal[hidden] { display: none; }
.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
}
.modal__fig {
  position: relative;
  margin: 0;
  max-width: 92vw;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.modal__fig img {
  max-width: 92vw;
  max-height: 84vh;
  object-fit: contain;
  border-radius: 8px;
}
.modal__fig figcaption {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
  max-width: 92vw;
  text-align: center;
  line-height: 1.45;
  word-break: break-word;
}

.progress {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
}
.progress[hidden] { display: none; }
.progress__box {
  width: min(420px, 80vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
}
.progress__text {
  font-size: 13px;
  color: var(--text);
  margin-bottom: 10px;
}
.progress__bar {
  height: 8px;
  border-radius: 999px;
  background: #0f1115;
  overflow: hidden;
}
.progress__fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.15s ease;
}
.progress__fill.is-indeterminate {
  width: 35%;
  animation: progress-indeterminate 1.2s ease-in-out infinite;
}
@keyframes progress-indeterminate {
  0% { transform: translateX(-120%); }
  100% { transform: translateX(320%); }
}
