/* ================================================================
   合同 PDF 生成工具 — 应用样式
   ================================================================ */

/* ----- CSS 变量（设计令牌） ----- */
:root {
  --primary: #1a56db;
  --primary-hover: #1e40af;
  --primary-light: #dbeafe;
  --accent: #059669;
  --accent-hover: #047857;
  --danger: #dc2626;
  --danger-hover: #b91c1c;

  --bg-body: #f0f2f5;
  --bg-card: #ffffff;
  --bg-sidebar: #ffffff;
  --bg-header: linear-gradient(135deg, #1e3a5f 0%, #1a56db 100%);
  --bg-input: #f9fafb;
  --bg-section: #f8fafc;

  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --text-white: #ffffff;

  --border: #e5e7eb;
  --border-focus: #1a56db;
  --border-light: #f3f4f6;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.03);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

  --transition: 0.2s ease;
  --sidebar-width: 460px;
}

/* ----- 全局重置 ----- */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 14px;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-body);
  color: var(--text-primary);
  min-height: 100vh;
  overflow: hidden;
}

/* ----- 顶部头部 ----- */
.app-header {
  background: var(--bg-header);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 100;
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-logo-icon {
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  backdrop-filter: blur(4px);
}

.app-logo h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-white);
  letter-spacing: 0.02em;
}

.app-logo span {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.65);
  font-weight: 400;
}

/* ----- 合同类型切换标签 ----- */
.tab-group {
  display: flex;
  gap: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-md);
  padding: 4px;
  backdrop-filter: blur(4px);
}

.tab-btn {
  padding: 8px 24px;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.tab-btn:hover {
  color: var(--text-white);
  background: rgba(255, 255, 255, 0.1);
}

.tab-btn.active {
  background: var(--text-white);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

/* ----- 头部操作按钮 ----- */
.header-actions {
  display: flex;
  gap: 8px;
}

.header-btn {
  padding: 7px 14px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.85);
  font-size: 13px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: 6px;
  backdrop-filter: blur(4px);
}

.header-btn:hover {
  background: rgba(255, 255, 255, 0.18);
  color: var(--text-white);
  border-color: rgba(255, 255, 255, 0.4);
}

/* ----- 主体布局 ----- */
.app-main {
  display: flex;
  height: calc(100vh - 60px);
  overflow: hidden;
}

/* ----- 左侧表单面板 ----- */
.form-panel {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.form-panel-header {
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.form-panel-header h2 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

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

.form-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 24px;
  scroll-behavior: smooth;
}

.form-scroll::-webkit-scrollbar {
  width: 5px;
}

.form-scroll::-webkit-scrollbar-track {
  background: transparent;
}

.form-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.form-scroll::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ----- 表单分组 ----- */
.form-section {
  margin-bottom: 24px;
  border: none;
  background: transparent;
}

.form-section-title {
  padding: 8px 0;
  background: transparent;
  border-bottom: 2px solid var(--primary);
  font-size: 14px;
  font-weight: 700;
  color: var(--primary);
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  margin-bottom: 14px;
  transition: opacity var(--transition);
}

.form-section-title:hover {
  opacity: 0.85;
}

.form-section-title .icon {
  font-size: 16px;
  opacity: 0.8;
}

.form-section-title .collapse-arrow {
  margin-left: auto;
  font-size: 12px;
  transition: transform 0.3s;
  color: var(--text-muted);
}

.form-section.collapsed .collapse-arrow {
  transform: rotate(-90deg);
}

.form-section.collapsed .form-section-body {
  display: none;
}

.form-section-body {
  padding: 4px 0 0;
}

/* 子分组（如付款约定底下的定金、首付、贷款等） */
.form-subsection {
  margin-top: 14px;
  margin-bottom: 14px;
  padding: 14px 16px;
  background: var(--bg-card);
  border-left: 3px solid var(--primary); /* 标志性的左侧指示条，明确子层级 */
  border-top: 1px solid var(--border-light);
  border-right: 1px solid var(--border-light);
  border-bottom: 1px solid var(--border-light);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
}

.form-subsection-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px dashed var(--border-light);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ----- 表单行 ----- */
.form-row {
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

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

.form-row label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

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

.form-row-inline {
  display: flex;
  gap: 12px;
}

.form-row-inline .form-row {
  flex: 1;
}

/* ----- 输入控件 ----- */
input[type="text"],
input[type="number"],
input[type="date"],
input[type="tel"],
textarea,
select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 13px;
  font-family: var(--font-sans);
  transition: all var(--transition);
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--border-focus);
  background: var(--bg-card);
  box-shadow: 0 0 0 3px rgba(26, 86, 219, 0.08);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-muted);
}

textarea {
  resize: vertical;
  min-height: 72px;
  line-height: 1.5;
}

select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M6 8L1 3h10z' fill='%239ca3af'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* 金额提示 */
.amount-hint {
  font-size: 12px;
  color: var(--accent);
  margin-top: 2px;
  font-weight: 500;
  min-height: 18px;
}

/* 必填红星 */
.required-star {
  color: #ef4444 !important;
  margin-left: 3px;
  font-weight: bold;
}

/* 单选/多选组 */
.radio-group,
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 4px 0;
}

.radio-group label,
.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: all var(--transition);
  color: var(--text-primary);
  font-weight: 400;
}

