:root {
  --bg: #070910;
  --panel: rgba(16, 20, 34, 0.88);
  --panel-border: rgba(255, 255, 255, 0.08);
  --surface: rgba(255, 255, 255, 0.04);
  --surface-hover: rgba(255, 255, 255, 0.08);
  --text: #eef2ff;
  --muted: #94a3b8;
  --accent: #6366f1;
  --accent-2: #22d3ee;
  --danger: #f43f5e;
  --radius: 14px;
  --radius-sm: 10px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 12% 10%, rgba(99, 102, 241, 0.16), transparent 36%),
    radial-gradient(circle at 88% 85%, rgba(34, 211, 238, 0.12), transparent 38%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1580px;
  margin: 0 auto;
  padding: 14px 18px 20px;
  gap: 14px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.brand-mark span {
  border-radius: 3px;
}
.brand-mark span:nth-child(1) { background: #6366f1; }
.brand-mark span:nth-child(2) { background: #22d3ee; }
.brand-mark span:nth-child(3) { background: #f472b6; }
.brand-mark span:nth-child(4) { background: #fbbf24; }

.brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.stats-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--surface);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats-pill .dot {
  opacity: 0.45;
}

/* Workspace 3-column grid */
.workspace {
  display: grid;
  grid-template-columns: 315px minmax(360px, 1fr) 315px;
  gap: 14px;
  align-items: start;
  flex: 1;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 14px;
  backdrop-filter: blur(14px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel-section {
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.panel-section + .panel-section {
  padding-top: 14px;
  border-top: 1px solid var(--panel-border);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-header h2 {
  margin: 0;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}

.tiny-note {
  font-size: 0.72rem;
  color: var(--accent-2);
  background: rgba(34, 211, 238, 0.12);
  padding: 2px 8px;
  border-radius: 999px;
}

/* Upload Cards */
.upload-card {
  display: flex;
  gap: 11px;
  padding: 10px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px dashed rgba(255, 255, 255, 0.14);
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.15s ease;
  cursor: pointer;
}

.upload-card:hover,
.upload-card.dragover {
  border-color: var(--accent-2);
  background: rgba(34, 211, 238, 0.08);
}

.upload-thumb-wrap {
  position: relative;
  width: 74px;
  height: 74px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
  background: #0d101c;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.upload-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.upload-tag {
  position: absolute;
  bottom: 3px;
  left: 3px;
  font-size: 0.58rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 2px 5px;
  border-radius: 4px;
  background: rgba(99, 102, 241, 0.9);
  color: #fff;
}

.target-tag {
  background: rgba(14, 165, 233, 0.92);
}

.upload-meta {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
  flex: 1;
}

.upload-title {
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.upload-hint {
  font-size: 0.72rem;
  color: var(--muted);
}

.upload-controls {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

/* Form Fields */
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.field-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

label {
  font-size: 0.8rem;
  color: #cbd5e1;
  font-weight: 500;
}

.field-help {
  font-size: 0.7rem;
  color: var(--muted);
  line-height: 1.35;
}

.value-badge {
  font-size: 0.74rem;
  color: var(--accent-2);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

select {
  width: 100%;
  padding: 7px 10px;
  border-radius: 8px;
  background: #101526;
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text);
  font-size: 0.8rem;
  outline: none;
  cursor: pointer;
}

select:focus {
  border-color: var(--accent);
}

.select-sm {
  padding: 4px 6px;
  font-size: 0.74rem;
  flex: 1;
  min-width: 0;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent-2);
  cursor: pointer;
  margin: 2px 0;
}

/* Buttons */
.btn {
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--surface);
  color: var(--text);
  border-radius: 9px;
  padding: 7px 13px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.btn:hover {
  background: var(--surface-hover);
  border-color: rgba(255, 255, 255, 0.22);
}

.btn:active {
  transform: scale(0.98);
}

.btn-sm {
  padding: 4px 9px;
  font-size: 0.74rem;
  border-radius: 7px;
}

.btn-primary {
  background: linear-gradient(135deg, #6366f1, #0ea5e9);
  border-color: transparent;
  color: #fff;
}

.btn-primary:hover {
  filter: brightness(1.08);
  border-color: transparent;
}

.btn-accent {
  background: rgba(99, 102, 241, 0.2);
  border-color: rgba(99, 102, 241, 0.5);
  color: #e0e7ff;
}

.btn-accent:hover {
  background: rgba(99, 102, 241, 0.34);
}

.btn-ghost {
  background: transparent;
}

.btn-full {
  width: 100%;
  padding: 9px 12px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent-2);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}

.btn-link:hover {
  background: rgba(34, 211, 238, 0.12);
}

/* Center Stage */
.stage-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.canvas-frame {
  position: relative;
  width: 100%;
  max-width: 680px;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  overflow: hidden;
  background: #090b13;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow:
    0 24px 70px rgba(0, 0, 0, 0.65),
    0 0 0 1px rgba(99, 102, 241, 0.18);
}

#morphCanvas,
#drawCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

#drawCanvas {
  cursor: crosshair;
  touch-action: none;
}

#drawCanvas[hidden],
#morphCanvas[hidden] {
  display: none;
}

/* Workspace Mode Tabs */
.workspace-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
}

.tab-pill {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 650;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab-pill:hover {
  color: var(--text);
}

.tab-pill.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.42), rgba(34, 211, 238, 0.25));
  color: #fff;
}

.tab-badge {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(34, 211, 238, 0.2);
  color: #a5f3fc;
}

/* Live Background Solve Floating Pill */
.solve-overlay {
  position: absolute;
  inset: auto 0 14px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 10;
}

.solve-overlay[hidden] {
  display: none;
}

.solve-pill {
  width: min(320px, calc(100% - 28px));
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(12, 16, 28, 0.86);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  gap: 11px;
}

.solve-pill-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.solve-pill-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.spinner-sm {
  width: 18px;
  height: 18px;
  border-width: 2.5px;
  flex-shrink: 0;
}

/* Draw Toolbar */
.draw-toolbar {
  width: 100%;
  max-width: 680px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 11px 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

.draw-toolbar[hidden],
.transport-bar[hidden] {
  display: none;
}

.draw-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.swatch-strip {
  display: flex;
  align-items: center;
  gap: 5px;
}

.swatch-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.2);
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.swatch-btn:hover {
  transform: scale(1.12);
}

.swatch-btn.active {
  border-color: #fff;
  transform: scale(1.14);
  box-shadow: 0 0 0 2px rgba(34, 211, 238, 0.5);
}

#drawColorPicker {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  padding: 0;
}

.draw-slider-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 120px;
}

.draw-slider-group label {
  font-size: 0.74rem;
  color: var(--muted);
  white-space: nowrap;
}

.draw-actions {
  justify-content: space-between;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--accent-2);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.solve-stage {
  font-size: 0.84rem;
  font-weight: 600;
}

.progress-track {
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.12s linear;
}

.solve-pct {
  font-size: 0.76rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.record-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border-radius: 999px;
  background: rgba(244, 63, 94, 0.9);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  box-shadow: 0 4px 16px rgba(244, 63, 94, 0.4);
}

.record-badge[hidden] {
  display: none;
}

.rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #fff;
  animation: pulse 0.9s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* Transport Bar */
.transport-bar {
  width: 100%;
  max-width: 680px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  flex-wrap: wrap;
}

.transport-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-icon {
  width: 36px;
  height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
}

.btn-toggle.active {
  background: rgba(34, 211, 238, 0.18);
  border-color: rgba(34, 211, 238, 0.5);
  color: #cffafe;
}

.scrubber-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 180px;
}

.scrub-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.scrub-pct {
  width: 36px;
  text-align: right;
  font-size: 0.76rem;
  color: var(--accent-2);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.speed-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 145px;
}

.speed-wrap label {
  font-size: 0.74rem;
  color: var(--muted);
}

/* Mode Cards */
.mode-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.mode-card {
  text-align: left;
  width: 100%;
  padding: 9px 11px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.mode-card:hover {
  background: var(--surface-hover);
  border-color: rgba(255, 255, 255, 0.18);
}

.mode-card.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.24), rgba(34, 211, 238, 0.14));
  border-color: rgba(34, 211, 238, 0.55);
}

