:root {
  --bg: #17140f;
  --panel: #201c15;
  --panel-2: #292319;
  --line: #453d2d;
  --accent: #ff6a1f;
  --accent-dim: #c94f14;
  --paper: #f4ecd8;
  --ink: #f3ecdd;
  --ink-soft: #cabfa6;
  --muted: #988d76;
  --mark-ink: #1b1710;
  --guide: #c4b48a;
  --stage: #131009;
  --stage-dot: #2b2517;
  --sheet-shadow: 0 24px 55px rgba(0, 0, 0, 0.55), 0 5px 14px rgba(0, 0, 0, 0.4);
  --focus-ring: 0 0 0 3px rgba(255, 106, 31, 0.18);
  --ui-font: "Archivo", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", Arial, sans-serif;
  --mono-font: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", monospace;
  --display-font: "Big Shoulders Stencil Display", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", Arial, sans-serif;
  --mark-font: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", Arial, sans-serif;
  --mark-size: 28px;
  color: var(--ink);
  font-family: var(--ui-font);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--bg); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button, input, textarea, select { font: inherit; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; color-scheme: dark; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 92px;
  padding: 18px clamp(22px, 4vw, 64px);
  border-bottom: 3px solid var(--accent);
  background: var(--panel);
}

.brand-lockup, .topbar-actions, .preview-actions, .control-footer, .style-toolbar, .textarea-heading, .card-heading, .field-row, .range-row, .preview-footer, .legend-item, .preview-tip { display: flex; align-items: center; }

.brand-lockup { gap: 13px; }
.brand-symbol {
  display: grid;
  grid-template-columns: repeat(2, 11px);
  grid-template-rows: repeat(2, 11px);
  gap: 3px;
  width: 30px;
  height: 30px;
  padding: 2px;
  border: 2px solid var(--accent);
  border-radius: 0;
  background: repeating-linear-gradient(135deg, var(--accent) 0 6px, var(--bg) 6px 12px);
  transform: rotate(-6deg);
}
.brand-symbol span { background: transparent; }
.eyebrow { margin: 0 0 3px; color: var(--muted); font-family: var(--mono-font); font-size: 13px; font-weight: 700; letter-spacing: 0.2em; line-height: 1.2; text-transform: uppercase; }
.accent-eyebrow { color: var(--accent); }
.brand-lockup .eyebrow { color: var(--accent); }
h1, h2, h3, p { margin-top: 0; }
h1 { margin: 0; font-family: var(--display-font); font-size: 30px; font-weight: 700; letter-spacing: 0.03em; line-height: 1.15; text-transform: uppercase; }
.topbar-actions { gap: 14px; }
.saved-status { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-family: var(--mono-font); font-size: 12px; letter-spacing: 0.06em; white-space: nowrap; }
.status-dot { width: 7px; height: 7px; border-radius: 0; background: var(--accent); box-shadow: 0 0 0 4px rgba(255, 106, 31, 0.16); }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 46px;
  padding: 0 18px;
  border: 2px solid var(--accent);
  border-radius: 2px;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.02em;
  transition: background 150ms ease, border-color 150ms ease, color 150ms ease;
}
.button svg, .text-button svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.7; }
.button-primary { color: var(--bg); background: var(--accent); }
.button-primary:hover { background: var(--accent-dim); border-color: var(--accent-dim); }
.button-quiet { color: var(--accent); background: transparent; }
.button-quiet:hover { background: rgba(255, 106, 31, 0.12); }

.workspace {
  display: grid;
  grid-template-columns: minmax(330px, 400px) minmax(0, 1fr);
  gap: clamp(24px, 3.5vw, 54px);
  width: min(1440px, calc(100% - 48px));
  margin: 0 auto;
  padding: 42px 0 56px;
}

.controls-panel { min-width: 0; }
.panel-intro { padding: 4px 4px 23px; }
.panel-intro h2, .preview-toolbar h2 { margin: 0; color: var(--ink); font-family: var(--display-font); font-size: 26px; font-weight: 700; letter-spacing: 0.03em; line-height: 1.2; text-transform: uppercase; }
.panel-intro p:last-child { max-width: 310px; margin: 11px 0 0; color: var(--ink-soft); font-size: 16px; line-height: 1.7; }

