:root {
  --bg: #f4f6fb;
  --surface: rgba(255, 255, 255, 0.72);
  --surface-solid: #ffffff;
  --border: rgba(148, 163, 184, 0.35);
  --text: #0f172a;
  --muted: #64748b;
  --accent: #6366f1;
  --accent-2: #22d3ee;
  --danger: #ef4444;
  --radius: 16px;
  --shadow: 0 12px 40px rgba(15, 23, 42, 0.08);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --tap: 44px;
}

body.theme-dark {
  --bg: #0b1020;
  --surface: rgba(15, 23, 42, 0.65);
  --surface-solid: #111827;
  --border: rgba(148, 163, 184, 0.2);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #818cf8;
  --accent-2: #38bdf8;
  --danger: #f87171;
  --shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.glass {
  background: var(--surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.app-root {
  max-width: 1600px;
  margin: 0 auto;
  padding: 12px 16px 32px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  font-size: 28px;
  color: var(--accent);
}

.brand-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-sub {
  margin: 0;
  font-size: 0.8rem;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.78rem;
}

.mono {
  font-family: ui-monospace, "Cascadia Code", monospace;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-solid);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
  min-height: var(--tap);
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 18px rgba(15, 23, 42, 0.1);
}

.btn:active {
  transform: translateY(0);
}

.btn.primary {
  background: linear-gradient(135deg, var(--accent), #4f46e5);
  color: #fff;
  border-color: transparent;
}

.btn.secondary {
  background: transparent;
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  min-height: 40px;
  padding: 8px 12px;
}

.btn.sm,
.mini-btn {
  padding: 6px 10px;
  font-size: 0.8rem;
  min-height: 36px;
}

.row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.row.wrap {
  flex-wrap: wrap;
}

.banner.premium {
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid rgba(251, 191, 36, 0.5);
  background: rgba(251, 191, 36, 0.12);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.app-body {
  display: grid;
  grid-template-columns: minmax(240px, 280px) 1fr minmax(240px, 300px);
  gap: 12px;
  flex: 1;
  min-height: 0;
}

@media (max-width: 1100px) {
  .app-body {
    grid-template-columns: 1fr;
  }

  .sidebar.right {
    order: 3;
  }

  .main-stage {
    order: 2;
  }

  .sidebar:not(.right) {
    order: 1;
  }
}

.sidebar {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(100dvh - 120px);
  overflow: auto;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-title {
  margin: 0 0 4px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
}

.field.inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.field input[type="number"],
.field input[type="text"],
.field select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  color: var(--text);
  font-size: 0.95rem;
  min-height: var(--tap);
}

body.theme-dark .field input,
body.theme-dark .field select {
  background: #0f172a;
  border-color: #334155;
}

.field input[type="color"] {
  width: 100%;
  height: var(--tap);
  padding: 4px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  cursor: pointer;
}

.check input {
  width: 20px;
  height: 20px;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.tool-btn {
  min-height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  font-size: 1.2rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s, border-color 0.2s;
}

.tool-btn:hover {
  border-color: var(--accent);
  transform: scale(1.02);
}

.tool-btn.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.25), rgba(34, 211, 238, 0.15));
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.25);
}

.tool-btn.locked {
  opacity: 0.45;
  cursor: not-allowed;
  filter: grayscale(0.4);
}

.tool-context p {
  margin: 0 0 6px;
}

.tool-context p.hidden {
  display: none;
}

.swatch-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.swatch {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 2px solid var(--border);
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s;
}

.swatch.sm {
  width: 24px;
  height: 24px;
}

.swatch:hover {
  transform: scale(1.08);
}

.swatch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.custom-palette-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px;
  align-items: start;
  margin-bottom: 8px;
}

.file-btn {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.file-btn input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.gradient-preview {
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, #6366f1, transparent);
}

.main-stage {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.floating-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
}

.toolbar-hint {
  flex: 1;
  min-width: 140px;
}

.zoom {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
}

.zoom input[type="range"] {
  width: min(220px, 40vw);
}

.canvas-shell {
  position: relative;
  flex: 1;
  min-height: min(72dvh, 720px);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
}

#mainCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
  position: relative;
  z-index: 1;
}

.empty-hint {
  position: absolute;
  inset: 0;
  z-index: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  pointer-events: none;
  color: var(--muted);
  text-align: center;
  padding: 24px;
}

.empty-art {
  font-size: 48px;
  opacity: 0.35;
}

.timeline {
  padding: 12px 14px;
}

