@font-face { font-family: 'Great Vibes'; src: url('/assets/fonts/GreatVibes-Regular.ttf') format('truetype'); font-display: swap; }
@font-face { font-family: 'Allura'; src: url('/assets/fonts/Allura-Regular.ttf') format('truetype'); font-display: swap; }

:root {
  --navy: #10294A; --navy-2: #1c3d68; --gold: #B08C1A; --gold-2: #C9A227;
  --ink: #16202e; --muted: #5f6b7a; --line: #dde2ea; --bg: #f4f5f8; --card: #fff;
  --ok: #1a7a3e; --warn: #9a6a00; --err: #b3261e;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,41,74,.06), 0 4px 16px rgba(16,41,74,.06);
  --ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --serif: 'Times New Roman', Times, 'Liberation Serif', Georgia, serif;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; font: 15px/1.5 var(--ui); color: var(--ink); background: var(--bg); }
a { color: var(--navy-2); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* ---------- header / footer ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px clamp(16px, 3vw, 32px); background: rgba(255,255,255,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); }
.brand img { display: block; height: 36px; width: auto; }
.topbar nav { display: flex; gap: 18px; flex-wrap: wrap; }
.topbar nav a { color: var(--navy); text-decoration: none; font-weight: 500; }
.topbar nav a:hover { color: var(--gold); }
.footer { text-align: center; color: var(--muted); font-size: 13px; padding: 28px 16px 36px; }

.hero { max-width: 900px; margin: 0 auto; padding: 44px 20px 18px; text-align: center; }
.hero h1 { font: 700 clamp(28px, 4.2vw, 44px)/1.15 Georgia, var(--serif); color: var(--navy); margin: 0 0 12px; }
.hero h1 span { color: var(--gold); font-weight: 400; }
.hero p { color: var(--muted); font-size: 17px; margin: 0 auto; max-width: 720px; }

/* ---------- builder layout ---------- */
.workspace { display: grid; grid-template-columns: minmax(0, 520px) minmax(0, 1fr); gap: 24px;
  max-width: 1440px; margin: 0 auto; padding: 20px clamp(16px, 3vw, 32px) 40px; align-items: start; }
