:root {
  --bg: #eef2f7;
  --card: #fff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #d97706;
  --warn-soft: #fff7ed;
  --ok: #059669;
  --ok-soft: #ecfdf5;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
  --header-h: 56px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* —— 登录 —— */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 10px 40px rgba(15, 23, 42, 0.08);
}

.login-card h1 { margin: 0 0 8px; font-size: 24px; }
.login-sub { margin: 0 0 8px; color: var(--muted); font-size: 14px; }
.login-hint { margin: 0 0 16px; color: var(--muted); font-size: 12px; line-height: 1.5; }

.field { display: block; margin-bottom: 16px; }
.field span { display: block; margin-bottom: 6px; font-size: 13px; color: var(--muted); }
.field input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
}

.error { color: var(--danger); font-size: 13px; margin: 0 0 12px; }

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-ghost {
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--text);
}
.btn-sm { padding: 8px 14px; font-size: 13px; }
.btn-link {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 13px;
  cursor: pointer;
  padding: 0;
  margin-left: auto;
}

/* —— 主界面 —— */
.app-view {
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px 20px 32px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 700; }
.sync-meta { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.sync-stale {
  margin: 6px 0 0;
  color: var(--danger);
  font-size: 12px;
  font-weight: 600;
}

.profit-line {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}
.profit-line b { color: var(--text); font-weight: 700; }
.profit-line .sep { margin: 0 4px; opacity: 0.5; }
.profit-line b:first-of-type { color: var(--ok); }

.clickable-row { cursor: pointer; }
.mobile-item[data-id] { cursor: pointer; }

.detail-panel { width: min(420px, 100vw); }
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
  margin-bottom: 16px;
}
.detail-item .k {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 2px;
}
.detail-item .v { font-size: 14px; word-break: break-word; }
.detail-item.full { grid-column: 1 / -1; }
.detail-section-title {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 600;
}
.detail-bills { display: grid; gap: 10px; }
.detail-bill {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fafafa;
}
.detail-bill-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 6px;
}
.detail-bill-meta, .detail-bill-money {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
.detail-bill-money { color: var(--text); margin-top: 4px; }
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* 搜索区 */
.search-bar {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px 12px;
  background: #f9fafb;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-wrap:focus-within {
  border-color: #93c5fd;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
  background: #fff;
}

.search-icon {
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
}

.search-wrap input {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 4px;
  font-size: 15px;
  outline: none;
  min-width: 0;
}

.btn-clear-search {
  border: none;
  background: #e5e7eb;
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  flex-shrink: 0;
}
.btn-clear-search:hover { background: #d1d5db; color: var(--text); }

.search-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.result-count {
  font-size: 13px;
  color: var(--muted);
  flex: 1;
  min-width: 120px;
}

.select-inline {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  background: #fff;
  color: var(--text);
  max-width: 160px;
}

/* 快捷筛选 */
.quick-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.quick-filter {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
  transition: all 0.15s;
}
.quick-filter:hover { border-color: #cbd5e1; background: #f9fafb; }
.quick-filter.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.qf-count {
  display: inline-block;
  min-width: 1.2em;
  margin-left: 2px;
  font-size: 12px;
  opacity: 0.85;
  font-weight: 700;
}
.quick-filter.active .qf-count { opacity: 1; }

/* 面板 */
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.panel-sub { font-size: 13px; color: var(--muted); margin-left: 4px; }

/* 表格 */
.table-panel { margin-bottom: 0; }
.table-panel-head { margin-bottom: 10px; }

.table-wrap {
  overflow: auto;
  max-height: min(52vh, 520px);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.data-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f8fafc;
}
.data-table th,
.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}
.data-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
}
.data-table tbody tr:hover { background: #f8fafc; }
.data-table tr.soon { background: var(--warn-soft); }
.data-table tr.expired { background: var(--danger-soft); }
.data-table tr.idle-row { background: #f0fdfa; }
.data-table tr.inactive-row { background: #f9fafb; color: var(--muted); }
.data-table tr.soon:hover { background: #ffedd5; }
.data-table tr.expired:hover { background: #fee2e2; }
.data-table tr.idle-row:hover { background: #ccfbf1; }

.countdown {
  display: inline-block;
  margin-left: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--ok-soft);
  color: var(--ok);
  white-space: nowrap;
}
.countdown.warn { background: var(--warn-soft); color: var(--warn); }
.countdown.danger { background: var(--danger-soft); color: var(--danger); }
.countdown.muted { background: #f3f4f6; color: var(--muted); }

.expiry-type {
  display: inline-block;
  font-size: 11px;
  color: var(--muted);
  margin-right: 4px;
}

.data-table .col-price,
.data-table td.col-price {
  font-weight: 600;
  color: var(--primary);
  white-space: nowrap;
}
.data-table .col-account { font-weight: 600; min-width: 100px; }
.data-table .col-expiry { white-space: nowrap; }
.data-table .col-time,
.mobile-time {
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
}

/* 设置面板 */
.settings-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 100;
}
.settings-panel {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(360px, 100vw);
  background: var(--card);
  z-index: 101;
  box-shadow: -8px 0 32px rgba(15, 23, 42, 0.12);
  display: flex;
  flex-direction: column;
}
.settings-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}
.settings-panel-head h2 { margin: 0; font-size: 17px; }
.btn-close-settings {
  border: none;
  background: #f3f4f6;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
}
.settings-body { padding: 16px 18px 24px; overflow: auto; flex: 1; }
.settings-field { display: block; margin-bottom: 16px; }
.settings-field span {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.settings-field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
}
.settings-hint {
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 16px;
  line-height: 1.5;
}
.settings-group {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px 8px;
  margin: 0 0 16px;
}
.settings-group legend {
  font-size: 13px;
  color: var(--muted);
  padding: 0 6px;
}
.settings-check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  margin-bottom: 8px;
  cursor: pointer;
}
.settings-check input { width: 16px; height: 16px; flex-shrink: 0; }
.btn-block { width: 100%; }

.mobile-only { display: none; }
.mobile-list { display: none; }

.status-pill {
  display: inline-block;
  font-size: 12px;
  font-weight: 500;
  padding: 3px 8px;
  border-radius: 999px;
}
.status-renting { background: #dbeafe; color: #1d4ed8; }
.status-idle { background: var(--ok-soft); color: #047857; }
.status-expired { background: #fee2e2; color: #b91c1c; }
.status-inactive { background: #f3f4f6; color: #6b7280; }

.empty-hint {
  text-align: center;
  color: var(--muted);
  padding: 28px 16px;
  font-size: 14px;
}

.mark {
  background: #fef08a;
  padding: 0 2px;
  border-radius: 2px;
}

@media (max-width: 768px) {
  .desktop-only { display: none !important; }
  .mobile-only { display: block; }

  body {
    background: #f3f4f6;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .app-view {
    padding: 10px 12px 20px;
    max-width: none;
  }

  .topbar {
    margin-bottom: 10px;
    align-items: center;
  }
  .topbar h1 { font-size: 18px; }
  .sync-meta { font-size: 11px; }
  .topbar-actions .btn-sm {
    padding: 6px 10px;
    font-size: 12px;
  }

  .search-bar {
    padding: 10px 12px;
    margin-bottom: 8px;
    border-radius: 12px;
  }
  .search-wrap input { font-size: 16px; }
  .result-count.mobile-only {
    margin: 8px 2px 0;
    font-size: 12px;
  }

  .quick-filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin-bottom: 10px;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .quick-filters::-webkit-scrollbar { display: none; }
  .quick-filter {
    flex-shrink: 0;
    padding: 8px 14px;
    font-size: 13px;
  }

  .table-panel {
    padding: 0;
    border: none;
    background: transparent;
    box-shadow: none;
  }

  .mobile-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .mobile-item {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px 14px;
    box-shadow: var(--shadow);
  }
  .mobile-item.soon {
    border-color: #fed7aa;
    background: var(--warn-soft);
  }
  .mobile-item.expired {
    border-color: #fecaca;
    background: var(--danger-soft);
  }
  .mobile-item.idle-row {
    border-color: #99f6e4;
    background: #f0fdfa;
  }
  .mobile-item.inactive-row {
    border-color: #e5e7eb;
    background: #f9fafb;
    opacity: 0.92;
  }

  .mobile-item-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 10px;
  }
  .mobile-head-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
  }
  .mobile-name {
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
    word-break: break-all;
    flex: 1;
    min-width: 0;
  }

  .mobile-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 12px;
  }
  .mobile-grid.cols-1 { grid-template-columns: 1fr; }
  .mobile-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
  .mobile-kv {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
  }
  .mobile-kv .k {
    font-size: 11px;
    color: var(--muted);
  }
  .mobile-kv .v {
    font-size: 14px;
    word-break: break-word;
  }
  .mobile-price {
    color: var(--primary);
    font-weight: 700;
  }

  .search-meta {
    flex-wrap: wrap;
    margin-top: 8px;
    gap: 8px;
  }

  .empty-hint { padding: 32px 12px; }

  .settings-panel { width: 100%; max-width: 100%; }
}

@media (max-width: 900px) {
  .app-view { padding: 12px; }
  .topbar { flex-direction: column; align-items: flex-start; }
  .search-meta { flex-direction: column; align-items: stretch; }
  .select-inline { max-width: none; width: 100%; }
  .table-wrap { max-height: 45vh; }
}

a.btn { text-decoration: none; display: inline-flex; align-items: center; }

.stats-view { max-width: 1440px; }
.group-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.stats-detail { width: min(460px, 100vw); }
.stats-table tr.clickable-row, .stat-card[data-id] { cursor: pointer; }
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.kpi-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.kpi-label { display: block; font-size: 12px; color: var(--muted); }
.kpi-card strong { display: block; margin-top: 6px; font-size: 22px; letter-spacing: -0.02em; }
.kpi-unit { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); }

.cost-formula {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.6;
}
.cost-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.cost-item {
  background: #f8fafc;
  border-radius: 12px;
  padding: 10px 12px;
}
.cost-item span { display: block; font-size: 12px; color: var(--muted); }
.cost-item b { font-size: 16px; }
.cost-item.is-result { background: #ecfdf5; }
.cost-item.is-result b { color: #047857; }

.plan-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}
.plan-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  background: linear-gradient(180deg, #eff6ff, #fff 48%);
}
.plan-name { font-weight: 700; }
.plan-count { font-size: 28px; font-weight: 700; line-height: 1.2; margin: 4px 0 8px; }
.plan-count span { font-size: 13px; font-weight: 500; color: var(--muted); }
.plan-meta { font-size: 12px; color: var(--muted); line-height: 1.6; }

.bar-chart {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  min-height: 210px;
  padding: 8px 4px 0;
}
.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.bar {
  width: 100%;
  max-width: 56px;
  border-radius: 10px 10px 4px 4px;
  background: linear-gradient(#93c5fd, #2563eb);
}
.bar-value { font-size: 12px; font-weight: 700; }
.bar-label { font-size: 13px; }
.bar-sub { font-size: 11px; color: var(--muted); }

.split-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.hbar-row {
  display: grid;
  grid-template-columns: 88px 1fr 72px;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
  font-size: 13px;
}
.hbar-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { height: 10px; background: #f3f4f6; border-radius: 999px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; border-radius: 999px; }
.hbar-num { text-align: right; font-variant-numeric: tabular-nums; }

.stats-table-wrap { max-height: 70vh; }
.stats-table { min-width: 1180px; }
.stats-table .sticky-col {
  position: sticky;
  left: 0;
  background: #fff;
  z-index: 1;
  box-shadow: 4px 0 8px rgba(15, 23, 42, 0.04);
}
.stats-table thead .sticky-col { background: #f8fafc; z-index: 3; }
.cell-sub { font-size: 11px; color: var(--muted); font-weight: 400; }
.expiry-stack { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.expiry-stack .countdown { margin-left: 0; }
.stats-table td.num-pos, .num-pos { color: #047857; font-weight: 700; }
.stats-table td.num-neg, .num-neg { color: #b91c1c; font-weight: 700; }

.stats-cards { display: none; }
.stat-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
}
.stat-card-head, .stat-dates, .stat-money {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.stat-card-name { font-size: 16px; font-weight: 700; }
.stat-card-profit { font-size: 18px; }
.stat-plan { margin: 8px 0; font-size: 13px; color: #1d4ed8; }
.stat-dates { margin-bottom: 8px; }
.stat-dates > div, .stat-money > div { flex: 1; min-width: 0; }
.stat-dates span, .stat-money span { display: block; font-size: 11px; color: var(--muted); }
.stat-money {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: #f8fafc;
  border-radius: 10px;
  padding: 8px;
}

@media (max-width: 900px) {
  .kpi-grid, .cost-grid, .split-2 { grid-template-columns: 1fr 1fr; }
  .stats-view .desktop-only { display: none !important; }
  .stats-view .mobile-only.stats-cards { display: block; }
  .hbar-row { grid-template-columns: 72px 1fr 64px; }
}
@media (max-width: 560px) {
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .cost-grid { grid-template-columns: 1fr 1fr; }
  .plan-grid { grid-template-columns: 1fr 1fr; }
}
