*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f11;
  --surface: #1a1a1f;
  --surface2: #242429;
  --border: #2e2e36;
  --accent: #7c6af7;
  --accent-dim: #4e46a0;
  --text: #e8e8f0;
  --text-dim: #888898;
  --sidebar-w: 190px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  overflow: hidden;
}

/* ── App shell: viewer is full-screen; sidebar floats on top ── */
#app {
  position: relative;
  width: 100%;
  height: 100vh;
}

/* ── Sidebar (overlay — does not reduce viewer size) ── */
#sidebar {
  position: absolute;
  right: 0;
  left: auto;
  top: 0;
  bottom: 0;
  z-index: 25;
  width: var(--sidebar-w);
  overflow: hidden;
  transition: width .22s ease;
  pointer-events: auto;
}

#sidebar.collapsed {
  width: 0;
  pointer-events: none;
}

/* Scrollable content column */
#sidebar-inner {
  width: var(--sidebar-w);
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  background: rgba(26, 26, 31, 0.96);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-left: 1px solid var(--border);
  border-right: none;
  box-shadow: -6px 0 28px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
}

/* Toggle on #app (not #viewer) so it stacks above the overlay sidebar */
#sidebar-toggle {
  position: absolute;
  top: 10px;
  right: 10px;
  left: auto;
  z-index: 40;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 26, 31, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(46, 46, 54, 0.45);
  border-radius: 7px;
  color: var(--text-dim);
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
#sidebar-toggle:hover {
  color: var(--text);
  border-color: var(--accent);
  background: rgba(26, 26, 31, 0.45);
}

/* Header */
#sidebar-header {
  padding: 14px 48px 10px 14px;
  flex-shrink: 0;
}

#sidebar-header h1 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  line-height: 1.4;
  color: var(--text);
}

/* Sections */
.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px;
}

.sidebar-sep {
  height: 1px;
  background: var(--border);
  flex-shrink: 0;
  margin: 0;
}

/* File groups */
.file-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.file-btn {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  transition: border-color .15s, background .15s;
  white-space: nowrap;
  align-self: flex-start;
}
.file-btn:hover { border-color: var(--accent); background: #2a2a32; }

.file-btn-pair {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--text);
  width: 100%;
  justify-content: center;
  cursor: pointer;
  font-size: 12px;
  font-family: inherit;
}
.file-btn-pair:hover { background: var(--accent); border-color: var(--accent); }
.file-btn input[type=file] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer; font-size: 0;
}

.ctrl-label { font-size: 11px; color: var(--text-dim); white-space: nowrap; }

.file-name {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: calc(var(--sidebar-w) - 28px);
}
.file-name.loaded { color: #7ecf8e; }

/* Sliders */
.ctrl-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.ctrl-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

input[type=range] {
  -webkit-appearance: none;
  flex: 1;
  min-width: 0;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  cursor: pointer;
}

.ctrl-value {
  font-size: 11px;
  color: var(--text);
  min-width: 32px;
  text-align: right;
  flex-shrink: 0;
}

select {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 12px;
  padding: 6px 28px 6px 10px;
  cursor: pointer;
  outline: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23888898'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  transition: border-color .15s;
}
select:hover { border-color: var(--accent); }
select option { background: var(--surface2); }

.btn {
  display: block;
  width: 100%;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  white-space: nowrap;
  text-align: left;
}
.btn:hover { border-color: var(--accent); background: #2a2a32; }

.btn-with-icon {
  display: flex;
  align-items: center;
  gap: 8px;
}
.btn-with-icon .btn-icon {
  flex-shrink: 0;
  opacity: 0.9;
}

/* Instructions at bottom of sidebar */
#instructions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
}

.instr {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--text-dim);
}

.key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 18px;
  padding: 0 5px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  font-size: 10px;
  color: var(--text);
  line-height: 1;
  flex-shrink: 0;
}

/* GitHub link at bottom of sidebar */
#github-link {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px 12px;
  font-size: 11px;
  color: var(--text-dim);
  text-decoration: none;
  transition: color .15s;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#github-link:hover { color: var(--text); }

/* ── Viewer (full viewport — layout math uses full width/height) ── */
#viewer {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  cursor: grab;
}
#viewer.dragging { cursor: grabbing; }
#viewer.dragging .frame { cursor: none; }

/* hint overlay */
#hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  pointer-events: none;
  color: var(--text-dim);
  z-index: 10;
}
#hint svg { opacity: 0.25; }
#hint p { font-size: 15px; opacity: 0.5; }
#hint small { font-size: 13px; opacity: 0.35; }
#hint.hidden { display: none; }

#row {
  position: absolute;
  top: 0; left: 0;
  display: flex;
  align-items: flex-start;
  transform-origin: 0 0;
  user-select: none;
  -webkit-user-select: none;
}

.frame {
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
  border-radius: 4px;
  background: #111;
  cursor: none;
}

.frame img {
  display: block;
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  pointer-events: none;
}

#frame-gap {
  flex-shrink: 0;
  align-self: stretch;
}

/* zoom badge */
#zoom-badge {
  position: absolute;
  bottom: 14px; right: 18px;
  background: rgba(0,0,0,.6);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--text-dim);
  pointer-events: none;
  opacity: 0;
  transition: opacity .3s;
}
#zoom-badge.visible { opacity: 1; }

/* ── Crosshair cursor ── */
.crosshair {
  position: absolute;
  width: 18px;
  height: 18px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  display: none;
}
.crosshair::before,
.crosshair::after {
  content: '';
  position: absolute;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 0 2px rgba(0,0,0,0.8);
}
.crosshair::before { left: 0; right: 0; top: 50%; height: 1px; margin-top: -0.5px; }
.crosshair::after  { top: 0; bottom: 0; left: 50%; width: 1px; margin-left: -0.5px; }
.crosshair.visible { display: block; }
