* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-app: #0a0b0f;
  --bg-panel: #111319;
  --bg-surface: #171a24;
  --bg-surface-hover: #212533;
  --border-color: #272b38;
  --border-subtle: #1d202b;
  --text-primary: #f1f2f6;
  --text-secondary: #c7c9d3;
  --text-muted: #8b90a5;
  --text-faint: #5e6378;
  --accent: #ee4d2d;
  --accent-hover: #ff6242;
  --checker-color: #181b24;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body.light-theme {
  --bg-app: #f3f4f6;
  --bg-panel: #ffffff;
  --bg-surface: #f8f9fb;
  --bg-surface-hover: #eef0f4;
  --border-color: #dcdfe6;
  --border-subtle: #e6e8ee;
  --text-primary: #181a20;
  --text-secondary: #374151;
  --text-muted: #5a6275;
  --text-faint: #8c93a4;
  --checker-color: #dde1e8;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-app);
  color: var(--text-primary);
  overflow: hidden;
  overscroll-behavior: none;
}

#appContainer {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* 1. FULLSCREEN VIEWPORT */
.main-viewport-area {
  flex: 1;
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--bg-app);
  overflow: hidden;
}

.main-viewport-area.bg-black { background: #000000 !important; }
.main-viewport-area.bg-black .viewport-grid { opacity: 0; }
.main-viewport-area.bg-greenscreen { background: #00b140 !important; }
.main-viewport-area.bg-greenscreen .viewport-grid { opacity: 0; }

.viewport-grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(45deg, var(--checker-color) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-color) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-color) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-color) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
  opacity: 0.65;
  pointer-events: none;
}

.render-zone {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.scene-frame {
  width: 100%;
  height: 100%;
  border: none;
  display: none;
  background: transparent;
}
.scene-frame.active { display: block; }

/* TOP FLOATING PILLS */
.top-floating-bar {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  z-index: 15;
  pointer-events: none;
}

.top-pill-group {
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}

.top-pill {
  background: rgba(17, 19, 25, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: 7px 13px;
  font-size: 11.5px;
  font-weight: 700;
  color: #f1f2f6;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
.top-pill:active { transform: scale(0.95); }
.top-pill.accent {
  background: var(--accent);
  border-color: rgba(255, 255, 255, 0.25);
  color: #fff;
}

/* EMPTY STATE */
.viewport-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  z-index: 2;
  pointer-events: none;
}
.viewport-empty.hidden { display: none; }
.viewport-empty-icon {
  width: 60px;
  height: 60px;
  border-radius: 18px;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  margin-bottom: 14px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
}
.viewport-empty-title {
  font-size: 15px;
  font-weight: 800;
  margin-bottom: 6px;
  color: var(--text-primary);
}
.viewport-empty-text {
  font-size: 12.5px;
  color: var(--text-muted);
  max-width: 290px;
  line-height: 1.5;
}

/* LOADING OVERLAY */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 11, 15, 0.85);
  backdrop-filter: blur(8px);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 60;
}
.loading-overlay.active { display: flex; }
.spinner-ring {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(238, 77, 45, 0.25);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 14px; font-weight: 700; margin-top: 14px; color: #fff; }
.loading-sub { font-size: 12px; color: var(--text-muted); margin-top: 6px; text-align: center; }

/* FLOATING PLAYBACK & EXPORT FAB */
.floating-playback {
  position: absolute;
  bottom: calc(68px + var(--safe-bottom));
  left: 12px;
  right: 74px;
  background: rgba(17, 19, 25, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 8px 12px;
  display: none;
  align-items: center;
  gap: 10px;
  z-index: 15;
}
.floating-playback.active { display: flex; }
.pb-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  padding: 2px 4px;
}
.pb-scrubber { flex: 1; accent-color: var(--accent); }

.fab-export {
  position: absolute;
  bottom: calc(68px + var(--safe-bottom));
  right: 12px;
  width: 50px;
  height: 50px;
  border-radius: 25px;
  background: var(--accent);
  color: #fff;
  border: none;
  box-shadow: 0 6px 20px rgba(238, 77, 45, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 15;
  cursor: pointer;
}
.fab-export.active { display: flex; }
.fab-export:active { transform: scale(0.92); }

/* 2. BOTTOM NAVIGATION BAR */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(58px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: stretch;
  justify-content: space-around;
  z-index: 30;
}
.nav-item {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 10.5px;
  font-weight: 700;
  cursor: pointer;
}
.nav-item.active { color: var(--accent); }
.nav-item:active { background: var(--bg-surface); }

/* 3. BOTTOM SHEETS & BACKDROP */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  display: none;
}
.sheet-backdrop.active { display: block; }

.bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 680px;
  margin: 0 auto;
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  border-top-left-radius: 22px;
  border-top-right-radius: 22px;
  box-shadow: 0 -10px 35px rgba(0, 0, 0, 0.65);
  z-index: 50;
  transform: translateY(105%);
  transition: transform 0.26s cubic-bezier(0.22, 1, 0.36, 1);
  display: flex;
  flex-direction: column;
  max-height: 84dvh;
  padding-bottom: calc(14px + var(--safe-bottom));
}
.bottom-sheet.active { transform: translateY(0); }