.control-card { padding: 19px 20px 20px; border: 1px solid var(--line); border-radius: 2px; background: var(--panel); }
.control-card + .control-card { margin-top: 12px; }
.card-heading { justify-content: space-between; margin-bottom: 18px; }
.card-heading > div { display: flex; align-items: center; gap: 10px; }
.step-number { color: var(--accent); font-family: var(--mono-font); font-size: 12px; font-weight: 700; letter-spacing: 0.08em; }
.card-heading h3 { margin: 0; font-size: 17px; letter-spacing: 0.01em; }
.heading-note { color: var(--muted); font-size: 13px; }
.a4-badge { padding: 5px 8px; border-radius: 2px; color: var(--bg); background: var(--accent); font-family: var(--mono-font); font-size: 12px; font-weight: 700; letter-spacing: 0.06em; }
.field-label { display: block; color: var(--ink-soft); font-size: 14px; font-weight: 700; line-height: 1.4; }
.field-label span { margin-left: 3px; color: var(--muted); font-family: var(--mono-font); font-size: 11px; font-weight: 500; letter-spacing: 0.03em; }
.text-input, textarea, select, .number-input-wrap { width: 100%; border: 1px solid var(--line); border-radius: 2px; outline: none; color: var(--ink); background: var(--panel-2); transition: border-color 150ms ease, box-shadow 150ms ease, background 150ms ease; }
.text-input { height: 46px; margin-top: 8px; padding: 0 12px; font-size: 16px; }
textarea { display: block; min-height: 124px; margin-top: 8px; padding: 11px 12px; resize: vertical; font-family: var(--mono-font); font-size: 15px; line-height: 1.7; }
.text-input::placeholder, textarea::placeholder { color: #857a63; }
.text-input:focus, textarea:focus, select:focus, .number-input-wrap:focus-within { border-color: var(--accent); box-shadow: var(--focus-ring); }
.textarea-group + .textarea-group { margin-top: 15px; padding-top: 15px; border-top: 1px dashed var(--line); }
.textarea-heading { justify-content: space-between; }
.char-count { color: var(--muted); font-family: var(--mono-font); font-size: 11px; }
.line-size-panel { margin-top: 9px; padding: 9px 10px 10px; border: 1px dashed var(--line); border-radius: 2px; background: var(--bg); }
.line-size-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 7px; }
.line-size-heading > div { display: flex; align-items: baseline; gap: 7px; min-width: 0; }
.line-size-heading strong { color: var(--ink-soft); font-size: 12px; }
.line-size-heading small { color: var(--muted); font-size: 11px; }
.line-reset-button { flex: none; padding: 0; border: 0; color: var(--accent); background: transparent; font-size: 11px; font-weight: 700; }
.line-reset-button:hover { text-decoration: underline; }
.line-size-list { display: grid; gap: 5px; }
.line-size-field { display: flex; align-items: center; gap: 7px; min-width: 0; }
.line-size-field > span { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; color: var(--ink-soft); font-size: 12px; }
.line-number { flex: none; color: var(--muted); font-family: var(--mono-font); font-size: 11px; }
.line-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.line-size-input { width: 68px; height: 36px; padding: 0 6px; border: 1px solid var(--line); border-radius: 2px; outline: none; color: var(--ink); background: var(--panel); font-size: 14px; text-align: right; }
.line-size-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255, 106, 31, 0.18); }
.line-size-empty { color: var(--muted); font-size: 11px; }

.field-row { gap: 10px; align-items: flex-end; }
.field-wrap { flex: 1; min-width: 0; }
.field-wrap > .field-label { margin-bottom: 8px; }
.select-wrap { position: relative; }
select { height: 44px; appearance: none; padding: 0 33px 0 10px; font-size: 15px; }
select option { background: var(--panel-2); color: var(--ink); }
.select-wrap > svg { position: absolute; top: 50%; right: 10px; width: 15px; height: 15px; pointer-events: none; transform: translateY(-50%); fill: none; stroke: var(--ink-soft); stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.7; }
.number-input-wrap { display: flex; align-items: center; height: 44px; padding-right: 10px; }
.number-input-wrap input { width: 100%; min-width: 0; height: 100%; padding: 0 0 0 10px; border: 0; outline: 0; color: var(--ink); background: transparent; font-size: 16px; }
.number-input-wrap span { color: var(--muted); font-family: var(--mono-font); font-size: 12px; }

