:root {
  --header-h: 112px;
  --footer-h: 96px;
  --genre-rail-head-h: 34px;
  --genre-rail-chips-h: 48px;
  --genre-rail-h: calc(var(--genre-rail-head-h) + var(--genre-rail-chips-h));
  --inline-preview-h: 0px;
  --touch-min: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

body {
  margin: 0;
  overflow: hidden;
  background: #0b0b0b;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.mobile-only {
  display: none;
}

.desk-only {
  display: revert;
}

.touch-target {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
}

/* Desktop top bar media filters — makes the main types immediately visible and intuitive */
.top-media-filters {
  display: flex;
  gap: 4px;
  align-items: center;
  margin-left: 16px;
  padding-left: 12px;
  border-left: 1px solid #333;
}
.top-media-filters .media-chip {
  font-size: 10px;
  padding: 3px 10px;
  border-radius: 999px;
  background: #1f1f25;
  color: #aaa;
  border: 1px solid #3a3a42;
  cursor: pointer;
  transition: all 0.1s ease;
  font-weight: 500;
  letter-spacing: 0.3px;
}
.top-media-filters .media-chip:hover {
  background: #2a2a32;
  border-color: #555;
}
.top-media-filters .media-chip.on {
  background: var(--chip-color, #3366aa);
  color: white;
  border-color: var(--chip-color, #3366aa);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.icon-btn {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  padding: 0 12px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #222;
  color: #eee;
  font-size: 14px;
  cursor: pointer;
}

.btn-primary {
  min-height: var(--touch-min);
  padding: 0 16px;
  font-weight: 600;
  border-radius: 8px;
  background: #3a5a8a;
  border-color: #5a7ab0;
}

canvas {
  display: block;
}

/* Scroll wall (mobile browse mode) */
.scroll-viewport,
.genre-rail-wrap,
.inline-preview {
  display: none;
}

body.scroll-mode .scroll-viewport,
body.scroll-mode .genre-rail-wrap {
  display: block;
}

.scroll-viewport {
  position: fixed;
  top: calc(var(--header-h) + var(--safe-top));
  left: 0;
  right: 0;
  bottom: calc(
    var(--footer-h) + var(--genre-rail-h) + var(--inline-preview-h) + var(--safe-bottom)
  );
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  overscroll-behavior-x: auto;
  touch-action: pan-y;
  z-index: 5;
  background: #0b0b0b;
  --pull-distance: 0px;
  transition: transform 0.2s cubic-bezier(0.25, 0.82, 0.25, 1);
}

.scroll-viewport.pull-refresh-dragging {
  transition: none;
}

.scroll-viewport.pull-refresh-active {
  transform: translateY(var(--pull-distance));
}

.pull-refresh-hint {
  position: sticky;
  top: 0;
  z-index: 6;
  margin: 0 auto;
  width: fit-content;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid #2f2f36;
  background: rgba(16, 16, 19, 0.92);
  color: #8d8d96;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  transform: translateY(-125%);
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.scroll-viewport.pull-refresh-active .pull-refresh-hint {
  transform: translateY(8px);
  opacity: 1;
}

.scroll-viewport.pull-refresh-armed .pull-refresh-hint {
  color: #c7c7ff;
  border-color: #5a5a8a;
}

.pull-refresh-btn {
  display: none;
  position: fixed;
  bottom: calc(
    var(--genre-rail-h) + var(--inline-preview-h) + var(--safe-bottom) + 12px
  );
  right: calc(12px + var(--safe-right));
  z-index: 9;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(20, 20, 26, 0.92);
  color: #d6d6e0;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.6s ease;
  pointer-events: auto;
}

.pull-refresh-btn:hover,
.pull-refresh-btn:focus-visible {
  background: rgba(40, 40, 50, 0.96);
  color: #fff;
  outline: none;
}

.pull-refresh-btn.spinning {
  transform: rotate(360deg);
  color: #c7c7ff;
  border-color: #5a5a8a;
}

body.scroll-mode .pull-refresh-btn {
  display: inline-flex;
}

body.preview-open .pull-refresh-btn,
body.sheet-open .pull-refresh-btn {
  display: none;
}

.scroll-wall {
  padding: 12px;
  box-sizing: border-box;
}

.scroll-grid {
  display: grid;
  gap: 4px;
  width: 100%;
}

/* Sphere / stream — vertical feed, no section blocks */
.scroll-wall-stream {
  width: 100%;
  max-width: 100%;
}

.scroll-grid-stream {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 3px;
}

body.scroll-layout-stream .scroll-viewport {
  overflow-x: hidden;
}

/* Checkerboard / freeform — 2D canvas, flat mosaic (genre via rail only) */
.scroll-grid-freeform {
  grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
  gap: 4px;
}

.scroll-tile {
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: 6px;
  overflow: hidden;
  background: color-mix(in srgb, var(--tile-accent, #333) 28%, #1a1a1a);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.scroll-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.scroll-tile-ph {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 11px;
  font-weight: 700;
  color: #ccc;
}

.scroll-tile-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 5px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
}

.scroll-tile.selected {
  outline: 2px solid #8af;
  outline-offset: 1px;
  box-shadow: 0 0 0 1px rgba(136, 170, 255, 0.4);
}

.scroll-tile:active {
  opacity: 0.85;
}

/* Heatmap ticker tiles (scroll / mobile 2D wall) */
.scroll-tile.ticker {
  background: #111;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  text-align: center;
  line-height: 1.05;
  padding: 3px 4px;
  gap: 1px;
}
.scroll-tile.ticker .sym {
  font-size: 14px;
  font-weight: 800;
  letter-spacing: -0.4px;
}
.scroll-tile.ticker .pct {
  font-size: 10px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
.scroll-tile.ticker .px {
  font-size: 9px;
  opacity: 0.82;
  margin-top: 1px;
}

.genre-rail-wrap {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--footer-h) + var(--inline-preview-h) + var(--safe-bottom));
  z-index: 20;
  background: rgba(12, 12, 14, 0.96);
  border-top: 1px solid #2a2a32;
  box-sizing: border-box;
}

.genre-rail-head {
  padding: 2px 6px 0;
}

.genre-rail-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: var(--genre-rail-head-h);
  padding: 4px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #999;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
}

.genre-rail-toggle:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #ccc;
}

.genre-rail-summary {
  flex: 1;
  text-align: left;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 600;
  color: #9a8acc;
}

.genre-rail-chevron {
  flex-shrink: 0;
  font-size: 11px;
  color: #666;
}

body.genre-rail-collapsed {
  --genre-rail-h: var(--genre-rail-head-h);
}

.genre-rail {
  display: flex;
  gap: 8px;
  padding: 4px 10px 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  max-height: var(--genre-rail-chips-h);
  box-sizing: border-box;
  align-items: center;
  transition: max-height 0.22s ease, opacity 0.18s ease, padding 0.22s ease;
}

body.genre-rail-collapsed .genre-rail {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
}

.genre-rail::-webkit-scrollbar {
  display: none;
}

.genre-chip {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid #3a3a44;
  background: #1c1c22;
  color: #bbb;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.genre-chip.active {
  background: #4a3a7a;
  border-color: #7a6aaa;
  color: #fff;
}

.inline-preview {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--footer-h) + var(--genre-rail-h) + var(--safe-bottom));
  z-index: 22;
  max-height: 0;
  overflow: hidden;
  background: #141418;
  border-top: 1px solid #333;
  transition: max-height 0.25s ease;
  pointer-events: none;
}

