:root {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --soft: #f8fafc;
  --line: #dfe5ee;
  --line-strong: #c5cfdd;
  --ink: #202833;
  --muted: #687483;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --danger: #b44949;
  --ok: #238451;
  color-scheme: light;
}

html {
  -webkit-text-size-adjust: 100%;
}

[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", "Segoe UI", Arial, sans-serif;
}

.auth-screen {
  display: none;
}

body.auth-locked {
  background: #eef3f8;
}

body.auth-locked .app-shell {
  display: none;
}

body.auth-locked .auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.auth-card {
  width: min(100%, 360px);
  display: grid;
  gap: 12px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 18px 45px rgba(30, 45, 64, 0.12);
}

.auth-card h1 {
  margin: 0;
  font-size: 20px;
}

.auth-card p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.auth-card input {
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  outline: none;
}

.auth-card input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.auth-message {
  color: var(--danger);
  font-size: 13px;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: 52px 1fr;
}

.topbar {
  display: grid;
  grid-template-columns: 220px 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.brand strong {
  font-size: 15px;
}

.brand span,
.card-head span,
.sidebar-head span,
.status-note,
.contract-item span {
  color: var(--muted);
  font-size: 12px;
}

.tabs,
.top-actions,
.action-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tabs button,
.ghost-button,
.primary-button {
  min-height: 32px;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  font-size: 13px;
}

.tabs button {
  border-color: transparent;
  color: var(--muted);
}

.tabs button.is-active {
  background: #edf3ff;
  color: var(--accent);
}

.primary-button {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.primary-button:hover {
  background: var(--accent-dark);
}

.ghost-button:hover {
  background: var(--soft);
}

.primary-button:disabled,
.ghost-button:disabled,
input:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.workspace {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  min-height: calc(100vh - 52px);
  padding: 14px;
  border-right: 1px solid var(--line);
  background: #fbfcfe;
}

.sidebar-head,
.card-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.sidebar-head h2,
.card-head h2,
.signer-card h2 {
  margin: 0;
  font-size: 14px;
}

.search-input,
.share-box input,
.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

.field input[type="file"] {
  height: auto;
  padding: 6px;
}

.search-input,
.share-box input,
.field input,
.field select {
  height: 34px;
  padding: 0 9px;
}

.field textarea {
  min-height: 74px;
  padding: 8px 9px;
  resize: vertical;
}

.search-input:focus,
.share-box input:focus,
.field input:focus,
.field textarea:focus,
.field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.editable-contract .contract-page {
  font-family: "Microsoft YaHei", "PingFang SC", "Segoe UI", Arial, sans-serif;
  font-size: 12px;
}

.editable-block {
  padding-top: 6mm;
  margin-top: 6mm;
  border-top: 1px solid #dfe5ee;
}

.editable-block h3 {
  margin: 0 0 10px;
  font-size: 14px;
}

.embedded-grid {
  display: grid;
  gap: 10px 18px;
}

.embedded-grid.two-col {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.embedded-field {
  display: grid;
  gap: 5px;
  min-width: 0;
  margin-bottom: 10px;
}

.embedded-field.is-wide {
  grid-column: 1 / -1;
}

.embedded-label {
  color: #334155;
  font-size: 12px;
}

.embedded-field input,
.embedded-field textarea,
.embedded-field select {
  width: 100%;
  border: 1px solid #dbe3ee;
  border-radius: 5px;
  background: #f6f9fd;
  color: var(--ink);
  outline: none;
}

.embedded-field input,
.embedded-field select {
  height: 34px;
  padding: 0 9px;
}

.embedded-field textarea {
  min-height: 62px;
  padding: 8px 9px;
  resize: vertical;
}

.embedded-field input[type="file"] {
  height: auto;
  padding: 6px;
}

.option-manager {
  display: grid;
  gap: 8px;
}

.option-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 34px;
  padding: 0 9px;
  border: 1px solid #dbe3ee;
  border-radius: 5px;
  background: #f6f9fd;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.option-trigger-value {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.option-trigger-action {
  flex: 0 0 auto;
  margin-left: 12px;
  color: var(--muted);
  font-size: 12px;
}

.option-manager.is-open .option-trigger {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.option-panel {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid #dbe3ee;
  border-radius: 6px;
  background: #fbfcff;
}

.option-list {
  display: grid;
  gap: 6px;
}

.option-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.option-select,
.option-delete {
  min-height: 34px;
  border-radius: 5px;
}

.option-select {
  width: 100%;
  padding: 0 10px;
  border: 1px solid #dbe3ee;
  background: #fff;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.option-select.is-selected {
  border-color: #b7cbf7;
  background: #edf4ff;
  color: #1d4ed8;
}

.option-delete {
  padding: 0 10px;
  border: 1px solid #edc5c5;
  background: #fff7f7;
  color: var(--danger);
  cursor: pointer;
}

.option-add-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.option-add-row input {
  min-width: 0;
}

.embedded-field input:focus,
.embedded-field textarea:focus,
.embedded-field select:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.retention-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 34px;
  color: #334155;
  font-size: 12px;
  white-space: nowrap;
}

.inline-check input {
  width: 16px;
  height: 16px;
}

.month-stats {
  display: grid;
  gap: 8px;
  margin: 14px 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.month-stats strong {
  font-size: 13px;
}

.month-stats div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.month-stats b {
  color: var(--ink);
  font-size: 13px;
}

.contract-list {
  display: grid;
  gap: 8px;
}

.contract-item {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  text-align: left;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: start;
}

.contract-select-button {
  width: 100%;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
  color: inherit;
  cursor: pointer;
}

.contract-item:hover,
.contract-item.is-active {
  border-color: var(--accent);
}

.contract-item strong,
.contract-item-main,
.contract-item-main span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-contract-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 26px;
  padding: 0 8px;
  border: 1px solid #edc5c5;
  border-radius: 5px;
  background: #fff7f7;
  color: var(--danger);
  font-size: 12px;
}

.delete-contract-button:hover {
  background: #ffecec;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 22px;
  margin-top: 8px;
  padding: 0 8px;
  border-radius: 999px;
  background: #e7edf5;
  color: #4b5868;
  font-size: 12px;
}

.status-signed {
  background: #e3f3e9;
  color: var(--ok);
}

.status-published,
.status-confirmed {
  background: #edf3ff;
  color: var(--accent);
}

.status-revoked {
  background: #f8e8e8;
  color: var(--danger);
}

.main-area {
  min-width: 0;
  padding: 14px;
}

.view-panel {
  display: none;
}

.view-panel.is-active {
  display: block;
}

.signer-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  max-width: 1180px;
  margin: 0 auto;
  align-items: start;
}

.card {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
}

.form-card,
.signer-card,
.clause-card {
  padding: 16px;
}

.form-section {
  padding: 0 0 14px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.form-section:last-child {
  margin-bottom: 0;
  border-bottom: 0;
}

.form-section h3 {
  margin: 0 0 10px;
  font-size: 13px;
}

.field {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  margin-top: 8px;
}

.field.is-wide {
  grid-template-columns: 1fr;
  gap: 6px;
}

.field span {
  color: #3a4654;
  font-size: 12px;
}

.upload-field {
  display: grid;
  gap: 8px;
}

.upload-preview {
  display: block;
  max-width: 140px;
  max-height: 54px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  padding: 4px;
}

.upload-empty {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
}

.upload-inline-status {
  color: var(--muted);
  font-size: 12px;
}

.signer-upload-preview {
  display: block;
  max-width: 220px;
  max-height: 120px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 6px;
}

.party-a-sign-pad {
  width: min(100%, 640px);
  aspect-ratio: 4 / 1;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: #fff;
  overflow: hidden;
}

#partyASignatureCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.signature-tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.required {
  color: var(--danger);
  margin-left: 2px;
}

.preview-card {
  overflow: hidden;
}

.preview-card .card-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfe;
}

