:root {
  color-scheme: dark;
  --bg: #151515;
  --bg-soft: #1b1b1a;
  --panel: #20201f;
  --panel-raised: #262624;
  --line: #3b3934;
  --line-strong: #565146;
  --text: #f1eee6;
  --muted: #aaa59a;
  --paper: #c6a973;
  --paper-bright: #e5cca0;
  --accent: #d4b16e;
  --accent-dark: #6f5930;
  --danger: #e09886;
  --shadow: 0 24px 70px rgba(0, 0, 0, .36);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); }
body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 50% -20%, rgba(190, 151, 78, .11), transparent 38rem),
    var(--bg);
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
input, textarea, select { color: var(--text); }

.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 14px clamp(16px, 3vw, 44px);
  border-bottom: 1px solid var(--line);
  background: rgba(21, 21, 21, .92);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid #6e5b36;
  border-radius: 12px;
  color: #1d1810;
  background: linear-gradient(145deg, #e4c988, #a77d3e);
  font: 800 24px Georgia, serif;
}
.brand p, .eyebrow { margin: 0; color: var(--accent); font-size: 10px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.brand h1 { margin: 1px 0 0; font: 700 24px Georgia, serif; }
.header-actions { display: flex; align-items: center; gap: 9px; min-width: 0; }
.user-badge { max-width: 250px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
.status-pill, .save-state {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(255,255,255,.02);
  font-size: 11px;
  white-space: nowrap;
}
.status-pill.is-online { border-color: #5e704d; color: #bdd0aa; }

.notice {
  width: min(1160px, calc(100% - 28px));
  margin: 14px auto 0;
  padding: 11px 14px;
  border: 1px solid #806632;
  border-radius: 10px;
  color: #e5d2aa;
  background: #2a2419;
  font-size: 13px;
}
.notice code { color: #f0d48d; }

.app-layout {
  width: min(1510px, calc(100% - 28px));
  margin: 22px auto 42px;
  display: grid;
  grid-template-columns: minmax(285px, 350px) minmax(430px, 650px) minmax(260px, 330px);
  align-items: start;
  justify-content: center;
  gap: 20px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 15px;
  background: linear-gradient(180deg, rgba(39,39,37,.96), rgba(29,29,28,.98));
  box-shadow: 0 18px 50px rgba(0,0,0,.16);
}
.editor-panel, .library-panel { padding: 18px; }
.editor-panel, .library-panel { position: sticky; top: 98px; max-height: calc(100vh - 118px); overflow: auto; }
.panel-heading, .section-heading, .preview-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.panel-heading h2, .preview-heading h2 { margin: 3px 0 0; font: 700 20px Georgia, serif; }
.section-heading h3, .control-section > h3 { margin: 3px 0 0; font: 700 16px Georgia, serif; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 18px; }
.field { display: grid; gap: 6px; margin-top: 12px; }
.field-wide { grid-column: 1 / -1; margin-top: 0; }
.field > span, .range-field > span { color: #d7d1c5; font-size: 12px; font-weight: 700; }
.field small { justify-self: end; color: var(--muted); font-size: 10px; }
input, textarea, select {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  outline: none;
  background: #171716;
  padding: 10px 11px;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
textarea { resize: vertical; min-height: 120px; line-height: 1.5; }
#infoInput { font-size: 15.5px; font-weight: 600; }
input:focus, textarea:focus, select:focus { border-color: #a8874b; box-shadow: 0 0 0 3px rgba(192, 153, 78, .12); }

.stat-editor { display: grid; gap: 8px; margin-top: 15px; }
.stat-heading-row, .stat-row { display: grid; grid-template-columns: 84px minmax(0, 1fr); align-items: center; gap: 8px; }
.stat-heading-row { padding: 0 2px; color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .08em; }
.stat-row strong { font: 700 13px Georgia, serif; }
.stat-row input { min-width: 0; padding: 9px 10px; }

.section-divider { height: 1px; margin: 20px 0; background: var(--line); }
.control-section { display: grid; gap: 12px; }
.upload-box {
  position: relative;
  min-height: 82px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 4px;
  border: 1px dashed #706853;
  border-radius: 10px;
  color: #e5dfd2;
  background: rgba(18,18,17,.58);
  text-align: center;
  cursor: pointer;
}
.upload-box:hover { border-color: #9a8356; background: rgba(25,24,21,.72); }
.upload-box input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.upload-box span { font-size: 13px; font-weight: 750; }
.upload-box small { color: var(--muted); font-size: 10px; }
.upload-box-compact { min-height: 68px; }
.range-field { display: grid; gap: 7px; }
.range-field > span { display: flex; justify-content: space-between; }
.range-field output { color: var(--accent); }
.range-field input { padding: 0; accent-color: #c9a45d; }
.help-text, .export-note { margin: 0; color: var(--muted); font-size: 11px; line-height: 1.5; }

.button-row, .primary-actions, .dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.button {
  min-height: 39px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 9px 13px;
  color: var(--text);
  background: #302f2b;
  font-size: 12px;
  font-weight: 750;
  line-height: 1;
  transition: transform 100ms ease, border-color 120ms ease, background 120ms ease;
}
.button:hover:not(:disabled) { border-color: #82775f; background: #39372f; }
.button:active:not(:disabled) { transform: translateY(1px); }
.button-small { min-height: 32px; padding: 7px 10px; font-size: 11px; }
.button-quiet { background: transparent; }
.button-primary { color: #17130d; border-color: #e1bd74; background: linear-gradient(180deg, #dec27f, #b88b45); }
.button-primary:hover:not(:disabled) { background: linear-gradient(180deg, #e8ce8d, #c39850); }
.button-danger { color: var(--danger); }

.preview-panel { min-width: 0; display: grid; justify-items: center; gap: 14px; }
.preview-heading { width: min(100%, 575px); }
.canvas-stage {
  width: min(100%, 575px);
  aspect-ratio: 5 / 7;
  padding: 10px;
  border: 1px solid #3f3d37;
  border-radius: 18px;
  background: #0e0e0d;
  box-shadow: var(--shadow);
}
#cardCanvas { width: 100%; height: 100%; display: block; border-radius: 10px; touch-action: none; cursor: grab; }
#cardCanvas.is-dragging { cursor: grabbing; }
.primary-actions { width: min(100%, 575px); justify-content: center; }
.export-note { text-align: center; }

.search-box { display: block; margin: 16px 0 12px; }
.search-box input { padding: 9px 11px; }
.library-status { padding: 22px 6px; color: var(--muted); text-align: center; font-size: 12px; line-height: 1.5; }
.card-library { display: grid; gap: 11px; }
.saved-card {
  display: grid;
  grid-template-columns: 72px minmax(0,1fr);
  gap: 11px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(15,15,14,.56);
}
.saved-card-preview { width: 72px; aspect-ratio: 5/7; overflow: hidden; border-radius: 7px; background: #111; }
.saved-card-preview img { width: 100%; height: 100%; display: block; object-fit: cover; }
.saved-card-body { min-width: 0; display: grid; align-content: center; gap: 5px; }
.saved-card h3 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: 700 14px Georgia, serif; }
.saved-card p { margin: 0; color: var(--muted); font-size: 10px; }
.saved-card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 3px; }
.saved-card-actions .button { min-height: 29px; padding: 6px 8px; font-size: 10px; }

.login-dialog {
  width: min(430px, calc(100% - 26px));
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  color: var(--text);
  background: var(--panel);
  box-shadow: 0 32px 100px rgba(0,0,0,.65);
}
.login-dialog::backdrop { background: rgba(5,5,5,.78); backdrop-filter: blur(5px); }
.login-card { display: grid; gap: 13px; padding: 24px; }
.login-card h2 { margin: 3px 0 6px; font: 700 23px Georgia, serif; }
.login-card p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.form-error { min-height: 18px; color: var(--danger) !important; }
.dialog-actions { justify-content: flex-end; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 100;
  max-width: min(520px, calc(100% - 28px));
  padding: 11px 14px;
  border: 1px solid #826d42;
  border-radius: 9px;
  color: #19150e;
  background: #dfc383;
  box-shadow: var(--shadow);
  transform: translate(-50%, 18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease, transform 150ms ease;
  font-size: 12px;
  font-weight: 700;
}
.toast.visible { opacity: 1; transform: translate(-50%,0); }
.toast.error { color: #fff2ef; border-color: #9b5a4c; background: #733d34; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
body.busy { cursor: progress; }
body.busy button { cursor: progress; }

@media (max-width: 1190px) {
  .app-layout { grid-template-columns: minmax(280px, 340px) minmax(430px, 1fr); }
  .library-panel { grid-column: 1 / -1; position: static; max-height: none; }
  .card-library { grid-template-columns: repeat(auto-fill, minmax(245px, 1fr)); }
}
@media (max-width: 820px) {
  .site-header { position: static; }
  .app-layout { width: min(640px, calc(100% - 16px)); grid-template-columns: 1fr; margin-top: 12px; }
  .preview-panel { grid-row: 1; }
  .editor-panel { grid-row: 2; position: static; max-height: none; }
  .library-panel { grid-row: 3; }
  .canvas-stage { width: min(100%, 510px); }
}
@media (max-width: 540px) {
  .site-header { padding: 12px; }
  .brand h1 { font-size: 20px; }
  .brand-mark { width: 38px; height: 38px; }
  .status-pill, .user-badge { display: none; }
  .app-layout { width: calc(100% - 10px); }
  .editor-panel, .library-panel { padding: 14px; border-radius: 12px; }
  .form-grid { grid-template-columns: 1fr; }
  .field-wide { grid-column: auto; }
  .stat-heading-row, .stat-row { grid-template-columns: 72px minmax(0, 1fr); gap: 6px; }
  .primary-actions .button { flex: 1 1 120px; }
}
