:root {
  color-scheme: dark;
  --ink: #0e0b09;
  --panel: #19130f;
  --panel-soft: #211914;
  --line: rgba(218, 177, 96, 0.22);
  --gold: #d8ad5b;
  --gold-bright: #f0cf82;
  --cream: #f5ead4;
  --muted: #ab9e8d;
  --danger: #f08a75;
  font-family: Inter, "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--ink);
  color: var(--cream);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 70% -20%, rgba(150, 93, 26, 0.18), transparent 38rem),
    linear-gradient(145deg, #120e0b 0%, #090807 60%, #15100c 100%);
}

button, input, textarea, select { font: inherit; }
button { color: inherit; }

.app-shell {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: 22px clamp(16px, 3vw, 44px) 36px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 60px;
  margin-bottom: 18px;
}

.brand-mark {
  display: grid;
  width: 44px;
  height: 50px;
  place-items: center;
  color: #17100a;
  font: 800 1.45rem Georgia, serif;
  background: linear-gradient(155deg, #f3d68e, #ad7429);
  clip-path: polygon(50% 0, 94% 17%, 84% 82%, 50% 100%, 16% 82%, 6% 17%);
  filter: drop-shadow(0 4px 14px rgba(216, 173, 91, 0.25));
}

.topbar h1 { margin: 0; font: 650 clamp(1.35rem, 2.2vw, 1.8rem)/1.1 Georgia, "Noto Serif SC", serif; letter-spacing: .04em; }
.topbar p { margin: 5px 0 0; color: var(--muted); font-size: .82rem; }
.ghost-button { margin-left: auto; border: 1px solid var(--line); background: transparent; padding: 9px 16px; border-radius: 8px; cursor: pointer; }
.ghost-button:hover, .ghost-button:focus-visible { border-color: var(--gold); color: var(--gold-bright); }

.workspace {
  display: grid;
  grid-template-columns: minmax(340px, 430px) minmax(430px, 1fr);
  gap: clamp(18px, 3vw, 44px);
  align-items: start;
}

.editor-panel, .preview-panel {
  min-width: 0;
  border: 1px solid var(--line);
  background: linear-gradient(150deg, rgba(31, 24, 19, .97), rgba(18, 14, 11, .98));
  box-shadow: 0 24px 70px rgba(0, 0, 0, .3);
}

.editor-panel { padding: 26px; border-radius: 18px; }
.preview-panel { position: sticky; top: 18px; padding: 22px; border-radius: 22px; overflow: hidden; }
.preview-panel::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
}

.section-heading { display: flex; align-items: center; gap: 10px; margin: 4px 0 18px; }
.section-heading:not(:first-child) { margin-top: 30px; }
.section-heading span { color: var(--gold); font: 700 .72rem/1 monospace; letter-spacing: .12em; }
.section-heading h2 { margin: 0; font: 600 1rem/1.2 Georgia, "Noto Serif SC", serif; letter-spacing: .08em; }