body.scroll-mode.preview-open {
  --inline-preview-h: 50vh;
}

.inline-preview.open {
  display: block;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  pointer-events: auto;
}

.inline-preview-close {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 3;
  min-width: 40px;
  min-height: 36px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #222;
  color: #ccc;
  font-size: 16px;
  cursor: pointer;
}

.inline-preview-counter {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 3;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(20, 20, 26, 0.85);
  border: 1px solid #2e2e38;
  color: #b8b8c4;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  pointer-events: none;
}

.inline-preview-nav {
  position: absolute;
  top: 38%;
  z-index: 4;
  width: 40px;
  height: 56px;
  border: 1px solid #2e2e38;
  border-radius: 10px;
  background: rgba(20, 20, 26, 0.78);
  color: #c8c8d4;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.7;
  display: flex;
  align-items: center;
  justify-content: center;
}

.inline-preview-nav:active {
  background: rgba(34, 34, 42, 0.96);
  color: #fff;
  transform: scale(0.96);
}

.inline-preview-prev {
  left: 8px;
}

.inline-preview-next {
  right: 8px;
}

.inline-preview-media {
  padding: 10px 12px 0;
}

.inline-preview-media img,
.inline-preview-media video,
.inline-preview-media audio {
  width: 100%;
  max-height: 32vh;
  object-fit: contain;
  border-radius: 8px;
}

.inline-preview-meta {
  padding: 8px 12px 12px;
  font-size: 13px;
  color: #ccc;
}

.inline-preview-meta .preview-title {
  margin: 6px 0 2px;
  font-weight: 600;
}

.inline-preview-meta .preview-open-original {
  display: inline-block;
  margin: 4px 0 6px;
  color: #8af;
  font-size: 13px;
}

