:root {
  color-scheme: light;
  --ink: #1b2233;
  --muted: #5b6475;
  --line: #d9dee8;
  --bg: #eef1f6;
  --surface: #ffffff;
  --header: #1f2a44;
  --accent: #1d3fa5;
  --sign: #f2a900;
  --sign-bg: rgba(255, 196, 0, 0.18);
  --danger: #c62828;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
  padding-bottom: 110px;
}
body.modal-open { overflow: hidden; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--header);
  color: #fff;
}
.top h1 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.doc-title { margin: 2px 0 0; font-size: 0.9rem; opacity: 0.85; overflow-wrap: anywhere; }
.lang-btn {
  flex: none;
  font: inherit;
  font-size: 0.85rem;
  color: #fff;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
}
.intro { max-width: 900px; margin: 14px auto 10px; padding: 0 16px; font-size: 0.95rem; }

/* ---------- pages and fields ---------- */
#pages {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.status { text-align: center; color: var(--muted); padding: 48px 16px; }
.page {
  position: relative;
  width: 100%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.14), 0 1px 2px rgba(16, 24, 40, 0.06);
}
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.field {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
}
.field .label { pointer-events: none; white-space: nowrap; }
.field.signature {
  border: 2px dashed var(--sign);
  background: var(--sign-bg);
  color: #7a5600;
  font-weight: 600;
  cursor: pointer;
}
.field.signature img {
  display: none;
  position: absolute;
  inset: 2px;
  width: calc(100% - 4px);
  height: calc(100% - 4px);
  object-fit: contain;
}
.field.signature.placed { border: 1px solid rgba(29, 63, 165, 0.35); background: transparent; }
.field.signature.placed .label { display: none; }
.field.signature.placed img { display: block; }
.field .remove {
  display: none;
  position: absolute;
  top: -12px;
  inset-inline-end: -12px;
  width: 26px;
  height: 26px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 17px;
  line-height: 26px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.field.signature.placed .remove { display: block; }
.field.date { border: 1px dashed #9aa3b5; color: var(--accent); }
.field:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.field.flash { animation: flash 1.1s ease-out 2; }
@keyframes flash {
  from { box-shadow: 0 0 0 0 rgba(242, 169, 0, 0.9); }
  to { box-shadow: 0 0 0 16px rgba(242, 169, 0, 0); }
}

/* ---------- bottom bar ---------- */
.bar {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 10px rgba(16, 24, 40, 0.08);
}
.progress { font-size: 0.9rem; color: var(--muted); }
.bar-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-inline-start: auto; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 10px 16px;
  font: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 0.9rem; }

/* ---------- signature pad ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(15, 20, 35, 0.55);
}
.sheet {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 640px;
  padding: 14px 16px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-radius: 14px 14px 0 0;
}
@media (min-width: 700px) {
  .modal { align-items: center; }
  .sheet { border-radius: 14px; }
}
.sheet-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h2 { margin: 0; font-size: 1.05rem; }
.tabs { display: flex; padding: 3px; background: var(--bg); border-radius: 8px; }
.tab {
  padding: 6px 12px;
  font: inherit;
  font-size: 0.9rem;
  color: var(--muted);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
.tab.active { color: var(--ink); background: var(--surface); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.pad-area {
  position: relative;
  height: clamp(170px, 38vh, 300px);
  background: #fff linear-gradient(#c9cfdb, #c9cfdb) no-repeat center bottom 26% / calc(100% - 40px) 1px;
  border: 1px solid var(--line);
  border-radius: 10px;
}
#pad-canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.pad-hint {
  position: absolute;
  inset-inline: 0;
  top: 38%;
  text-align: center;
  color: #9aa3b5;
  font-size: 0.95rem;
  pointer-events: none;
}
.type-area { display: flex; flex-direction: column; gap: 10px; }
#type-input {
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  font-size: 1.05rem;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.type-preview { position: relative; height: 120px; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
#type-preview { position: absolute; inset: 0; width: 100%; height: 100%; }
.pad-tools { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.colors { display: flex; gap: 10px; }
.swatch {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
}
.swatch[data-color="blue"] { background: #1d3fa5; }
.swatch[data-color="black"] { background: #111111; }
.swatch.active { box-shadow: 0 0 0 2px var(--accent); }
.tool-buttons { display: flex; gap: 8px; }
.sheet-foot { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- overlays ---------- */
.busy {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(255, 255, 255, 0.88);
}
.spinner {
  width: 42px;
  height: 42px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result {
  position: fixed;
  inset: 0;
  z-index: 25;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  background: rgba(238, 241, 246, 0.97);
}
.result h2 { margin: 0; font-size: 1.4rem; }
.result p { margin: 0; max-width: 420px; }

/* ---------- message pages (expired / signed / not found) ---------- */
body.message-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}
.message {
  max-width: 460px;
  padding: 28px 24px;
  text-align: center;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.14);
}
.message p { margin: 0 0 10px; }
.message p[lang="en"] { margin: 0; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  .field.flash { animation: none; outline: 3px solid var(--sign); }
  .spinner { animation-duration: 2.5s; }
}