.sheet-drag-handle {
  width: 40px;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  margin: 10px auto 6px;
  flex-shrink: 0;
}
.sheet-header {
  padding: 4px 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sheet-title { font-size: 14.5px; font-weight: 800; color: var(--text-primary); }
.sheet-close {
  width: 30px;
  height: 30px;
  border-radius: 15px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.sheet-content {
  overflow-y: auto;
  padding: 16px 18px;
  flex: 1;
}

/* FORMS & CONTROLS */
.form-group { margin-bottom: 14px; }
.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-group label {
  display: block;
  font-size: 11.5px;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 700;
}
.form-input, .form-select {
  width: 100%;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 11px;
  color: var(--text-primary);
  padding: 11px 13px;
  font-size: 13.5px;
  outline: none;
}
.form-input:focus, .form-select:focus { border-color: var(--accent); }

.prompt-textarea {
  width: 100%;
  height: 92px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 12px;
  color: var(--text-primary);
  font-size: 13.5px;
  line-height: 1.45;
  resize: none;
  outline: none;
}
.prompt-textarea:focus { border-color: var(--accent); }

.ai-chips-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 12px;
}
.ai-chip {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: 6px 13px;
  font-size: 11.5px;
  color: var(--text-secondary);
  white-space: nowrap;
  font-weight: 700;
  cursor: pointer;
}
.ai-chip.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(238, 77, 45, 0.12);
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary:active:not(:disabled) { transform: scale(0.98); }

.btn-secondary {
  width: 100%;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 11px;
  padding: 10px 12px;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-secondary:active { border-color: var(--accent); }

/* PRESET & GALLERY CARDS */
.preset-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 11px 13px;
  margin-bottom: 9px;
  cursor: pointer;
}
.preset-card:active { border-color: var(--accent); }
.preset-card-title { font-size: 12.5px; font-weight: 800; color: var(--text-primary); margin-bottom: 3px; }
.preset-card-desc { font-size: 11px; color: var(--text-muted); line-height: 1.4; }

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.gallery-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 8px;
  cursor: pointer;
}
.gallery-thumb {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 8px;
  object-fit: cover;
  background: #000;
  margin-bottom: 6px;
}
.gallery-card-title { font-size: 11.5px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* PARAMETER SLIDERS */
.ctrl-row {
  padding: 11px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.ctrl-label-row {
  display: flex;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 8px;
}
input[type="range"] { width: 100%; accent-color: var(--accent); height: 26px; }
input[type="color"] { width: 100%; height: 38px; border: 1px solid var(--border-color); border-radius: 8px; background: var(--bg-surface); }

/* TOAST */
.toast {
  position: fixed;
  top: calc(14px + var(--safe-top));
  left: 14px;
  right: 14px;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 12.5px;
  font-weight: 700;
  display: none;
  z-index: 9999;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}
.toast.active { display: block; }
.error-toast { background: #2c1414; border: 1px solid #632525; color: #f8b4b4; }
.success-toast { background: #132a17; border: 1px solid #225e2b; color: #a7f3d0; }
