/* ===== 基础 ===== */
* { box-sizing: border-box; }

:root {
  --primary: #2f5fff;
  --primary-hover: #1f4ce0;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bg: #f5f7fb;
  --card-bg: #ffffff;
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.04);
  --shadow: 0 4px 12px rgba(17, 24, 39, 0.06);
}

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

input, textarea, select, button { font-family: inherit; font-size: inherit; color: inherit; }

/* ===== 顶栏 ===== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { font-size: 22px; }
.brand-title { font-size: 17px; font-weight: 600; }

.header-actions { display: flex; gap: 10px; }

/* ===== 按钮 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease;
  font-weight: 500;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}
.btn-ghost:hover { background: #f3f4f6; }
.btn-xs { padding: 4px 10px; font-size: 12px; }
.btn-dashed {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px dashed #cbd5e1;
  color: var(--muted);
  border-radius: 8px;
}
.btn-dashed:hover {
  border-color: var(--primary);
  color: var(--primary);
}

/* ===== 主体布局 ===== */
.layout {
  display: grid;
  grid-template-columns: minmax(420px, 1fr) auto;
  gap: 24px;
  padding: 24px;
  max-width: 1600px;
  margin: 0 auto;
  align-items: start;
}

.panel { min-width: 0; }

.form-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.preview-panel {
  position: sticky;
  top: 80px;
  display: flex;
  justify-content: center;
  align-self: start;
}

