:root {
  color-scheme: light;
  --bg: #f5f5f7;
  --surface: #fff;
  --surface-subtle: #f2f2f7;
  --text: #1d1d1f;
  --text-secondary: #5e5e63;
  --border: #c7c7cc;
  --border-strong: #8e8e93;
  --action: #0066cc;
  --action-hover: #0055b3;
  --success: #16733a;
  --warning: #8a4b00;
  --danger: #b4233d;
  --info-bg: #edf6ff;
  --success-bg: #ecf8f0;
  --warning-bg: #fff4df;
  --danger-bg: #fff0f3;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-overlay: 0 12px 36px rgb(0 0 0 / 18%), 0 2px 8px rgb(0 0 0 / 10%);
  --sidebar: 248px;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  font-optical-sizing: auto;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--bg); }
body { margin: 0; color: var(--text); background: var(--bg); min-width: 320px; }
button, input, textarea, select { font: inherit; }
button { touch-action: manipulation; }
button:focus-visible, input:focus-visible, [tabindex]:focus-visible { outline: 3px solid rgb(0 102 204 / 35%); outline-offset: 2px; }

.skip-link { position: fixed; left: 1rem; top: -5rem; z-index: 100; background: var(--surface); color: var(--action); padding: .75rem 1rem; border-radius: var(--radius-md); }
.skip-link:focus { top: 1rem; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.demo-banner { min-height: 36px; display: flex; align-items: center; justify-content: center; gap: .65rem; padding: .4rem 1rem; background: #1d1d1f; color: #fff; font-size: .875rem; letter-spacing: .01em; }
.topbar { min-height: 92px; display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: 1.25rem 2rem; background: rgb(255 255 255 / 92%); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; backdrop-filter: blur(18px) saturate(160%); }
.topbar h1 { margin: 0; font-size: 1.5rem; line-height: 1.2; letter-spacing: -.01em; }
.eyebrow { margin: 0 0 .2rem; color: var(--text-secondary); font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.topbar-actions, .button-row { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.persistence-label { color: var(--text-secondary); font-size: .8rem; }
.persistence-label[data-mode="disk"] { color: var(--success); }
.persistence-label[data-mode="browser"] { color: var(--warning); }

.app-shell { max-width: 1440px; min-height: calc(100vh - 128px); margin: 0 auto; display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); }
.stage-nav { padding: 2rem 1rem; border-right: 1px solid var(--border); background: var(--surface); }
.stage-nav ol { list-style: none; margin: 0; padding: 0; display: grid; gap: .45rem; }
.stage-button { width: 100%; min-height: 52px; display: grid; grid-template-columns: 32px 1fr auto; align-items: center; gap: .75rem; padding: .6rem .75rem; color: var(--text); background: transparent; border: 1px solid transparent; border-radius: var(--radius-md); text-align: left; cursor: pointer; }
.stage-button:hover:not(:disabled) { background: var(--surface-subtle); }
.stage-button[aria-current="step"] { background: var(--info-bg); border-color: #a9cff5; }
.stage-button:disabled { color: var(--text-secondary); opacity: .62; cursor: not-allowed; }
.stage-number { width: 28px; height: 28px; display: inline-grid; place-items: center; border: 1px solid var(--border-strong); border-radius: 50%; font-size: .8rem; font-weight: 700; }
.stage-button.completed .stage-number { color: #fff; background: var(--success); border-color: var(--success); }
.stage-label { display: grid; }
.stage-label small { color: var(--text-secondary); }
.stage-check { color: var(--success); font-weight: 800; }

main { min-width: 0; padding: 2.5rem clamp(1rem, 4vw, 3rem) 6rem; }
.screen { max-width: 1040px; margin: 0 auto; }
.screen-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1.5rem; margin-bottom: 1.5rem; }
.screen-header h2 { margin: .1rem 0 .35rem; font-size: clamp(1.75rem, 3vw, 2.25rem); line-height: 1.22; letter-spacing: -.02em; }
.lede { max-width: 68ch; margin: 0; color: var(--text-secondary); }
.screen-grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.5rem; align-items: start; }
.stack { display: grid; gap: 1rem; }
.stack-sm { display: grid; gap: .65rem; }

.card { padding: 1.25rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.card h3 { margin: 0 0 .85rem; font-size: 1.1rem; }
.card h4 { margin: 1rem 0 .35rem; font-size: 1rem; }
.summary-card { position: sticky; top: 116px; }
.summary-card .amount { margin: .15rem 0; font-size: 2rem; line-height: 1.2; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.meta { color: var(--text-secondary); font-size: .875rem; }
.divider { height: 1px; margin: 1rem 0; background: var(--border); }
.fact-list { display: grid; gap: .65rem; }
.fact { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; }
.fact dt, .fact dd { margin: 0; }
.fact dt { color: var(--text-secondary); }
.fact dd { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

.button { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .45rem; padding: .65rem 1rem; border-radius: var(--radius-md); border: 1px solid transparent; font-weight: 650; cursor: pointer; transition: transform 100ms ease-out, background-color 160ms ease, border-color 160ms ease; }
.button:active:not(:disabled) { transform: scale(.98); }
.button.primary { color: #fff; background: var(--action); }
.button.primary:hover:not(:disabled) { background: var(--action-hover); }
.button.secondary { color: var(--action); background: var(--surface); border-color: var(--border); }
.button.secondary:hover:not(:disabled), .button.tertiary:hover:not(:disabled) { background: var(--surface-subtle); }
.button.tertiary { color: var(--text); background: transparent; border-color: transparent; }
.button.danger { color: #fff; background: var(--danger); }
.button:disabled { opacity: .48; cursor: not-allowed; }
.button.full { width: 100%; }
.icon-button { width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--surface-subtle); color: var(--text); font-size: 1.4rem; cursor: pointer; }

.badge { min-height: 28px; display: inline-flex; align-items: center; gap: .35rem; padding: .2rem .65rem; border-radius: 999px; font-size: .8rem; font-weight: 700; white-space: nowrap; }
.badge.info { color: var(--action); background: var(--info-bg); }
.badge.success { color: var(--success); background: var(--success-bg); }
.badge.warning { color: var(--warning); background: var(--warning-bg); }
.badge.danger { color: var(--danger); background: var(--danger-bg); }
.status-box { padding: 1rem; border: 1px solid var(--border); border-left-width: 4px; border-radius: var(--radius-md); background: var(--surface); }
.status-box.info { border-left-color: var(--action); background: var(--info-bg); }
.status-box.success { border-left-color: var(--success); background: var(--success-bg); }
.status-box.warning { border-left-color: var(--warning); background: var(--warning-bg); }
.status-box.danger { border-left-color: var(--danger); background: var(--danger-bg); }
.status-box strong { display: block; margin-bottom: .25rem; }

.line-items, .file-list, .timeline, .contract-sections { list-style: none; margin: 0; padding: 0; display: grid; gap: .65rem; }
.line-item, .file-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 1rem; align-items: center; padding: .85rem 0; border-bottom: 1px solid var(--border); }
.line-item:last-child, .file-row:last-child { border-bottom: 0; }
.line-item strong, .file-row strong { display: block; }
.line-item .money { font-variant-numeric: tabular-nums; font-weight: 650; }
.contract-layout { display: grid; grid-template-columns: 180px minmax(0, 1fr); gap: 1.25rem; }
.contract-sections button { width: 100%; padding: .6rem .75rem; text-align: left; color: var(--text); background: transparent; border: 0; border-radius: var(--radius-sm); cursor: pointer; }
.contract-sections button:hover { background: var(--surface-subtle); }
.contract-copy { max-height: 470px; overflow: auto; padding-right: 1rem; scroll-behavior: smooth; }
.contract-copy section { padding-bottom: 1.6rem; }
.contract-copy p { line-height: 1.72; }
.contract-copy mark { color: inherit; background: var(--warning-bg); }

.field { display: grid; gap: .4rem; }
.field label, .checkbox-label { font-weight: 650; }
.field input[type="text"] { min-height: 46px; padding: .65rem .75rem; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-md); }
.field input[type="file"], .field textarea { width: 100%; min-height: 46px; padding: .65rem .75rem; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-md); }
.field textarea { resize: vertical; line-height: 1.5; }
.checkbox-label { display: grid; grid-template-columns: 24px 1fr; gap: .65rem; align-items: start; }
.checkbox-label input { width: 20px; height: 20px; margin: .15rem 0 0; }
.helper { margin: 0; color: var(--text-secondary); font-size: .875rem; }
.signature-preview { min-height: 100px; display: grid; place-items: center; border: 1px dashed var(--border-strong); border-radius: var(--radius-md); background: var(--surface-subtle); font-family: "Bradley Hand", "Kaiti TC", cursive; font-size: 2rem; }

.progress-track { height: 8px; overflow: hidden; border-radius: 999px; background: #dedee3; }
.progress-bar { height: 100%; border-radius: inherit; background: var(--action); transition: width 200ms ease; }
.file-row { grid-template-columns: minmax(0, 1fr) auto auto; }
.timeline li { position: relative; padding: .2rem 0 .9rem 1.7rem; }
.timeline li::before { content: ""; position: absolute; left: .35rem; top: .45rem; width: 9px; height: 9px; border-radius: 50%; background: var(--action); }
.timeline li::after { content: ""; position: absolute; left: .6rem; top: 1.1rem; bottom: -.2rem; width: 1px; background: var(--border); }
.timeline li:last-child::after { display: none; }

dialog { width: min(560px, calc(100vw - 2rem)); padding: 0; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-overlay); }
dialog::backdrop { background: rgb(0 0 0 / 42%); }
.dialog-content { padding: 1.5rem; }
.dialog-content h2 { margin: .15rem 0 .5rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .75rem; margin-top: 1.5rem; }

.mobile-action { margin-top: 1rem; }
.completion-summary { margin-top: 1rem; }
.completion-panel { padding: 1.5rem; border-radius: var(--radius-lg); background: var(--success-bg); border: 1px solid #a6d6b7; }
.completion-panel h3 { margin-top: 0; }
.invoice-summary, .installment-heading, .operator-summary { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.invoice-summary { margin: 1rem 0; }
.invoice-summary h3, .installment-heading h3 { margin: 0; }
.invoice-summary .amount, .installment-heading .amount { margin: 0; font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.payment-plan-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.installment-card { display: grid; align-content: start; gap: 1rem; }
fieldset { display: grid; gap: .65rem; margin: 0; padding: 1rem; border: 1px solid var(--border); border-radius: var(--radius-md); }
legend { padding: 0 .35rem; font-weight: 650; }
.scenario-option { min-height: 52px; display: grid; grid-template-columns: 24px 1fr; gap: .75rem; align-items: start; padding: .75rem; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); }
.scenario-option input { width: 20px; height: 20px; margin-top: .15rem; }
.scenario-option span { display: grid; }
.radio-inline { min-height: 44px; display: inline-flex; align-items: center; gap: .45rem; margin-right: 1rem; }
.radio-inline input { width: 20px; height: 20px; }
a.button { text-decoration: none; }

@media (max-width: 900px) {
  .topbar { padding: 1rem 1.25rem; }
  .app-shell { grid-template-columns: 1fr; }
  .stage-nav { position: sticky; top: 92px; z-index: 15; padding: .65rem 1rem; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .stage-nav ol { display: flex; width: max-content; }
  .stage-button { width: auto; min-width: 132px; grid-template-columns: 28px 1fr; }
  .stage-check { display: none; }
  main { padding-top: 1.5rem; }
  .screen-grid { grid-template-columns: 1fr; }
  .payment-plan-grid { grid-template-columns: 1fr; }
  .summary-card { position: static; }
}

@media (max-width: 640px) {
  .demo-banner { align-items: flex-start; flex-direction: column; gap: 0; }
  .topbar { position: static; min-height: 0; align-items: flex-start; }
  .topbar h1 { font-size: 1.25rem; }
  .topbar-actions { justify-content: flex-end; }
  .topbar-actions .button { min-width: 44px; padding-inline: .75rem; }
  .stage-nav { top: 0; }
  .stage-button { min-width: 112px; min-height: 48px; padding: .45rem .4rem; gap: .45rem; }
  .stage-label small { display: none; }
  main { padding: 1.25rem 1rem 3rem; }
  .screen-header { display: grid; }
  .screen-header h2 { font-size: 1.75rem; }
  .contract-layout { grid-template-columns: 1fr; }
  .contract-sections { grid-template-columns: repeat(4, minmax(100px, 1fr)); overflow-x: auto; }
  .contract-copy { max-height: none; overflow: visible; padding-right: 0; }
  .file-row { grid-template-columns: minmax(0, 1fr) auto; }
  .file-row .file-action { grid-column: 1 / -1; }
  .mobile-action { position: static; padding-top: .75rem; margin-top: 1rem; border-top: 1px solid var(--border); }
  .mobile-action .button { width: 100%; }
  .dialog-actions { display: grid; grid-template-columns: 1fr; }
  .dialog-actions .button.primary { order: -1; }
  .invoice-summary, .installment-heading, .operator-summary { align-items: flex-start; flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

@media (prefers-reduced-transparency: reduce) {
  .topbar, .mobile-action { background: var(--surface); backdrop-filter: none; }
}

@media (prefers-contrast: more) {
  :root { --border: #6e6e73; --border-strong: #1d1d1f; }
  .card, .button.secondary, .status-box { border-width: 2px; }
}