.share-box {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #f5f9ff;
}

.share-box label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
}

.share-box div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.validation-box {
  margin: 12px 14px 0;
  padding: 10px;
  border: 1px solid #e7bbbb;
  border-radius: 5px;
  background: #fff7f7;
  color: var(--danger);
  font-size: 13px;
}

.doc-scroll {
  height: calc(100vh - 150px);
  overflow: auto;
  padding: 18px;
  background: #e4e9f0;
}

.contract-document {
  display: grid;
  gap: 18px;
  justify-content: center;
}

.contract-page {
  position: relative;
  overflow: hidden;
  isolation: isolate;
  width: 210mm;
  min-height: 297mm;
  padding: 18mm 25mm;
  background: #fff;
  color: #111;
  box-shadow: 0 12px 30px rgba(30, 45, 64, 0.14);
  font-family: SimSun, "Songti SC", serif;
  font-size: 11pt;
  line-height: 1.72;
}

.contract-page-content {
  position: relative;
  z-index: 1;
}

.contract-watermark {
  position: absolute;
  inset: 0;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 92%;
  margin: auto;
  pointer-events: none;
  user-select: none;
  font-family: SimSun, "Songti SC", serif;
  font-size: clamp(72px, 18vw, 180px);
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.12em;
  white-space: nowrap;
  text-align: center;
  color: rgba(80, 80, 80, 0.2);
  transform: rotate(-30deg) scale(1.15);
  transform-origin: center;
}