.radio-group label:hover,
.checkbox-group label:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.radio-group input:checked + span,
.checkbox-group input:checked + span {
  color: var(--primary);
  font-weight: 500;
}

.radio-group label:has(input:checked),
.checkbox-group label:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-light);
}

/* ----- 按钮通用 ----- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  font-family: var(--font-sans);
}

.btn-primary {
  background: var(--primary);
  color: var(--text-white);
}
.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: var(--shadow-md);
}

.btn-success {
  background: var(--accent);
  color: var(--text-white);
}
.btn-success:hover {
  background: var(--accent-hover);
  box-shadow: var(--shadow-md);
}

.btn-danger {
  background: var(--danger);
  color: var(--text-white);
}
.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-outline {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-outline:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.btn-xs {
  padding: 3px 8px;
  font-size: 11px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 设备清单动态表格 */
.equipment-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
}

.equipment-table th,
.equipment-table td {
  padding: 6px 8px;
  border: 1px solid var(--border);
  font-size: 12px;
}

.equipment-table th {
  background: var(--bg-section);
  font-weight: 500;
  color: var(--text-secondary);
  text-align: left;
}

.equipment-table input {
  border: none;
  background: transparent;
  padding: 4px;
  width: 100%;
  font-size: 12px;
}

.equipment-table input:focus {
  box-shadow: none;
}

/* 设备行删除按钮 */
.btn-remove-equipment,
.equipment-table .btn-remove-row {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 500;
  opacity: 0.7;
  transition: all var(--transition);
  border-radius: var(--radius-sm);
}

.btn-remove-equipment:hover,
.equipment-table .btn-remove-row:hover {
  opacity: 1;
  background: #fef2f2;
}

/* 虚线添加设备按钮 */
.btn-add-equipment {
  width: 100%;
  padding: 9px;
  margin-top: 12px;
  border: 1px dashed var(--primary);
  background: transparent;
  color: var(--primary);
  font-weight: 600;
  font-size: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  display: block;
  text-align: center;
}

.btn-add-equipment:hover {
  background: var(--primary-light);
}

/* 经纪方预设按钮 */
.btn-preset {
  background: var(--primary-light);
  color: var(--primary);
  border: none;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
}

.btn-preset:hover {
  background: var(--primary);
  color: var(--text-white);
}

/* ----- 右侧预览面板 ----- */
.preview-panel {
  flex: 1;
  background: #f1f5f9; /* 优雅的灰蓝色背景，衬托白纸 */
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 20px 40px;
  position: relative;
}

.preview-panel::-webkit-scrollbar {
  width: 8px;
}

.preview-panel::-webkit-scrollbar-track {
  background: transparent;
}

.preview-panel::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.preview-label {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 600;
  letter-spacing: 0.05em;
}

/* A4 纸张容器 */
.preview-paper {
  background: transparent;
  width: auto;
  position: relative;
  margin: 0 auto;
}

/* ----- 表单底栏操作区 ----- */
.form-panel-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  gap: 12px;
  flex-shrink: 0;
  box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.03);
}

.form-panel-footer .btn {
  flex: 1;
  padding: 11px 16px;
  font-size: 13px;
}

.form-panel-footer .btn-primary {
  flex: 1.5;
}

/* 加载遮罩 */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(2px);
}

.loading-overlay.active {
  display: flex;
}

.loading-content {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 32px 48px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 0 auto 16px;
}

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

.loading-text {
  font-size: 14px;
  color: var(--text-secondary);
}

/* 消息提示 */
.toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 24px;
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  z-index: 10000;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.success {
  border-color: #86efac;
  color: #166534;
  background: #f0fdf4;
}

.toast.error {
  border-color: #fca5a5;
  color: #991b1b;
  background: #fef2f2;
}

.toast.info {
  border-color: #93c5fd;
  color: #1e40af;
  background: #eff6ff;
}

/* 历史记录面板 */
.history-panel {
  position: fixed;
  top: 60px;
  right: -400px;
  width: 380px;
  height: calc(100vh - 60px);
  background: var(--bg-card);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.08);
  z-index: 300;
  transition: right 0.3s ease;
  display: flex;
  flex-direction: column;
}

.history-panel.open {
  right: 0;
}

.history-panel-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.history-panel-header h3 {
  font-size: 15px;
  font-weight: 600;
}

.history-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  cursor: pointer;
  transition: all var(--transition);
}

.history-item:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.history-item .hi-info {
  flex: 1;
  min-width: 0;
  margin-right: 8px;
}

.history-item .hi-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item .hi-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.btn-delete-history {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.btn-delete-history:hover {
  background: #fee2e2;
  color: var(--danger);
}

.history-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

/* ----- 打印样式 ----- */
@media print {
  body {
    background: white;
    overflow: visible;
  }

  .app-header,
  .form-panel,
  .action-bar,
  .preview-toolbar,
  .history-panel,
  .loading-overlay,
  .toast {
    display: none !important;
  }

  .app-main {
    display: block;
    height: auto;
    overflow: visible;
  }

  .preview-panel {
    overflow: visible;
    background: white;
    padding: 0;
  }

  .preview-paper {
    box-shadow: none;
    width: 100%;
    min-height: auto;
  }
}