.stepper-row { display: flex; align-items: center; gap: 8px; }
.stepper-row .number-input-wrap { flex: 1; width: auto; min-width: 0; }

.stepper-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: var(--panel-2);
  color: var(--ink);
  font-size: 19px;
  font-weight: 700;
  line-height: 1;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease;
}
.stepper-button:hover { border-color: var(--accent); color: var(--accent); background: var(--panel); }
.stepper-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.stepper-button:active { background: var(--accent-dim); color: var(--bg); border-color: var(--accent-dim); }
.stepper-button-compact { width: 34px; height: 34px; font-size: 16px; }

.line-size-stepper { display: flex; align-items: center; gap: 6px; flex: none; }

.field-hint { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

#fontSizeNumber::-webkit-inner-spin-button,
#fontSizeNumber::-webkit-outer-spin-button,
.line-size-input::-webkit-inner-spin-button,
.line-size-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
#fontSizeNumber, .line-size-input {
  appearance: textfield;
  -moz-appearance: textfield;
}
.range-row { display: block; margin-top: 17px; }
input[type="range"] { display: block; width: 100%; height: 5px; accent-color: var(--accent); cursor: pointer; }
.range-labels { display: flex; justify-content: space-between; margin-top: 6px; color: var(--muted); font-size: 12px; }
.style-toolbar { justify-content: space-between; flex-wrap: wrap; gap: 10px; row-gap: 12px; margin-top: 17px; }
.segmented-control { display: inline-flex; padding: 3px; border: 1px solid var(--line); border-radius: 2px; background: var(--panel-2); }
.segmented-control input { position: absolute; opacity: 0; pointer-events: none; }
.segmented-control label { display: inline-flex; align-items: center; justify-content: center; min-width: 48px; height: 32px; border-radius: 2px; color: var(--muted); font-size: 13px; font-weight: 700; cursor: pointer; transition: background 140ms ease, color 140ms ease; }
.segmented-control input:checked + label { color: var(--ink); background: var(--panel); box-shadow: inset 0 0 0 1px var(--line); }
.segmented-control input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 1px; }
.align-control label { min-width: 36px; }
.align-control svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.5; }

.options-card { padding-top: 7px; padding-bottom: 7px; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; min-height: 56px; cursor: pointer; }
.toggle-row + .toggle-row { border-top: 1px dashed var(--line); }
.toggle-copy { display: flex; flex-direction: column; gap: 2px; }
.toggle-copy strong { color: var(--ink-soft); font-size: 14px; }
.toggle-copy small { color: var(--muted); font-size: 12px; }
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-ui { position: relative; width: 34px; height: 20px; border: 1px solid var(--line); border-radius: 2px; background: var(--panel-2); transition: background 160ms ease, border-color 160ms ease; }
.toggle-ui::after { position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 2px; background: var(--paper); content: ""; transition: transform 160ms ease, background 160ms ease; }
.toggle-row input:checked + .toggle-ui { background: var(--accent); border-color: var(--accent); }
.toggle-row input:checked + .toggle-ui::after { background: var(--bg); transform: translateX(14px); }
.toggle-row input:focus-visible + .toggle-ui { outline: 2px solid var(--accent); outline-offset: 3px; }
.control-footer { justify-content: space-between; padding: 19px 4px 0; }
.text-button { display: inline-flex; align-items: center; gap: 6px; padding: 0; border: 0; color: var(--ink-soft); background: transparent; font-size: 13px; font-weight: 700; }
.text-button:hover { color: var(--accent); }
.control-footer p { display: inline-flex; align-items: center; gap: 3px; margin: 0; color: var(--muted); font-size: 12px; }
.keyboard-key { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 2px; color: var(--ink-soft); background: var(--panel-2); font-family: var(--mono-font); font-size: 11px; }