.mode-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 3px;
}

.mode-name {
  font-size: 0.84rem;
  font-weight: 650;
}

.mode-badge {
  font-size: 0.64rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #cbd5e1;
}

.mode-card.active .mode-badge {
  background: rgba(34, 211, 238, 0.22);
  color: #a5f3fc;
}

.mode-desc {
  font-size: 0.71rem;
  color: var(--muted);
  line-height: 1.35;
}

/* Segmented Control */
.subtle-presets {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}

.chip-btn {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 600;
  padding: 5px 4px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.chip-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.chip-btn.active {
  background: rgba(34, 211, 238, 0.2);
  border-color: rgba(34, 211, 238, 0.55);
  color: #cffafe;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 3px;
  background: rgba(0, 0, 0, 0.28);
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 600;
  padding: 6px 4px;
  border-radius: 7px;
  cursor: pointer;
}

.seg-btn.active {
  background: rgba(99, 102, 241, 0.35);
  color: #fff;
}

/* Switch */
.toggle-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.switch {
  appearance: none;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  position: relative;
  cursor: pointer;
  transition: background 0.18s ease;
  flex-shrink: 0;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.18s ease;
}

.switch:checked {
  background: var(--accent-2);
}

.switch:checked::after {
  transform: translateX(18px);
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 14, 0.72);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: rgba(16, 20, 36, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 16px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--panel-border);
}

.modal-header h3 {
  margin: 0;
  font-size: 0.96rem;
  font-weight: 700;
}

.btn-icon-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 4px 7px;
  border-radius: 6px;
}

.btn-icon-close:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.modal-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--panel-border);
  background: rgba(0, 0, 0, 0.18);
}

.segmented-2 {
  grid-template-columns: repeat(2, 1fr);
}

.segmented-4 {
  grid-template-columns: repeat(4, 1fr);
}

/* Responsive */
@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 290px 1fr;
  }
  .panel-right {
    grid-column: 1 / -1;
  }
  .mode-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  }
}

@media (max-width: 820px) {
  .workspace {
    grid-template-columns: 1fr;
  }
}