.preview-wrap { position: sticky; top: 76px; max-height: calc(100vh - 92px); display: flex; flex-direction: column;
  background: #e9ecf1; border-radius: var(--radius); border: 1px solid var(--line); overflow: hidden; }
.preview-head { display: flex; justify-content: space-between; padding: 10px 16px; font-size: 13px; font-weight: 600;
  color: var(--navy); border-bottom: 1px solid var(--line); background: #fff; }
.preview-wrap .doc { overflow: auto; margin: 16px; box-shadow: var(--shadow); }
.edit-banner { max-width: 1440px; margin: 16px auto 0; padding: 12px 16px; background: #fff8e1; border: 1px solid #f0d98a;
  border-radius: var(--radius); width: calc(100% - 2 * clamp(16px, 3vw, 32px)); }
.edit-banner .warn { margin-top: 6px; color: var(--warn); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px;
  margin-bottom: 16px; box-shadow: var(--shadow); }
.card h2 { font-size: 15px; margin: 0 0 14px; color: var(--navy); display: flex; align-items: center; gap: 10px; }
.side .card h2 { margin-top: 4px; display: block; }
.side .card h2 + * { margin-top: 0; }
.side .card h2:not(:first-child) { margin-top: 20px; }
.step { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--navy);
  color: #fff; font-size: 12px; font-weight: 700; }

.field { margin-bottom: 12px; min-width: 0; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: #394657; margin-bottom: 4px; }
.field label small { font-weight: 400; color: var(--muted); }
.field.narrow { max-width: 260px; }
input, select, textarea { width: 100%; font: inherit; color: inherit; padding: 9px 11px; border: 1px solid #c9d0db;
  border-radius: 8px; background: #fff; transition: border-color .15s, box-shadow .15s; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--navy-2); box-shadow: 0 0 0 3px rgba(28,61,104,.15); }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; }
.hint { font-size: 12.5px; color: var(--warn); background: #fffaeb; border-radius: 8px; padding: 8px 10px; margin: 4px 0 0; }
.hint:empty { display: none; }

fieldset.party { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px 4px; margin: 0 0 12px; }
fieldset.party legend { font-weight: 700; color: var(--navy); padding: 0 6px; font-size: 13.5px; }
.seg { display: inline-flex; background: #eef1f5; border-radius: 8px; padding: 3px; margin-bottom: 12px; }
.seg label { cursor: pointer; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; inset-inline-start: 0; }
.seg span { display: block; padding: 5px 14px; border-radius: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg input:checked + span { background: #fff; color: var(--navy); box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.seg input:focus-visible + span { outline: 2px solid var(--navy-2); }

/* toggle "slider" */
.switch { display: flex; align-items: flex-start; gap: 12px; cursor: pointer; margin: 0 0 12px; position: relative; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .slider { flex: 0 0 44px; height: 24px; border-radius: 12px; background: #c3cad5; position: relative; transition: background .2s; margin-top: 1px; }
.switch .slider::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch input:checked + .slider { background: var(--navy-2); }
.switch input:checked + .slider::after { transform: translateX(20px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--gold); outline-offset: 2px; }
.switch .lbl { display: flex; flex-direction: column; }
.switch .lbl small { color: var(--muted); font-size: 12.5px; }
.switch.compact { align-items: center; margin-bottom: 8px; }

.check { display: flex; gap: 10px; align-items: flex-start; margin: 10px 0; cursor: pointer; }
.check input { width: 18px; height: 18px; flex: 0 0 18px; margin: 2px 0 0; accent-color: var(--navy-2); }

/* buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: 600 14px var(--ui);
  padding: 9px 16px; border-radius: 8px; border: 1px solid #c3cad5; background: #fff; color: var(--navy);
  cursor: pointer; text-decoration: none; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s; }
.btn:hover { background: #f1f4f8; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn.primary:hover { background: var(--navy-2); }
.btn.gold { background: var(--gold-2); border-color: var(--gold); color: #1b1300; }
.btn.gold:hover { background: #d8b33a; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.actions.sticky { position: sticky; bottom: 0; background: linear-gradient(to top, var(--bg) 70%, transparent);
  padding: 16px 0 12px; z-index: 5; }
.disclaimer { font-size: 12px; color: var(--muted); }

/* ---------- document ---------- */
.doc { background: #fff; padding: clamp(20px, 5vw, 64px) clamp(18px, 5vw, 68px); font: 15px/1.55 var(--serif); color: #111; }
.doc.paper { border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow); }
.d-title { text-align: center; text-transform: uppercase; letter-spacing: .06em; font-size: 20px; margin: 0 0 28px; }
.d-p { text-align: justify; margin: 0 0 11px; }
.d-h { display: flex; font-size: 15px; text-transform: uppercase; margin: 22px 0 9px; }
.d-num { display: flex; text-align: justify; margin: 0 0 9px; }
.d-num .n, .d-h .n { flex: 0 0 44px; }
.d-num > div { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.d-l { margin-inline-start: 44px; }
.ph { background: #fff1a8; border-radius: 2px; padding: 0 2px; font-family: var(--ui); font-size: .82em; color: #6b5200; }
.d-sigs { display: grid; grid-template-columns: 1fr 1fr; gap: 32px; margin-top: 28px; font-size: 14px; }
.d-sig .who { font-weight: 700; }
.d-sig .role { color: var(--muted); margin-bottom: 8px; }
.d-sig .img { height: 70px; border-bottom: 1px solid #333; display: flex; align-items: flex-end; margin-bottom: 6px; }
.d-sig .img img { max-height: 66px; max-width: 100%; }
.d-sig .stamp { margin-top: 6px; font: 12px var(--ui); color: var(--ok); }

/* ---------- agreement page ---------- */
.loading { text-align: center; padding: 80px 16px; color: var(--muted); }
.error-page { max-width: 560px; margin: 60px auto; text-align: center; padding: 0 16px; }
.agr { max-width: 1440px; margin: 0 auto; padding: 22px clamp(16px, 3vw, 32px) 20px; }
.agr-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.agr-head h1 { font: 700 clamp(19px, 2.4vw, 25px)/1.25 Georgia, var(--serif); color: var(--navy); margin: 0 0 8px; overflow-wrap: anywhere; }
.meta { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.badge { font-size: 12px; font-weight: 700; padding: 3px 10px; border-radius: 999px; background: #e8ecf2; color: var(--navy); }
.badge.draft { background: #eef1f5; color: #4a5667; }
.badge.part { background: #fff3cd; color: #7a5600; }
.badge.done { background: #dff3e6; color: var(--ok); }
.badge.role { background: #e6ebf5; }
.ver { font-size: 13px; display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.ver select { width: auto; padding: 3px 8px; font-size: 13px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar.dim { opacity: .45; pointer-events: none; }
.notice { background: #eef3fb; border: 1px solid #cbd9ee; border-radius: var(--radius); padding: 12px 16px; margin-bottom: 14px; }
.notice.ok { background: #eaf7ef; border-color: #bfe3cc; }
.sign-cta { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap;
  background: var(--navy); color: #fff; border-radius: var(--radius); padding: 14px 18px; margin-bottom: 16px; }
.sign-cta .muted { color: #c6d2e3; }
.agr-body { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 24px; align-items: start; }
.agr-body > .doc { grid-column: 1; }
.side { grid-column: 2; grid-row: 1 / span 2; position: sticky; top: 76px; max-height: calc(100vh - 92px); overflow: auto; }
.linkrow { margin-bottom: 10px; }
.linkrow label { display: block; font-size: 12.5px; font-weight: 600; color: #394657; margin-bottom: 3px; }
.linkrow label small { font-weight: 400; color: var(--muted); }
.copy { display: flex; gap: 6px; }
.copy input { font-size: 12px; padding: 6px 8px; background: #f7f8fa; }
.sigstatus, .activity, .versions { list-style: none; margin: 0; padding: 0; }
.sigstatus li { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.sigstatus li:last-child { border: 0; }
.sigstatus .st { text-align: right; color: var(--muted); }
.sigstatus li.signed .st { color: var(--ok); font-weight: 600; }
.versions li { padding: 6px 0; border-bottom: 1px solid #eef0f4; display: flex; flex-direction: column; }
.versions li.cur a { font-weight: 700; color: var(--gold); }
.activity li { padding: 7px 0; border-bottom: 1px solid #eef0f4; font-size: 13px; overflow-wrap: anywhere; }

/* ---------- direct editor ---------- */
.editor { padding: 20px; font-family: var(--ui); }
.editor-bar { position: sticky; top: 64px; z-index: 4; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: #fff; padding: 8px 0 12px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.editor-actions { display: flex; gap: 8px; align-items: flex-start; }
.ed-row { position: relative; display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 0 8px; padding: 4px 6px; border-radius: 8px; }
.ed-row:hover, .ed-row:focus-within { background: #f6f8fb; }
.ed-n { font: 600 13px var(--serif); color: var(--muted); padding-top: 9px; text-align: end; }
.ed-t { font: 15px/1.5 var(--serif); padding: 7px 9px; min-height: 38px; resize: none; overflow: hidden; }
.k-h .ed-t, .k-title .ed-t { font-weight: 700; text-transform: uppercase; }
.k-l { margin-inline-start: 30px; }
.ed-tools { position: absolute; inset-inline-end: 12px; top: -14px; z-index: 3; display: flex; gap: 4px; align-items: center; background: #fff;
  border: 1px solid var(--line); border-radius: 8px; padding: 3px; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .15s; }
.ed-row:hover .ed-tools, .ed-row:focus-within .ed-tools { opacity: 1; pointer-events: auto; }
.ed-tools select { width: auto; padding: 2px 6px; font-size: 12px; }
.ed-tools button { border: 1px solid #c9d0db; background: #fff; border-radius: 6px; padding: 2px 8px; cursor: pointer; font-size: 13px; }
.ed-tools button.danger { color: var(--err); }
.ed-sig { display: block; border: 1px dashed #c9d0db; margin-top: 12px; }
.ed-sig-title { font-weight: 700; margin-bottom: 8px; color: var(--navy); }
.ed-sig-party { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; margin-bottom: 10px; align-items: end; }
.ed-sig-party strong { grid-column: 1 / -1; }
.ed-sig-party label { font-size: 12px; color: var(--muted); }
@media (hover: none) { .ed-row { padding-bottom: 36px; } .ed-tools { opacity: 1; pointer-events: auto; top: auto; bottom: 2px; } }

/* ---------- modal / signing ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(10,20,35,.55); display: grid; place-items: center; padding: 16px; overflow: auto; }
.modal-box { background: #fff; border-radius: 14px; padding: 22px 24px; width: 100%; max-width: 520px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-box.wide { max-width: 720px; }
.modal-box h2 { margin: 0 0 8px; color: var(--navy); font: 700 21px Georgia, var(--serif); }
.modal-box #smDoc { overflow-wrap: anywhere; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 6px 0 12px; }
.tab { border: 0; background: none; font: 600 14px var(--ui); padding: 8px 14px; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tab.active { color: var(--navy); border-bottom-color: var(--gold-2); }
.padwrap { position: relative; border: 1px solid #c9d0db; border-radius: 10px; background: #fcfcfd; margin-bottom: 8px; }
.padwrap canvas { display: block; width: 100%; aspect-ratio: 10 / 3; height: auto; touch-action: none; cursor: crosshair; }
.padwrap.typed canvas { cursor: default; }
.padline { position: absolute; left: 6%; right: 6%; bottom: 18%; border-top: 1px solid #b8c0cc; font-size: 11px; color: #98a2b0;
  padding-top: 3px; pointer-events: none; text-align: center; }
.fontpick { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.fontopt { border: 1px solid #c9d0db; background: #fff; border-radius: 8px; padding: 4px 16px; cursor: pointer; font-size: 26px; color: #0b1f3a; }
.fontopt.active { border-color: var(--navy-2); box-shadow: 0 0 0 2px rgba(28,61,104,.2); }
.f-gv { font-family: 'Great Vibes', cursive; }
.f-al { font-family: 'Allura', cursive; }
.consent { font-size: 13.5px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column;
  gap: 8px; width: min(92vw, 460px); }
.toast { background: var(--navy); color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.2);
  font-size: 14px; transition: opacity .5s, transform .5s; }
.toast.err { background: var(--err); }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- static pages ---------- */
.page { max-width: 820px; margin: 0 auto; padding: 36px 20px; }
.page h1 { font: 700 30px Georgia, var(--serif); color: var(--navy); }
.page h2 { color: var(--navy); font-size: 18px; margin-top: 28px; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .agr-body { grid-template-columns: minmax(0, 1fr); }
  .side { grid-column: 1; grid-row: auto; position: static; max-height: none; }
}
@media (max-width: 960px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .preview-wrap { position: static; max-height: none; }
}
@media (max-width: 560px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .d-sigs { grid-template-columns: 1fr; }
  .d-num .n, .d-h .n { flex-basis: 36px; }
  .d-l { margin-inline-start: 20px; }
  .d-p, .d-num { text-align: left; }
  .d-title { font-size: 17px; }
  .topbar nav { gap: 12px; font-size: 14px; }
  .brand img { height: 30px; }
  .ed-row { grid-template-columns: 38px minmax(0, 1fr); }
  .k-l { margin-inline-start: 10px; }
}

@media print {
  .topbar, .footer, .toolbar, .side, .notice, .sign-cta, .hero, .form, .preview-head { display: none !important; }
  body { background: #fff; }
  .doc, .doc.paper { box-shadow: none; border: 0; padding: 0; }
  .agr-body, .workspace { display: block; }
}
.linkrow.off { opacity: .45; }
.off-tag { display: none; color: var(--err); font-weight: 600; font-style: normal; }
.linkrow.off .off-tag { display: inline; }

/* ---------- i18n additions ---------- */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.langsw { display: inline-flex; align-items: center; gap: 4px; color: var(--navy); }
.langsw select { width: auto; padding: 4px 8px; font-size: 13px; border-radius: 6px; }
.topbar nav { align-items: center; }
.hero .accent { color: var(--gold); font-weight: 400; }
.hints { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 6px; }
.hints:empty { display: none; }
.hints li { font-size: 12.5px; color: #6b4d00; background: #fffaeb; border-radius: 8px; padding: 7px 10px; }
.badge.lang { background: #f3eedc; color: #6b5200; }
.d-sig .seal { margin-top: 10px; color: var(--muted); }
.field small.muted { display: block; margin-top: 4px; font-size: 12px; }

/* Right-to-left documents / UI */
[dir="rtl"] .switch .slider::after { left: auto; right: 3px; }
[dir="rtl"] .switch input:checked + .slider::after { transform: translateX(-20px); }
[dir="rtl"] .doc, .doc[dir="rtl"] { font-family: 'Noto Naskh Arabic', 'Noto Sans Arabic', 'Times New Roman', serif; }
.doc[lang="zh"], .doc[lang="ja"] { font-family: 'Noto Serif CJK SC', 'Noto Serif CJK JP', 'Songti SC', 'SimSun', 'Hiragino Mincho ProN', 'Yu Mincho', serif; text-align: start; }
.doc[lang="zh"] .d-p, .doc[lang="zh"] .d-num, .doc[lang="ja"] .d-p, .doc[lang="ja"] .d-num { text-align: justify; }
.doc[lang="hi"] { font-family: 'Noto Serif Devanagari', 'Noto Sans Devanagari', 'Mangal', 'Nirmala UI', serif; }
.doc .d-num .n, .doc .d-h .n { flex: 0 0 auto; min-width: 44px; padding-inline-end: 8px; }

/* length slider + structured address */
.sizefield { margin-top: 14px; }
.sizefield input[type=range] { width: 100%; padding: 0; border: 0; accent-color: var(--navy-2); box-shadow: none; }
.size-ticks { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.size-ticks span.on { color: var(--navy); font-weight: 700; }
.addr-title { font-size: 12.5px; font-weight: 700; color: var(--navy); margin: 4px 0 6px; }
.grid-addr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; }
.grid-addr .wide { grid-column: 1 / -1; }