.inline-preview-details-toggle {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid #3b3b46;
  border-radius: 8px;
  background: #202028;
  color: #cfcfe2;
  font-size: 12px;
  font-weight: 600;
}

.inline-preview-details {
  margin-top: 8px;
}

body.scroll-mode #drawer {
  display: none !important;
}

body.scroll-mode #c {
  display: none !important;
}

/* TOP CHROME — panel is sibling of header (avoids backdrop-filter trap) */
.top-chrome {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: calc(6px + var(--safe-top)) calc(8px + var(--safe-right)) 6px calc(8px + var(--safe-left));
  background: rgba(15, 15, 15, 0.94);
  border-bottom: 1px solid #222;
  color: #ccc;
  font-size: 13px;
}

body.sheet-open {
  overflow: hidden;
}

body.sheet-open .top-chrome {
  z-index: 50;
}

#header.top-bar {
  position: static;
  flex: 1 1 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 0;
  background: transparent;
  border: none;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  color: #fff;
  letter-spacing: 0.02em;
}

.desk-brand,
.desk-label {
  display: none;
}

.desk-toolbar,
.desk-row {
  display: contents;
}

.control-panel {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  min-width: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

#header .header-right {
  margin-left: auto;
}

.panel-section {
  display: contents;
}

.panel-heading {
  display: none;
}

.field-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: #aaa;
  font-size: 12px;
}

.sheet-header,
.sheet-backdrop,
.metrics-backdrop {
  display: none;
}

.panel-sort {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.panel-mode {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.mode-toggle {
  display: inline-flex;
  gap: 4px;
  background: rgba(255,255,255,0.03);
  border-radius: 6px;
  padding: 2px;
}
.mode-toggle .touch-target {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 5px;
  border: 1px solid transparent;
  background: transparent;
  color: #ccc;
  transition: background .1s ease, color .1s ease;
}
.mode-toggle .touch-target.active {
  background: #2a2f3a;
  color: #fff;
  border-color: #445;
}

.panel-view .view-tools {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.count-block {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 5px;
}

.count-row-main {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.count-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #aaa;
}

#countSlider {
  width: 120px;
  accent-color: #666;
}

#countInput {
  width: 4.5rem;
  padding: 3px 6px;
  border: 1px solid #444;
  border-radius: 4px;
  background: #1a1a1a;
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  font-family: "SF Mono", "Consolas", monospace;
}

#countInput:focus {
  border-color: #666;
  outline: none;
}

.count-presets {
  display: flex;
  align-items: center;
  gap: 3px;
  flex-wrap: wrap;
  padding-left: 0;
}

.count-btn {
  padding: 8px 10px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #222;
  color: #ccc;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.count-btn:hover {
  background: #333;
  color: #fff;
}

.header-right {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-left: auto;
  padding-top: 2px;
}

.sort-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #aaa;
}

.sort-label.field-label {
  flex-direction: row;
  align-items: center;
}

#sortMode {
  padding: 10px 8px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #222;
  color: #fff;
  font-size: 16px;
  max-width: 11rem;
  min-height: var(--touch-min);
}

.media-filters {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  align-items: center;
  flex: 1;
  min-width: 0;
  justify-content: flex-start;
}

.media-chip {
  padding: 10px 12px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #1a1a1a;
  color: #aaa;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.media-chip.on {
  color: #eee;
  border-color: color-mix(in srgb, var(--chip-color, #666) 55%, #3a3a44);
  background: color-mix(in srgb, var(--chip-color, #444) 18%, #1a1a1a);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--chip-color, #888) 40%, transparent);
}

.media-chip.off {
  opacity: 0.45;
}

#perf.perf-compact {
  font-family: "SF Mono", "Consolas", monospace;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
  background: #1a1a1a;
  border: 1px solid #333;
  white-space: nowrap;
  color: #ccc;
  cursor: pointer;
  pointer-events: auto;
  margin: 0;
}

.perf-mobile-label {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

#perf.perf-open {
  border-color: #6a8ac4;
  background: #2a3548;
  color: #fff;
}

#perf.perf-compact:hover {
  background: #252525;
  border-color: #555;
}

#perf.perf-compact[aria-expanded="false"] {
  border-color: #555;
}

.perf-override-btn.active {
  border-color: #7a6a3a;
  background: color-mix(in srgb, #aa8833 22%, #222);
  color: #ffe8a8;
}

body.perf-guard-active #perfOverrideBtn:not(.active) {
  border-color: rgba(136, 170, 255, 0.35);
}

body.perf-tier-critical .view-tools #blurBtn,
body.perf-tier-critical .view-tools #blendBtn {
  opacity: 0.55;
}

