/* =========================================================================
   書法工坊 — Vermilion editorial workbench
   Layout: top bar (desktop nav) + bottom tab bar (mobile) · 3 views
   ========================================================================= */

:root {
  --font-serif: "Noto Serif CJK TC", "Noto Serif TC", "Songti TC", "Songti SC", serif;
  --font-ui: "Noto Sans CJK TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --bg: #faf9f4;
  --surface: #faf9f4;
  --surface-2: #f0eee6;
  --section-heading-bg: #efede5;
  --paper: #f8f3e9;
  --ink: #2d302a;
  --muted: #73756c;
  --line: #d5d2c8;
  --accent: #a32f23;
  --accent-hover: #83251b;
  --accent-soft: #f3e6df;
  --seal: #a32f23;
  --ok: #2f7d4f;
  --warn: #a8590f;
  --radius: 2px;
  --shadow: none;
  --tabbar-h: 50px;
  --topbar-h: 91px;
  --folio-h: 44px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

body.theme-gold {
  --bg: #f3edd9; --surface: #fdfaf3; --surface-2: #f4ebd2; --paper: #fbf5e6; --line: #ddcfba;
  --section-heading-bg: #e8e0cb;
}
body.theme-rubbing {
  --bg: #1a1a1a; --surface: #242424; --surface-2: #2c2c2c; --paper: #141414;
  --section-heading-bg: #141414;
  --ink: #ece7de; --muted: #9e9a93; --line: #3b3b3b;
  --accent: #b83324; --accent-hover: #cf4331; --accent-soft: #3d2420; --seal: #d94a38;
  --shadow: 0 2px 10px rgba(0, 0, 0, .35);
}

/* Fixed catalog reference swatches retain their source dark ink. Actual artwork
   receives its paper and ink colors from the renderer, never CSS filters. */
body.theme-rubbing .font-current-glyph,
body.theme-rubbing .picker-sample,
body.theme-rubbing .glyph-sample {
  background: #faf7f0;
  color: #1c1b18;
}
body.theme-rubbing .stage-paper,
body.theme-rubbing .viewer,
body.theme-rubbing .export-output-box,
body.theme-rubbing .job-thumb,
body.theme-rubbing #detail-viewer {
  background: #141414;
  color: #ffffff;
}
body.theme-rubbing .job-thumb .placeholder { color: #aaa69f; }

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font-family: var(--font-ui); line-height: 1.55; min-height: 100vh;
  transition: background-color .25s, color .25s;
}
/* Gold belongs to the selected paper; application chrome stays quiet. */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 30; background: var(--surface); }
.topbar-inner { max-width: 1320px; margin: 0 auto; height: var(--topbar-h); padding: 0; display: flex; align-items: center; gap: 32px; border-bottom: 2px solid var(--ink); }
.brand { display: flex; align-items: center; gap: 10px; }
.seal { width: 32px; height: 32px; display: grid; place-items: center; background: var(--seal); color: #fff; border-radius: 6px; font-weight: 700; font-size: 18px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-family: var(--font-serif); font-weight: 500; font-size: 32px; letter-spacing: .12em; }
.brand-text small { font-size: 11px; color: var(--muted); }
.nav-tabs { display: flex; gap: 28px; margin-left: 28px; }
.nav-tabs a { padding: 10px 0; border-bottom: 1px solid transparent; color: var(--muted); font-weight: 400; font-size: 14px; }
.nav-tabs a:hover { background: var(--surface-2); color: var(--ink); }
body[data-view="create"] [data-nav="create"],
body[data-view="history"] [data-nav="history"],
body[data-view="fonts"] [data-nav="fonts"] { color: var(--accent); }
.nav-tabs a[aria-current="page"] { border-bottom-color: var(--accent); color: var(--ink); }
.topbar-tools { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.github-star { display: inline-flex; align-items: center; gap: 5px; padding: 6px 9px; border: 1px solid transparent; white-space: nowrap; font-size: 12px; color: var(--muted); text-decoration: none; }
.github-star span { color: #b98316; }
.github-star:hover, .github-star:focus-visible { border-color: var(--accent); background: var(--surface-2); }
.job-indicator { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 5px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.spinner { width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.badge-count { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--seal); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-style: normal; font-weight: 600; }

/* ---------- Views ---------- */
.views { max-width: 1320px; margin: 0 auto; padding: 26px 0 40px; }
.view[hidden] { display: none !important; }

.card { background: transparent; border: 0; border-bottom: 1px solid var(--line); padding: 16px 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-head h2 { font-family: var(--font-serif); font-size: 19px; font-weight: 500; display: flex; align-items: baseline; gap: 9px; letter-spacing: .06em; }
.card-head h2 small { font: 12px/1.5 var(--font-ui); color: var(--muted); letter-spacing: .02em; }
.step { width: 20px; height: 20px; border-radius: 50%; background: var(--accent); color: #fff; display: inline-grid; place-items: center; font-size: 12px; font-weight: 600; }

/* ---------- Controls ---------- */
.btn { border: 1px solid transparent; border-radius: 2px; padding: 11px 16px; font-weight: 500; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: background .15s, transform .05s; min-height: 44px; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: wait; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); border-color: var(--line); color: var(--ink); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn-sm { padding: 6px 12px; font-size: 13px; min-height: 36px; }
.icon-btn { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; font-size: 18px; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); }

.seg { display: inline-flex; background: transparent; border: 1px solid var(--line); border-radius: 2px; padding: 2px; gap: 2px; }
.seg.full { display: flex; width: 100%; }
.seg-btn { flex: 1; border: 0; background: transparent; border-radius: 0; padding: 6px 10px; font-size: 13px; color: var(--muted); min-height: 32px; white-space: nowrap; }
.seg-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }

.chip-row { display: flex; gap: 6px; }
.chip-row.wrap { flex-wrap: wrap; }
.chip-row.scroll { overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
.chip-row.scroll::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: transparent; border-radius: 2px; padding: 5px 10px; font-size: 12px; color: var(--muted); min-height: 32px; white-space: nowrap; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

textarea, .search, select { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 2px; padding: 10px 12px; }
textarea { resize: vertical; min-height: 84px; max-height: 40vh; line-height: 1.6; font-size: 17px; }
textarea:focus, .search:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.exceeded { border-color: var(--seal); }
.field-foot { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; font-size: 12px; color: var(--muted); min-height: 18px; }
.counter.exceeded { color: var(--seal); font-weight: 700; }
.warn { color: var(--seal); }
#font-glyph-warning:not([hidden]),
#char-warning:not([hidden]) {
  display: block;
  font-size: 16px;
  font-weight: 700;
  color: #b71c1c;
  background: #ffebee;
  border: 2px solid #d32f2f;
  border-radius: 10px;
  padding: 12px 14px;
  margin-top: 8px;
  line-height: 1.5;
  box-shadow: 0 2px 10px rgba(183, 28, 28, 0.15);
}
.stamp-controls-group {
  border-top: 1px dashed var(--line);
  padding-top: 8px;
}
.stamp-options-row {
  margin-top: 8px;
}
.field { margin-top: 12px; }
.field:first-of-type { margin-top: 0; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field-label, .label-row label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 6px; }
.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.label-row output { font-size: 13px; color: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); height: 28px; }
.check { display: flex; gap: 8px; align-items: center; margin-top: 12px; font-size: 14px; color: var(--muted); min-height: 32px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.note { font-size: 13px; color: var(--muted); margin-top: 8px; }
.note.warn { color: var(--warn); }
.presets-block { margin-top: 10px; }
.presets-block .field-label { margin-bottom: 6px; }
.presets-block .chip-row { gap: 6px; }

/* current-font button */
.font-current { width: 100%; display: flex; align-items: center; gap: 16px; padding: 8px 0; background: transparent; border: 1px solid transparent; border-radius: 0; text-align: left; }
.font-current:hover { border-color: var(--accent); }
.font-current-glyph { width: 54px; height: 60px; flex: none; display: grid; place-items: center; font-size: 34px; background: var(--paper); line-height: 1; }
.font-current-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.font-current-meta strong { font-family: var(--font-serif); font-weight: 500; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.font-current-meta small { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.font-current-cta { color: var(--accent); font-weight: 400; font-size: 12px; flex: none; }
#font-recent { margin-top: 10px; }
#font-recent:empty { display: none; }

/* ---------- Create layout ---------- */
.create-grid { display: grid; grid-template-columns: minmax(280px, 340px) minmax(0, 1fr); gap: 36px; align-items: start; }
.compose { display: flex; flex-direction: column; gap: 0; padding-right: 24px; border-right: 1px solid var(--line); scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
.canvas { position: relative; background: transparent; border: 0; padding: 0 46px 0 0; min-width: 0; }
.canvas-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.canvas-meta { font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--seal); vertical-align: middle; margin-left: 2px; }
.canvas-title-group { display: inline-flex; align-items: center; gap: 8px; }
.btn-jump-preview, .btn-jump-text { display: none !important; }

.action-bar { display: flex; gap: 8px; position: sticky; bottom: 0; z-index: 5; margin-top: auto; padding: 14px 0 4px; background: var(--bg); border-top: 1px solid var(--line); }
.action-bar .btn { flex: 1; }

/* job progress */
.job-progress { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 13px; color: var(--accent); }
.job-progress-bar { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.job-progress-bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }

/* ---------- Live stage (automatically lengthens with canvas content) ---------- */
.stage-viewport {
  width: 100%;
  min-height: 400px;
  height: auto;
  overflow-x: auto;
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 32px 24px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: min-height 0.2s ease;
}
.stage-paper {
  position: relative;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 3px 12px rgba(40, 30, 20, .04);
  border-radius: 0;
  padding: 24px 28px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: auto;
  flex-shrink: 0;
  max-width: 100%;
  transition: width 0.15s ease, height 0.15s ease;
}
.stage-paper.vertical {
  align-items: center;
  justify-content: center;
}
.stage-paper.horizontal {
  align-items: center;
  justify-content: center;
}
.stage-text {
  visibility: hidden;
  white-space: pre;
  word-break: normal;
  overflow-wrap: normal;
  line-height: 1.45;
  color: var(--ink);
  user-select: none;
  font-family: var(--font-serif);
}
.stage-paper.vertical .stage-text {
  writing-mode: vertical-rl;
  text-orientation: upright;
  height: max-content;
  max-height: 100%;
}
.stage-paper.horizontal .stage-text {
  writing-mode: horizontal-tb;
  width: max-content;
  max-width: 100%;
}
.stage-caption { margin-top: 8px; font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
#editor-preview { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; border-radius: inherit; user-select: none; -webkit-user-drag: none; image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; }
#editor-preview[hidden], #editor-preview-status[hidden] { display: none; }
#editor-preview-status { margin: 8px 12px; font-size: 13px; color: var(--muted); }
.stamp-drag-handle {
  position: absolute;
  box-sizing: border-box;
  z-index: 4;
  border: 1.5px dashed transparent;
  background: transparent;
  border-radius: 3px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  transition: border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}
.stamp-drag-handle[hidden] {
  display: none;
}
.stamp-drag-handle:hover {
  border-color: rgba(182, 34, 24, 0.45);
  background: rgba(182, 34, 24, 0.05);
}
.stamp-drag-handle.active-drag {
  cursor: grabbing;
  border-color: rgba(182, 34, 24, 0.9);
  background: rgba(182, 34, 24, 0.18);
  box-shadow: 0 3px 12px rgba(182, 34, 24, 0.3);
}

/* result viewer: fits by default and zoomed out slightly for comfortable viewing, scroll when actual size */
.viewer, .export-output-box {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-height: 260px;
  max-height: 520px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  box-sizing: border-box;
  position: relative;
}
.export-output-box.hidden { display: none !important; }
.preview-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
.preview-box.hidden { display: none !important; }
.viewer > svg, .viewer > img, #preview-img, #preview-svg-container svg {
  max-width: 88%;
  max-height: 440px;
  width: auto;
  height: auto;
  display: block;
  border-radius: 6px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.08);
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}
#preview-img.hidden { display: none !important; }
#video-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}
.viewer > video, #result-video {
  background: #000;
  max-width: 88%;
  max-height: 420px;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
}
.video-actions-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  width: 100%;
}
.viewer.actual, .export-output-box.actual {
  display: block;
  padding: 0;
  max-height: none;
}
.viewer.actual > svg, .viewer.actual > img, .viewer.actual > video,
.export-output-box.actual #preview-img,
.export-output-box.actual #preview-svg-container svg,
.export-output-box.actual #result-video {
  max-width: none;
  max-height: none;
  width: auto;
  height: auto;
  border-radius: 0;
  box-shadow: none;
  image-rendering: pixelated;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}
.result-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; justify-content: flex-end; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.empty p { font-size: 16px; color: var(--ink); }
.empty small { max-width: 320px; }

/* ---------- Page heads (history / fonts) ---------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { font-family: var(--font-serif); font-weight: 500; font-size: 30px; letter-spacing: .08em; margin-bottom: 8px; }
.page-desc { font-size: 13px; color: var(--muted); }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; min-width: 0; max-width: 100%; }
.toolbar .search { width: 240px; }

/* ---------- History ---------- */
.history-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.job-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; text-align: left; padding: 0; box-shadow: var(--shadow); transition: transform .12s, border-color .12s; cursor: pointer; }
.job-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.job-thumb { aspect-ratio: 4 / 3; background: var(--paper); display: grid; place-items: center; overflow: hidden; border-bottom: 1px solid var(--line); position: relative; }
.job-thumb img, .job-thumb video { width: 100%; height: 100%; object-fit: contain; }
.job-thumb .placeholder { color: var(--muted); font-size: 13px; text-align: center; padding: 8px; }
.job-kind { position: absolute; top: 8px; left: 8px; font-size: 11px; background: rgba(0,0,0,.55); color: #fff; border-radius: 6px; padding: 1px 7px; z-index: 1; }
.job-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.job-text { font-size: 15px; display: -webkit-box; -webkit-line-clamp: 3; line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; min-height: 3em; word-break: break-all; }
.job-meta { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; align-items: baseline; }
.job-meta span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font-weight: 600; flex: none; font-size: 12px; }
.status.succeeded { color: var(--ok); }
.status.failed { color: var(--seal); }
.status.running, .status.queued { color: var(--accent); }
.mini-bar { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.mini-bar i { display: block; height: 100%; background: var(--accent); }
.job-retention { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 3px 0; overflow-wrap: anywhere; }
.retention-guide { margin-bottom: 14px; }
.job-error { color: var(--seal); font-size: 12px; margin-top: 2px; word-break: break-all; }
.job-action { display: flex; gap: 6px; margin-top: auto; padding-top: 8px; }
.job-action .btn, .job-action .btn-play-mini, .job-action .btn-download { flex: 1; font-size: 12px; padding: 4px 8px; border-radius: 6px; text-align: center; text-decoration: none; border: 1px solid var(--line); background: var(--surface-2); color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.job-action .btn-play-mini { background: var(--accent-soft); color: var(--accent); border-color: transparent; font-weight: 600; }
.job-action .btn-play-mini:hover { background: var(--accent); color: #fff; }
.job-action .btn-download:hover { border-color: var(--accent); color: var(--accent); }
.empty-jobs { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 48px; }
#detail-viewer { display: flex; align-items: center; justify-content: center; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); min-height: 260px; overflow: hidden; }
#detail-viewer img, #detail-viewer video { max-width: 100%; max-height: 360px; object-fit: contain; image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; }
#detail-viewer.actual img, #detail-viewer.actual video { max-width: none; max-height: none; image-rendering: pixelated; image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; }

/* ---------- Fonts view ---------- */
.filters { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 14px 10px; margin-bottom: 14px; }
.filter-block { display: flex; align-items: center; gap: 10px; margin-top: 6px; min-width: 0; }
.filter-block .field-label { margin: 0; flex: none; width: 36px; }
.fonts-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.font-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); }
.font-card h3 { font-size: 16px; }
.font-card .en { font-size: 12px; color: var(--muted); }
.badges { display: flex; gap: 4px; flex-wrap: wrap; }
.badge { font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.glyph-sample { font-size: 38px; line-height: 1.3; padding: 8px 10px; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.font-card details { font-size: 13px; color: var(--muted); }
.font-card summary { cursor: pointer; color: var(--accent); min-height: 28px; }
.meta-list { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 13px; margin-top: 6px; }
.meta-list dt { color: var(--muted); }
.meta-list dd { color: var(--ink); word-break: break-word; }
.aesthetic { margin-top: 8px; font-size: 13px; line-height: 1.7; color: var(--ink); }
.font-card .btn-row { display: flex; gap: 8px; margin-top: auto; }
.font-card .btn-row .btn { flex: 1; }

/* ---------- Dialogs / sheets ---------- */
dialog.sheet { border: 0; padding: 0; background: transparent; max-width: none; max-height: none; width: 100%; height: 100%; color: var(--ink); }
dialog.sheet::backdrop { background: rgba(20, 15, 10, .5); backdrop-filter: blur(2px); }
.sheet-inner { position: absolute; inset: 0 0 0 auto; width: min(560px, 100%); background: var(--surface); display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0,0,0,.25); }
.sheet-wide .sheet-inner { width: min(980px, 100%); inset: 4vh 0 4vh 0; margin: 0 auto; border-radius: var(--radius); right: 0; left: 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 10px 18px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 17px; }
.sheet-tools { padding: 10px 16px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--line); }
.picker-list { list-style: none; overflow-y: auto; flex: 1; padding: 6px 8px calc(12px + var(--safe-b)); }
.picker-item { display: flex; align-items: center; gap: 10px; padding: 10px 8px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; }
.picker-item:hover { background: var(--surface-2); }
.picker-item[aria-selected="true"] { background: var(--accent-soft); border-color: var(--accent); }
.picker-sample { flex: none; width: 118px; min-height: 52px; display: flex; align-items: center; justify-content: center; font-size: 30px; line-height: 1.2; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 4px; overflow: hidden; white-space: nowrap; }
.picker-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.picker-info strong { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-info small { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.star { flex: none; width: 40px; height: 40px; border: 0; background: transparent; font-size: 20px; color: var(--muted); border-radius: 50%; }
.star.on { color: #d19a1d; }
.picker-empty { padding: 32px; text-align: center; color: var(--muted); }

.detail-body { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; padding: 16px; overflow-y: auto; flex: 1; min-height: 0; }
.detail-media { min-width: 0; }
.detail-info { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.detail-info h3 { font-size: 14px; color: var(--muted); }
.detail-text { white-space: pre-wrap; word-break: break-all; font-family: var(--font-ui); font-size: 15px; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; max-height: 32vh; overflow: auto; }

.toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-b) + 84px); z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: min(92vw, 420px); }
.toast { background: #2b2b2b; color: #fff; padding: 9px 16px; border-radius: 10px; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.25); animation: fadein .2s; }
.toast.error { background: var(--seal); }
.toast.success { background: var(--ok); }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } }

.tabbar { display: none; }

/* Desktop keeps controls and artwork in separate viewport-height scroll panes. */
@media (min-width: 961px) {
  body[data-view="create"] .views { padding-bottom: 16px; }
  .create-grid {
    height: calc(100dvh - var(--topbar-h) - var(--folio-h) - 43px);
    min-height: 0;
    align-items: stretch;
  }
  .create-grid > .compose, .create-grid > .canvas {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    scrollbar-gutter: stable;
  }
  .compose > * { flex-shrink: 0; }
}

/* ---------- Tablet / Narrow screen (<= 960px) ---------- */
@media (max-width: 960px) {
  /* Stretch cards to the viewport instead of their font names/chips' intrinsic width. */
  .create-grid { display: flex; flex-direction: column; align-items: stretch; gap: 14px; }
  .compose { display: contents; }
  /* Explicit bounds also cover display:contents children in mobile WebKit. */
  .compose > .card, .create-grid > .canvas { width: 100%; max-width: 100%; }
  .creation-note { order: 6; }
  #card-text { order: 1; }
  .canvas { order: 2; position: static; }
  #card-font { order: 3; }
  #card-paper { order: 4; }
  #card-layout { order: 5; }
  #card-output { order: 6; }
  .action-bar { order: 7; }
  .detail-body { grid-template-columns: 1fr; }
  .canvas, #card-text { scroll-margin-top: calc(var(--topbar-h) + 12px); }
  .btn-jump-preview {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 4px;
    flex: 0 0 auto;
    white-space: nowrap;
  }
  .btn-jump-text {
    display: inline-flex !important;
    align-items: center;
    gap: 2px;
    padding: 2px 8px;
    font-size: 12px;
    color: var(--accent);
    background: var(--accent-soft);
    border: 1px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    line-height: 1.5;
    transition: background .15s;
  }
  .btn-jump-text:hover {
    background: color-mix(in srgb, var(--accent-soft) 80%, var(--accent));
  }
}

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  :root { --topbar-h: 68px; --folio-h: 36px; }
  .brand-text small, .nav-tabs { display: none; }
  .topbar-inner { padding: 0; gap: 8px; }
  .topbar-tools { gap: 6px; min-width: 0; }
  .brand, .brand-text { min-width: 0; }
  .seal, .github-star { flex-shrink: 0; }
  .brand-text strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-inner:has(.job-indicator:not([hidden])) .brand-text { display: none; }
  .views { padding: 18px 0 calc(var(--tabbar-h) + var(--safe-b) + 90px); }

  .tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); height: calc(var(--tabbar-h) + var(--safe-b)); }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; color: var(--muted); font-size: 12px; position: relative; }
  .tabbar a i { font-style: normal; font-size: 20px; line-height: 1; }
  .tabbar .badge-count { position: absolute; top: 6px; left: calc(50% + 8px); }
  body[data-view="create"] .tabbar [data-nav="create"],
  body[data-view="history"] .tabbar [data-nav="history"],
  body[data-view="fonts"] .tabbar [data-nav="fonts"] { color: var(--accent); font-weight: 700; }

  .action-bar { position: fixed; left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + var(--safe-b)); border-radius: 0; z-index: 35; gap: 6px; padding: 10px 0; }
  .action-bar .btn { min-width: 0; padding: 10px 6px; font-size: 13px; }
  .btn-jump-preview { padding: 10px 10px; }
  .card, .canvas { padding: 16px 0; }
  .card-head, .field-foot, .label-row { flex-wrap: wrap; }
  .field-foot .counter, .label-row output { margin-left: auto; }
  .font-current { gap: 8px; padding: 10px; }
  .search, .card select { font-size: 16px; }
  .viewer { height: clamp(220px, 38vh, 380px); }
  .stage-viewport { min-height: 240px; height: auto; padding: 14px; overflow-x: auto; }
  .stage-paper { padding: 18px 20px; }
  .field-row { grid-template-columns: minmax(0, 1fr); }
  .toolbar { width: 100%; flex-direction: column; align-items: stretch; }
  .toolbar .search { width: 100%; }
  .history-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .job-action, .video-actions-row { flex-wrap: wrap; }
  .job-text { font-size: 14px; }
  .fonts-grid { grid-template-columns: minmax(0, 1fr); }
  .filter-block { flex-direction: column; align-items: stretch; gap: 4px; }
  .filter-block .chip-row.wrap { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .picker-sample { width: 96px; font-size: 26px; }

  /* sheets become bottom sheets */
  .sheet-inner, .sheet-wide .sheet-inner { inset: auto 0 0 0; width: 100%; height: 92dvh; border-radius: 2px 2px 0 0; margin: 0; }
  .sheet-wide .sheet-inner { height: 94dvh; }
  .toasts { bottom: calc(var(--tabbar-h) + var(--safe-b) + 84px); }
}

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
/* Let content shrink inside its own card instead of widening the page. */
.create-grid > *, .compose > *, .field-row > *, .font-card,
.sheet-inner > *, .filter-block > .chip-row { min-width: 0; }
textarea, .search, select, .font-current, .chip-row { min-width: 0; max-width: 100%; }
.font-card, .note, .creation-note, .canvas-meta, .toast { overflow-wrap: anywhere; }
.chip-row.scroll { overscroll-behavior-x: contain; }

