:root {
  --signing-primary: #4f46e5;
  --signing-primary-light: #eef2ff;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: #fff;
  color: #1a1a2e;
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ===== Screens ===== */
.screen {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.screen.active {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/* ===== Loading ===== */
#loading-screen {
  gap: 16px;
}

#loading-screen p {
  font-size: 15px;
  color: #666;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #e0e0e0;
  border-top-color: var(--signing-primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Error ===== */
.error-content {
  text-align: center;
  padding: 24px;
}

.error-icon {
  margin-bottom: 16px;
}

.error-content h2 {
  font-size: 20px;
  margin-bottom: 8px;
  color: #e74c3c;
}

.error-content p {
  font-size: 15px;
  color: #666;
  max-width: 320px;
}

/* ===== Success ===== */
.success-content {
  text-align: center;
  padding: 24px;
}

.success-icon {
  margin-bottom: 16px;
}

.success-content h2 {
  font-size: 20px;
  margin-bottom: 8px;
  color: #27ae60;
}

.success-content p {
  font-size: 15px;
  color: #666;
}

/* ===== Signing Screen ===== */
#signing-screen.active {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: stretch;
  padding: 0;
  gap: 0;
}

.signing-header {
  position: absolute;
  top: 8px;
  right: 12px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
}

.signing-label {
  display: none;
}

/* ===== Canvas Area ===== */
#draw-mode, #type-mode {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 0;
  position: relative;
}

.canvas-wrapper {
  position: absolute;
  inset: 0;
  background: #fff;
  border: none;
  border-radius: 0;
  overflow: hidden;
}

#signature-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.signature-line {
  position: absolute;
  bottom: 20%;
  left: 24px;
  right: 24px;
  border-bottom: 2px dashed #d0d4dc;
  pointer-events: none;
}

.signature-hint {
  position: absolute;
  bottom: calc(20% - 22px);
  left: 0;
  right: 0;
  text-align: center;
  font-size: 12px;
  color: #aab0bd;
  pointer-events: none;
  transition: opacity 0.2s;
}

.signature-hint.hidden {
  opacity: 0;
}

/* ===== Type Mode ===== */
.type-area {
  display: flex;
  flex-direction: column;
  position: absolute;
  inset: 0;
}

.type-preview-wrapper {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #fff;
  border: none;
  border-radius: 0;
  overflow: hidden;
}

#type-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.type-controls {
  position: absolute;
  bottom: 50px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 10;
  pointer-events: auto;
}

.name-input {
  flex: 1;
  height: 36px;
  padding: 0 12px;
  font-size: 16px;
  border: 1.5px solid #dde1e8;
  border-radius: 8px;
  outline: none;
  background: rgba(255, 255, 255, 0.9);
  color: #1a1a2e;
  transition: border-color 0.15s;
  -webkit-user-select: text;
  user-select: text;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.name-input:focus {
  border-color: var(--signing-primary);
  background: #fff;
}

.font-selector {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.font-btn {
  width: 36px;
  height: 36px;
  font-size: 18px;
  border: 1.5px solid #dde1e8;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  color: #1a1a2e;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.font-btn.active {
  border-color: var(--signing-primary);
  background: var(--signing-primary-light);
}

/* ===== Actions (overlaid on canvas) ===== */
.signing-actions {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 10px 12px;
  pointer-events: none;
  z-index: 10;
}

.signing-actions .btn {
  pointer-events: auto;
  opacity: 0.75;
  height: 34px;
  font-size: 13px;
  padding: 0 14px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.signing-actions .btn:active {
  opacity: 1;
}

.actions-left {
  display: flex;
  gap: 6px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}

.btn-text {
  background: rgba(255, 255, 255, 0.7);
  color: var(--signing-primary);
  padding: 0 10px;
  font-weight: 500;
  font-size: 12px;
  height: 30px;
  border-radius: 6px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0.8;
}

.btn-text:active {
  background: color-mix(in srgb, var(--signing-primary-light) 90%, transparent);
  opacity: 1;
}

.btn-neutral {
  background: rgba(232, 234, 239, 0.85);
  color: #3a3f4b;
}

.btn-neutral:active {
  background: rgba(212, 215, 222, 0.95);
}

.btn-primary {
  background: color-mix(in srgb, var(--signing-primary) 85%, transparent);
  color: #fff;
}

.btn-primary:active:not(:disabled) {
  background: color-mix(in srgb, var(--signing-primary) 95%, transparent);
}

.btn-success {
  background: #27ae60;
  color: #fff;
}

.btn-success:active {
  background: #1e8e4e;
}

/* ===== Overlays ===== */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.overlay.active {
  display: flex;
}

.overlay-content {
  background: #fff;
  border-radius: 14px;
  padding: 24px;
  width: 90%;
  max-width: 540px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}

.overlay-content h2 {
  font-size: 18px;
  margin-bottom: 16px;
  color: #1a1a2e;
}

.preview-wrapper {
  background: #f7f8fa;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  min-height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#signature-preview {
  max-width: 100%;
  max-height: 120px;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.overlay-actions .btn {
  min-width: 140px;
}

/* ===== Rotate Overlay ===== */
#rotate-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #f7f8fa;
  align-items: center;
  justify-content: center;
}

.rotate-content {
  text-align: center;
  padding: 24px;
  color: #555;
}

.rotate-icon {
  margin-bottom: 20px;
  color: var(--signing-primary);
}

.rotate-content h2 {
  font-size: 20px;
  color: #1a1a2e;
  margin-bottom: 8px;
}

.rotate-content p {
  font-size: 15px;
  color: #777;
}

@media screen and (orientation: portrait) {
  #rotate-overlay {
    display: flex;
  }
}

/* ===== Hidden Utility ===== */
.hidden {
  display: none !important;
}