#metricsPanel {
  position: fixed;
  top: var(--header-h, 76px);
  right: 8px;
  width: 200px;
  padding: 10px 12px;
  background: rgba(12, 12, 12, 0.92);
  border: 1px solid #333;
  border-radius: 8px;
  z-index: 38;
  font-size: 11px;
  color: #aaa;
  backdrop-filter: blur(8px);
  pointer-events: auto;
  transform: translateX(0);
  opacity: 1;
  visibility: visible;
  transition: transform 0.22s ease, opacity 0.2s ease, visibility 0.22s;
}

#metricsPanel.collapsed,
#metricsPanel[hidden] {
  display: none !important;
  pointer-events: none !important;
  visibility: hidden !important;
}

.metrics-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #666;
  margin-bottom: 8px;
}

.metrics-title-text {
  flex: 1;
}

.metrics-close-mobile {
  display: none;
}

.metrics-collapse-btn {
  padding: 0 4px;
  border: none;
  background: transparent;
  color: #888;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  pointer-events: auto;
}

.metrics-collapse-btn:hover {
  color: #fff;
}

#metricsPanel.collapsed .metrics-collapse-btn {
  transform: rotate(-90deg);
}

.metric-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 5px;
  line-height: 1.35;
}

.metric-row strong {
  color: #e8e8e8;
  font-weight: 600;
  font-family: "SF Mono", "Consolas", monospace;
  text-align: right;
}

.metric-wide {
  font-size: 10px;
  max-width: 118px;
  word-break: break-word;
}

.metrics-hint {
  margin: 10px 0 0;
  font-size: 10px;
  color: #555;
  line-height: 1.4;
  pointer-events: auto;
}

.metrics-hint code {
  font-size: 9px;
  color: #888;
}

/* Visual Processing Pipeline indicator (right metrics panel) */
.pipeline-section {
  margin: 10px 0 4px;
  padding-top: 6px;
  border-top: 1px solid #222;
  font-size: 10px;
}

.pipeline-label {
  font-size: 9px;
  color: #666;
  margin-bottom: 4px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.pipeline-steps {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-bottom: 3px;
}

.pstep {
  padding: 1px 5px;
  border-radius: 3px;
  background: #1a1a1a;
  color: #555;
  font-size: 9px;
  border: 1px solid #333;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.pstep.active {
  background: #2a4a2a;
  color: #9f9;
  border-color: #3a6;
  box-shadow: 0 0 4px rgba(60, 160, 80, 0.3);
}

.pstep.processing {
  background: #3a3a1a;
  color: #ee6;
  border-color: #886;
  animation: pipeline-pulse 1.2s infinite ease-in-out;
}

@keyframes pipeline-pulse {
  0%, 100% { opacity: 0.75; }
  50% { opacity: 1; }
}

.pipeline-status {
  font-family: monospace;
  font-size: 9px;
  color: #777;
  min-height: 13px;
  padding-left: 1px;
}

#mFps {
  border: 1px solid #333;
  padding: 1px 5px;
  border-radius: 3px;
}

/* FOOTER */
footer.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: calc(8px + var(--safe-bottom)) calc(10px + var(--safe-right)) 8px calc(10px + var(--safe-left));
  background: rgba(15, 15, 15, 0.94);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
  z-index: 30;
  border-top: 1px solid #222;
}

.search-footer-row {
  display: flex;
  gap: 8px;
  align-items: center;
  min-width: 0;
}

.search-footer-primary {
  flex: 1 1 auto;
}

.search-footer-meta {
  flex: 1 1 auto;
}

.search-status {
  font-size: 12px;
  color: #888;
  flex: 1 1 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

button {
  padding: 10px 14px;
  border: 1px solid #444;
  border-radius: 8px;
  background: #222;
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  background: #333;
}

button.active {
  background: #444;
  border-color: #888;
}

#search,
#metaSearch {
  flex: 1 1 8rem;
  min-width: 0;
  min-height: var(--touch-min);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 16px;
  outline: none;
}

#search {
  border: 1px solid #444;
  background: #1a1a1a;
  color: #fff;
}

#search:focus {
  border-color: #666;
}

#search::placeholder {
  color: #666;
}

#metaSearch {
  border: 1px solid #3a3a44;
  background: #14141a;
  color: #c8b8ff;
}

#metaSearch:focus {
  border-color: #6655aa;
}

#metaSearch::placeholder {
  color: #555;
  font-size: 12px;
}

#searchStatus {
  font-size: 12px;
  color: #888;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

