:root {
  --bg: #0b101b;
  --surface: #131b2e;
  --surface-2: #1c2742;
  --surface-3: #253356;
  --border: #2a395c;
  --text: #f1f5f9;
  --muted: #9db0ce;
  --amber: #f59e0b;
  --amber-hover: #fbbf24;
  --amber-text: #0b101b;
  --cyan: #38bdf8;
  --cyan-soft: rgba(56, 189, 248, 0.14);
  --amber-soft: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #fb923c;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.36);
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #e2e8f0;
  --border: #cbd5e1;
  --text: #0f172a;
  --muted: #475569;
  --amber: #d97706;
  --amber-hover: #b45309;
  --amber-text: #ffffff;
  --cyan: #0284c7;
  --cyan-soft: rgba(2, 132, 199, 0.12);
  --amber-soft: rgba(217, 119, 6, 0.12);
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #ea580c;
  --shadow: 0 6px 20px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.84rem;
}

.stack-xs {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.stack-sm {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.stack-md {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.row-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.row-between {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

/* Login */
.login-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.login-card {
  width: 100%;
  max-width: 410px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.65rem;
  box-shadow: var(--shadow);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 1.2rem;
}

.login-brand h1 {
  margin: 0;
  font-size: 1.25rem;
}

.login-brand p {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

/* App Shell */
.app-shell {
  max-width: 1460px;
  margin: 0 auto;
  padding: 0.75rem 0.9rem 2.5rem;
}

@media (min-width: 980px) {
  .app-shell {
    padding: 0.95rem 1.35rem 2.5rem;
  }
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem 0.95rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}

.brand-mini {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-mini img {
  width: 34px;
  height: 34px;
  border-radius: 9px;
}

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

.brand-mini small {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.nav-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.nav-tab {
  appearance: none;
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 0.48rem 0.85rem;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.15s ease;
}

.nav-tab:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-tab.active {
  background: var(--cyan-soft);
  border-color: rgba(56, 189, 248, 0.4);
  color: var(--cyan);
}

/* Cards & Controls */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.card-sub {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.8rem;
}

.section-title {
  margin: 0 0 0.65rem;
  font-size: 0.98rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

label.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="search"],
select,
textarea {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.52rem 0.68rem;
  font: inherit;
  font-size: 0.92rem;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--cyan);
  outline-offset: -1px;
}

textarea {
  resize: vertical;
  min-height: 76px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.65rem;
}

@media (max-width: 680px) {
  .grid-2,
  .grid-3 {
    grid-template-columns: 1fr;
  }
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0.52rem 0.85rem;
  border-radius: 9px;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: 0.14s ease;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--surface-3);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.82rem;
}

.btn-primary {
  background: var(--amber);
  border-color: var(--amber);
  color: var(--amber-text);
}

.btn-primary:hover:not(:disabled) {
  background: var(--amber-hover);
  border-color: var(--amber-hover);
}

.btn-cyan {
  background: var(--cyan-soft);
  border-color: rgba(56, 189, 248, 0.45);
  color: var(--cyan);
}

.btn-cyan:hover:not(:disabled) {
  background: rgba(56, 189, 248, 0.24);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.14);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

[data-theme="light"] .btn-danger {
  color: #b91c1c;
}

.btn-block {
  width: 100%;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.77rem;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.badge-ok {
  background: rgba(34, 197, 94, 0.15);
  border-color: rgba(34, 197, 94, 0.45);
  color: #4ade80;
}

.badge-warn {
  background: rgba(251, 146, 60, 0.16);
  border-color: rgba(251, 146, 60, 0.45);
  color: #fdba74;
}

.badge-bad {
  background: rgba(239, 68, 68, 0.16);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

[data-theme="light"] .badge-ok {
  color: #15803d;
}
[data-theme="light"] .badge-warn {
  color: #c2410c;
}
[data-theme="light"] .badge-bad {
  color: #b91c1c;
}

/* Generate Tab */
.gen-layout {
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  gap: 1rem;
  align-items: start;
}

@media (max-width: 1020px) {
  .gen-layout {
    grid-template-columns: 1fr;
  }
}

.family-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.38rem;
}

.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.3rem 0.68rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.chip:hover {
  color: var(--text);
  border-color: var(--cyan);
}

.chip.active {
  background: var(--cyan-soft);
  border-color: var(--cyan);
  color: var(--cyan);
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.presets-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(195px, 1fr));
  gap: 0.55rem;
  max-height: 455px;
  overflow-y: auto;
  padding-right: 0.2rem;
}

.preset-card {
  text-align: left;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.62rem 0.72rem;
  color: var(--text);
  cursor: pointer;
  transition: 0.12s ease;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.preset-card:hover {
  border-color: var(--cyan);
}

.preset-card.active {
  border-color: var(--amber);
  background: var(--amber-soft);
}

.preset-head {
  display: flex;
  align-items: center;
  gap: 0.42rem;
  font-weight: 700;
  font-size: 0.86rem;
}

.preset-desc {
  color: var(--muted);
  font-size: 0.77rem;
  line-height: 1.32;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem;
  text-align: center;
  background: var(--surface-2);
  cursor: pointer;
  transition: 0.14s ease;
}

.dropzone:hover,
.dropzone.dragover {
  border-color: var(--cyan);
  background: var(--cyan-soft);
}

.ref-preview-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.ref-preview-row img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}

/* Studio / Cutter Tab */
.studio-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  margin-bottom: 0.85rem;
}

.title-edit-input {
  max-width: 310px;
  font-weight: 700;
}

.studio-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) 390px;
  gap: 1rem;
  align-items: start;
}

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

.canvas-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.45rem;
  margin-bottom: 0.55rem;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  height: 530px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: #090d16;
  touch-action: none;
}

@media (max-width: 720px) {
  .canvas-wrap {
    height: 390px;
  }
}

.canvas-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}