.sheet-confirm .sheet-inner { inset: 0; margin: auto; width: min(440px, calc(100% - 32px)); height: fit-content; max-height: calc(100% - 32px); overflow-y: auto; border-radius: var(--radius); }
.notice-body { padding: 24px; gap: 14px; }
.notice-body h2 { font-size: 20px; }
.notice-body p { margin: 0; line-height: 1.7; }
.notice-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.notice-actions .btn { flex: 1; white-space: nowrap; }
#wechat-page-url { width: 100%; min-width: 0; }
.delete-history-body { padding: 18px; display: grid; gap: 16px; }
#delete-history-error { color: var(--accent); }

.font-raster-sample { display: block; width: 100%; height: 100%; max-height: 100px; object-fit: contain; }

.creation-note, .stage-hint { color: var(--muted); font-size: 12px; margin: 8px 12px; }

/* Work previews occupy the available viewport; media is never capped at 420px. */
.work-dialog .sheet-inner { inset: 12px; width: min(1180px, calc(100% - 24px)); height: calc(100dvh - 24px); max-height: calc(100dvh - 24px); }
.work-dialog .sheet-head { flex-shrink: 0; }
.work-dialog .detail-body { grid-template-columns: minmax(0, 1fr) minmax(220px, 280px); overflow: hidden; }
.work-dialog .detail-media, #result-body { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.work-dialog .detail-info { overflow-y: auto; }
.work-dialog #detail-viewer, .work-dialog .export-output-box { flex: 1; width: 100%; height: 100%; min-height: 0; max-height: none; padding: 8px; }
.work-dialog #detail-viewer > video, .work-dialog #result-video { width: 100%; height: 100%; max-width: 100%; max-height: 100%; min-height: 0; object-fit: contain; }
.work-dialog #detail-viewer > img, .work-dialog #preview-img, .work-dialog #preview-svg-container svg { max-width: 100%; max-height: 100%; object-fit: contain; }
.work-dialog #detail-viewer.actual > img, .work-dialog .export-output-box.actual #preview-img, .work-dialog .export-output-box.actual #preview-svg-container svg { max-width: none; max-height: none; image-rendering: pixelated; image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; }
.work-dialog .result-actions { flex-shrink: 0; }
#result-body { flex: 1; padding: 16px; }
#result-body[hidden] { display: none; }
.work-dialog .preview-box { height: 100%; min-height: 0; }
.share-body { display: grid; gap: 16px; padding: 18px; overflow-y: auto; }
#video-share-url { min-width: 0; width: 100%; font-size: 16px; }
#video-share-status { overflow-wrap: anywhere; }
@media (max-width: 640px) {
  .work-dialog .sheet-inner { inset: 0; width: 100%; height: 100dvh; max-height: 100dvh; border-radius: 0; padding-bottom: env(safe-area-inset-bottom); }
  .work-dialog .detail-body { display: flex; flex-direction: column; gap: 12px; padding: 10px; overflow-y: auto; }
  .work-dialog .detail-media { flex: none; height: calc(100dvh - 76px - env(safe-area-inset-bottom)); min-height: 220px; }
  .work-dialog .detail-info { overflow: visible; flex: none; }
  .work-dialog .result-actions { justify-content: center; }
}