#searchBtn:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* Side sheet (right anchored) for desktop canvas mode.
   Keeps the infinite wall fully interactive (pan + zoom) while inspecting an item.
   On narrow screens it falls back to bottom-sheet behavior. */
#drawer.detail-lightbox {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 92vw);
  z-index: 45;
  display: none;
  flex-direction: column;
  background: #14141a;
  border-left: 1px solid #2e2e38;
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.45);
  color: #eceaf2;
  pointer-events: auto;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  overflow: hidden;
}

#drawer.detail-lightbox.open {
  display: flex;
  transform: translateX(0);
}

/* Narrow / mobile: behave more like a bottom sheet */
@media (max-width: 720px) {
  #drawer.detail-lightbox {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: min(78vh, 620px);
    border-left: none;
    border-top: 1px solid #2e2e38;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.45);
    transform: translateY(100%);
  }
  #drawer.detail-lightbox.open {
    transform: translateY(0);
  }

  /* On bottom sheet, stack media + meta vertically */
  #drawer .detail-layout {
    grid-template-columns: 1fr;
  }
  #drawer .detail-meta {
    border-left: none;
    border-top: 1px solid #252530;
  }
}

/* Desktop: hide the duplicate media chips inside the control panel
   since we now show them directly in the top bar for better intuition */
@media (min-width: 901px) {
  .panel-media {
    display: none;
  }
}

body.detail-open {
  /* We no longer force overflow hidden because the wall should stay interactive */
}

/* Visual feedback: dim the wall slightly when the side sheet is open,
   so it's clear the sheet has focus, while still allowing full pan/zoom + clicking other tiles */
body.detail-open #c {
  filter: brightness(0.65) saturate(0.8);
  transition: filter 0.15s ease;
}

#drawer .detail-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 4, 8, 0.82);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none; /* allow canvas behind to receive pan/zoom events */
}

/* In side-sheet mode the drawer itself is the panel */
#drawer .detail-panel {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #14141a;
  border: none;
  border-radius: 0;
  box-shadow: none;
  overflow: hidden;
}

/* On narrow screens the inner panel gets rounded top corners to match bottom sheet */
@media (max-width: 720px) {
  #drawer .detail-panel {
    border-radius: 14px 14px 0 0;
  }
}

#drawer .detail-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 10;
  width: 42px;
  height: 42px;
  border: 1px solid #4a4a55;
  border-radius: 10px;
  background: #1f1f28;
  color: #ddd;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.1s ease;
}

#drawer .detail-close:hover {
  background: #2a2a36;
  color: #fff;
  border-color: #666;
}

#drawer .detail-close:hover {
  color: #fff;
  border-color: #555;
  background: #222228;
}

#drawer .detail-counter {
  position: absolute;
  top: 14px;
  left: 12px;
  z-index: 3;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(20, 20, 26, 0.85);
  border: 1px solid #2e2e38;
  color: #b8b8c4;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  pointer-events: none;
}

#drawer .detail-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 4;
  width: 48px;
  height: 64px;
  border: 1px solid #2e2e38;
  border-radius: 12px;
  background: rgba(20, 20, 26, 0.78);
  color: #c8c8d4;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.55;
  transition: opacity 0.15s ease, background 0.15s ease, color 0.15s ease, transform 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
}

#drawer .detail-nav:hover {
  opacity: 1;
  background: rgba(34, 34, 42, 0.96);
  color: #fff;
}

#drawer .detail-nav:active {
  transform: translateY(-50%) scale(0.96);
}

#drawer .detail-nav-prev {
  left: 14px;
}

/* Sit at the right edge of the media column, not the panel — so the next
   chevron overlays the image just like the prev chevron does on the left. */
#drawer .detail-nav-next {
  right: calc(min(300px, 34%) + 14px);
}

/* In side-sheet mode the big floating nav arrows take too much space.
   Hide them — users can still use keyboard arrows and the preview list. */
#drawer.detail-lightbox .detail-nav {
  display: none;
}

#drawer .detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) min(300px, 34%);
  min-height: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

#drawer .detail-media {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 180px;
  max-height: 58vh; /* side sheet is tall, so media doesn't dominate */
  padding: 16px;
  background: #08080c;
  overflow: hidden;
}

#drawer .detail-media img,
#drawer .detail-media video {
  max-width: 100%;
  max-height: 52vh;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
}

#drawer .detail-media audio {
  width: min(100%, 480px);
}

#drawer .detail-meta {
  overflow-y: auto;
  padding: 18px 16px 20px;
  border-left: 1px solid #252530;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  min-height: 0;
}

#drawer .drawer-meta-lite {
  margin: 0;
}

#drawer .drawer-headline {
  margin: 0;
  padding-right: 28px;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  color: #eceaf2;
}