.preview-panel { min-width: 0; padding-top: 4px; }
.preview-toolbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; padding: 0 3px 18px; }
.preview-actions { gap: 15px; }
.preview-meta { color: var(--muted); font-size: 13px; white-space: nowrap; }
.zoom-controls { display: inline-flex; align-items: center; height: 36px; border: 1px solid var(--line); border-radius: 2px; overflow: hidden; background: var(--panel); }
.zoom-controls button { width: 32px; height: 100%; border: 0; color: var(--ink-soft); background: transparent; font-size: 20px; line-height: 1; }
.zoom-controls button:hover { color: var(--accent); background: var(--panel-2); }
.zoom-controls span { display: inline-flex; justify-content: center; width: 52px; color: var(--ink-soft); font-family: var(--mono-font); font-size: 12px; }
.preview-stage { display: flex; justify-content: center; min-height: 700px; padding: clamp(28px, 5vw, 64px) 24px; overflow: hidden; border: 1px solid var(--line); border-radius: 2px; background: var(--stage); background-image: radial-gradient(var(--stage-dot) 0.8px, transparent 0.8px); background-size: 14px 14px; }
.sheet-wrap { display: flex; align-items: flex-start; justify-content: center; width: 100%; transform-origin: top center; transition: transform 180ms ease; }
.print-sheet { position: relative; width: min(100%, 700px); aspect-ratio: 210 / 297; overflow: hidden; color: var(--mark-ink); background: var(--paper); box-shadow: var(--sheet-shadow); }

/* 對位記號：只裝飾畫面上的預覽紙（.preview-sheet），不會出現在 #printSheet / 列印輸出 */
.preview-sheet::before, .preview-sheet::after {
  content: "";
  position: absolute;
  left: 6px;
  right: 6px;
  height: 18px;
  pointer-events: none;
  background-image: linear-gradient(var(--accent), var(--accent)), linear-gradient(var(--accent), var(--accent)), linear-gradient(var(--accent), var(--accent)), linear-gradient(var(--accent), var(--accent));
  background-size: 18px 3px, 3px 18px, 18px 3px, 3px 18px;
  background-repeat: no-repeat;
}
.preview-sheet::before { top: 6px; background-position: left top, left top, right top, right top; }
.preview-sheet::after { bottom: 6px; background-position: left bottom, left bottom, right bottom, right bottom; }