.job-action { flex-wrap: wrap; }
.job-action .btn { min-width: 0; flex: 1 1 auto; }
.work-dialog #detail-viewer.actual { overflow: auto; }

.job-warning { flex-shrink: 0; margin: .4rem 0; color: var(--ink); font-size: .85rem; line-height: 1.5; }

/* Folio identity: serif titles, functional sans labels, and a vermilion spine. */
.topbar-inner, .folio-caption, .views { width: calc(100% - 80px); }
.folio-mark { flex: none; font: 500 28px/1 var(--font-serif); color: var(--accent); }
.folio-caption { max-width: 1320px; height: var(--folio-h); margin: 0 auto; display: flex; justify-content: space-between; align-items: center; gap: 16px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: 11px; }
.folio-caption span:first-child { font: 10px/1.5 Arial, sans-serif; letter-spacing: .17em; }
.compose-heading { display: flex; align-items: center; gap: 12px; padding: 0 0 7px; font-family: var(--font-serif); font-size: 16px; }
.compose-heading small { font: 10px/1.5 Arial, sans-serif; letter-spacing: .13em; color: var(--muted); margin-left: 2px; }
.vermilion-square { display: inline-block; flex: none; width: 17px; height: 17px; background: var(--accent); }
#card-text .card-head { margin-bottom: 6px; }
#text-input { font-family: var(--font-serif); font-size: 23px; letter-spacing: .09em; line-height: 1.9; border: 1px solid var(--line); border-left: 3px solid var(--accent); background: var(--accent-soft); padding: 12px 16px; min-height: 128px; }
#text-input:focus { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); outline: none; }
#text-input.exceeded { border-color: var(--seal); box-shadow: inset 0 0 0 1px var(--seal); }
.field-foot { font-size: 11px; }
.secondary-settings { margin-top: 10px; font-size: 12px; color: var(--muted); }
#card-text .secondary-settings { display: inline-block; vertical-align: top; width: calc(50% - 3px); padding-right: 12px; }
#card-text .secondary-settings[open] { width: 100%; padding-right: 0; }
#font-note { font-size: 11px; line-height: 1.7; }
.status-message { margin-top: 10px; font-size: 12px; color: var(--muted); }
.status-message.error { color: var(--seal); }
.status-message.hidden { display: none; }
.secondary-settings summary { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 34px; cursor: pointer; }
.secondary-settings[open] summary { color: var(--ink); margin-bottom: 8px; }
.secondary-settings summary::after, .control-section > summary::after { content: '+'; flex: none; font: 18px/1 var(--font-ui); color: var(--accent); }
.secondary-settings[open] > summary::after, .control-section[open] > summary::after { content: '−'; }
.secondary-settings summary::-webkit-details-marker, .control-section > summary::-webkit-details-marker { display: none; }
.control-section > summary { cursor: pointer; list-style: none; margin: 0; min-height: 32px; }
.control-section > summary h2 { flex: 1; }
.compose .card-head,
.compose .secondary-settings > summary {
  background: var(--section-heading-bg);
  padding: 8px 10px;
  border-radius: var(--radius);
}
.compose .secondary-settings > summary { padding: 6px 8px; }
.section-hint { font-size: 11px; color: var(--muted); }
.section-body { padding-top: 18px; }
.presets-block .chip-row { margin-top: 8px; }
.card .text-input { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 2px; background: var(--surface); font-size: 13px; }
.sub-label { font-size: 11px; color: var(--muted); }
.field-head { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.canvas { display: flex; flex-direction: column; }
.canvas-title-group strong { font-family: var(--font-serif); font-size: 15px; font-weight: 500; }
.canvas-title-group strong small { color: var(--muted); font: 11px/1.5 var(--font-ui); margin-left: 8px; }
.folio-side-label { position: absolute; top: 80px; right: 0; writing-mode: vertical-rl; font: 16px/1.5 var(--font-serif); letter-spacing: .24em; }
.folio-side-label small { margin-top: 24px; color: var(--muted); font: 10px/1.5 Arial, sans-serif; letter-spacing: .16em; }
#pane-live { display: flex; flex-direction: column; flex: 1; }
.stage-viewport { flex: 1; }
.stage-paper::before { content: ''; position: absolute; top: -2px; left: 0; right: 0; height: 2px; background: var(--accent); z-index: 2; pointer-events: none; }
.stage-caption { border-top: 1px solid var(--line); margin-top: 18px; padding: 14px 0; font-size: 11px; }
.stage-hint, #editor-preview-status { margin-left: 0; margin-right: 0; font-size: 12px; }
.creation-note { margin: 16px 0; font-size: 11px; line-height: 1.8; }
.page-head { padding-bottom: 20px; border-bottom: 1px solid var(--ink); }
.font-card h3, .sheet-head h2 { font-family: var(--font-serif); font-weight: 500; }
.font-card { border: 0; border-bottom: 1px solid var(--line); padding: 24px 0; }
.fonts-grid { gap: 12px 32px; }
.filters { background: transparent; border-width: 0 0 1px; padding: 12px 0 18px; }
.glyph-sample, .picker-sample, .picker-item, .detail-text, .job-action .btn, .job-kind, .badge { border-radius: 2px; }
.job-card:hover { transform: none; }
.sheet-head { padding: 18px 20px; border-bottom-color: var(--ink); }
.sheet-head h2 { font-size: 22px; }
.sheet-tools { padding: 16px 20px; }
.picker-item { border-width: 0 0 1px; border-color: var(--line); padding: 14px 12px; }
.picker-item[aria-selected="true"] { border-bottom-color: var(--accent); }

@media (max-width: 1100px) {
  .topbar-inner, .folio-caption, .views { width: calc(100% - 48px); }
  .nav-tabs { margin-left: 0; }
  .folio-mark { margin-left: 8px; }
  .create-grid { gap: 24px; }
  .compose { padding-right: 20px; }
}
@media (max-width: 960px) {
  .compose { padding: 0; border: 0; }
  .compose-heading { display: none; }
  .canvas { padding: 20px 32px 0 0; }
  .folio-side-label { top: 90px; }
  .stage-viewport { min-height: 340px; }
  .card { padding: 20px 0; }
  .action-bar { margin-top: 0; }
}
@media (max-width: 640px) {
  .topbar-inner, .folio-caption, .views { width: calc(100% - 32px); }
  .topbar-inner { gap: 12px; }
  .brand-text strong { font-size: 24px; }
  .folio-mark, .github-star { display: none; }
  .folio-caption span:first-child { font-size: 9px; letter-spacing: .1em; }
  .folio-caption span:last-child { font-size: 10px; }
  .card, .canvas { padding: 16px 0; }
  .folio-side-label { display: none; }
  #text-input { font-size: 22px; min-height: 100px; }
  .secondary-settings summary, .control-section > summary { min-height: 44px; }
  .card-head h2 { font-size: 18px; }
  .canvas-title-group { flex-wrap: wrap; }
  .stage-viewport { min-height: 300px; padding: 20px 10px; }
  .stage-caption { margin-top: 12px; }
  .creation-note { margin: 4px 0 12px; }
  .tabbar a { font-size: 13px; letter-spacing: .08em; }
  .tabbar a[aria-current="page"]::before { content: ''; width: 18px; height: 2px; background: var(--accent); position: absolute; top: 0; }
  .font-card { padding: 18px 0; }
  .sheet-head { padding: 12px 16px; }
  .sheet-head h2 { font-size: 20px; }
}

/* Preview controls stay at UI scale; only the artwork responds to zoom. */
.preview-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.preview-quality { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.preview-quality select { width: auto; min-height: 32px; padding: 4px 8px; }
.preview-zoom { display: flex; border: 1px solid var(--line); }
.preview-zoom button { background: var(--surface); border: 0; min-width: 32px; min-height: 32px; cursor: pointer; font-size: 12px; }
.preview-zoom button + button { border-left: 1px solid var(--line); }
.preview-zoom button:hover { background: var(--accent-soft); }
.preview-frame { position: relative; flex: 1; min-width: 0; min-height: 240px; isolation: isolate; }
.preview-frame .stage-viewport { position: absolute; inset: 0; width: 100%; height: 100%; min-height: 0; overflow: auto; overscroll-behavior: contain; align-items: safe center; justify-content: safe center; }
#pane-live { min-height: 0; }
@media (max-width: 960px) { .preview-frame { flex: none; height: 60dvh; min-height: 340px; } }
.preview-frame .stage-paper { max-width: none; }
.preview-render-overlay { position: absolute; inset: 0; z-index: 5; display: flex; align-items: center; justify-content: center; gap: 12px; background: color-mix(in srgb, var(--surface) 72%, transparent); color: var(--ink); font-size: 14px; pointer-events: none; }
.preview-render-overlay[hidden] { display: none; }
.render-indicator { width: 17px; height: 17px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: preview-spin 1s linear infinite; }
@keyframes preview-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .render-indicator { animation: none; } }

/* A lightweight stamp layer follows the drag handle without rerasterizing text. */
#stamp-preview { position: absolute; pointer-events: none; user-select: none; mix-blend-mode: multiply; }
#stamp-preview[hidden] { display: none; }