#drawer .drawer-snippet {
  margin: 0;
  font-size: 13px;
  color: #7a7a86;
  line-height: 1.4;
}

#drawer .drawer-tech-toggle {
  margin-top: 4px;
  padding: 7px 12px;
  border: 1px solid #444;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: #b8b8c4;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

#drawer .drawer-tech-toggle:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #eaeaf4;
}

#drawer .drawer-tech {
  width: 100%;
  margin-top: 4px;
  padding-top: 10px;
  border-top: 1px solid #2a2a2e;
}

#drawer .preview-meta-body {
  font-size: 12px;
  line-height: 1.65;
}

#drawer .preview-meta-body p {
  margin: 0 0 5px;
}

#drawer h2 {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: 600;
}

#drawer .media-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 4px;
  margin-bottom: 8px;
  background: #333;
}

#drawer .open-link {
  display: inline-block;
  color: #8af;
  font-size: 13px;
}

#drawer .hint {
  font-size: 13px;
  color: #888;
  line-height: 1.5;
  text-align: center;
}

#drawer .meta {
  font-size: 14px;
  line-height: 1.8;
}

#drawer .meta span {
  color: #aaa;
}

/* SETTINGS PANEL */
#settings {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(18,18,18,0.97);
  color: white;
  padding: 24px;
  border-radius: 12px;
  z-index: 40;
  display: none;
  width: 360px;
  border: 1px solid #333;
  backdrop-filter: blur(8px);
}

#settings.open {
  display: block;
}

#settings h2 {
  margin: 0 0 16px;
  font-size: 18px;
}

#settings label {
  display: block;
  margin-bottom: 12px;
  font-size: 14px;
  color: #aaa;
}

#settings label input {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid #444;
  border-radius: 4px;
  background: #222;
  color: #fff;
  font-size: 13px;
  outline: none;
}

#settings label input:focus {
  border-color: #666;
}

#settings .hint {
  font-size: 12px;
  color: #666;
  margin-top: 12px;
  line-height: 1.5;
}

#settings .hint a {
  color: #88f;
}

#settings .close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  color: #888;
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

#settings .close-btn:hover {
  color: #fff;
  background: none;
}

