:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --sidebar: #0f172a;
  --sidebar-hover: #1e293b;
  --sidebar-active: #2563eb;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-light: #eff6ff;
  --success: #059669;
  --success-bg: #ecfdf5;
  --warning: #d97706;
  --warning-bg: #fffbeb;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --purple: #7c3aed;
  --purple-bg: #f5f3ff;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15,23,42,.06), 0 1px 2px rgba(15,23,42,.04);
}

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

.num { font-variant-numeric: tabular-nums; font-family: "Segoe UI", Consolas, monospace; }

/* Sidebar */
.sidebar {
  width: 236px;
  background: var(--sidebar);
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 30;
}
.brand {
  padding: 20px 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.brand-title { font-size: 16px; font-weight: 700; color: #fff; letter-spacing: .5px; }
.brand-sub { font-size: 11px; color: #94a3b8; margin-top: 3px; }
.nav { padding: 12px 10px; flex: 1; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  color: #cbd5e1; font-size: 13.5px; margin-bottom: 4px;
  transition: all .15s; border: none; background: transparent; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-badge {
  margin-left: auto; background: #ef4444; color: #fff;
  font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px;
}
.sidebar-footer {
  padding: 14px 16px; border-top: 1px solid rgba(255,255,255,.08);
  font-size: 11.5px; color: #94a3b8;
}
.storage-dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: #10b981; margin-right: 6px;
}

/* Main */
.main {
  margin-left: 236px;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 10px;
  position: sticky; top: 0; z-index: 20;
}
.topbar > div:first-child { min-width: 240px; flex: 1; }
.topbar-title { font-size: 18px; font-weight: 700; white-space: nowrap; }
.topbar-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.content { padding: 20px 24px 40px; max-width: 1600px; width: 100%; }
.view { display: none; }
.view.active { display: block; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 7px; font-size: 13px; font-weight: 500;
  cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: all .15s; white-space: nowrap; flex-shrink: 0;
}
.btn:hover { background: #f8fafc; border-color: #cbd5e1; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: #1d4ed8; }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-success:hover { background: #047857; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* Cards & Grid */
.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);
  position: relative;
  overflow: hidden;
}
.kpi-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--primary);
}
.kpi-card.warn::before { background: var(--warning); }
.kpi-card.danger::before { background: var(--danger); }
.kpi-card.success::before { background: var(--success); }
.kpi-card.purple::before { background: var(--purple); }
.kpi-label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.kpi-value { font-size: 26px; font-weight: 700; margin: 6px 0 4px; color: var(--text); }
.kpi-sub { font-size: 12px; color: var(--text-muted); }