.print-grid { display: grid; width: 100%; height: 100%; grid-template-columns: repeat(var(--columns), minmax(0, 1fr)); grid-template-rows: repeat(var(--rows), minmax(0, 1fr)); }
.mark-cell { position: relative; display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; padding: clamp(14px, 3.2%, 36px); overflow: hidden; border: 1px dashed var(--guide); text-align: center; }
.print-sheet[data-guides="false"] .mark-cell { border-color: transparent; }
.cell-index { position: absolute; top: 9px; right: 11px; color: #a3936b; font-family: var(--mono-font); font-size: 9px; letter-spacing: 0.05em; }
.mark-cell.single .mark-block { width: 100%; }
.mark-cell.combined { display: flex; flex-direction: column; gap: clamp(15px, 4.2%, 38px); }
.mark-block { width: 100%; min-height: 0; }
.block-kicker { margin: 0 0 9px; color: var(--accent-dim); font-family: var(--mono-font); font-size: 9px; font-weight: 700; letter-spacing: 0.18em; }
.combined .mark-block + .mark-block { padding-top: clamp(15px, 4.2%, 38px); border-top: 1px solid var(--guide); }
.print-sheet[data-divider="false"] .combined .mark-block + .mark-block { border-top-color: transparent; }
/* app.js 會依「字型」選單 inline 覆寫 --mark-font；箱面文字只用樸實的內容字型 */
.mark-content { max-width: 100%; color: var(--mark-ink); font-family: var(--mark-font); font-size: var(--mark-size); font-weight: 700; line-height: 1.32; }
.mark-line { display: block; width: 100%; white-space: nowrap; }
.mark-content[data-autofit="false"] .mark-line { overflow-wrap: anywhere; white-space: pre-wrap; }
.mark-cell[data-weight="regular"] .mark-content { font-weight: 500; }
.mark-cell[data-align="left"] { text-align: left; }
.mark-cell[data-align="right"] { text-align: right; }
.mark-cell[data-align="left"] .mark-content, .mark-cell[data-align="right"] .mark-content { margin-right: auto; }
.mark-cell[data-align="right"] .mark-content { margin-left: auto; margin-right: 0; }
.mark-cell.single[data-valign="top"] { align-items: flex-start; }
.mark-cell.single[data-valign="bottom"] { align-items: flex-end; }
.mark-cell.combined[data-valign="top"] { justify-content: flex-start; }
.mark-cell.combined[data-valign="bottom"] { justify-content: flex-end; }
.mark-content[data-empty="true"] { min-height: 1em; }
.mark-content[data-empty="true"]::before { color: #a3936b; content: "在左側輸入內容"; font-family: var(--ui-font); font-size: 14px; font-weight: 500; letter-spacing: 0; }
.combined .mark-content[data-empty="true"]::before { font-size: 13px; }
.preview-footer { justify-content: space-between; gap: 18px; padding: 16px 4px 0; color: var(--muted); font-size: 12px; }
.legend-item { gap: 7px; }
.legend-line { width: 20px; border-top: 1px dashed var(--guide); }
.preview-tip { gap: 5px; }
.preview-tip svg { width: 14px; height: 14px; fill: none; stroke: var(--accent); stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.5; }
.toast { position: fixed; right: 26px; bottom: 24px; z-index: 10; padding: 11px 15px; border: 1px solid var(--accent-dim); border-radius: 2px; color: var(--ink); background: var(--panel); box-shadow: 0 14px 30px rgba(0, 0, 0, 0.45); font-size: 15px; opacity: 0; pointer-events: none; transform: translateY(8px); transition: opacity 180ms ease, transform 180ms ease; }
.toast.is-visible { opacity: 1; transform: translateY(0); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.print-only { display: none; }

@media (max-width: 980px) {
  .topbar { align-items: flex-start; gap: 18px; }
  .topbar-actions { flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
  .saved-status { width: 100%; justify-content: flex-end; }
  .workspace { grid-template-columns: minmax(300px, 360px) minmax(0, 1fr); width: min(100% - 32px, 860px); gap: 24px; padding-top: 28px; }
  .preview-stage { min-height: 610px; padding-right: 16px; padding-left: 16px; }
}

@media (max-width: 720px) {
  .topbar { display: block; padding: 18px 18px 16px; }
  .topbar-actions { justify-content: flex-start; margin-top: 16px; }
  .saved-status { width: auto; margin-right: auto; }
  .button { height: 42px; padding: 0 14px; font-size: 14px; }
  .workspace { display: block; width: min(100% - 28px, 520px); padding-top: 26px; }
  .controls-panel { margin-bottom: 36px; }
  .panel-intro { padding-left: 0; }
  .preview-toolbar { align-items: flex-start; display: block; }
  .preview-actions { justify-content: space-between; margin-top: 13px; }
  .preview-stage { min-height: 0; padding: 32px 12px; }
  .preview-footer { display: block; }
  .preview-tip { margin-top: 9px; }
}

@media print {
  @page { size: A4 portrait; margin: 0; }
  html, body { width: 210mm; min-width: 210mm; height: 297mm; min-height: 297mm; margin: 0; padding: 0; background: white; }
  .screen-only { display: none !important; }
  .print-only { display: block !important; }
  .print-root, .print-sheet { width: 210mm; height: 297mm; margin: 0; padding: 0; }
  .print-sheet { overflow: hidden; box-shadow: none; }
  .print-grid { width: 210mm; height: 297mm; }
  .mark-cell { padding: 12mm; }
  .cell-index, .block-kicker { display: none !important; }
  .mark-content[data-empty="true"]::before { content: none; }
}