/* ——— Mobile: narrow widths, OR short height (landscape phone / squat window / small tablet rotated) ——— */
@media (max-width: 899px), (max-height: 620px) {
  :root {
    --header-h: calc(56px + var(--safe-top));
    --footer-h: calc(100px + var(--safe-bottom));
  }

  .mobile-only {
    display: inline-flex;
    align-items: center;
  }

  #menuBtn.mobile-only {
    display: inline-flex;
  }

  .brand.mobile-only {
    display: inline;
    line-height: var(--touch-min);
  }

  .desk-only {
    display: none !important;
  }

  .top-chrome {
    flex-wrap: nowrap;
    align-items: center;
  }

  #header.top-bar {
    align-items: center;
    flex: 1;
    min-width: 0;
  }

  #header .header-right {
    position: fixed;
    top: calc(6px + var(--safe-top));
    right: calc(8px + var(--safe-right));
    z-index: 60;
    margin: 0;
    flex-shrink: 0;
    pointer-events: auto;
  }

  #perf.perf-compact {
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    padding: 0 12px;
    font-size: 12px;
    border-radius: 8px;
    border-color: rgba(255, 255, 255, 0.14);
    background: rgba(22, 22, 28, 0.96);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
  }

  #perf.perf-compact.perf-open {
    border-color: #7a9acc;
    background: #3a4a62;
  }

  .metrics-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 34;
    background: rgba(0, 0, 0, 0.35);
  }

  .metrics-backdrop.open {
    display: block;
  }

  body.metrics-open .scroll-viewport,
  body.metrics-open #c {
    pointer-events: none;
  }

  .top-chrome .control-panel {
    display: none;
  }

  .top-chrome .control-panel.open {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 45;
    flex: none;
    width: auto;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: 0;
    padding-bottom: var(--safe-bottom);
    background: #0f0f12;
    overflow: hidden;
  }

  /* Don't trap the sheet inside the fixed header bar */
  body.sheet-open .top-chrome {
    position: static;
    height: 0;
    min-height: 0;
    padding: 0;
    margin: 0;
    border: none;
    background: transparent;
    overflow: visible;
  }

  body.sheet-open #header.top-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 52;
    flex: none;
    padding: calc(6px + var(--safe-top)) calc(8px + var(--safe-right)) 6px calc(8px + var(--safe-left));
    background: rgba(15, 15, 15, 0.94);
    border-bottom: 1px solid #222;
  }

  .sheet-header {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: calc(12px + var(--safe-top)) 56px 12px;
    border-bottom: 1px solid #333;
    flex-shrink: 0;
    text-align: center;
  }

  .sheet-title {
    margin: 0;
    font-size: 18px;
    color: #fff;
    text-align: center;
    flex: 1;
  }

  #sheetClose {
    position: absolute;
    top: 50%;
    right: 12px;
    transform: translateY(calc(-50% + (var(--safe-top) / 2)));
    min-width: var(--touch-min);
    min-height: var(--touch-min);
  }

  .control-panel > .panel-section,
  .control-panel > .panel-section.panel-sort,
  .control-panel > .panel-section.panel-view {
    display: block;
    padding: 12px 16px;
    border-bottom: 1px solid #1a1a1a;
    overflow: visible;
  }

  .panel-heading {
    display: block;
    margin: 0 0 10px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #666;
  }

  .panel-section {
    display: block;
  }

  .count-block {
    width: 100%;
  }

  #countSlider {
    flex: 1;
    min-width: 80px;
    width: auto;
  }

  .count-presets {
    margin-top: 8px;
  }

  .count-btn,
  .media-chip {
    flex: 0 1 auto;
  }

  .media-filters {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    width: 100%;
  }

  .media-chip {
    text-align: center;
    justify-content: center;
  }

  #sortMode {
    width: 100%;
    max-width: none;
  }

  .sort-label {
    width: 100%;
  }

  .view-tools {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .view-tools button {
    width: 100%;
  }

  .sheet-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, 0.55);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s;
  }

  .sheet-backdrop.open {
    opacity: 1;
    visibility: visible;
  }

  .control-panel .sheet-body-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  footer.bottom-bar {
    gap: 6px;
  }

  .search-footer-row {
    width: 100%;
  }

  #search,
  #metaSearch {
    flex: 1;
    min-width: 0;
  }

  #searchBtn {
    flex-shrink: 0;
  }

  #searchStatus,
  .search-status {
    display: none;
  }

  footer.bottom-bar .search-status.show-mobile-status {
    display: block;
    position: fixed;
    bottom: calc(var(--footer-h) + 4px);
    left: 8px;
    right: 8px;
    padding: 6px 10px;
    background: rgba(20, 20, 20, 0.9);
    border-radius: 8px;
    z-index: 25;
    flex: none;
  }

  #metricsPanel {
    z-index: 55;
    top: auto;
    bottom: calc(var(--footer-h) + 8px);
    left: 8px;
    right: 8px;
    width: auto;
    max-height: min(42vh, 280px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.5);
  }

  #metricsPanel:not(.collapsed):not([hidden]) {
    display: block;
  }

  .metrics-close-mobile {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--touch-min);
    padding: 0 14px;
    border-radius: 8px;
    border: 1px solid #555;
    background: #2e2e36;
    color: #f0f0f4;
    font-size: 13px;
    font-weight: 600;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    text-transform: none;
    letter-spacing: normal;
    cursor: pointer;
  }

  #metricsPanel .metrics-title {
    position: sticky;
    top: 0;
    z-index: 1;
    margin: -10px -12px 8px;
    padding: 10px 12px;
    background: rgba(14, 14, 16, 0.98);
    border-bottom: 1px solid #2a2a32;
  }

  #metricsPanel .metrics-collapse-btn {
    display: none;
  }

  /* Landscape: sheet below header, clear of fixed Stats button */
  @media (orientation: landscape) {
    #metricsPanel {
      top: calc(var(--header-h, 56px) + 8px);
      bottom: auto;
      left: 8px;
      right: calc(56px + var(--safe-right));
      width: auto;
      max-height: min(50vh, 240px);
    }

  }

  :root {
    --header-h: 52px;
  }

  body.scroll-mode .scroll-viewport {
    top: calc(var(--header-h) + var(--safe-top));
  }

  .scroll-grid-stream {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 2px;
  }

  .scroll-wall-stream {
    padding: 6px 4px;
  }

  .scroll-grid-freeform {
    grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
    gap: 3px;
  }

  .scroll-tile-badge {
    font-size: 8px;
    padding: 1px 4px;
    opacity: 0.85;
  }

  #drawer.detail-lightbox.open .detail-panel {
    width: 100%;
    max-height: 100%;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }

  #drawer.detail-lightbox.open .detail-layout {
    grid-template-columns: 1fr;
    max-height: 100%;
  }

  #drawer.detail-lightbox.open .detail-meta {
    border-left: none;
    border-top: 1px solid #252530;
    max-height: 38vh;
  }

  /* Stacked layout: meta is below the media, so next chevron sits at the
     panel's right edge again. */
  #drawer.detail-lightbox.open .detail-nav-next {
    right: 14px;
  }

  body.scroll-mode #drawer.open {
    display: none !important;
  }
}

