* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f10;
  --panel: #18181a;
  --panel-2: #212124;
  --border: #2a2a2e;
  --text: #e8e8ea;
  --text-dim: #8a8a90;
  --accent: #4a9eff;
  --accent-hover: #6ab0ff;
  --success: #34d399;
}

html, body { height: 100dvh; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* Top bar */
.topbar {
  min-height: 56px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  gap: 12px;
  flex-shrink: 0;
}
.brand { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; flex-shrink: 0; }
.brand span { color: var(--accent); }
.brand em { font-style: normal; color: var(--text-dim); font-weight: 400; font-size: 12px; margin-left: 6px; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}
.btn:hover:not(:disabled) { background: #2a2a2e; border-color: #3a3a3e; }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-accent { background: var(--success); border-color: var(--success); color: #06231a; font-weight: 600; }
.btn-accent:hover:not(:disabled) { filter: brightness(1.1); }

/* Layout */
.layout { display: flex; flex: 1; min-height: 0; }

.sidebar {
  width: 300px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: #2a2a2e; border-radius: 4px; }
.sidebar::-webkit-scrollbar-thumb:hover { background: #3a3a3e; }

.section { border-bottom: 1px solid var(--border); padding: 14px 16px; }
.section-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  font-weight: 700;
  margin-bottom: 12px;
}

.control { margin-bottom: 16px; }
.control:last-child { margin-bottom: 0; }
.control-header {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 8px;
  color: #c8c8cc;
}
.control-header .value { color: var(--text-dim); font-variant-numeric: tabular-nums; }

.hint {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 6px;
  opacity: 0.7;
}

input[type="range"] {
  width: 100%;
  height: 3px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--panel-2);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 15px;
  height: 15px;
  background: var(--text);
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.1s;
}
input[type="range"]:hover::-webkit-slider-thumb { background: var(--accent); }
input[type="range"]::-moz-range-thumb {
  width: 15px; height: 15px; background: var(--text);
  border-radius: 50%; cursor: pointer; border: none;
}
input[type="range"]:hover::-moz-range-thumb { background: var(--accent); }

select {
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  margin-bottom: 8px;
  outline: none;
}
select:last-child { margin-bottom: 0; }

.toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #c8c8cc;
  margin-bottom: 12px;
  cursor: pointer;
  user-select: none;
}
.toggle-row:last-child { margin-bottom: 0; }
.toggle-row input[type="checkbox"] {
  accent-color: var(--accent);
  cursor: pointer;
  width: 16px;
  height: 16px;
}

.sidebar-footer {
  padding: 14px 16px 20px;
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  line-height: 1.5;
  opacity: 0.7;
}

/* Stage */
.stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a0a0b;
  position: relative;
  overflow: hidden;
  padding: 24px;
  min-width: 0;
  min-height: 0;
}

canvas {
  max-width: 100%;
  max-height: 100%;
  border-radius: 4px;
  display: none;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
}
canvas.visible { display: block; }

.empty-state {
  color: var(--text-dim);
  text-align: center;
  font-size: 14px;
  padding: 20px;
}
.empty-state svg { display: block; margin: 0 auto 16px; opacity: 0.35; }
.empty-state .sub { margin-top: 8px; font-size: 11px; opacity: 0.6; }

.loading {
  position: absolute;
  inset: 0;
  background: rgba(10,10,11,0.85);
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 10;
}
.loading.visible { display: flex; }
.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-right: 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   TOUCH DEVICES — bigger targets, thicker sliders
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
  input[type="range"] { height: 5px; }
  input[type="range"]::-webkit-slider-thumb {
    width: 26px; height: 26px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.5);
  }
  input[type="range"]::-moz-range-thumb {
    width: 26px; height: 26px;
  }
  .control { margin-bottom: 20px; }
  .btn { padding: 9px 14px; }
  .toggle-row input[type="checkbox"] { width: 20px; height: 20px; }
  select { padding: 10px 12px; font-size: 13px; }
}

/* ============================================================
   MOBILE — stack layout, canvas on top, sidebar below
   ============================================================ */
@media (max-width: 768px) {
  body { overflow: hidden; }

  .topbar {
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .brand { font-size: 14px; }
  .actions { gap: 6px; }
  .btn { padding: 8px 12px; font-size: 12px; }

  .layout {
    flex-direction: column-reverse;
    overflow: hidden;
  }

  .stage {
    flex: 1 1 55%;
    min-height: 45vh;
    padding: 12px;
  }

  .sidebar {
    width: 100%;
    max-height: 45vh;
    flex-shrink: 0;
    border-right: none;
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .empty-state svg { width: 44px; height: 44px; }
  canvas { box-shadow: 0 6px 20px rgba(0,0,0,0.5); }
}

/* ============================================================
   TINY PHONES — trim a bit more
   ============================================================ */
@media (max-width: 380px) {
  .brand em { display: none; }
  .btn { padding: 7px 10px; font-size: 11px; }
  .section { padding: 12px 12px; }
}