.doc-header {
  text-align: center;
  padding-bottom: 3mm;
  margin-bottom: 9mm;
  border-bottom: 1px solid #222;
  font-size: 10pt;
}

.doc-title {
  margin: 0 0 8mm;
  text-align: center;
  font-size: 20pt;
  line-height: 1.3;
}

.party-grid,
.info-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 6mm;
}

.party-grid th,
.party-grid td,
.info-table th,
.info-table td {
  border: 1px solid #222;
  padding: 2.2mm 2.5mm;
  vertical-align: top;
}

.party-grid th,
.info-table th {
  width: 24mm;
  background: #f5f5f5;
  font-weight: 700;
}

.doc-section {
  margin-top: 6mm;
}

.doc-section h3 {
  margin: 0 0 3mm;
  font-size: 13pt;
}

.doc-section p {
  margin: 0 0 2.8mm;
  text-indent: 2em;
}

.signature-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20mm;
  margin-top: 10mm;
  break-inside: avoid;
}

.signature-col {
  min-height: 38mm;
}

.signature-line,
.date-line strong,
.stamp-line {
  display: block;
  min-height: 12mm;
  border-bottom: 1px solid #333;
}

.signature-line img {
  max-width: 58mm;
  max-height: 18mm;
  object-fit: contain;
}

.date-line {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2mm;
  align-items: end;
}

.final-meta {
  margin-top: 8mm;
  color: #444;
  font-size: 9pt;
  word-break: break-all;
}

.signer-card {
  display: grid;
  gap: 12px;
  width: min(100%, 720px);
  margin: 0 auto;
}

.signer-card-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.confirm-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  color: #344252;
  font-size: 13px;
}

.confirm-line input {
  width: 18px;
  height: 18px;
}

.signature-pad {
  width: 100%;
  aspect-ratio: 3 / 1;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: #fff !important;
  overflow: hidden;
  color-scheme: only light;
  forced-color-adjust: none;
  overscroll-behavior: contain;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#signatureCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #fff !important;
  touch-action: none;
  color-scheme: only light;
  forced-color-adjust: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.status-note {
  padding: 9px 10px;
  border-radius: 5px;
  background: #f4f7fb;
  line-height: 1.45;
}

.clause-list {
  display: grid;
  gap: 12px;
}

.clause-item {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfe;
}

.clause-item h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.clause-item p {
  margin: 5px 0 0;
  color: #344252;
  font-size: 13px;
  line-height: 1.65;
}

.clause-textarea {
  min-height: 132px;
  line-height: 1.65;
}

#printRoot {
  display: none;
}

body.exporting-pdf {
  background: #fff;
}

body.exporting-pdf .app-shell {
  display: none;
}

body.exporting-pdf #printRoot {
  display: block;
}

body.exporting-pdf .contract-document {
  display: block;
}

body.exporting-pdf .contract-page {
  margin: 0 auto;
  box-shadow: none;
}

.signer-portal .topbar {
  grid-template-columns: 1fr;
}

.signer-portal .tabs,
.signer-portal .top-actions,
.signer-portal .sidebar {
  display: none;
}

.signer-portal .workspace {
  grid-template-columns: 1fr;
}

.signer-portal .main-area {
  padding: 16px;
}

.signer-portal .signer-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  max-width: 1180px;
  margin: 0 auto;
}

.signer-portal .signer-card {
  width: min(100%, 720px);
  margin: 0 auto;
}