@media (min-width: 900px) and (min-height: 621px) {
  :root {
    --header-h: 118px;
    --footer-h: 60px;
  }

  .metrics-close-mobile {
    display: none !important;
  }

  #metricsPanel .metrics-collapse-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .top-chrome {
    display: block;
    padding: 12px 20px 14px;
    gap: 0;
    background: rgba(11, 11, 13, 0.97);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  }

  #header.top-bar {
    position: absolute;
    top: 14px;
    right: 18px;
    left: auto;
    width: auto;
    flex: none;
    z-index: 5;
  }

  #header .mobile-only,
  #header .brand {
    display: none !important;
  }

  .desk-brand {
    display: block;
  }

  .desk-label {
    display: inline;
  }

  .desk-toolbar {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
  }

  .desk-row {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
    min-width: 0;
  }

  /* Reserve room for the absolute-positioned #perf button in the top-right
     corner so the sort/count controls never get covered. */
  .desk-row-primary {
    padding-right: 170px;
  }

  .desk-row-secondary {
    padding-top: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    justify-content: space-between;
  }

  .desk-brand {
    flex: 0 0 auto;
    font-size: 17px;
    font-weight: 650;
    letter-spacing: -0.03em;
    color: #eceaf2;
    margin-right: 4px;
  }

  .desk-label {
    flex: 0 0 auto;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: #5c5c68;
  }

  .top-chrome .control-panel {
    position: static;
    display: block;
    flex: none;
    width: 100%;
    padding: 0;
    margin: 0;
    background: transparent;
    overflow: visible;
    border: none;
  }

  .top-chrome .control-panel[aria-hidden="true"] {
    display: block;
  }

  .top-chrome .sheet-body-scroll {
    overflow: visible;
  }

  .top-chrome .panel-section {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
  }

  .top-chrome .panel-count {
    flex: 1 1 auto;
    min-width: 0;
  }

  .top-chrome .panel-count .count-block {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 14px;
  }

  .top-chrome .count-presets {
    padding-left: 14px;
    margin-left: 2px;
    border-left: 1px solid rgba(255, 255, 255, 0.07);
  }

  .top-chrome .count-btn {
    padding: 5px 9px;
    font-size: 11px;
    border-radius: 6px;
    border-color: rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.04);
    color: #a8a8b0;
  }

  .top-chrome .count-btn:hover {
    background: rgba(255, 255, 255, 0.09);
    color: #e8e8ec;
  }

  #countSlider {
    width: 100px;
  }

  #countInput {
    width: 4rem;
    border-radius: 6px;
  }

  .top-chrome .panel-sort {
    flex: 0 0 auto;
  }

  .top-chrome .sort-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #5c5c68;
  }

  #sortMode {
    min-height: 32px;
    padding: 4px 10px;
    font-size: 13px;
    border-radius: 6px;
    max-width: 10rem;
  }

  .top-chrome .panel-media {
    flex: 1 1 auto;
    min-width: 0;
    gap: 12px;
  }

  .top-chrome .media-filters {
    gap: 6px;
    flex-wrap: wrap;
  }

  .top-chrome .media-chip {
    padding: 5px 11px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.03em;
    border-radius: 6px;
  }

  .top-chrome .panel-view {
    flex: 0 0 auto;
  }

  .top-chrome .view-tools {
    gap: 6px;
  }

  .top-chrome .view-tools button {
    min-height: 32px;
    padding: 5px 12px;
    font-size: 12px;
    border-radius: 6px;
    border-color: rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.04);
    color: #b0b0b8;
  }

  .top-chrome .view-tools button:hover,
  .top-chrome .view-tools button.active {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.18);
    color: #f0f0f4;
  }

  #perf.perf-compact {
    font-size: 11px;
    padding: 5px 10px;
    border-radius: 6px;
    border-color: rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.04);
  }

  #metricsPanel {
    top: calc(var(--header-h) + 8px);
  }

  footer.bottom-bar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding-bottom: calc(8px + var(--safe-bottom));
  }

  .search-footer-primary {
    flex: 1 1 12rem;
    min-width: 10rem;
  }

  .search-footer-meta {
    flex: 1 1 10rem;
    min-width: 8rem;
    max-width: 18rem;
  }

  #searchStatus {
    flex: 1 1 8rem;
    min-width: 0;
  }

  .panel-view {
    display: flex;
    align-items: center;
  }

  .panel-view .panel-heading {
    display: none;
  }
}
