:root {
  --ink: #1a1a1a;
  --red: #b3121b;
  --red-dark: #8c0d14;
  --paper: #f4efe4;
  --card: #fffdf8;
  --line: #d8d0bf;
  --muted: #6b6457;
  --serif: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  --sans: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; background: var(--paper); color: var(--ink);
  font-family: var(--sans); font-size: 18px; line-height: 1.5;
  background-image: radial-gradient(rgba(0,0,0,.05) 1px, transparent 1.2px); background-size: 6px 6px;
}
.app { max-width: 720px; margin: 0 auto; padding: 24px 16px calc(24px + env(safe-area-inset-bottom)); min-height: 100vh; display: flex; flex-direction: column; }
.view { flex: 1; }
[hidden] { display: none !important; }

/* 標題 */
.hero { text-align: center; padding: 8px 0 20px; }
.hero-mast {
  display: inline-block; font-family: var(--serif); font-weight: 900; color: var(--red); font-size: 22px; letter-spacing: .2em;
  border-top: 3px solid var(--ink); border-bottom: 1px solid var(--ink); padding: 4px 6px 2px;
}
.hero-mast span { display: block; font-size: 11px; letter-spacing: .3em; color: var(--ink); font-family: Georgia, "Times New Roman", serif; }
.hero h1 { font-family: var(--serif); font-weight: 900; font-size: clamp(32px, 8vw, 54px); line-height: 1.2; margin: 18px 0 8px; }
.hero h1 span { display: inline-block; }
.hero-sub { margin: 0; font-size: clamp(18px, 4.2vw, 22px); color: var(--muted); }

