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

:root {
  --bg: #0a0a0b;
  --surface: #111113;
  --surface2: #1a1a1d;
  --surface3: #242428;
  --border: #2a2a2f;
  --border-light: #3a3a40;
  --text: #f0f0f4;
  --text-secondary: #8e8e99;
  --text-muted: #4a4a55;
  --accent: #0066ff;
  --accent-bright: #2979ff;
  --accent-hover: #4d8eff;
  --accent-glow: rgba(0, 102, 255, 0.25);
  --accent-subtle: rgba(0, 102, 255, 0.08);
  --accent-mid: rgba(0, 102, 255, 0.15);
  --green: #00c47a;
  --red: #ff3b55;
  --radius: 10px;
  --radius-lg: 16px;
  --sidebar-width: clamp(240px, 22vw, 360px);
  --header-height: 60px;
  --font-sans: 'Sora', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

/* ─── Base ─────────────────────────────────────────────────────────────────── */

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ─── Header ───────────────────────────────────────────────────────────────── */

header {
  height: var(--header-height);
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(20px);
}

.header-brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

header h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.6px;
  color: var(--text);
  line-height: 1;
}

header h1 em {
  font-style: normal;
  color: var(--accent-bright);
}

.header-tag {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 2px 7px;
  border-radius: 20px;
}

header .subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
  letter-spacing: 0.1px;
}

/* ─── Sidebar section row (label + toggle on same line) ───────────────────── */

.section-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.section-row .section-label {
  margin-bottom: 0;
}

/* ─── Sidebar Toggle Button ────────────────────────────────────────────────── */

.sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  color: var(--text-muted);
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.sidebar-toggle svg {
  width: 16px;
  height: 16px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-toggle:hover {
  border-color: var(--border-light);
  background: var(--surface3);
  color: var(--text-secondary);
}

/* Collapsed: button escapes sidebar and pins to left edge of viewport */
.app-layout.sidebar-collapsed .sidebar-toggle {
  position: fixed;
  left: 12px;
  top: calc(var(--header-height) + 18px);
  z-index: 50;
  background: var(--surface2);
  border-color: var(--border-light);
  box-shadow: 2px 0 12px rgba(0,0,0,0.4);
}

.app-layout.sidebar-collapsed .sidebar-toggle svg {
  transform: rotate(180deg);
}

/* ─── Layout ───────────────────────────────────────────────────────────────── */

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: calc(100vh - var(--header-height));
  transition: grid-template-columns 0.32s cubic-bezier(0.4, 0, 0.2, 1);
}

.app-layout.sidebar-collapsed {
  grid-template-columns: 0 1fr;
}

.app-layout.sidebar-collapsed .sidebar {
  overflow: hidden;
  border-right-color: transparent;
}

/* ─── Sidebar backdrop (mobile overlay) ───────────────────────────────────── */

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  backdrop-filter: blur(4px);
}

.sidebar-backdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

/* ─── Sidebar ──────────────────────────────────────────────────────────────── */

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

.section {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.section:last-child {
  border-bottom: none;
}

.section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

/* ─── Model picker ─────────────────────────────────────────────────────────── */

.model-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.model-btn {
  width: 100%;
  padding: 9px 12px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  transition: all 0.15s;
  font-family: var(--font-sans);
  position: relative;
  overflow: hidden;
}

.model-btn::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%) scaleY(0);
  width: 2px;
  height: 60%;
  background: var(--accent-bright);
  border-radius: 0 2px 2px 0;
  transition: transform 0.2s;
}

.model-btn:hover {
  border-color: var(--border-light);
  background: var(--surface2);
  color: var(--text);
}

.model-btn.active {
  border-color: var(--border-light);
  background: var(--surface2);
  color: var(--text);
}

.model-btn.active::before {
  transform: translateY(-50%) scaleY(1);
}

/* ─── Color picker ─────────────────────────────────────────────────────────── */