/* ===== 卡片 ===== */
.card {
  background: var(--card-bg);
  border-radius: 10px;
  padding: 18px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.card-title {
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.field:last-child { margin-bottom: 0; }

.field .label {
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.field input[type="text"],
.field input[type="email"],
.field input[type="number"],
.field input[type="date"],
.field select,
.field textarea,
textarea#f-notes {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus,
.field select:focus,
.field textarea:focus,
textarea#f-notes:focus {
  border-color: var(--primary);
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

/* ===== Logo 上传 ===== */
.logo-uploader {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.logo-box {
  width: 110px;
  height: 80px;
  border: 1px dashed #cbd5e1;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  background: #fafbff;
  color: var(--muted);
  font-size: 12px;
}
.logo-box:hover { border-color: var(--primary); color: var(--primary); }
.logo-box img { width: 100%; height: 100%; object-fit: contain; }

/* ===== 明细清单 ===== */
.items-head,
.item-row {
  display: grid;
  grid-template-columns: 2fr 0.7fr 1.4fr 1fr 32px;
  gap: 8px;
  align-items: center;
}
.items-head {
  padding: 6px 0;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.items-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.item-row input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  outline: none;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
.item-row input:focus { border-color: var(--primary); }
.item-row .col-amount {
  padding: 6px 8px;
  color: var(--text);
  text-align: right;
}
.btn-del {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--muted);
}
.btn-del:hover { background: #fee2e2; color: #dc2626; }

/* ===== 合计条 ===== */
.total-bar {
  margin-top: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: var(--primary);
  color: #fff;
  border-radius: 8px;
  font-weight: 600;
  font-size: 16px;
}

/* ===== 右侧 A4 预览 ===== */
.a4-page {
  width: 210mm;
  min-height: 297mm;
  background: #fff;
  padding: 18mm;
  box-shadow: 0 10px 40px rgba(17, 24, 39, 0.1);
  border-radius: 4px;
  color: #1f2937;
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

.pv-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
}
.pv-from { display: flex; gap: 14px; align-items: flex-start; }
.pv-logo {
  width: 80px;
  height: 80px;
  object-fit: contain;
  border-radius: 6px;
}
.pv-from-name {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}
.pv-muted {
  color: var(--muted);
  font-size: 11px;
  white-space: pre-line;
}
.pv-title-box { text-align: right; }
.pv-title { font-size: 28px; font-weight: 700; color: var(--primary); letter-spacing: 2px; }
.pv-number { color: var(--muted); font-size: 12px; margin-top: 4px; }

.pv-divider {
  height: 2px;
  background: #111827;
  margin: 14px 0 18px;
}

.pv-meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  gap: 20px;
}
.pv-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  display: block;
  margin-bottom: 2px;
}
.pv-to-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.pv-dates { text-align: right; }
.pv-date-row { margin-bottom: 6px; }
.pv-date-row .pv-label { display: inline-block; margin-right: 8px; margin-bottom: 0; }

.pv-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}
.pv-table thead th {
  background: #f3f4f6;
  padding: 8px 10px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: #374151;
  border-bottom: 1px solid var(--border);
}
.pv-col-qty, .pv-col-rate, .pv-col-amount { text-align: right !important; }
.pv-table tbody td {
  padding: 10px;
  border-bottom: 1px solid #f3f4f6;
  font-size: 12px;
  vertical-align: top;
}
.pv-table tbody td.pv-td-qty,
.pv-table tbody td.pv-td-rate,
.pv-table tbody td.pv-td-amount { text-align: right; }

.pv-totals {
  margin-left: auto;
  width: 260px;
  margin-bottom: 20px;
}
.pv-total-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 12px;
}
.pv-total-final {
  border-top: 2px solid #111827;
  margin-top: 6px;
  padding-top: 10px;
  font-size: 14px;
  font-weight: 700;
}

/* 图标文字行（地址/电话/邮箱） */
.pv-line {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}
.pv-icon {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  margin-top: 2px;
  color: #6b7280;
}
.pv-line span {
  flex: 1;
  white-space: pre-line;
  word-break: break-word;
}

/* 左下角：备注 + 银行信息 */
.pv-bottom-zone {
  margin-top: auto;
  padding-top: 16px;
}
.pv-bank {
  padding-top: 14px;
  border-top: 1px dashed var(--border);
  margin-bottom: 14px;
}
.pv-bank-title {
  font-size: 11px;
  font-weight: 600;
  color: #111827;
  margin-bottom: 8px;
  letter-spacing: 0.5px;
}
.pv-bank-grid {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pv-bank-line {
  font-size: 11px;
  color: #374151;
  display: flex;
  gap: 8px;
}
.pv-bank-label {
  color: var(--muted);
  min-width: 120px;
  flex-shrink: 0;
}

.pv-notes {
  padding-top: 14px;
  border-top: 1px dashed var(--border);
}
.pv-notes-title { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
#pv-notes { font-size: 11px; color: #374151; white-space: pre-line; }

/* ===== 语言选择 ===== */
.lang-select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s;
}
.lang-select:hover { border-color: var(--primary); }

/* ===== 下拉菜单 ===== */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 240px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(17, 24, 39, 0.12);
  padding: 8px;
  z-index: 20;
}
.dropdown-item {
  display: block;
  width: 100%;
  padding: 8px 10px;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
}
.dropdown-item:hover { background: #f3f4f6; color: var(--primary); }
.dropdown-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}
.dropdown-label {
  padding: 4px 10px;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.dropdown-empty {
  padding: 10px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.tpl-row {
  display: flex;
  align-items: center;
  gap: 4px;
}
.tpl-apply {
  flex: 1;
  padding: 8px 10px;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
}
.tpl-apply:hover { background: #eef2ff; color: var(--primary); }
.tpl-del {
  width: 28px; height: 28px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
}
.tpl-del:hover { background: #fee2e2; color: #dc2626; }

/* ===== 模态框 ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-box {
  width: 100%;
  max-width: 480px;
  background: #fff;
  border-radius: 12px;
  padding: 22px;
  box-shadow: 0 20px 60px rgba(17, 24, 39, 0.25);
}
.modal-box h3 {
  margin: 0 0 8px;
  font-size: 17px;
}
.modal-tip {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

/* ===== 发票编号行内按钮 ===== */
.input-with-btn {
  display: flex;
  gap: 6px;
  align-items: stretch;
}
.input-with-btn input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
}
.input-with-btn input:focus { border-color: var(--primary); }
.input-with-btn .btn { white-space: nowrap; }

/* ===== 登录遮罩 ===== */
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: linear-gradient(135deg, #f0f4ff 0%, #e8efff 50%, #fbf2ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-box {
  width: 100%;
  max-width: 440px;
  background: #fff;
  border-radius: 14px;
  padding: 32px 32px 28px;
  box-shadow: 0 20px 60px rgba(17, 24, 39, 0.15);
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.login-brand h2 { margin: 0; font-size: 20px; }
.login-tip {
  margin: 0 0 22px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}
.login-tabs {
  display: flex;
  gap: 0;
  margin: 0 -2px 14px;
  border-bottom: 1px solid var(--border);
}
.login-tab {
  flex: 1;
  padding: 10px 12px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  font-weight: 500;
  margin-bottom: -1px;
}
.login-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}
.login-tab:hover { color: var(--text); }
.login-tab.active:hover { color: var(--primary); }

.login-btn {
  width: 100%;
  margin-top: 8px;
  padding: 11px 16px;
}
.login-error {
  margin-top: 12px;
  padding: 10px 12px;
  background: #fef2f2;
  color: #dc2626;
  border-radius: 6px;
  font-size: 12px;
  border: 1px solid #fecaca;
}
.login-foot {
  margin: 16px 0 0;
  font-size: 11px;
  color: #9ca3af;
  text-align: center;
}

/* ===== 顶栏右侧用户 chip ===== */
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #f3f4f6;
  border-radius: 20px;
  font-size: 12px;
  color: #374151;
  margin-left: 4px;
}
.user-chip #user-email {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-link {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--primary);
  padding: 0;
  font-size: 12px;
  text-decoration: underline;
}
.btn-link:hover { color: var(--primary-hover); }

/* ===== 响应式 ===== */
@media (max-width: 1200px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .preview-panel {
    position: static;
    overflow-x: auto;
  }
  .a4-page { margin: 0 auto; }
}

@media (max-width: 640px) {
  .app-header { padding: 12px 14px; }
  .layout { padding: 14px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .items-head, .item-row { grid-template-columns: 1.6fr 0.6fr 1.4fr 1fr 28px; font-size: 12px; }
}

/* ===== 打印 ===== */
@media print {
  .app-header, .form-panel { display: none !important; }
  body { background: #fff; }
  .layout { display: block; padding: 0; }
  .preview-panel { position: static; }
  .a4-page {
    box-shadow: none;
    border-radius: 0;
    width: 100%;
    min-height: auto;
  }
}
