:root {
  --ink: #2c1b17;
  --muted: #7a6660;
  --line: #eadfd5;
  --panel: #fffaf5;
  --accent: #a91116;
  --accent-dark: #7d0d11;
  --gold: #bd761b;
  --stage: #1d1717;
  --poster-ratio: 1086 / 1448;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background: #f3ede8;
  font-family: system-ui, -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
}

button, input, select { font: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }

.topbar {
  min-height: 76px;
  padding: 14px clamp(18px, 4vw, 54px);
  display: flex;
  align-items: center;
  gap: 14px;
  background: #fffdfa;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 34px;
  border-radius: 7px;
  color: #fff;
  background: var(--accent);
  font-size: 15px;
  font-weight: 800;
  letter-spacing: .04em;
}

h1, h2, p { margin: 0; }

h1 { font-size: 22px; line-height: 1.2; letter-spacing: .02em; }
.topbar p { margin-top: 4px; color: var(--muted); font-size: 14px; }

.top-actions { margin-left: auto; display: flex; gap: 10px; }

.button {
  border: 1px solid transparent;
  border-radius: 9px;
  min-height: 38px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 650;
  transition: transform .15s ease, box-shadow .15s ease, background .15s ease;
}
.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button:disabled { cursor: not-allowed; opacity: .65; transform: none; box-shadow: none; }
.button-primary { color: #fff; background: var(--accent); box-shadow: 0 4px 14px #a9111629; }
.button-primary:hover { background: var(--accent-dark); }
.button-quiet { color: #5e4a43; border-color: var(--line); background: #fff; }
.button-outline { display: inline-grid; place-items: center; color: var(--accent); border-color: #dca9a2; background: #fff9f7; }
.upload-button { width: 100%; cursor: pointer; }

.app-shell {
  width: min(1920px, 100%);
  margin: 0 auto;
  padding: 16px clamp(18px, 4vw, 54px) 4px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 320px);
  grid-template-areas: "preview controls";
  gap: 26px;
}

.control-panel {
  grid-area: controls;
  align-self: start;
  border: 1px solid var(--line);
  border-radius: 14px;
  max-height: calc(100vh - 112px);
  max-height: calc(100dvh - 112px);
  overflow: auto;
  background: var(--panel);
  box-shadow: 0 8px 28px #6d35250c;
}

.panel-section { padding: 20px; border-bottom: 1px solid var(--line); }
.panel-section:last-child { border-bottom: 0; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
h2 { font-size: 16px; letter-spacing: .04em; }
.pill, .state { border-radius: 999px; padding: 3px 8px; font-size: 13px; white-space: nowrap; }
.pill { color: var(--accent); background: #f9e9e6; }
.state-warning { color: #8c591e; background: #fff0d9; }
.state-ok { color: #246348; background: #e5f5ea; }
.font-lock { display: flex; align-items: center; gap: 9px; margin-top: 12px; padding: 10px 11px; border: 1px solid #d9e9dc; border-radius: 9px; color: #305d42; background: #f3fbf4; font-size: 14px; }
.lock-icon { display: grid; place-items: center; width: 19px; height: 19px; flex: 0 0 auto; border-radius: 50%; color: #fff; background: #4b9467; font-size: 12px; font-weight: 800; }

.field { display: grid; grid-template-columns: 70px minmax(0, 1fr); align-items: center; gap: 10px; margin-top: 13px; }
.field[hidden] { display: none; }
.field span { color: var(--muted); font-size: 15px; }
.field input, .field select {
  width: 100%;
  min-width: 0;
  border: 1px solid #e0d2ca;
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 17px;
  color: var(--ink);
  background: #fff;
  outline: none;
  appearance: auto;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field input:focus, .field select:focus { border-color: #c86b65; box-shadow: 0 0 0 3px #bb49411c; }

.help-text, .file-name, .font-note, .tips-section li { color: var(--muted); font-size: 14px; line-height: 1.65; }
.help-text { margin: 10px 0 13px; }
.layout-note { margin-bottom: 0; }
.layout-note.layout-warning { color: var(--accent); }
.template-help { margin-bottom: 0; }
.file-name { margin-top: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.font-note { display: flex; gap: 7px; margin-top: 13px; padding: 9px 10px; border-radius: 8px; background: #fff6e9; }
.font-retry { color: var(--primary, #b51418); padding: 2px 0; border: 0; background: transparent; font: inherit; text-decoration: underline; cursor: pointer; }
.note-dot { flex: 0 0 auto; width: 7px; height: 7px; margin-top: 6px; border-radius: 50%; background: #c47a1d; }
code { font-size: 11px; color: #7d4e1b; }
.tips-section ul { margin: 12px 0 0; padding-left: 18px; }
.tips-section li + li { margin-top: 7px; }

.preview-panel { grid-area: preview; min-width: 0; }
.preview-toolbar { display: flex; flex-wrap: wrap; gap: 6px 12px; justify-content: space-between; align-items: center; margin: 0 0 8px; }
.preview-toolbar strong { font-size: 17px; }
#previewHint { margin-left: 10px; color: var(--muted); font-size: 14px; }
#mobileExportHint { display: none; margin-left: 10px; color: var(--muted); font-size: 14px; }
.load-state { color: var(--muted); font-size: 14px; }
.load-state.loaded { color: #246348; }

.poster-layout { display: grid; grid-template-columns: 188px minmax(0, 1fr); gap: 14px; align-items: start; }
.template-switcher { min-width: 0; min-height: 0; display: flex; flex-direction: column; padding: 12px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: 0 8px 28px #6d35250c; }
.template-switcher > .section-title-row { gap: 6px; }
.template-switcher > .section-title-row > .pill {
  flex: 0 0 auto;
  padding: 3px 6px;
  color: #923930;
  background: #f4ded7;
  font-size: 14px;
  font-weight: 600;
}
.template-heading { display: flex; align-items: center; gap: 4px; }
.template-heading h2 { white-space: nowrap; }
.template-edit-toggle { display: grid; place-items: center; flex: 0 0 32px; width: 32px; height: 32px; padding: 0; border: 0; border-radius: 7px; color: var(--muted); background: transparent; cursor: pointer; }
.template-edit-toggle:hover { color: var(--accent); background: #f9e9e6; }
.template-edit-toggle[aria-pressed="true"] { color: var(--accent); background: #f9e9e6; }
.template-edit-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.template-done-icon, .template-edit-toggle[aria-pressed="true"] .template-edit-icon { display: none; }
.template-edit-toggle[aria-pressed="true"] .template-done-icon { display: block; }
.template-edit-toggle[hidden], .template-drag-handle[hidden], .template-help[hidden] { display: none; }
.template-options { display: grid; gap: 9px; margin-top: 15px; }
.template-item { position: relative; min-width: 0; }
.template-item > .template-card { height: 100%; }
.is-editing-order .template-item > .template-card, .template-drag-ghost > .template-card { padding-right: 37px; }
.template-card {
  display: grid;
  grid-template-columns: minmax(0, max-content) 46px;
  grid-template-areas: "label thumb";
  justify-content: center;
  align-items: center;
  gap: 8px;
  width: 148px;
  max-width: 100%;
  justify-self: center;
  padding: 7px;
  border: 1px solid #e0d2ca;
  border-radius: 10px;
  color: var(--ink);
  background: #fff;
  text-align: left;
  cursor: pointer;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease, transform .15s ease;
}
.template-card:hover { border-color: #c86b65; transform: translateY(-1px); }
.template-card:focus-visible { outline: 3px solid #bb49414a; outline-offset: 1px; }
.template-card.is-active {
  border-color: var(--accent);
  background: #fff7f5;
  box-shadow: 0 0 0 2px #a9111622, 0 5px 14px #6d352512;
}
.template-thumb {
  display: block;
  grid-area: thumb;
  width: 46px;
  overflow: hidden;
  border-radius: 6px;
  background: #24191a;
}
.template-thumb img { display: block; width: 100%; height: auto; object-fit: contain; }
.template-card-label { grid-area: label; justify-self: start; min-width: 0; max-width: 100%; overflow: hidden; text-overflow: ellipsis; font-size: 17px; line-height: 1.3; font-weight: 700; text-align: left; white-space: nowrap; }
.template-rename-hint { grid-area: rename; justify-self: start; color: var(--accent); font-size: 11px; line-height: 1.3; text-decoration: underline; text-underline-offset: 2px; white-space: nowrap; }
.template-rename-hint[hidden] { display: none; }
.is-editing-order .template-card, .template-drag-ghost .template-card { grid-template-areas: "label thumb" "rename thumb"; align-content: center; row-gap: 2px; }
.template-name-dialog { width: min(380px, calc(100vw - 32px)); box-sizing: border-box; padding: 22px; border: 1px solid var(--line); border-radius: 14px; color: var(--ink); background: #fffaf5; }
.template-name-dialog::backdrop { background: #24191a80; }
.template-name-dialog h2 { margin: 0 0 18px; font-size: 19px; }
.template-name-dialog label { display: grid; gap: 8px; }
.template-name-dialog input { box-sizing: border-box; width: 100%; min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 7px; font-size: 17px; }
.template-name-dialog p { font-size: 13px; line-height: 1.5; color: var(--muted); }
.template-name-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.template-name-actions button { flex: 1; min-height: 40px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 7px; color: var(--accent); background: #fff; cursor: pointer; }
.template-name-actions button[type="submit"] { color: #fff; border-color: var(--accent); background: var(--accent); }
.template-switcher .template-help { margin: 12px 0 0; }
.template-sync-state { margin-top: 6px; font-size: 12px; line-height: 1.4; color: var(--muted); overflow-wrap: anywhere; }
.template-sync-state[data-state="synced"] { color: #246348; }
.template-sync-state[data-state="offline"] { color: #a65c13; }
.template-drag-handle { position: absolute; right: 3px; top: 50%; transform: translateY(-50%); display: grid; place-items: center; width: 32px; height: 44px; padding: 0; border: 0; border-radius: 6px; color: #927d74; background: transparent; font-size: 26px; line-height: 1; cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.template-drag-handle:hover, .template-drag-handle:focus-visible { color: var(--accent); background: #f9e9e6; }
.template-drag-handle:focus-visible, .template-order-controls button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.template-options.is-sorting, .template-drag-ghost { user-select: none; -webkit-user-select: none; }
.is-sorting .template-card { transform: none; }
.is-drag-source { opacity: .3; }
.is-drop-target { outline: 2px dashed var(--accent); outline-offset: 3px; border-radius: 10px; }
.is-drop-target .template-card { background: #fce7e1; }
.template-drag-ghost { position: fixed; top: 0; left: 0; z-index: 30; pointer-events: none; opacity: .94; filter: drop-shadow(0 12px 16px #4b201b44); }
.template-drag-ghost .template-card { border-color: var(--accent); }
.template-drag-ghost .template-drag-handle { cursor: grabbing; }
.template-order-controls[hidden] { display: none; }
.template-order-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 14px; }
.template-order-name { flex: 1 0 100%; color: var(--muted); }
.template-order-controls button { flex: 1; min-height: 36px; padding: 4px; border: 1px solid var(--line); border-radius: 6px; color: var(--accent); background: #fff; cursor: pointer; }
.template-order-controls button:disabled { color: #ab9b95; background: #f3ede8; cursor: default; }

.poster-stage {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 8px;
  border: 1px solid #3d2b2b;
  border-radius: 14px;
  background: var(--stage);
  box-shadow: 0 18px 50px #3e191922;
}

.poster {
  position: relative;
  width: min(100%, var(--poster-max-width, 100%));
  max-width: 100%;
  aspect-ratio: var(--poster-ratio);
  overflow: hidden;
  isolation: isolate;
  background: #fff;
  box-shadow: 0 12px 38px #0008;
}
.poster-canvas { display: block; width: 100%; height: 100%; object-fit: contain; }
.poster-preview { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.poster-preview[hidden] { display: none; }
.poster[data-state]:not([data-state="ready"]) .poster-canvas { visibility: hidden; }
.poster-load { position: absolute; inset: 0; z-index: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px; background: #fff7ee; color: #78534a; text-align: center; }
.poster-load-info { display: flex; flex-direction: column; align-items: center; gap: 12px; max-width: 100%; }
.poster.has-preview .poster-load { justify-content: flex-end; background: transparent; pointer-events: none; padding: 8px; }
.poster.has-preview .poster-load-info { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 6px 10px; border-radius: 8px; padding: 5px 10px; color: #5b3028; background: #fff7eef2; box-shadow: 0 2px 10px #0002; font-size: 12px; pointer-events: auto; }
.poster.has-preview #retryPosterButton { min-height: 32px; padding: 3px 10px; font-size: 12px; }
.poster-load p { margin: 0; line-height: 1.7; }
.poster-load[hidden], #retryPosterButton[hidden] { display: none; }

.poster-canvas { user-select: none; }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 10; width: max-content; max-width: min(90vw, 500px); padding: 10px 15px; border-radius: 999px; color: #fff; background: #2b211eeb; box-shadow: 0 8px 24px #0003; font-size: 15px; opacity: 0; transform: translate(-50%, 12px); pointer-events: none; transition: opacity .2s ease, transform .2s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.save-modal[hidden] { display: none; }
.save-modal { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 18px; background: #1d1717d9; }
.save-modal-card { position: relative; display: grid; justify-items: center; gap: 12px; width: min(100%, 520px); max-height: 92dvh; overflow: auto; padding: 22px; border-radius: 16px; background: #fffaf5; text-align: center; box-shadow: 0 18px 60px #0005; }
.save-modal-card > p { color: var(--muted); font-size: 14px; line-height: 1.6; }
.save-modal-close { position: absolute; top: 8px; right: 10px; width: 36px; height: 36px; border: 0; border-radius: 50%; color: var(--muted); background: #f3ede8; font-size: 24px; line-height: 1; cursor: pointer; }
.save-modal-image { display: block; max-width: 100%; max-height: 66dvh; width: auto; height: auto; object-fit: contain; border-radius: 6px; background: #fff; -webkit-touch-callout: default; user-select: auto; -webkit-user-select: auto; }
.save-modal-card > .save-modal-hint { font-size: 12px; }

@media (max-width: 1000px) {
  .topbar { align-items: flex-start; flex-wrap: wrap; }
  .top-actions { width: 100%; margin-left: 60px; }
  .top-actions .button { flex: 1; }
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas: "preview" "controls";
    padding-top: 18px;
  }
  .control-panel { max-height: none; overflow: hidden; }
  .poster-stage { min-height: 0; padding: 12px; }
  .poster-layout { grid-template-columns: 1fr; gap: 12px; align-items: start; }
  .template-switcher { order: 1; display: block; min-height: auto; }
  .template-options {
    grid-auto-flow: column;
    grid-auto-columns: calc((100% - 20px) / 6);
    grid-template-rows: auto;
    gap: 4px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
    padding-bottom: 4px;
  }
  .template-item { scroll-snap-align: start; }
  .template-options.is-sorting { scroll-snap-type: none; }
  /* Reserve the same bottom label row, regardless of each artwork's aspect ratio. */
  .template-card { grid-template-columns: 1fr; grid-template-rows: 1fr auto; grid-template-areas: "thumb" "label"; justify-items: center; justify-self: stretch; width: 100%; gap: 3px; padding: 4px 2px; }
  .template-card:hover { transform: none; }
  .template-card-label { justify-self: center; }
  .template-card-label {
    max-width: 100%;
    font-size: clamp(10px, 3.1vw, 14px);
    line-height: 1.15;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-all;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  .is-editing-order .template-card, .template-drag-ghost .template-card { grid-template-rows: 1fr auto auto; grid-template-areas: "thumb" "label" "rename"; align-content: stretch; }
  .template-rename-hint { justify-self: center; font-size: 10px; }
  .is-editing-order .template-item > .template-card, .template-drag-ghost > .template-card { padding-right: 2px; }
  .template-thumb { justify-self: center; width: 100%; height: 64px; max-width: 52px; max-height: 64px; border-radius: 5px; display: grid; place-items: center; background: transparent; }
  .template-thumb img { width: auto; height: auto; max-width: 100%; max-height: 64px; object-fit: contain; }
  .is-editing-order .template-thumb, .template-drag-ghost .template-thumb { justify-self: start; width: calc(100% - 14px); height: 64px; max-height: 64px; }
  .template-drag-handle { top: 2px; right: 0; transform: none; width: 20px; height: 24px; font-size: 19px; }
  .poster-stage { order: 2; }
  .poster { width: 100%; height: auto; }
  #previewHint { display: none; }
  #mobileExportHint { display: inline; }
  .poster-canvas { cursor: pointer; }
  .save-modal { padding: 10px; }
  .save-modal-card { gap: 9px; padding: 17px 14px; }
  .save-modal-image { max-height: 66dvh; }
}

@media (max-width: 600px) {
  .template-switcher { padding: 12px; }
  .top-actions { margin-left: 0; }
  .topbar { gap: 12px; }
  .brand-mark { flex-shrink: 0; }
}

/* Exact complement avoids an unstyled gap at fractional widths/Windows zoom. */
@media not all and (max-width: 1000px) {
  .template-options {
    flex: 1 1 0;
    min-height: 0;
    grid-auto-rows: calc((100% - 45px) / 6);
    align-content: start;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior-y: contain;
    scrollbar-width: thin;
  }
  .template-item { min-height: 0; }
  .template-card {
    width: 148px;
    height: 100%;
    margin-inline: auto;
    grid-template-columns: minmax(0, 1fr) 66px;
    grid-template-rows: minmax(0, 1fr);
    justify-content: stretch;
    gap: 6px;
    padding: 6px;
  }
  .template-card-label {
    justify-self: stretch;
    max-height: 100%;
    font-size: 18px;
    line-height: 1.2;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  .template-thumb { width: 66px; height: 100%; max-height: 86px; min-height: 0; display: grid; place-items: center; background: transparent; }
  .template-thumb img { width: auto; height: auto; max-width: 100%; max-height: 100%; object-fit: contain; }
  .is-editing-order .template-card, .template-drag-ghost .template-card {
    grid-template-columns: minmax(0, 1fr) 46px;
    grid-template-rows: minmax(0, 1fr) auto;
    align-content: stretch;
    row-gap: 2px;
  }
  .is-editing-order .template-thumb, .template-drag-ghost .template-thumb { width: 46px; max-height: 66px; }
  .is-editing-order .template-card-label, .template-drag-ghost .template-card-label { font-size: 17px; white-space: nowrap; display: block; text-align: left; }
  .is-editing-order .template-rename-hint, .template-drag-ghost .template-rename-hint { align-self: end; }
}
.site-update-status { position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 1500; width: max-content; max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 10px; background: #49251e; color: #fff; font-size: 14px; text-align: center; pointer-events: none; }
.site-update-status[hidden] { display: none; }