.color-grid {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.color-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 400;
  transition: all 0.15s;
  font-family: var(--font-sans);
  width: 100%;
  text-align: left;
  position: relative;
}

.color-btn::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%) scaleY(0);
  width: 2px;
  height: 60%;
  background: var(--accent-bright);
  border-radius: 0 2px 2px 0;
  transition: transform 0.2s;
}

.color-btn:hover {
  border-color: var(--border-light);
  background: var(--surface2);
  color: var(--text);
}

.color-btn.active {
  border-color: var(--border-light);
  background: var(--surface2);
  color: var(--text);
}

.color-btn.active::before {
  transform: translateY(-50%) scaleY(1);
}

.color-swatch {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.12);
  flex-shrink: 0;
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

/* ─── Upload area ──────────────────────────────────────────────────────────── */

.upload-area {
  border: 1.5px dashed var(--border-light);
  border-radius: var(--radius-lg);
  padding: 22px 16px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  background: var(--surface2);
}

.upload-area:hover,
.upload-area.drag-over {
  border-color: var(--accent-bright);
  background: var(--accent-subtle);
}

.upload-icon {
  font-size: 24px;
  margin-bottom: 8px;
  display: block;
  opacity: 0.5;
}

.upload-area p {
  color: var(--text-secondary);
  font-size: 12.5px;
  line-height: 1.5;
}

.upload-area .upload-hint {
  font-size: 10.5px;
  color: var(--text-muted);
  margin-top: 5px;
  letter-spacing: 0.3px;
}

#file-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.uploaded-name {
  margin-top: 10px;
  font-size: 11.5px;
  color: var(--accent-hover);
  word-break: break-all;
  display: none;
  font-family: var(--font-mono);
  background: var(--accent-subtle);
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* ─── Calibration ──────────────────────────────────────────────────────────── */

.calibration-section details {
  width: 100%;
}

.calibration-section summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  list-style: none;
  user-select: none;
  transition: color 0.15s;
}

.calibration-section summary:hover {
  color: var(--text-secondary);
}

.calibration-section summary::-webkit-details-marker { display: none; }

.calibration-section summary .arrow {
  transition: transform 0.2s;
  font-size: 9px;
  color: var(--text-muted);
}

.calibration-section details[open] summary .arrow {
  transform: rotate(90deg);
}

.cal-body {
  margin-top: 14px;
}

.cal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.cal-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cal-field label {
  font-size: 10px;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.cal-field input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 8px;
  font-size: 12px;
  font-family: var(--font-mono);
  width: 100%;
  transition: border-color 0.15s;
}

.cal-field input:focus {
  outline: none;
  border-color: var(--accent-bright);
  background: var(--surface3);
}

.btn-row {
  display: flex;
  gap: 6px;
}

.btn {
  flex: 1;
  padding: 7px 10px;
  border: none;
  border-radius: 8px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font-sans);
  letter-spacing: 0.1px;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  border-color: var(--border-light);
  color: var(--text);
  background: var(--surface3);
}

.btn-primary {
  background: var(--accent);
  color: white;
  letter-spacing: 0.2px;
}

.btn-primary:hover {
  background: var(--accent-bright);
}

.cal-status {
  margin-top: 8px;
  font-size: 10.5px;
  min-height: 16px;
  font-family: var(--font-mono);
  letter-spacing: 0.1px;
}

.cal-status.ok  { color: var(--green); }
.cal-status.err { color: var(--red); }
.cal-status.info { color: var(--text-muted); }

/* ─── Download button ──────────────────────────────────────────────────────── */

.download-section {
  padding: 18px 20px;
  padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}

#btn-download {
  width: 100%;
  padding: 11px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font-sans);
  letter-spacing: 0.1px;
  position: relative;
  overflow: hidden;
}

#btn-download::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(255,255,255,0.1), transparent);
  pointer-events: none;
}

