:root {
  color-scheme: light;
  --page: #f2f5f7;
  --paper: #fff;
  --ink: #182b33;
  --muted: #536771;
  --line: #dce5e8;
  --accent: #095e58;
  --accent-dark: #074a46;
  --accent-soft: #edf6f3;
  --sans: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  --serif: "Songti SC", "Noto Serif CJK SC", SimSun, serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--page); }
body { margin: 0; color: var(--ink); font: 1rem/1.7 var(--sans); }
body:has(dialog[open]) { overflow: hidden; }
button, input { font: inherit; }
input { accent-color: var(--accent); }
button:focus-visible, input:focus-visible { outline: 3px solid #168f83; outline-offset: 3px; }

.page { margin: 0 auto; max-width: 870px; padding: 34px 32px 56px; }
.sheet {
  background: var(--paper);
  border: 1px solid var(--line);
  border-top: 4px solid var(--accent);
  border-radius: 12px;
  padding: 44px 56px;
  box-shadow: 0 8px 32px #203d4a08;
}
.sheet-header { padding-bottom: 26px; border-bottom: 1px solid var(--line); margin-bottom: 28px; }
.hospital-logo { display: block; width: min(100%, 20rem); height: auto; margin-bottom: 21px; }
.sheet-header h1 {
  margin: 0;
  font: 700 clamp(1.25rem, 6.5vw, 2.5rem) / 1.23 var(--serif);
  text-align: center;
  letter-spacing: .02em;
}
.sheet-header h1 span { display: block; }
.sheet-header h1 span + span { margin-top: .18em; }
.document-text, .agreement-form { font-size: 1rem; line-height: 1.85; overflow-wrap: anywhere; }
.document-text p, .agreement-form p { margin: 0 0 7px; }
.document-text section { margin-top: 25px; }
.sheet h2 { margin: 0 0 6px; font-size: 1.0625rem; font-weight: 750; }
.document-text ol { margin: 0; padding-inline-start: 1.6em; }
.document-text li + li { margin-top: 3px; }
.agreement-form { border-top: 1px solid var(--line); margin-top: 30px; padding-top: 25px; }
.agreement-option {
  display: flex;
  align-items: flex-start;
  cursor: pointer;
  gap: 10px;
  background: var(--accent-soft);
  border: 1px solid #cfdfdb;
  padding: 12px 14px;
  border-radius: 8px;
  margin: 11px 0 20px;
}
.agreement-option input { flex: none; width: 18px; height: 18px; margin: .35em 0 0; }
.agreement-form .reference { color: var(--muted); font-size: .875rem; margin: 0 0 14px; }

.button {
  min-height: 46px;
  padding: 7px 20px;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 650;
  white-space: nowrap;
}
.button.primary { background: var(--accent); color: #fff; }
.button.primary:not(:disabled):hover { background: var(--accent-dark); }
.button.secondary { background: var(--paper); color: var(--ink); border-color: #b8c8cd; }
.button.secondary:not(:disabled):hover { background: var(--accent-soft); }
.button:disabled { background: #e7edf0; color: #52646d; border-color: transparent; cursor: default; }
.button.full-width { width: 100%; }

#signature-dialog {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  color: var(--ink);
  background: var(--page);
  overscroll-behavior: contain;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
#signature-dialog::backdrop { background: var(--page); }
.signature-layout {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  max-width: 1100px;
  min-height: 255px;
  height: 100%;
  margin: auto;
  padding: max(20px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right))
           max(18px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
  gap: 9px;
}
.signature-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.signature-header h2 { margin: 0; font-size: 1.4375rem; line-height: 1.35; }
.signature-header p { margin: 4px 0 0; color: var(--muted); font-size: .9375rem; }
.signature-stage { position: relative; min-height: 0; container-type: size; }
.signature-pad {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(100%, 250cqh);
  aspect-ratio: 5 / 2;
  overflow: hidden;
  border: 1px solid #9fb4ba;
  border-radius: 12px;
  background: var(--paper);
}
.signature-pad::before { content: ""; position: absolute; border-top: 1px dashed #c4d4d9; inset: 72% 10% auto; pointer-events: none; }
.signature-placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #596e77; pointer-events: none; }
#signature-canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.signature-status { margin: 0; color: #913222; font-size: .875rem; }
.signature-status:empty { display: none; }
.signature-actions { display: flex; justify-content: space-between; gap: 12px; }
.signature-actions .primary { min-width: 9.5rem; }

.receipt { text-align: center; padding-block: 55px; }
.receipt-mark { width: 58px; height: 58px; margin: 0 auto 16px; border-radius: 50%; background: var(--accent-soft); display: grid; place-items: center; }
.receipt-mark::before { content: ""; display: block; width: 20px; height: 11px; border: solid var(--accent); border-width: 0 0 3px 3px; transform: translateY(-2px) rotate(-45deg); }
.receipt h1 { margin: 0; font-size: 1.875rem; }
.receipt p { margin: 4px 0 0; color: var(--muted); }

@media (max-width: 600px) {
  .page { padding: 13px 12px max(24px, env(safe-area-inset-bottom)); }
  .sheet { padding: 27px 19px; border-radius: 9px; }
  .sheet-header { padding-bottom: 19px; margin-bottom: 20px; }
  .hospital-logo { width: min(100%, 17rem); margin-bottom: 17px; }
  .document-text section { margin-top: 22px; }
  .agreement-form { margin-top: 23px; padding-top: 20px; }
  .signature-layout { grid-template-rows: auto auto auto auto; align-content: start; padding: 16px; }
  .signature-stage { height: min(52vw, 250px); margin-top: 6px; }
  .receipt { padding-block: 38px; }
}

@media (orientation: landscape) and (max-height: 540px),
       (orientation: portrait) and (pointer: coarse) {
  .signature-layout { grid-template-rows: auto minmax(0, 1fr) auto auto; align-content: stretch; gap: 5px; padding-block: max(9px, env(safe-area-inset-top)) max(9px, env(safe-area-inset-bottom)); }
  .signature-stage { height: auto; margin-top: 0; }
  .signature-header h2 { font-size: 1.25rem; }
}

@media (orientation: portrait) and (pointer: coarse) {
  #signature-dialog { overflow: hidden; overflow: clip; }
  .signature-layout {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100vh;
    width: 100dvh;
    height: 100vw;
    max-width: none;
    min-height: 0;
    margin: 0;
    /* Clockwise rotation maps the screen's right edge to the panel's top. */
    padding: max(9px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom))
             max(9px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-top));
    transform: translate(-50%, -50%) rotate(90deg);
  }
}

[data-theme="paper"] {
  --page: #f2f2f0;
  --ink: #242826;
  --muted: #4c514e;
  --line: #d3d5d2;
  --accent: #1762ea;
  --accent-dark: #124fbe;
  --accent-soft: #eaf2ff;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--page);
}
[data-theme="paper"] .page { max-width: 810px; }
[data-theme="paper"] .sheet { padding: 52px 66px; border: 1px solid var(--line); border-radius: 0; box-shadow: none; }
[data-theme="paper"] .sheet-header {
  padding-block: 4px 23px;
  margin-bottom: 22px;
  border-color: var(--ink);
}
[data-theme="paper"] .document-text { font-family: var(--serif); line-height: 1.9; }
[data-theme="paper"] .document-text > p + p { text-indent: 2em; }
[data-theme="paper"] .document-text section { margin-top: 20px; }
[data-theme="paper"] .document-text h2 { font-size: 1rem; }
[data-theme="paper"] .agreement-form { border-top: 1px solid var(--ink); }
[data-theme="paper"] .agreement-option {
  border: 1px solid transparent;
  border-radius: 0;
  padding: 10px 12px;
  background: #f5f6f4;
}
[data-theme="paper"] .agreement-option:has(input:checked) {
  background: var(--accent-soft);
  border-color: var(--accent);
}
[data-theme="paper"] .button { border-radius: 4px; }
[data-theme="paper"] .button.secondary:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent-dark);
}
[data-theme="paper"] :is(button, input):focus-visible { outline-color: var(--accent); }
[data-theme="paper"] .signature-pad { border-radius: 3px; }

@media (max-width: 600px) {
  [data-theme="paper"] { --page: #fff; }
  [data-theme="paper"] .page { padding-inline: 0; padding-top: 0; }
  [data-theme="paper"] .sheet { padding: 28px 23px; border: 0; }
}
