:root {
  --brand: #1565C0;
  --brand-dark: #0D47A1;
  --brand-light: #E3F2FD;
  --border: #E0E0E0;
  --muted: #616161;
  --text: #212121;
  --surface: #F5F7FA;
  --card: #ffffff;
  --success: #2E7D32;
  --error: #C62828;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: "Google Sans", "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 24px;
  background: linear-gradient(135deg, var(--brand-dark) 0%, var(--brand) 100%);
  color: #fff;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
  background: #fff;
  border-radius: 8px;
  padding: 4px;
  flex-shrink: 0;
}

.brand-logo.loading {
  opacity: 0;
}

.brand-title {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.brand-subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  opacity: 0.9;
  font-weight: 400;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.app-header button.secondary,
.app-header .btn.secondary {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.35);
}

.app-header button.secondary:hover,
.app-header .btn.secondary:hover {
  background: rgba(255, 255, 255, 0.22);
}

.app-footer {
  margin-top: auto;
  padding: 16px 24px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  background: #fff;
}

.app-footer a {
  color: var(--brand);
  text-decoration: none;
}

.app-footer a:hover {
  text-decoration: underline;
}

.page-main {
  flex: 1;
}

.admin-page {
  height: 100vh;
  overflow: hidden;
}

.admin-page-main {
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding-bottom: 16px;
}

.landing-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.landing-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
}

.landing-card {
  width: 100%;
  max-width: 480px;
  text-align: center;
}

.landing-card h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.landing-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
}