.timeline-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.frame-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.frame-chip {
  min-width: var(--tap);
  min-height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}

.frame-chip.active {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.2);
}

.layer-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.layer-row {
  display: grid;
  grid-template-columns: auto 1fr minmax(72px, 1fr) auto;
  gap: 6px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.layer-row.active {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.12);
}

.layer-row.drag-over {
  outline: 2px dashed var(--accent);
}

.icon-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  padding: 4px;
}

.layer-name {
  font-size: 0.85rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-opacity {
  width: 100%;
}

.layer-actions {
  display: flex;
  gap: 4px;
}

.mini-btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-solid);
  cursor: pointer;
  padding: 4px 6px;
  font-size: 0.75rem;
}

.mini-btn.danger {
  border-color: rgba(239, 68, 68, 0.45);
  color: var(--danger);
}

.mini-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.onboarding {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 32px));
  padding: 18px 22px 18px 20px;
  z-index: 9000;
  animation: slideUp 0.35s ease;
}

.onboarding-close {
  position: absolute;
  top: 10px;
  right: 10px;
  border: none;
  background: transparent;
  font-size: 18px;
  cursor: pointer;
  color: var(--muted);
}

.onboarding h2 {
  margin: 0 0 10px;
  font-size: 1rem;
}

.onboarding-list {
  margin: 0;
  padding-left: 18px;
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--muted);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translate(-50%, 12px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

.tool-extra-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 8px;
}

.tool-toggle-btn {
  background: linear-gradient(135deg, var(--surface-solid), var(--bg));
  color: var(--text);
  border: 1px solid var(--border);
  padding: 14px 18px;
  border-radius: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.tool-toggle-btn:hover {
  transform: translateY(-2px);
}

body.theme-dark .tool-toggle-btn {
  background: linear-gradient(135deg, #111827, #0f172a);
  border-color: #334155;
}

.legal-popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  padding: 20px;
  animation: fadeIn 0.3s ease;
}

.legal-popup-box {
  width: 100%;
  max-width: 520px;
  background: #ffffff;
  border-radius: 22px;
  padding: 28px;
  box-shadow: 0 25px 70px rgba(0, 0, 0, 0.25);
  text-align: center;
  animation: popIn 0.35s ease;
}

body.theme-dark .legal-popup-box {
  background: #0f172a;
  color: #f9fafb;
}

.legal-popup-icon {
  font-size: 42px;
  margin-bottom: 12px;
}

.legal-popup-box h3 {
  margin: 0 0 14px;
  font-size: 24px;
  color: inherit;
}

.legal-popup-box p {
  color: var(--muted);
  font-size: 15px;
  line-height: 1.7;
  margin: 0 0 14px;
}

body.theme-dark .legal-popup-box p {
  color: #cbd5e1;
}

.legal-popup-box button {
  margin-top: 10px;
  background: #2563eb;
  color: #fff;
  border: none;
  padding: 14px 24px;
  border-radius: 14px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: 0.25s;
}

.popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  padding: 20px;
  min-height: 100vh;
}

.popup-overlay.active {
  display: flex;
  animation: fadeIn 0.25s ease;
}

.popup-box {
  width: 100%;
  max-width: 720px;
  background: var(--surface-solid);
  border-radius: 24px;
  padding: 28px;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.25);
  position: relative;
  animation: popIn 0.28s ease;
  max-height: calc(100vh - 40px);
  overflow: auto;
  color: var(--text);
  border: 1px solid var(--border);
}

.popup-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: #f1f5f9;
  font-size: 18px;
  cursor: pointer;
}

body.theme-dark .popup-close {
  background: #1e293b;
  color: #f8fafc;
}

textarea {
  width: 100%;
  min-height: 160px;
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  resize: vertical;
  font-family: monospace;
  background: var(--surface-solid);
  color: var(--text);
}

.copy-btn {
  margin-top: 14px;
  background: #10b981;
  color: #fff;
  border: none;
  padding: 12px 18px;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
}

.similar-tools {
  background: var(--bg);
  border-radius: 14px;
  padding: 12px;
}

#similarToolsList {
  list-style: none;
  padding: 0;
  margin: 0;
}

#similarToolsList li {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  background: var(--surface-solid);
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 600px) {
  .legal-popup-box {
    padding: 22px;
    border-radius: 18px;
  }

  .tool-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .layer-row {
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
  }

  .layer-opacity {
    grid-column: 1 / -1;
  }

  .layer-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }
}