@media (max-width: 1160px) {
  .workspace,
  .signer-grid {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .contract-page {
    width: min(210mm, 100%);
    padding: 14mm 10mm;
  }

  .topbar {
    height: auto;
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .tabs,
  .top-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 720px) {
  body {
    background: #f7f9fc;
  }

  .app-shell {
    grid-template-rows: auto 1fr;
  }

  .topbar {
    gap: 10px;
    padding: 10px 12px;
  }

  .brand strong {
    font-size: 16px;
  }

  .tabs button,
  .ghost-button,
  .primary-button {
    min-height: 40px;
    padding: 0 12px;
    font-size: 14px;
  }

  .main-area {
    padding: 10px;
  }

  .doc-scroll {
    height: auto;
    max-height: none;
    padding: 10px;
    overflow: visible;
    background: #eef2f7;
  }

  .contract-document {
    display: block;
  }

  .contract-page {
    width: 100%;
    min-height: auto;
    padding: 18px 14px;
    border-radius: 6px;
    box-shadow: 0 8px 22px rgba(30, 45, 64, 0.1);
    font-size: 12px;
    line-height: 1.58;
  }

  .contract-watermark {
    width: 94%;
    font-size: clamp(54px, 20vw, 110px);
    letter-spacing: 0.08em;
    transform: rotate(-30deg) scale(1.05);
  }

  .doc-title {
    margin-bottom: 18px;
    font-size: 22px;
  }

  .party-grid,
  .info-table {
    table-layout: fixed;
    font-size: 12px;
  }

  .party-grid th,
  .party-grid td,
  .info-table th,
  .info-table td {
    padding: 7px 6px;
    word-break: break-word;
  }

  .info-table th,
  .party-grid th {
    width: 72px;
  }

  .embedded-grid.two-col {
    grid-template-columns: 1fr;
  }

  .embedded-field input,
  .embedded-field select {
    height: 42px;
    font-size: 15px;
  }

  .option-trigger,
  .option-select,
  .option-delete {
    min-height: 42px;
    font-size: 15px;
  }

  .option-item,
  .option-add-row {
    grid-template-columns: 1fr;
  }

  .embedded-field textarea {
    min-height: 90px;
    font-size: 15px;
  }

  .signature-grid {
    grid-template-columns: 1fr;
    gap: 14px;
    margin-top: 22px;
  }

  .signature-col {
    min-height: auto;
  }

  .signer-portal .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: 0 1px 10px rgba(20, 33, 45, 0.08);
  }

  .signer-portal .main-area {
    padding: 10px 10px 18px;
  }

  .signer-portal .signer-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .signer-portal .signer-card {
    position: static;
    padding: 14px;
    border-radius: 8px;
    box-shadow: 0 10px 28px rgba(30, 45, 64, 0.12);
  }

  .signer-card h2 {
    font-size: 16px;
  }

  .signer-card-head {
    align-items: center;
  }

  .signer-card-head .ghost-button {
    min-height: 36px;
    padding: 0 10px;
    font-size: 13px;
  }

  .signer-card .field {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .signer-card .field input {
    height: 44px;
    font-size: 16px;
  }

  .signer-card .field input[type="file"] {
    height: auto;
    min-height: 44px;
    padding: 10px 12px;
  }

  .confirm-line {
    align-items: flex-start;
    min-height: 42px;
    font-size: 14px;
    line-height: 1.45;
  }

  .confirm-line input {
    flex: 0 0 auto;
    width: 22px;
    height: 22px;
    margin-top: 1px;
  }

  .signature-pad {
    aspect-ratio: auto;
    height: clamp(180px, 34vh, 230px);
    border-width: 2px;
    border-radius: 8px;
    background:
      linear-gradient(transparent calc(100% - 1px), #eef2f7 1px),
      #fff;
    background-size: 100% 42px;
  }

  #signatureCanvas {
    background: #fff !important;
  }

  .signer-upload-preview {
    max-width: 100%;
    max-height: 140px;
  }

  .signer-card .action-row {
    position: static;
    display: grid;
    grid-template-columns: 0.9fr 1.1fr;
    gap: 8px;
    padding: 0;
    border-top: 0;
    background: transparent;
    box-shadow: none;
  }

  .signer-card .action-row button {
    width: 100%;
    min-height: 46px;
    border-radius: 7px;
    font-size: 15px;
  }

  .status-note {
    font-size: 13px;
  }
}

@page {
  size: A4;
  margin: 0;
}

@media print {
  body {
    background: #fff;
  }

  body > :not(#printRoot) {
    display: none !important;
  }

  #printRoot {
    display: block;
  }

  #printRoot .contract-document {
    display: block;
  }

  #printRoot .contract-page {
    width: 210mm;
    min-height: auto;
    margin: 0;
    padding: 18mm 25mm;
    box-shadow: none;
  }

  #printRoot .contract-watermark {
    width: 92%;
    font-size: 44mm;
    letter-spacing: 0.14em;
    color: rgba(80, 80, 80, 0.2);
    transform: rotate(-30deg) scale(1.08);
  }
}
