:root {
  --primary: #1e3a8a;
  --primary-dark: #172554;
  --primary-light: #3b82f6;
  --accent-green: #059669;
  --accent-red: #dc2626;
  --accent-gold: #d97706;
  --bg-main: #f8fafc;
  --bg-card: #ffffff;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --border-color: #e2e8f0;
  --border-focus: #93c5fd;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --radius: 10px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans TC", sans-serif;
}

body {
  background-color: var(--bg-main);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 頂部導覽列 */
.navbar {
  background-color: var(--primary-dark);
  color: #fff;
  padding: 0.85rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-md);
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 34px;
  height: 34px;
  background: linear-gradient(135deg, #60a5fa, #2563eb);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.1rem;
  color: white;
}

.brand-title {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.brand-badge {
  font-size: 0.75rem;
  background: rgba(255, 255, 255, 0.15);
  padding: 2px 8px;
  border-radius: 9999px;
  color: #bfdbfe;
  font-weight: 500;
}

.nav-tabs {
  display: flex;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  padding: 4px;
  border-radius: 8px;
}

.nav-tab-btn {
  background: transparent;
  border: none;
  color: #94a3b8;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.nav-tab-btn.active {
  background: #2563eb;
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.nav-tab-btn:hover:not(.active) {
  color: #f1f5f9;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.85rem;
}

.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f59e0b;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: bold;
}

.btn-logout {
  background: rgba(239, 68, 68, 0.2);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.4);
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-logout:hover {
  background: rgba(239, 68, 68, 0.4);
  color: #fee2e2;
}

/* 主要容器 */
.main-container {
  flex: 1;
  max-width: 1560px;
  width: 100%;
  margin: 0 auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* 篩選與控制面板 */
.filter-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

/* 膠囊選擇按鈕 */
.capsule-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.capsule-btn {
  background: #f1f5f9;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.capsule-btn:hover {
  background: #e2e8f0;
  color: var(--text-main);
}

.capsule-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

/* 輸入框樣式 */
.search-input-wrap {
  position: relative;
  flex: 1;
  min-width: 260px;
}

.search-input {
  width: 100%;
  padding: 8px 12px 8px 36px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 0.9rem;
  outline: none;
  background: #fff;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-input:focus {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  font-size: 1rem;
}

.date-input {
  padding: 6px 10px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--text-main);
  outline: none;
}

.date-input:focus {
  border-color: var(--primary-light);
}

.btn-action {
  background: #f8fafc;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}

.btn-action:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
}

.btn-export {
  background: #ecfdf5;
  border-color: #a7f3d0;
  color: #065f46;
}

.btn-export:hover {
  background: #d1fae5;
}

/* 財務與數據指標卡片 */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.kpi-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
}

.kpi-card.records::before { background: var(--primary-light); }
.kpi-card.income::before { background: var(--accent-green); }
.kpi-card.expense::before { background: var(--accent-red); }
.kpi-card.net::before { background: var(--accent-gold); }

.kpi-title {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.kpi-value {
  font-size: 1.45rem;
  font-weight: 800;
  color: var(--text-main);
  letter-spacing: -0.5px;
}

.kpi-card.income .kpi-value { color: var(--accent-green); }
.kpi-card.expense .kpi-value { color: var(--accent-red); }
.kpi-card.net .kpi-value { color: var(--text-main); }

/* 資料表格區域 */
.table-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.table-header-bar {
  padding: 0.85rem 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  background: #fdfdfd;
}

.table-header-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-responsive {
  overflow-x: auto;
  max-height: 65vh;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  text-align: left;
}

.data-table th {
  background: #f8fafc;
  color: var(--text-muted);
  font-weight: 700;
  padding: 10px 12px;
  position: sticky;
  top: 0;
  border-bottom: 1px solid var(--border-color);
  white-space: nowrap;
  z-index: 10;
}

.data-table td {
  padding: 8px 12px;
  border-bottom: 1px solid #f1f5f9;
  color: var(--text-main);
  white-space: nowrap;
}

.data-table tbody tr:hover {
  background-color: #f8fafc;
  cursor: pointer;
}

/* 標籤樣式 */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 600;
}

.badge-branch-復北 { background: #e0f2fe; color: #0369a1; }
.badge-branch-新田 { background: #dcfce7; color: #15803d; }
.badge-branch-初色 { background: #fef3c7; color: #b45309; }
.badge-branch-A8 { background: #f3e8ff; color: #7e22ce; }
.badge-branch-管顧 { background: #f1f5f9; color: #475569; }
.badge-branch-新店 { background: #ffedd5; color: #c2410c; }

.badge-inc { background: #ecfdf5; color: #047857; font-weight: 700; }
.badge-exp { background: #fef2f2; color: #b91c1c; font-weight: 700; }
.badge-other { background: #f1f5f9; color: #475569; }

.amount-income { color: var(--accent-green); font-weight: 700; }
.amount-expense { color: var(--accent-red); font-weight: 700; }

.serial-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  color: #475569;
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
}

/* 分頁列 */
.pagination-bar {
  padding: 0.75rem 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--border-color);
  background: #f8fafc;
  font-size: 0.85rem;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.page-btn {
  background: #fff;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

.page-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* 登入視窗 / 遮罩 */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 1rem;
}

.auth-modal {
  background: #ffffff;
  border-radius: 16px;
  width: 100%;
  max-width: 420px;
  padding: 2.25rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.2);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  animation: modalFadeIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalFadeIn {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.auth-header {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.auth-logo {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #1e3a8a, #3b82f6);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.5rem;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.auth-title {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--primary-dark);
}

.auth-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: #334155;
}

.form-input {
  padding: 10px 14px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 0.95rem;
  outline: none;
  transition: all 0.2s;
}

.form-input:focus {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.btn-login {
  background: linear-gradient(135deg, #1e3a8a, #2563eb);
  color: #fff;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
}

.btn-login:hover {
  opacity: 0.95;
}

.btn-login:active {
  transform: scale(0.98);
}

.auth-error {
  color: #dc2626;
  font-size: 0.85rem;
  text-align: center;
  background: #fef2f2;
  padding: 8px;
  border-radius: 6px;
  border: 1px solid #fee2e2;
  display: none;
}

/* 詳情彈窗 Modal */
.detail-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 800;
  padding: 1rem;
}

.detail-modal {
  background: #fff;
  border-radius: 12px;
  width: 100%;
  max-width: 680px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 0.75rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.detail-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
}

.detail-val {
  font-size: 0.92rem;
  color: var(--text-main);
  font-weight: 500;
  word-break: break-word;
}

.detail-full-width {
  grid-column: span 2;
}

/* 載入中進度條 */
.loading-banner {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e40af;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.88rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.progress-bar-bg {
  flex: 1;
  height: 8px;
  background: #dbeafe;
  border-radius: 4px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: #2563eb;
  width: 0%;
  transition: width 0.2s;
}

/* 響應式 */
@media (max-width: 768px) {
  .navbar {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
  .nav-tabs {
    width: 100%;
    overflow-x: auto;
  }
  .user-section {
    width: 100%;
    justify-content: space-between;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .detail-grid {
    grid-template-columns: 1fr;
  }
  .detail-full-width {
    grid-column: span 1;
  }
}