fieldset { border: 0; padding: 0; margin: 0 0 20px; }
legend, .field-label, .stat-grid label, .image-controls label { display: block; margin-bottom: 8px; color: #d8ccba; font-size: .88rem; font-weight: 600; }
.field-label output { float: right; color: var(--muted); font-size: .76rem; font-weight: 400; }

.template-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.card-type-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.rarity-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.template-option {
  position: relative;
  display: grid;
  grid-template-columns: 26px 1fr;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 10px;
  color: #cfc3b2;
  cursor: pointer;
  transition: border-color .16s ease, background .16s ease;
}
.template-option input { position: absolute; opacity: 0; pointer-events: none; }
.template-option.is-selected { border-color: var(--gold); background: rgba(216,173,91,.09); color: var(--cream); }
.template-option:focus-within { outline: 2px solid var(--gold-bright); outline-offset: 2px; }
.template-swatch { width: 26px; height: 30px; border: 2px solid #b88845; border-radius: 4px; background: #251c16; }
.template-swatch.classic { background: linear-gradient(#a87835 0 20%, #342a1d 20% 70%, #d4ad76 70%); }
.template-swatch.foil { border-color: #d7cbb8; background: linear-gradient(135deg, #b77999, #58a7af 48%, #d9b461); }
.template-swatch.card-animal { background: linear-gradient(#a87835 0 20%, #24352b 20% 70%, #b47b2b 70% 79%, #d4ad76 79%); }
.template-swatch.card-plant { background: linear-gradient(#a87835 0 20%, #31522e 20% 70%, #b47b2b 70% 79%, #d4ad76 79%); }
.template-swatch.card-nature { background: linear-gradient(#a87835 0 20%, #314837 20% 69%, #d4ad76 69%); }
.rarity-option {
  grid-template-columns: 1fr;
  justify-items: center;
  gap: 3px;
  min-height: 72px;
  padding: 6px 4px;
  font-size: .78rem;
}
.rarity-option img {
  display: block;
  width: 40px;
  height: 40px;
  object-fit: contain;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.48));
}

input[type="text"], input[type="number"], textarea, select {
  width: 100%;
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 9px;
  color: var(--cream);
  background: #100d0b;
  outline: 0;
  transition: border-color .16s ease, box-shadow .16s ease;
}
input[type="text"], input[type="number"], select { height: 45px; padding: 0 13px; }
textarea { min-height: 105px; padding: 12px 13px; line-height: 1.55; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(216,173,91,.12); }
.select-wrap { margin-bottom: 18px; }

.upload-area {
  padding: 10px;
  border: 1px dashed rgba(216,173,91,.34);
  border-radius: 12px;
  background: rgba(0,0,0,.16);
  transition: border-color .16s ease, background .16s ease;
}
.upload-area.is-dragging { border-color: var(--gold-bright); background: rgba(216,173,91,.09); }
.upload-button { display: flex; width: 100%; align-items: center; gap: 12px; padding: 6px; border: 0; text-align: left; background: transparent; cursor: pointer; }
.upload-icon { display: grid; flex: 0 0 38px; height: 38px; place-items: center; border-radius: 50%; color: #17100a; font-size: 1.4rem; background: var(--gold); }
.upload-button strong, .upload-button small { display: block; }
.upload-button small { margin-top: 4px; color: var(--muted); font-size: .75rem; }
.upload-area p { margin: 8px 6px 2px; color: var(--muted); font-size: .76rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-area.has-error p { color: var(--danger); }

.image-controls { padding: 14px 4px 0; }
.image-controls label { font-weight: 500; }
.image-controls output { float: right; color: var(--gold-bright); }
input[type="range"] { width: 100%; margin: 8px 0 0; accent-color: var(--gold); }
.two-columns, .stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat-grid { grid-template-columns: repeat(4, 1fr); margin: 18px 0; }
.stat-grid label { margin: 0; font-weight: 500; }
.stat-grid input { margin-top: 8px; }
.field-hint { margin: 7px 0 0; color: var(--muted); font-size: .76rem; }
.field-hint.is-warning { color: var(--danger); }

.preview-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.preview-header div { display: grid; gap: 4px; }
.eyebrow { color: var(--gold); font: 700 .68rem/1 monospace; letter-spacing: .16em; text-transform: uppercase; }
.preview-header strong { font-size: .88rem; font-weight: 500; }
.save-state { color: var(--muted); font-size: .75rem; }

.canvas-stage {
  position: relative;
  min-width: 0;
  display: grid;
  min-height: 520px;
  place-items: center;
  overflow: hidden;
  padding: clamp(14px, 3vw, 28px);
  border-radius: 16px;
  background:
    linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px),
    radial-gradient(circle at center, #30231a, #0c0a08 72%);
  background-size: 24px 24px, 24px 24px, auto;
}
#cardCanvas {
  display: block;
  width: min(100%, calc(67vh * 1080 / 1495));
  height: auto;
  max-height: 720px;
  aspect-ratio: 1080 / 1495;
  border-radius: 14px;
  box-shadow: 0 24px 48px rgba(0,0,0,.62), 0 0 0 1px rgba(255,255,255,.08);
}
.asset-error { position: absolute; inset: 50% auto auto 50%; transform: translate(-50%,-50%); display: grid; gap: 5px; width: min(280px,80%); padding: 18px; border: 1px solid rgba(240,138,117,.35); border-radius: 12px; text-align: center; background: rgba(24,12,9,.94); }
.asset-error span { color: var(--muted); font-size: .82rem; }
.asset-error[hidden] { display: none; }

.generate-button {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
  padding: 15px 20px;
  border: 1px solid #f2d181;
  border-radius: 11px;
  color: #1b1209;
  background: linear-gradient(180deg, #eccb7b, #bd8431);
  box-shadow: inset 0 1px rgba(255,255,255,.42), 0 8px 24px rgba(176,112,30,.2);
  cursor: pointer;
}
.generate-button:hover { filter: brightness(1.06); }
.generate-button:active { transform: translateY(1px); }
.generate-button:disabled { cursor: wait; opacity: .55; }
.generate-button span { font-weight: 800; letter-spacing: .04em; }
.generate-button small { padding-left: 12px; border-left: 1px solid rgba(31,19,7,.25); font-size: .72rem; }
.privacy-note { margin: 11px 0 0; color: var(--muted); text-align: center; font-size: .75rem; }

.toast { position: fixed; z-index: 20; right: 24px; bottom: 24px; max-width: min(340px, calc(100vw - 32px)); padding: 12px 16px; border: 1px solid var(--line); border-radius: 10px; color: var(--cream); background: #211710; box-shadow: 0 15px 40px rgba(0,0,0,.45); opacity: 0; transform: translateY(12px); pointer-events: none; transition: opacity .2s, transform .2s; }
.toast.is-visible { opacity: 1; transform: none; }

@media (max-width: 900px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .preview-panel { position: static; order: -1; }
  .canvas-stage { min-height: 420px; }
  #cardCanvas { height: min(62vh, 650px); }
}

@media (max-width: 520px) {
  .app-shell { padding: 14px 12px 28px; }
  .topbar { margin-bottom: 12px; }
  .topbar p { display: none; }
  .ghost-button { padding: 8px 11px; }
  .editor-panel, .preview-panel { padding: 16px; border-radius: 14px; }
  .preview-panel { width: 100%; max-width: 100%; }
  .canvas-stage { width: 100%; max-width: 100%; min-height: 0; padding: 14px; }
  #cardCanvas {
    width: 100%;
    min-width: 0;
    height: auto;
    max-width: 100%;
  }
  .template-grid { gap: 6px; }
  .card-type-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .rarity-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .template-option { grid-template-columns: 21px 1fr; padding: 7px 6px; font-size: .8rem; }
  .template-swatch { width: 21px; height: 26px; }
  .rarity-option { grid-template-columns: 1fr; min-height: 68px; padding: 5px 2px; font-size: .75rem; }
  .rarity-option img { width: 36px; height: 36px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .preview-panel { margin-inline: 0; }
  .generate-button small { display: none; }
  .toast { right: 16px; bottom: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
