:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --purple: #7c3aed;
  --bg: #eef2f7;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --danger: #ef4444;
  --ok: #10b981;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(15, 23, 42, 0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }

/* ===== Login ===== */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #e8eef8 0%, #f5f7fb 50%, #eef2ff 100%);
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 20px;
  padding: 40px 36px 32px;
  box-shadow: 0 20px 60px rgba(37, 99, 235, 0.12);
  text-align: center;
}
.login-logo {
  width: 64px; height: 64px;
  margin: 0 auto 16px;
  border-radius: 16px;
  background: linear-gradient(135deg, #3b82f6, #7c3aed);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 28px; font-weight: 700;
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.35);
}
.login-card h1 { font-size: 26px; font-weight: 700; margin-bottom: 6px; }
.login-sub { color: var(--muted); font-size: 13px; margin-bottom: 28px; }
.field {
  position: relative;
  margin-bottom: 14px;
}
.field .icon {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  color: #9ca3af; font-size: 16px; pointer-events: none;
}
.field input {
  width: 100%;
  padding: 12px 14px 12px 42px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f9fafb;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.15);
  background: #fff;
}
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--primary); color: #fff;
  border-radius: 10px; padding: 12px 20px;
  font-weight: 600; transition: background .15s, transform .1s;
  box-shadow: 0 4px 14px rgba(37,99,235,.3);
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(.98); }
.btn-primary.block { width: 100%; margin-top: 8px; }
.login-hint {
  margin-top: 20px; font-size: 13px; color: var(--primary);
}
.login-note { margin-top: 8px; font-size: 12px; color: #9ca3af; }
.login-error {
  color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px;
}

/* ===== App shell ===== */
.app-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 12px 28px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, #7c3aed, #6366f1);
  color: #fff; font-weight: 700; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}
.brand-title { font-size: 16px; font-weight: 700; }
.brand-sub { font-size: 11px; color: var(--muted); letter-spacing: .4px; text-transform: uppercase; }
.header-actions { display: flex; align-items: center; gap: 14px; }
.user-chip { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: linear-gradient(135deg, #7c3aed, #a78bfa);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px;
}
.link-btn { color: var(--muted); font-size: 13px; }
.link-btn:hover { color: var(--primary); }
.btn-add {
  background: var(--primary); color: #fff;
  border-radius: 999px; padding: 8px 18px; font-weight: 600;
  box-shadow: 0 4px 12px rgba(37,99,235,.25);
}
.btn-add:hover { background: var(--primary-hover); }

.main { max-width: 1280px; margin: 0 auto; padding: 22px 24px 48px; }

/* Stats */
.stats {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 18px;
}
.stat-card {
  background: var(--card); border-radius: var(--radius); padding: 18px 20px;
  box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.stat-card::after {
  content: ''; position: absolute; right: -8px; top: -8px;
  width: 56px; height: 56px; border-radius: 50%; opacity: .12;
}
.stat-card:nth-child(1)::after { background: #3b82f6; }
.stat-card:nth-child(2)::after { background: #10b981; }
.stat-card:nth-child(3)::after { background: #f59e0b; }
.stat-card:nth-child(4)::after { background: #8b5cf6; }
.stat-label { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.stat-value { font-size: 28px; font-weight: 700; letter-spacing: -.5px; }
.stat-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }

/* Filters */
.filter-bar {
  background: #fff; border-radius: var(--radius); padding: 12px 14px;
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  box-shadow: var(--shadow); margin-bottom: 16px;
}
.search-wrap { flex: 1; min-width: 220px; position: relative; }
.search-wrap input {
  width: 100%; padding: 9px 12px 9px 36px;
  border: 1px solid var(--border); border-radius: 8px; background: #f9fafb;
  outline: none;
}
.search-wrap input:focus { border-color: var(--primary); background: #fff; }
.search-wrap .s-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #9ca3af; }
.filter-bar select {
  padding: 9px 28px 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; min-width: 130px;
  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 fill='%239ca3af' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
.btn-ghost {
  padding: 9px 14px; border-radius: 8px; color: var(--muted);
  border: 1px solid transparent;
}
.btn-ghost:hover { background: #f3f4f6; }
.btn-outline {
  padding: 9px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: #fff; color: var(--text);
  display: inline-flex; align-items: center; gap: 6px;
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }

/* Table */
.table-card {
  background: #fff; border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; min-height: 360px;
}
.table-wrap { overflow-x: auto; }
table.data {
  width: 100%; border-collapse: collapse; min-width: 960px;
}
table.data th {
  text-align: left; padding: 12px 14px; font-weight: 500;
  color: var(--muted); font-size: 13px; background: #fafbfc;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data td {
  padding: 14px; border-bottom: 1px solid #f3f4f6; vertical-align: middle;
}
table.data tr:hover td { background: #f8fafc; }
.cell-company { font-weight: 600; }
.cell-contact { color: var(--muted); font-size: 12px; margin-top: 2px; }
.badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 500;
}
.badge-follow { background: #eff6ff; color: #2563eb; }
.badge-done { background: #ecfdf5; color: #059669; }
.badge-fail { background: #fef2f2; color: #dc2626; }
.badge-hold { background: #f3f4f6; color: #6b7280; }
.badge-unpay { background: #fff7ed; color: #ea580c; }
.badge-part { background: #fef9c3; color: #ca8a04; }
.badge-paid { background: #ecfdf5; color: #059669; }
.badge-noinv { background: #f3f4f6; color: #6b7280; }
.badge-inv { background: #ede9fe; color: #7c3aed; }
.actions { display: flex; gap: 8px; }
.act-edit { color: var(--primary); font-size: 13px; }
.act-del { color: var(--danger); font-size: 13px; }
.act-edit:hover, .act-del:hover { text-decoration: underline; }

.empty {
  padding: 64px 20px; text-align: center; color: var(--muted);
}
.empty-icon {
  width: 72px; height: 72px; margin: 0 auto 16px;
  border-radius: 16px; background: #f3f4f6;
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; color: #9ca3af;
}

/* Modal */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15,23,42,.45);
  z-index: 100; display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto;
}
.modal {
  background: #fff; border-radius: 16px; width: 100%; max-width: 820px;
  box-shadow: 0 25px 80px rgba(0,0,0,.2); margin-bottom: 40px;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 24px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 18px; font-weight: 700; }
.modal-close {
  width: 32px; height: 32px; border-radius: 8px; color: var(--muted);
  display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.modal-close:hover { background: #f3f4f6; }
.modal-body { padding: 20px 24px 8px; max-height: calc(100vh - 200px); overflow-y: auto; }
.form-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px;
}
.form-grid .full { grid-column: 1 / -1; }
.form-grid .third { grid-column: span 1; }
.fg label {
  display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px;
}
.fg label .req { color: var(--danger); }
.fg input, .fg select, .fg textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; outline: none;
}
.fg input:focus, .fg select:focus, .fg textarea:focus {
  border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
.fg textarea { min-height: 72px; resize: vertical; }

.section-title {
  display: flex; align-items: center; justify-content: space-between;
  margin: 22px 0 12px; font-weight: 700; font-size: 15px;
}
.btn-add-row {
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; color: var(--text);
}
.btn-add-row:hover { border-color: var(--primary); color: var(--primary); }

.items-table { width: 100%; border-collapse: collapse; }
.items-table th {
  text-align: left; font-size: 12px; color: var(--muted); font-weight: 500;
  padding: 6px 6px 8px;
}
.items-table td { padding: 4px 6px; }
.items-table input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; outline: none;
}
.items-table input:focus { border-color: var(--primary); }
.items-table .subtotal {
  color: var(--muted); font-variant-numeric: tabular-nums; padding-left: 8px;
}
.row-del {
  width: 28px; height: 28px; border-radius: 6px; color: #9ca3af;
  display: inline-flex; align-items: center; justify-content: center;
}
.row-del:hover { background: #fef2f2; color: var(--danger); }

.totals-box {
  margin-top: 16px; background: #f0f7ff; border-radius: 12px;
  padding: 14px 18px; display: grid; gap: 10px;
}
.totals-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.totals-row .label { color: var(--text); }
.totals-row .label.purple { color: var(--purple); font-weight: 600; }
.totals-row .hint { display: block; font-size: 11px; color: var(--muted); font-weight: 400; margin-top: 2px; }
.totals-row .val { font-weight: 700; font-variant-numeric: tabular-nums; }
.totals-row .val.blue { color: var(--primary); font-size: 16px; }
.totals-row input {
  width: 140px; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: 8px; text-align: right; background: #fff;
}

.modal-foot {
  padding: 14px 24px 20px; display: flex; justify-content: flex-end; gap: 10px;
  border-top: 1px solid var(--border);
}
.btn-cancel {
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--border);
  background: #fff;
}
.btn-cancel:hover { background: #f9fafb; }

/* Password modal */
.pwd-modal .modal { max-width: 420px; }

@media (max-width: 960px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .stats { grid-template-columns: 1fr; }
  .app-header { padding: 10px 14px; flex-wrap: wrap; gap: 10px; }
}

/* ===== AI 沟通 ===== */
.btn-ai {
  background: linear-gradient(135deg, #6366f1, #2563eb);
  color: #fff;
  border-radius: 999px;
  padding: 8px 16px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(99,102,241,.28);
}
.btn-ai:hover { filter: brightness(1.06); }

.ai-modal .ai-panel {
  max-width: 560px;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 80px);
}
.ai-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
  font-weight: 400;
}
.ai-body {
  flex: 1;
  max-height: none !important;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding-bottom: 0;
}
.ai-list {
  flex: 1;
  overflow-y: auto;
  max-height: min(48vh, 420px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-right: 4px;
}
.ai-empty {
  text-align: center;
  color: var(--muted);
  padding: 36px 12px;
  font-size: 14px;
}
.ai-msg {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.ai-msg-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.ai-time { font-size: 12px; color: var(--muted); }
.ai-status {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
}
.ai-st-pending { background: #fff7ed; color: #ea580c; }
.ai-st-running { background: #eff6ff; color: #2563eb; }
.ai-st-done { background: #ecfdf5; color: #059669; }
.ai-st-failed { background: #fef2f2; color: #dc2626; }
.ai-content {
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}
.ai-result {
  margin-top: 8px;
  padding: 8px 10px;
  background: #eff6ff;
  border-radius: 8px;
  font-size: 12px;
  color: #1e40af;
  white-space: pre-wrap;
  word-break: break-word;
}
.ai-compose {
  border-top: 1px solid var(--border);
  padding: 14px 24px 18px;
  background: #fff;
  border-radius: 0 0 16px 16px;
}
.ai-compose textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  resize: vertical;
  min-height: 72px;
  font-family: inherit;
}
.ai-compose textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
.ai-compose-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
}
.ai-hint { font-size: 12px; color: var(--muted); }
