:root {
  --bg: #0a0e14;
  --surface: #141b24;
  --border: #2a3441;
  --text: #e7edf4;
  --muted: #8b98a8;
  --accent: #3b82f6;
}

* { box-sizing: border-box; }

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

.bar {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.bar h1 { margin: 0.25rem 0 0; font-size: 1.2rem; }
.bar .ver { font-size: 0.7rem; font-weight: 600; color: var(--accent); vertical-align: middle; }
.bar .hint { margin: 0.2rem 0 0; color: var(--muted); font-size: 0.85rem; }
.bar .hint a { color: var(--accent); }
.back { color: var(--accent); text-decoration: none; font-size: 0.9rem; }

.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - 90px);
  overflow: hidden;
}

.sidebar {
  padding: 1rem;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  overflow-y: auto;
  min-height: 0;
}

.sidebar label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.sidebar select,
.sidebar input[type="range"] {
  width: 100%;
}

.btn {
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); border-color: var(--accent); }
.btn-ghost:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.saved-view-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.35;
}

.hidden { display: none !important; }

.slider-val {
  float: right;
  color: var(--accent);
  font-weight: 600;
}

.status { font-size: 0.85rem; color: var(--muted); margin: 0; }
.status-busy { color: #93c5fd; }
.status-error { color: #f87171; }
.status-ok { color: #86efac; }

.meta { font-size: 0.78rem; color: var(--muted); line-height: 1.4; word-break: break-all; margin: 0; }

.step { display: flex; flex-direction: column; gap: 0.5rem; }
.step-title { margin: 0; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

.progress-block { display: grid; gap: 0.35rem; }
.progress-block.hidden { display: none; }
.progress-bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}
.progress-bar > span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2563eb, #7c3aed);
  transition: width 0.15s ease-out;
}
.progress-label { margin: 0; font-size: 0.72rem; color: var(--muted); }

.log {
  margin: 0;
  max-height: 140px;
  overflow: auto;
  padding: 0.5rem;
  font-size: 0.68rem;
  line-height: 1.35;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: #94a3b8;
  white-space: pre-wrap;
  word-break: break-word;
}

.help {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--muted);
}

.help h2 { margin: 0 0 0.5rem; font-size: 0.9rem; color: var(--text); }
.help code { display: block; margin-top: 0.5rem; padding: 0.5rem; background: var(--bg); border-radius: 6px; font-size: 0.72rem; overflow-x: auto; }

.viewport-wrap {
  position: relative;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  touch-action: none;
}

.check-row {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--text);
  cursor: pointer;
}
.check-row input { width: auto; margin: 0; }

.preview-2d {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #000;
  cursor: grab;
  touch-action: none;
}

.preview-2d:active { cursor: grabbing; }

.preview-2d.mode-orbit { cursor: grab; }
.preview-2d.mode-orbit:active { cursor: grabbing; }

.gl-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  pointer-events: none;
  cursor: grab;
}

.gl-layer.active {
  opacity: 0.5;
  pointer-events: auto;
}

.viewport-hint {
  position: absolute;
  bottom: 0.75rem;
  right: 1rem;
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
  pointer-events: none;
}

.renderer-badge {
  position: absolute;
  top: 0.75rem;
  right: 1rem;
  margin: 0;
  padding: 0.25rem 0.45rem;
  border: 1px solid rgba(148, 163, 184, 0.28);
  border-radius: 6px;
  background: rgba(2, 6, 23, 0.58);
  font-size: 0.72rem;
  color: #cbd5e1;
  letter-spacing: 0.01em;
  pointer-events: none;
}

.controls-backdrop,
.controls-close-btn,
.mobile-controls-btn {
  display: none;
}

@media (max-width: 768px) {
  body {
    height: 100dvh;
    overflow: hidden;
  }

  .bar {
    padding: 0.7rem 0.9rem;
  }

  .bar h1 {
    font-size: 1rem;
  }

  .layout {
    display: block;
    height: calc(100dvh - 64px);
    overflow: hidden;
  }

  .viewport-wrap {
    height: 100%;
    min-height: 0;
  }

  .viewport-hint {
    left: 0.75rem;
    right: 5.5rem;
    bottom: 0.65rem;
    font-size: 0.72rem;
  }

  .renderer-badge {
    top: 0.65rem;
    left: 0.75rem;
    right: auto;
  }

  .mobile-controls-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    right: 0.75rem;
    bottom: 0.65rem;
    z-index: 12;
    min-height: 44px;
    padding: 0.55rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: rgba(20, 27, 36, 0.92);
    color: var(--text);
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  }

  .controls-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 80;
    background: rgba(0, 0, 0, 0.45);
  }

  body.controls-open .controls-backdrop {
    display: block;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 90;
    max-height: min(78dvh, 560px);
    border-right: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -10px 36px rgba(0, 0, 0, 0.42);
    transform: translateY(100%);
    transition: transform 0.22s ease;
    padding-top: 2.4rem;
    padding-bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px));
  }

  body.controls-open .sidebar {
    transform: translateY(0);
  }

  .controls-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0.55rem;
    right: 0.75rem;
    min-height: 36px;
    padding: 0.35rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: transparent;
    color: var(--muted);
    font-size: 0.78rem;
    cursor: pointer;
  }

  .btn,
  .step button,
  .btn-row button {
    min-height: 44px;
  }

  .sidebar select,
  .sidebar input[type="range"] {
    min-height: 40px;
  }

  .log {
    max-height: 72px;
  }

  .modal-panel {
    width: min(520px, calc(100vw - 1.25rem));
    max-height: calc(100dvh - 1.5rem);
    overflow: auto;
  }

  .levels-fields {
    grid-template-columns: 1fr;
  }
}

.pyramid-progress .bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.pyramid-progress .bar > span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2563eb, #7c3aed);
  transition: width 0.2s;
}

.pyramid-progress { display: grid; gap: 0.5rem; }

.help a { color: var(--accent); }

.levels-summary {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.35;
}

.channel-list {
  display: grid;
  gap: 0.45rem;
  margin-bottom: 0.5rem;
}

.channel-placeholder {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.channel-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.45rem;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
}

.channel-row label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
  font-size: 0.85rem;
}

.channel-row input[type="color"] {
  width: 2rem;
  height: 1.6rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
}

.debug-options {
  margin-top: 0.75rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 0.45rem;
  margin: 0;
  font-size: 0.82rem;
  line-height: 1.35;
  color: var(--muted, #9aa4b2);
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  margin-top: 0.15rem;
  flex-shrink: 0;
}

.checkbox-row:has(input:disabled) {
  opacity: 0.55;
  cursor: default;
}

.levels-channel-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 0.65rem;
  font-size: 0.85rem;
}

.levels-channel-row select {
  flex: 1;
  min-width: 0;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.modal-panel {
  position: relative;
  width: min(520px, 100%);
  padding: 1rem 1.1rem 1.1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 1rem;
}

.modal-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.25rem;
}

.modal-close:hover { color: var(--text); }

.modal-hint {
  margin: 0 0 0.75rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.levels-hist {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  cursor: crosshair;
  touch-action: none;
}

.levels-fields {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin: 0.75rem 0 0.35rem;
}

.levels-fields label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.72rem;
  color: var(--muted);
}

.levels-fields input[type="number"] {
  width: 100%;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
}

.levels-gamma-row {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;
}