#btn-download:hover:not(:disabled) {
  background: var(--accent-bright);
  transform: translateY(-1px);
  box-shadow: 0 8px 24px var(--accent-glow);
}

#btn-download:active:not(:disabled) {
  transform: translateY(0);
}

#btn-download:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* ─── Preview area ─────────────────────────────────────────────────────────── */

.preview-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  overflow: auto;
  background: var(--bg);
  position: relative;
}

/* Subtle grid texture */
.preview-area::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 32px 32px;
  opacity: 0.3;
  pointer-events: none;
}

.preview-placeholder {
  text-align: center;
  color: var(--text-secondary);
  position: relative;
  z-index: 1;
}

.preview-placeholder .big-icon {
  font-size: 56px;
  margin-bottom: 16px;
  display: block;
  opacity: 0.15;
  filter: grayscale(1);
}

.preview-placeholder p {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: -0.2px;
}

.preview-placeholder .sub {
  font-size: 12px;
  margin-top: 6px;
  color: var(--text-muted);
  font-weight: 400;
}

#preview-canvas {
  max-width: 100%;
  max-height: 100%;
  border-radius: 4px;
  box-shadow:
    0 40px 100px rgba(0,0,0,0.8),
    0 0 0 1px rgba(255,255,255,0.04);
  display: none;
  position: relative;
  z-index: 1;
}

.frame-status {
  position: absolute;
  bottom: 16px;
  right: 16px;
  font-size: 10.5px;
  font-family: var(--font-mono);
  color: var(--text-muted);
  background: rgba(17, 17, 19, 0.85);
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  backdrop-filter: blur(12px);
  z-index: 2;
}

/* ─── Scrollbar ────────────────────────────────────────────────────────────── */

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ─── Mobile open button (floating, outside sidebar) ──────────────────────── */

.sidebar-open-btn {
  display: none; /* hidden on desktop */
}

@media (max-width: 768px) {
  .sidebar-open-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    left: 12px;
    top: calc(var(--header-height) + 18px);
    z-index: 50;
    width: 36px;
    height: 36px;
    background: var(--surface2);
    border: 1px solid var(--border-light);
    border-radius: 9px;
    cursor: pointer;
    padding: 0;
    color: var(--text-secondary);
    box-shadow: 2px 0 12px rgba(0,0,0,0.4);
    transition: border-color 0.15s, background 0.15s, color 0.15s;
  }

  .sidebar-open-btn svg {
    width: 18px;
    height: 18px;
  }

  .sidebar-open-btn:hover {
    border-color: var(--accent-bright);
    background: var(--surface3);
    color: var(--text);
  }

  /* Hide the open button when sidebar is open (backdrop covers it anyway) */
  .sidebar-open-btn.hidden {
    display: none;
  }
}

/* ─── Mobile / Responsive ──────────────────────────────────────────────────── */

@media (max-width: 768px) {
  /* Show backdrop */
  .sidebar-backdrop {
    display: block;
  }

  /* Hide header subtitle on small screens */
  header .subtitle {
    display: none;
  }

  /* Full-height layout without sidebar column */
  .app-layout {
    grid-template-columns: 1fr;
    position: relative;
  }

  /* Sidebar becomes a fixed overlay drawer */
  .sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    width: min(var(--sidebar-width), 85vw);
    z-index: 201;
    transform: translateX(-105%);
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 4px 0 40px rgba(0, 0, 0, 0.6);
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  /* On mobile, never use the collapsed fixed-pin behavior */
  .app-layout.sidebar-collapsed .sidebar-toggle {
    position: static;
    box-shadow: none;
  }

  .app-layout.sidebar-collapsed .sidebar-toggle svg {
    transform: none;
  }

  /* Preview fills full width */
  .preview-area {
    padding: 24px 16px;
  }
}

@media (max-width: 400px) {
  header {
    padding: 0 14px;
    gap: 10px;
  }

  .header-tag {
    display: none;
  }
}