.landing-actions a.btn {
  display: block;
  text-decoration: none;
  text-align: center;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.card h2 {
  margin: 0 0 12px;
  font-size: 15px;
}

.muted { color: var(--muted); }

.status {
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-size: 13px;
}

.status.error { background: #FFEBEE; color: var(--error); }
.status.success { background: #E8F5E9; color: var(--success); }
.status.info { background: var(--brand-light); color: var(--brand-dark); }

button, .btn {
  appearance: none;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: var(--brand);
  color: #fff;
  text-decoration: none;
  display: inline-block;
}

button:disabled, .btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

button.secondary {
  background: #fff;
  color: var(--brand);
  border: 1px solid var(--border);
}

input[type="text"], input[type="email"], textarea, select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}

.field { margin-bottom: 14px; }

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.tab {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
}

.tab.active {
  background: var(--brand-light);
  color: var(--brand-dark);
  border-color: var(--brand);
}

.canvas-wrap {
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: repeating-conic-gradient(#ececec 0% 25%, #fff 0% 50%) 50% / 14px 14px;
  overflow: hidden;
}

canvas.draw-canvas {
  display: block;
  width: 100%;
  height: 140px;
  touch-action: none;
}

.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.badge.pending { background: #FFF8E1; color: #F57F17; }
.badge.partially_signed { background: #E3F2FD; color: #1565C0; }
.badge.signed { background: #E8F5E9; color: var(--success); }
.badge.expired { background: #ECEFF1; color: #546E7A; }
.badge.declined { background: #FFEBEE; color: #C62828; }
.badge.changes_requested { background: #FFF3E0; color: #E65100; }

button.secondary.danger {
  color: var(--error);
  border-color: #ef9a9a;
}

.review-response-panel {
  margin-top: 20px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fafafa;
}

.review-response-panel summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--brand-dark);
}

.review-response-body {
  margin-top: 12px;
}

.review-response-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.review-response-actions button {
  flex: 1;
  min-width: 140px;
}

textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  resize: vertical;
  min-height: 96px;
}

.timeline {
  list-style: none;
  margin: 0;
  padding: 0;
}

.timeline li {
  padding: 10px 0 10px 16px;
  border-left: 2px solid var(--border);
  margin-left: 6px;
}

.timeline li strong {
  display: block;
}

.pdf-stage {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: auto;
  background: #fafafa;
  max-height: 70vh;
}

.pdf-page-wrap {
  position: relative;
  margin: 12px auto;
  width: fit-content;
}

.pdf-page-wrap canvas {
  display: block;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.signature-overlay {
  position: absolute;
  border: 2px dashed var(--brand);
  cursor: move;
  touch-action: none;
  background: rgba(255, 255, 255, 0.2);
}

.signature-overlay img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.signature-field-guide {
  position: absolute;
  border: 2px dashed var(--brand);
  background: rgba(21, 101, 192, 0.08);
  pointer-events: none;
  box-sizing: border-box;
}

.signature-field-label {
  position: absolute;
  top: -22px;
  left: 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--brand-dark);
  background: #fff;
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid var(--brand);
}

.consent {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 13px;
  line-height: 1.45;
}

.legal-notice {
  background: #f8faf9;
  border: 1px solid #d8e8e0;
  border-radius: 12px;
  padding: 16px 18px;
  margin: 16px 0;
  font-size: 13px;
  line-height: 1.5;
}

.legal-notice h3 {
  margin: 0 0 12px;
  font-size: 15px;
  color: var(--brand-dark);
}

.legal-notice h4 {
  margin: 12px 0 4px;
  font-size: 13px;
  color: var(--brand-dark);
}

.legal-notice p {
  margin: 0;
  color: var(--muted);
}

.legal-section + .legal-section {
  margin-top: 4px;
}

.legal-accordion-stack {
  display: grid;
  gap: 8px;
}

details.legal-accordion {
  background: #f8faf9;
  border: 1px solid #d8e8e0;
  border-radius: 12px;
  overflow: hidden;
}

details.legal-accordion summary {
  padding: 14px 18px;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
  color: var(--brand-dark);
  list-style: none;
  user-select: none;
}

details.legal-accordion summary::-webkit-details-marker {
  display: none;
}

details.legal-accordion summary::after {
  content: "▸";
  float: right;
  color: var(--muted);
  transition: transform 0.15s ease;
}

details.legal-accordion[open] summary::after {
  transform: rotate(90deg);
}

details.legal-accordion .legal-notice-body {
  padding: 0 18px 16px;
  border-top: 1px solid #e8f0ec;
  font-size: 13px;
  line-height: 1.5;
}

details.legal-accordion .legal-notice-body .legal-summary {
  margin: 12px 0 0;
  color: var(--muted);
}

details.legal-accordion .legal-notice-body h4 {
  margin: 12px 0 4px;
  font-size: 13px;
  color: var(--brand-dark);
}

details.legal-accordion .legal-notice-body p {
  margin: 0;
  color: var(--muted);
}

.legal-policy-summary {
  background: #f5f8fc;
  border: 1px solid #dbe6f5;
  border-radius: 12px;
  padding: 12px 16px;
  margin: 12px 0 16px;
}

.legal-policy-summary .privacy-settings-summary {
  margin: 0;
}

.sign-legal-block {
  margin: 16px 0;
}

.sign-legal-intro {
  margin: 0 0 10px;
  font-size: 13px;
  line-height: 1.45;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

@media (max-width: 800px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* —— Signing page: mobile, touch, accessibility —— */

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 1000;
  padding: 10px 14px;
  background: #fff;
  color: var(--brand-dark);
  border-radius: 8px;
  font-weight: 600;
}

.skip-link:focus {
  left: 12px;
  top: 12px;
  outline: 3px solid var(--brand);
  outline-offset: 2px;
}

button:focus-visible,
.tab:focus-visible,
input:focus-visible,
select:focus-visible,
.pdf-stage:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
}

.sign-page .container {
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.sign-page .app-header {
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
}

.sign-page .brand-subtitle {
  display: none;
}

@media (min-width: 480px) {
  .sign-page .brand-subtitle {
    display: block;
  }
}

.sign-layout .sign-pdf-card {
  order: 1;
}

.sign-layout .sign-panel-card {
  order: 2;
}

.sign-mobile-nav {
  display: none;
  grid-column: 1 / -1;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 4px;
}

.sign-mobile-nav-btn {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--brand-dark);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}

.sign-mobile-nav-btn.active {
  background: var(--brand-light);
  border-color: var(--brand);
  color: var(--brand-dark);
}

.sign-pdf-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.sign-pdf-toolbar h2 {
  margin: 0;
}

.sign-pdf-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.sign-pdf-actions .secondary,
.btn-linkish {
  min-height: 36px;
  padding: 8px 12px;
  font-size: 12px;
  line-height: 1.2;
  white-space: nowrap;
}

.btn-linkish {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--brand);
  text-decoration: none;
  font-weight: 600;
}

.sign-pdf-hint {
  margin-top: 0;
}

.sign-jump-btn {
  width: 100%;
  margin-top: 12px;
}

.sr-only-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.upload-preview {
  display: block;
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  background: #fff;
  border-radius: 8px;
}

.pdf-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #111;
  display: flex;
  flex-direction: column;
}

.pdf-fullscreen[hidden] {
  display: none !important;
}

.pdf-fullscreen-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: #1a1a1a;
  color: #fff;
}

.pdf-fullscreen-stage {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  background: #2a2a2a;
}

.pdf-fullscreen-stage .pdf-page-wrap {
  margin: 12px auto;
}

body.pdf-fullscreen-open {
  overflow: hidden;
}

.sign-page .tab {
  flex: 1;
  min-height: 44px;
  padding: 10px 8px;
  font-size: 13px;
}

.sign-page button,
.sign-page .upload-action-btn {
  min-height: 44px;
}

.sign-page #submit-sign {
  width: 100%;
}

.sign-toolbar {
  display: flex;
  gap: 8px;
  margin: 12px 0;
  flex-wrap: wrap;
}

.upload-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.upload-action-btn {
  width: 100%;
}

.upload-hint {
  font-size: 12px;
  margin: 0 0 12px;
  line-height: 1.45;
}

.sign-page .canvas-wrap {
  min-height: 160px;
}

.sign-page canvas.draw-canvas {
  height: 160px;
}

.sign-page .consent input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin-top: 2px;
  flex-shrink: 0;
}

.sign-submit-bar {
  margin-top: 16px;
}

.sign-page .pdf-stage {
  max-height: min(70vh, 640px);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: auto;
}

.sign-page .pdf-page-wrap {
  margin: 8px auto;
  max-width: 100%;
}

.sign-page .pdf-page-wrap canvas {
  max-width: 100%;
  height: auto;
}

.sign-page .signature-field-label {
  top: auto;
  bottom: calc(100% + 4px);
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 800px) {
  .sign-mobile-nav {
    display: grid;
  }

  .sign-layout {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .sign-page .pdf-stage {
    max-height: none;
    overflow: visible;
    border: none;
    background: transparent;
    padding: 0;
  }

  .sign-page.sign-mobile-mode .sign-pdf-card[hidden],
  .sign-page.sign-mobile-mode .sign-panel-card[hidden] {
    display: none !important;
  }

  .sign-page .sign-submit-bar {
    position: sticky;
    bottom: 0;
    z-index: 20;
    background: linear-gradient(transparent, #fff 12%);
    padding: 12px 0 max(8px, env(safe-area-inset-bottom));
    margin-top: 8px;
  }

  .sign-page canvas.draw-canvas {
    height: 180px;
  }

  .container {
    padding: 16px;
  }

  .admin-nav {
    flex-wrap: wrap;
  }

  .admin-nav button {
    flex: 1;
    min-width: 120px;
    min-height: 44px;
  }

  #detail-view .btn {
    width: 100%;
    text-align: center;
  }
}

@media (max-width: 380px) {
  .upload-actions {
    grid-template-columns: 1fr;
  }
}
