:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --sidebar-bg: #0f172a;
  --sidebar-hover: #1e293b;
  --sidebar-active: #2563eb;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #059669;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}

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

.hidden { display: none !important; }

/* Login */
.login-shell {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 55%, #0f766e 100%);
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 430px;
  background: var(--surface);
  border-radius: 14px;
  padding: 32px 28px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.28);
}
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; }
.login-brand h1 { margin: 0; font-size: 20px; color: var(--text); }
.login-brand p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.brand-badge {
  width: 46px; height: 46px; border-radius: 12px;
  background: linear-gradient(135deg, #2563eb, #0d9488);
  color: #fff; font-weight: 800; font-size: 18px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.brand-badge.sm { width: 34px; height: 34px; font-size: 14px; border-radius: 8px; }
.captcha-row { display: flex; gap: 10px; align-items: center; }
.captcha-img {
  height: 40px; border-radius: 6px; border: 1px solid var(--border);
  cursor: pointer; flex-shrink: 0;
}
.login-sec-footer {
  margin-top: 20px; padding-top: 14px; border-top: 1px dashed var(--border);
  display: flex; flex-wrap: wrap; gap: 6px; justify-content: center;
}
.sec-pill {
  font-size: 11px; color: #0f766e; background: #f0fdfa;
  border: 1px solid #99f6e4; padding: 2px 8px; border-radius: 99px;
}

/* App Layout */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px; background: var(--sidebar-bg); color: #e2e8f0;
  display: flex; flex-direction: column; flex-shrink: 0;
  position: sticky; top: 0; height: 100vh;
}
.sidebar-header {
  display: flex; align-items: center; gap: 10px;
  padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,0.08);
}
.sidebar-title strong { display: block; font-size: 15px; color: #fff; }
.sidebar-title span { font-size: 11px; color: #94a3b8; }
.nav-list { flex: 1; padding: 12px 10px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  width: 100%; text-align: left; border: none; background: transparent;
  color: #cbd5e1; padding: 10px 12px; border-radius: 8px; cursor: pointer;
  font-size: 14px; transition: background 0.15s, color 0.15s;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-divider { height: 1px; background: rgba(255,255,255,0.09); margin: 8px 4px; }
.sidebar-footer { padding: 14px; border-top: 1px solid rgba(255,255,255,0.08); }
.user-card { margin-bottom: 10px; }
.user-name { font-weight: 600; color: #fff; font-size: 13px; }
.user-role { font-size: 11px; color: #94a3b8; }
.sidebar-actions { display: flex; gap: 8px; }

.main-area { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 14px 24px; display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 10;
}
.topbar h2 { margin: 0; font-size: 18px; }
.subtitle { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.ip-badge {
  font-size: 12px; color: #1e40af; background: #eff6ff;
  border: 1px solid #bfdbfe; padding: 4px 10px; border-radius: 99px;
}
.content-area { padding: 20px 24px 40px; }

/* Cards & Grids */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px; margin-bottom: 18px;
}
.kpi-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
}
.kpi-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.kpi-value { font-size: 22px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 12px; color: var(--muted); margin-top: 6px; }
.kpi-card.blue .kpi-value { color: #1d4ed8; }
.kpi-card.green .kpi-value { color: #059669; }
.kpi-card.amber .kpi-value { color: #d97706; }
.kpi-card.red .kpi-value { color: #dc2626; }
.kpi-card.clickable { cursor: pointer; transition: transform 0.12s, border-color 0.12s; }
.kpi-card.clickable:hover { transform: translateY(-2px); border-color: var(--primary); }
.kpi-card.active-card { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37,99,235,0.18); }
.pill-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.pill-tab {
  border: 1px solid #cbd5e1; background: #fff; color: #334155;
  padding: 5px 12px; border-radius: 99px; font-size: 12px; cursor: pointer; font-weight: 500;
}
.pill-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.modal-dialog.wide { max-width: 960px; }
.row-overdue td { background: #fff7ed; }
.chk-cell { width: 32px; text-align: center; }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 10px; margin-bottom: 14px;
}
.panel-head h3 { margin: 0; font-size: 15px; }
.two-col { display: grid; grid-template-columns: 1.35fr 1fr; gap: 16px; }
@media (max-width: 1024px) {
  .two-col { grid-template-columns: 1fr; }
  .sidebar { width: 200px; }
}

/* Forms & Controls */
.form-group { margin-bottom: 13px; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: #334155; margin-bottom: 5px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; padding: 8px 11px; border: 1px solid #cbd5e1; border-radius: 7px;
  font-size: 13px; font-family: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.12); }
textarea { min-height: 90px; resize: vertical; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  border: 1px solid transparent; border-radius: 7px; padding: 8px 14px;
  font-size: 13px; font-weight: 500; cursor: pointer; background: #e2e8f0; color: #1e293b;
  transition: all 0.15s;
}
.btn:hover { filter: brightness(0.96); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-warn { background: #fef3c7; color: #92400e; border-color: #fcd34d; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: #fff; border-color: #cbd5e1; color: #334155; }
.btn-danger-outline { background: #fff; border-color: #fca5a5; color: #dc2626; }
.btn-danger-outline:hover { background: #fef2f2; border-color: #ef4444; }
.btn-ghost { background: rgba(255,255,255,0.1); color: #e2e8f0; }
.btn-danger-ghost { background: rgba(220,38,38,0.2); color: #fecaca; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; border-radius: 5px; white-space: nowrap; }
.btn-block { width: 100%; padding: 10px; font-size: 14px; }

/* Tables */
.table-wrap { overflow-x: auto; position: relative; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { background: #f8fafc; color: #475569; font-weight: 600; white-space: nowrap; }
tr:hover td { background: #f8fafc; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }

/* Compact Accounts Receivable table with sticky right action column so buttons never clip */
.table-ar th, .table-ar td { padding: 8px 8px; font-size: 12.5px; }
.table-ar .col-cust { min-width: 190px; max-width: 260px; white-space: normal; word-break: break-word; line-height: 1.45; }
.col-actions {
  position: sticky;
  right: 0;
  background: #ffffff;
  box-shadow: -4px 0 10px rgba(15, 23, 42, 0.06);
  z-index: 2;
  white-space: normal;
  min-width: 136px;
}
th.col-actions { background: #f8fafc; z-index: 3; }
tr.row-overdue td.col-actions { background: #fff7ed; }
tr:hover td.col-actions { background: #f8fafc; }
.action-group { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; min-width: 124px; max-width: 156px; }

/* Badges */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600;
}
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-green { background: #d1fae5; color: #065f46; }
.badge-amber { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-gray { background: #f1f5f9; color: #475569; }
.badge-purple { background: #ede9fe; color: #5b21b6; }

.progress-bar {
  width: 90px; height: 7px; background: #e2e8f0; border-radius: 99px; overflow: hidden; display: inline-block; vertical-align: middle; margin-right: 6px;
}
.progress-fill { height: 100%; background: var(--primary); }
.progress-fill.green { background: var(--success); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px;
}
.modal-dialog {
  background: var(--surface); border-radius: 12px; width: 100%; max-width: 760px;
  max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}
.modal-header {
  padding: 14px 20px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-close { border: none; background: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px 20px; overflow-y: auto; }

/* Alerts & Toast */
.alert-error {
  background: #fef2f2; color: #991b1b; border: 1px solid #fecaca;
  padding: 8px 12px; border-radius: 7px; font-size: 13px; margin-bottom: 12px;
}
.toast-box { position: fixed; top: 18px; right: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 10px 25px rgba(0,0,0,0.2); max-width: 360px;
}
.toast.ok { background: #065f46; }
.toast.err { background: #991b1b; }

/* Contract preview paper */
.contract-preview {
  background: #fff; border: 1px solid #cbd5e1; border-radius: 8px;
  padding: 26px 30px; white-space: pre-wrap; font-family: "Songti SC", "SimSun", serif;
  font-size: 14px; line-height: 1.9; min-height: 360px; max-height: 540px; overflow-y: auto;
}
.chart-box { width: 100%; overflow-x: auto; }
.empty-state { text-align: center; padding: 32px; color: var(--muted); }

/* Print Sheet */
.print-sheet { display: none; }
@media print {
  .no-print, .app-shell, .login-shell, .toast-box, .modal-backdrop { display: none !important; }
  .print-sheet {
    display: block !important;
    font-family: "SimSun", "Songti SC", serif;
    color: #000; padding: 12mm 15mm; font-size: 12pt; line-height: 1.8;
  }
  .print-sheet h1 { text-align: center; font-size: 18pt; margin-bottom: 16px; }
  .print-sheet pre { white-space: pre-wrap; font-family: inherit; margin: 0; }
  .print-sheet table { border-collapse: collapse; width: 100%; margin: 12px 0; }
  .print-sheet th, .print-sheet td { border: 1px solid #333; padding: 6px 8px; font-size: 10.5pt; }
}