/* 表單 */
.card { background: var(--card); border: 2px solid var(--ink); box-shadow: 6px 6px 0 var(--ink); padding: 24px 22px 26px; }
.field { margin-bottom: 22px; }
.field > label, .field > .label { display: block; font-weight: 700; font-size: 20px; margin-bottom: 8px; }
.field em { font-style: normal; font-size: 14px; color: var(--red); margin-left: 4px; }
.opt { font-weight: 400; font-size: 14px; color: var(--muted); margin-left: 4px; }
input[type=text], select {
  width: 100%; height: 60px; font: inherit; font-size: 22px; color: var(--ink);
  background: #fff; border: 2px solid #9a927f; border-radius: 10px; padding: 0 14px; outline: none;
  -webkit-appearance: none; appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--ink) 50%), linear-gradient(135deg, var(--ink) 50%, transparent 50%);
  background-position: calc(100% - 22px) 50%, calc(100% - 15px) 50%; background-size: 7px 7px; background-repeat: no-repeat;
  padding-right: 36px;
}
input[type=text]:focus, select:focus { border-color: var(--ink); box-shadow: 0 0 0 3px rgba(179,18,27,.25); }
.date-row { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 10px; margin-top: 12px; }
.date-row > div { min-width: 0; }
.sublabel { display: block; font-size: 16px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
#bquick { letter-spacing: .12em; font-variant-numeric: tabular-nums; }
::placeholder { color: #6b6457; opacity: 1; }
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.hint { margin-top: 6px; font-size: 15px; color: var(--muted); }
.err { margin: 6px 0 0; color: var(--red); font-weight: 700; font-size: 16px; min-height: 0; }
.err:empty { display: none; }
.field.invalid input, .field.invalid select, .field.invalid .seg button { border-color: var(--red); }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.seg button {
  height: 72px; font: inherit; font-size: 28px; font-weight: 700; color: var(--ink); background: #fff;
  border: 2px solid #9a927f; border-radius: 12px; cursor: pointer; touch-action: manipulation;
}
.seg button { display: flex; align-items: center; justify-content: center; gap: 8px; }
.seg button .chk { display: none; font-size: 26px; line-height: 1; }
.seg button[aria-checked=true] { background: var(--red); border-color: var(--red-dark); color: #fff; box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--red-dark); }
.seg button[aria-checked=true] .chk { display: inline; }
.seg button:focus-visible { outline: 3px solid var(--ink); outline-offset: 4px; }

.form-err { margin: 0 0 14px; color: var(--red); font-weight: 700; text-align: center; }
.form-err:empty { display: none; }

/* 按鈕 */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  font: inherit; font-weight: 700; color: var(--ink); background: #fff; border: 2px solid var(--ink); border-radius: 12px;
  cursor: pointer; touch-action: manipulation; transition: transform .08s;
}
.btn:active { transform: translateY(2px); }
.btn:disabled { cursor: not-allowed; transform: none; }
/* 停用狀態用實色而非透明度，文字對比仍 ≥4.5:1 */
.btn:disabled, .btn-primary:disabled { background: #e4ded2; border-color: #a39b8a; color: #4f493e; box-shadow: none; }
.btn:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }
.btn.used-up small { display: block; font-size: 15px; }
.btn.used-up { flex-direction: column; line-height: 1.25; }
.btn-big { width: 100%; min-height: 68px; font-size: 24px; padding: 8px 16px; }
.btn-primary { background: var(--red); border-color: var(--red-dark); color: #fff; box-shadow: 0 4px 0 var(--red-dark); }
.btn-primary:active { box-shadow: 0 2px 0 var(--red-dark); }
.btn-ghost { background: transparent; }
.btn small { font-size: 16px; font-weight: 400; }

/* 印報中 */
#viewWait { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 30px 0; }
.wait-title { font-family: var(--serif); font-weight: 900; font-size: 40px; margin: 26px 0 6px; letter-spacing: .3em; padding-left: 1.8em; }
.wait-title::after { content: ""; display: inline-block; width: 1.5em; text-align: left; animation: dots 1.4s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "．"; } 50% { content: "．．"; } 75% { content: "．．．"; } }
.wait-msg { font-size: 22px; min-height: 2.6em; margin: 0; max-width: 22em; transition: opacity .35s; }
.wait-msg.fade { opacity: 0; }
.wait-time { color: var(--muted); font-size: 15px; margin: 6px 0 0; }

.press { position: relative; width: 300px; height: 230px; }
.press-paper {
  position: absolute; left: 30px; top: 0; width: 120px; height: 120px; overflow: hidden;
  background: #fff; border: 2px solid var(--ink); border-bottom: 0;
}
.press-paper-lines {
  position: absolute; left: 0; right: 0; top: -100%; height: 300%;
  background:
    repeating-linear-gradient(transparent 0 10px, #bbb 10px 12px, transparent 12px 16px, #bbb 16px 18px, transparent 18px 30px, var(--ink) 30px 36px, transparent 36px 48px);
  background-size: 100% 48px; animation: feed .9s linear infinite;
}
@keyframes feed { to { transform: translateY(48px); } }
.press-body {
  position: absolute; left: 0; top: 104px; width: 200px; height: 110px; background: var(--ink); border-radius: 10px 10px 4px 4px;
  box-shadow: 0 6px 0 #000;
}
.roller {
  position: absolute; top: -18px; width: 54px; height: 54px; border-radius: 50%; background: #444; border: 5px solid #777;
  background-image: repeating-conic-gradient(#555 0 20deg, #333 20deg 40deg);
  animation: spin 1s linear infinite;
}
.roller.r1 { left: 26px; }
.roller.r2 { left: 98px; animation-direction: reverse; }
@keyframes spin { to { transform: rotate(360deg); } }
.press-label { position: absolute; left: 0; right: 0; bottom: 30px; color: #fff; font-family: var(--serif); font-weight: 900; letter-spacing: .15em; font-size: 15px; }
.press-gauge { position: absolute; left: 18px; right: 18px; bottom: 12px; height: 8px; background: #333; border-radius: 4px; overflow: hidden; }
.press-gauge i { display: block; height: 100%; width: 40%; background: var(--red); animation: gauge 1.6s ease-in-out infinite alternate; }
@keyframes gauge { from { transform: translateX(-60%); } to { transform: translateX(210%); } }
.press-out { position: absolute; left: 190px; top: 150px; width: 110px; height: 80px; }
.sheet-fly {
  position: absolute; left: 0; top: 0; width: 64px; height: 44px; background: #fff; border: 2px solid var(--ink);
  background-image: linear-gradient(var(--red) 0 7px, transparent 7px), repeating-linear-gradient(transparent 0 11px, #999 11px 13px);
  background-position: 0 4px, 0 6px; background-size: 100% 100%, 100% 100%;
  opacity: 0; animation: fly 2.4s ease-out infinite;
}
.sheet-fly.s2 { animation-delay: .8s; }
.sheet-fly.s3 { animation-delay: 1.6s; }
@keyframes fly {
  0% { opacity: 0; transform: translate(-30px, 10px) rotate(0); }
  15% { opacity: 1; }
  70% { opacity: 1; transform: translate(30px, 30px) rotate(12deg); }
  100% { opacity: 0; transform: translate(40px, 36px) rotate(14deg); }
}

/* 完成 */
.done-title { font-family: var(--serif); font-weight: 900; font-size: clamp(26px, 6vw, 38px); text-align: center; margin: 4px 0 16px; }
.done-title span { color: var(--red); }
.preview { position: relative; width: 100%; max-width: 520px; margin: 0 auto 20px; }
.preview-box {
  position: relative; width: 100%; aspect-ratio: 257 / 364; overflow: hidden; background: #fffdf8;
  box-shadow: 0 2px 4px rgba(0,0,0,.15), 0 14px 30px rgba(0,0,0,.22);
}
.preview-box iframe {
  position: absolute; left: 0; top: 0; border: 0; width: 257mm; height: 364mm; transform-origin: 0 0; pointer-events: none; background: #fffdf8;
}
.preview-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.preview-loading.gone { display: none; }
.actions { display: grid; grid-template-columns: 1fr; gap: 12px; }
.actions .btn-primary { font-size: 28px; min-height: 76px; }

.foot { text-align: center; padding-top: 24px; }
.foot span { display: inline-block; background: var(--paper); color: #3f3a30; font-size: 14px; padding: 2px 10px; }

/* 錯誤框（含再試一次） */
.alert { margin: 0 0 16px; padding: 14px 16px; border: 2px solid var(--red); border-radius: 12px; background: #fff3f2; text-align: center; }
.alert-msg { margin: 0 0 10px; color: #8c0d14; font-weight: 700; font-size: 19px; }
.btn-alert { min-height: 52px; padding: 6px 28px; font-size: 20px; border-color: var(--red-dark); color: var(--red-dark); }
.alert:has(.btn-alert[hidden]) .alert-msg { margin-bottom: 0; }
.status-msg { margin: 0; text-align: center; color: #3f3a30; font-size: 16px; }
.status-msg:empty { display: none; }

/* 閒置倒數 */
.idle {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50;
  width: min(560px, calc(100% - 32px)); display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center;
  background: var(--ink); color: #fff; padding: 14px 18px; border-radius: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.35);
}
.idle p { margin: 0; font-size: 19px; flex: 1 1 220px; }
.idle b { font-size: 26px; color: #ffd7d4; font-variant-numeric: tabular-nums; }
.idle .btn { min-height: 52px; padding: 6px 22px; font-size: 20px; box-shadow: none; }

/* 列印用 iframe：不能 display:none（有些瀏覽器不渲染），移出畫面外 */
.print-frame { position: fixed; left: -10000px; top: 0; width: 257mm; height: 364mm; border: 0; opacity: 0; pointer-events: none; }

/* 平板橫向：預覽與按鈕並排 */
@media (min-width: 900px) and (orientation: landscape) {
  .app { max-width: 1100px; }
  #viewForm { display: grid; grid-template-columns: 1fr 1.2fr; gap: 40px; align-items: center; }
  .hero { text-align: left; }
  #viewDone { display: grid; grid-template-columns: auto 340px; grid-template-rows: auto 1fr; gap: 0 36px; align-items: start; }
  .done-title { grid-column: 1 / -1; }
  .preview { width: min(520px, calc((100vh - 170px) * 257 / 364)); margin: 0; }
  .actions { grid-column: 2; grid-row: 2; }
}
@media (max-width: 420px) {
  .card { padding: 18px 14px 20px; box-shadow: 4px 4px 0 var(--ink); }
  input[type=text], select { font-size: 20px; height: 56px; padding: 0 10px; }
  select { padding-right: 28px; background-position: calc(100% - 16px) 50%, calc(100% - 9px) 50%; }
  .date-row { gap: 6px; }
  .press { transform: scale(.85); }
}