.grid-2 { display: grid; grid-template-columns: 1.55fr 1fr; gap: 16px; margin-bottom: 16px; }
.grid-2-eq { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
@media (max-width: 1180px) { .grid-2, .grid-2-eq { grid-template-columns: 1fr; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.card-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;
}
.card-title { font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.card-body { padding: 16px 18px; }

/* Alert banners */
.alert {
  padding: 12px 16px; border-radius: 8px; margin-bottom: 14px;
  font-size: 13px; display: flex; align-items: flex-start; gap: 10px; justify-content: space-between;
}
.alert-danger { background: var(--danger-bg); border: 1px solid #fecaca; color: #991b1b; }
.alert-warn { background: var(--warning-bg); border: 1px solid #fde68a; color: #92400e; }
.alert-info { background: var(--primary-light); border: 1px solid #bfdbfe; color: #1e40af; }
.alert-success { background: var(--success-bg); border: 1px solid #a7f3d0; color: #065f46; }

/* Badges */
.badge {
  display: inline-flex; align-items: center; padding: 2px 8px;
  border-radius: 99px; font-size: 11.5px; font-weight: 600; white-space: nowrap;
}
.badge-danger { background: var(--danger-bg); color: var(--danger); border: 1px solid #fecaca; }
.badge-warn { background: var(--warning-bg); color: var(--warning); border: 1px solid #fde68a; }
.badge-success { background: var(--success-bg); color: var(--success); border: 1px solid #a7f3d0; }
.badge-info { background: var(--primary-light); color: var(--primary); border: 1px solid #bfdbfe; }
.badge-muted { background: #f1f5f9; color: #475569; border: 1px solid #cbd5e1; }

/* Filters & Forms */
.toolbar {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px;
}
.input, select.input {
  padding: 7px 11px; border: 1px solid #cbd5e1; border-radius: 7px;
  font-size: 13px; background: #fff; color: var(--text); outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.form-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px;
}
.form-group label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: #334155; }
.form-group .input { width: 100%; }

/* Tables */
.table-wrap { overflow-x: auto; max-height: 560px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  background: #f8fafc; color: #475569; font-weight: 600; text-align: left;
  padding: 9px 9px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5; white-space: nowrap;
}
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { background: #eff6ff; color: var(--primary); }
tbody td { padding: 8px 9px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
tbody tr:hover { background: #f8fafc; }
tbody tr.clickable { cursor: pointer; }
.text-right { text-align: right; }
.text-danger { color: var(--danger); font-weight: 600; }
.text-warn { color: var(--warning); font-weight: 600; }
.text-success { color: var(--success); font-weight: 600; }
.text-muted { color: var(--text-muted); }

/* Tabs */
.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tab {
  padding: 9px 16px; cursor: pointer; font-size: 13.5px; font-weight: 600;
  color: var(--text-muted); border-bottom: 2px solid transparent; background: none; border-top: none; border-left: none; border-right: none;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* Chart containers */
.chart-box { position: relative; width: 100%; }
.chart-legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-muted); align-items: center; }
.legend-dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; }
.svg-tooltip {
  position: fixed; pointer-events: none; background: rgba(15,23,42,.94); color: #fff;
  padding: 8px 12px; border-radius: 7px; font-size: 12px; z-index: 100;
  box-shadow: 0 8px 20px rgba(0,0,0,.22); display: none; line-height: 1.6;
}

/* Customer bar rows */
.bar-row { margin-bottom: 11px; cursor: pointer; padding: 4px 6px; border-radius: 6px; }
.bar-row:hover { background: #f8fafc; }
.bar-head { display: flex; justify-content: space-between; font-size: 12.5px; margin-bottom: 4px; }
.bar-track { height: 9px; background: #e2e8f0; border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 99px; transition: width .3s; }

/* Drawer */
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,.45);
  z-index: 50; display: none; justify-content: flex-end;
}
.drawer-backdrop.open { display: flex; }
.drawer {
  width: min(680px, 95vw); background: #fff; height: 100vh; overflow-y: auto;
  box-shadow: -8px 0 30px rgba(15,23,42,.2); display: flex; flex-direction: column;
}
.drawer-head {
  padding: 18px 22px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: flex-start; position: sticky; top: 0; background: #fff; z-index: 5;
}
.drawer-body { padding: 18px 22px 32px; }

/* Dropzone */
.dropzone {
  border: 2px dashed #94a3b8; border-radius: 10px; padding: 32px 20px; text-align: center;
  background: #f8fafc; cursor: pointer; transition: all .15s;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--primary); background: var(--primary-light); }

/* Toast */
.toast {
  position: fixed; bottom: 24px; right: 24px; background: #0f172a; color: #fff;
  padding: 11px 18px; border-radius: 8px; font-size: 13.5px; z-index: 120;
  box-shadow: 0 10px 25px rgba(0,0,0,.25); display: none;
}

/* Month selector pills */
.month-bar {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.month-pill {
  padding: 6px 13px; border-radius: 7px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--border); background: #f8fafc; color: #475569;
  cursor: pointer; transition: all .15s; display: inline-flex; align-items: center; gap: 5px;
}
.month-pill:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }
.month-pill.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.month-pill.empty { opacity: .55; }
.month-dot { width: 6px; height: 6px; border-radius: 50%; background: #10b981; }
.month-pill.active .month-dot { background: #fff; }
.mom-up { color: #059669; font-weight: 600; }
.mom-down { color: #dc2626; font-weight: 600; }

@media print {
  .sidebar, .topbar-actions, .toolbar, .btn, .auth-gate { display: none !important; }
  .main { margin-left: 0 !important; }
}

/* Cloud Sync Status & Account Controls */
.storage-dot.syncing { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245,158,11,.25); }
.storage-dot.error { background: #ef4444; box-shadow: 0 0 0 3px rgba(239,68,68,.25); }
.storage-dot.live { background: #10b981; box-shadow: 0 0 0 3px rgba(16,185,129,.22); }

.sidebar-sync-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.sidebar-user-bar {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.sidebar-user-email {
  color: #cbd5e1;
  font-size: 11.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-mini-btn {
  border: 1px solid rgba(255,255,255,.16);
  background: rgba(255,255,255,.06);
  color: #e2e8f0;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 5px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s;
}
.sidebar-mini-btn:hover { background: rgba(255,255,255,.16); }

/* Auth Gate Overlay */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: radial-gradient(circle at 20% 20%, #1e3a8a 0%, #0f172a 55%, #020617 100%);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-gate.open { display: flex; }
.auth-card {
  width: min(410px, 100%);
  background: #ffffff;
  border-radius: 14px;
  padding: 28px 28px 24px;
  box-shadow: 0 24px 60px rgba(0,0,0,.42);
  color: var(--text);
}
.auth-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 99px;
  background: var(--primary-light);
  color: var(--primary);
  font-size: 11.5px;
  font-weight: 600;
  margin-bottom: 12px;
}
.auth-title { font-size: 21px; font-weight: 700; color: #0f172a; }
.auth-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 4px; margin-bottom: 20px; }
.auth-field { margin-bottom: 14px; }
.auth-field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: #334155;
  margin-bottom: 5px;
}
.auth-field .input {
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
}
.auth-error {
  display: none;
  background: var(--danger-bg);
  border: 1px solid #fecaca;
  color: #991b1b;
  font-size: 12.5px;
  padding: 9px 12px;
  border-radius: 7px;
  margin-bottom: 14px;
}
.auth-submit {
  width: 100%;
  justify-content: center;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
}
.auth-hint {
  margin-top: 14px;
  font-size: 11.5px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.6;
}
body.auth-locked .sidebar,
body.auth-locked .main {
  filter: blur(4px);
  pointer-events: none;
  user-select: none;
}

@media (max-width: 820px) {
  body { flex-direction: column; }
  .sidebar {
    position: static;
    width: 100%;
    height: auto;
  }
  .nav {
    display: flex;
    overflow-x: auto;
    gap: 6px;
    padding: 8px 10px;
  }
  .nav-item { width: auto; white-space: nowrap; margin-bottom: 0; }
  .main { margin-left: 0; }
  .topbar { padding: 12px 14px; }
  .content { padding: 14px 12px 32px; }
}