.canvas-wrap canvas:active {
  cursor: grabbing;
}

.preview-strip-card {
  margin-top: 0.85rem;
}

.preview-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding: 0.55rem;
  border-radius: var(--radius-sm);
  background-color: #101727;
  background-image:
    linear-gradient(45deg, rgba(255, 255, 255, 0.04) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.04) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255, 255, 255, 0.04) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255, 255, 255, 0.04) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  min-height: 104px;
  align-items: center;
}

.preview-chip {
  appearance: none;
  border: 1px solid transparent;
  background: rgba(15, 23, 42, 0.68);
  border-radius: 10px;
  padding: 0.42rem;
  color: #e2e8f0;
  font: inherit;
  font-size: 0.74rem;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
}

.preview-chip.active {
  border-color: var(--amber);
  background: rgba(245, 158, 11, 0.16);
}

.preview-empty {
  margin: 0.5rem auto;
  font-size: 0.86rem;
}

.shape-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.35rem;
}

.shape-tab {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 8px;
  padding: 0.42rem 0.3rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.shape-tab.active {
  background: var(--cyan-soft);
  border-color: var(--cyan);
  color: var(--cyan);
}

.size-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.32rem;
}

.size-pill {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 7px;
  padding: 0.28rem 0.52rem;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.size-pill:hover,
.size-pill.active {
  border-color: var(--amber);
  background: var(--amber-soft);
  color: var(--amber);
}

.cutouts-list {
  display: flex;
  flex-direction: column;
  gap: 0.32rem;
  max-height: 170px;
  overflow-y: auto;
}

.cutout-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.38rem 0.55rem;
  font-size: 0.8rem;
  cursor: pointer;
}

.cutout-row.active {
  border-color: var(--amber);
  background: var(--amber-soft);
}

/* Library */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 0.9rem;
}

.lib-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.lib-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: #090d16;
  cursor: pointer;
}

.lib-body {
  padding: 0.78rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  flex: 1;
}

.lib-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
}

.lib-meta {
  font-size: 0.78rem;
  color: var(--muted);
}

/* Modal & Toast */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 16, 0.74);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 60;
}

.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.toast {
  position: fixed;
  bottom: 1.1rem;
  right: 1.1rem;
  z-index: 90;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--cyan);
  border-radius: 10px;
  padding: 0.65rem 0.95rem;
  font-size: 0.88rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  max-width: min(92vw, 380px);
}

.toast.error {
  border-color: var(--danger);
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.84rem;
  color: var(--text);
  cursor: pointer;
}

.checkbox-row input {
  width: auto;
  accent-color: var(--amber);
}

.spinner {
  width: 15px;
  height: 15px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
}

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