/* ── 基礎重設 ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-width: 240px;
  --sidebar-bg: #1a2e4a;
  --sidebar-hover: #243d60;
  --sidebar-active: #2563eb;
  --topbar-h: 56px;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --bg: #f1f5f9;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --income-color: #16a34a;
  --expense-color: #dc2626;
  --transfer-color: #2563eb;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang TC',
               'Microsoft JhengHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

a { text-decoration: none; color: inherit; }

/* ── 側邊欄 ──────────────────────────────────────────── */
.sidebar {
  position: fixed; top: 0; left: 0;
  width: var(--sidebar-width); height: 100vh;
  background: var(--sidebar-bg);
  display: flex; flex-direction: column;
  z-index: 200;
  transition: transform 0.25s ease;
}

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.sidebar-logo {
  display: flex; align-items: center; gap: 10px;
  color: #fff; font-size: 17px; font-weight: 700;
}
.sidebar-logo i { font-size: 22px; color: #60a5fa; }
.sidebar-close { display: none; background: none; border: none; color: rgba(255,255,255,0.6); font-size: 18px; cursor: pointer; }

.sidebar-nav {
  flex: 1; overflow-y: auto; padding: 12px 8px;
  scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.1) transparent;
}

.nav-section-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em;
  color: rgba(255,255,255,0.35); padding: 12px 8px 4px;
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 8px; margin-bottom: 2px;
  color: rgba(255,255,255,0.75); cursor: pointer;
  transition: background 0.15s, color 0.15s;
  font-size: 14px;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-item.disabled { opacity: 0.4; cursor: default; }
.nav-item.disabled:hover { background: none; }
.nav-item i { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,0.08);
  display: flex; align-items: center; gap: 10px;
}
.user-avatar { font-size: 28px; color: rgba(255,255,255,0.5); }
.user-name { color: #fff; font-size: 13px; font-weight: 600; }
.user-role { color: rgba(255,255,255,0.45); font-size: 11px; }
.logout-btn {
  margin-left: auto; color: rgba(255,255,255,0.4); font-size: 18px;
  transition: color 0.15s;
}
.logout-btn:hover { color: #f87171; }

/* ── 主內容區 ─────────────────────────────────────────── */
.main-wrapper {
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex; flex-direction: column;
}

.topbar {
  height: var(--topbar-h);
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  position: sticky; top: 0; z-index: 100;
}
.menu-toggle {
  display: none; background: none; border: none;
  font-size: 22px; cursor: pointer; color: var(--text-muted);
}
.topbar-title {
  font-size: 16px; font-weight: 600; flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.topbar-search {
  display: flex; align-items: center; gap: 6px;
  background: #f1f5f9; border: 1px solid transparent;
  padding: 6px 12px; border-radius: 8px;
  width: 320px; transition: all 0.15s; flex-shrink: 0;
}
.topbar-search:focus-within { background: #fff; border-color: var(--primary); }
.topbar-search i { color: var(--text-muted); }
.topbar-search input {
  border: none; background: transparent; outline: none;
  font-size: 13px; flex: 1; min-width: 0; color: var(--text);
}
/* mobile：搜尋框只留 icon、點 focus 才展開（節省 topbar 空間給 title 跟按鈕） */
@media (max-width: 768px) { .topbar-search { width: 140px; } }
@media (max-width: 480px) {
  .topbar-search {
    width: 38px; padding: 6px 10px; cursor: pointer;
  }
  .topbar-search input { display: none; }
  .topbar-search:focus-within {
    width: calc(100vw - 110px);
    position: absolute; right: 12px; top: 10px; z-index: 200;
  }
  .topbar-search:focus-within input { display: block; }
}

.main-content { padding: 24px; flex: 1; }

/* ── Flash 訊息 ──────────────────────────────────────── */
.flash-container { padding: 0 24px; margin-top: 8px; }
.flash {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-radius: 8px; margin-bottom: 8px;
  font-size: 14px; transition: opacity 0.5s;
}
.flash-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.flash-error   { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ── 卡片 ─────────────────────────────────────────────── */
.card {
  background: var(--card-bg); border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap; gap: 8px;
}
.card-title {
  font-size: 15px; font-weight: 600;
  display: flex; align-items: center; gap: 8px;
  white-space: nowrap; min-width: 0;
}
.card-body { padding: 20px; }

/* ── 統計卡片 ─────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px; margin-bottom: 24px;
}
.stat-card {
  background: var(--card-bg); border-radius: 12px;
  border: 1px solid var(--border); padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 700; }
.stat-sub   { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.stat-card.income  .stat-value { color: var(--income-color); }
.stat-card.expense .stat-value { color: var(--expense-color); }
.stat-card.twd     .stat-value { color: var(--primary); }
.stat-card.usd     .stat-value { color: #0891b2; }

/* ── 表格 ─────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  background: #f8fafc; font-size: 12px; font-weight: 600;
  color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em;
  padding: 10px 14px; text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 11px 14px; border-bottom: 1px solid #f1f5f9;
  color: var(--text);
  white-space: nowrap;
}
/* 例外：用 td.wrap 標記允許換行的欄（例如備註、客戶長名） */
td.wrap { white-space: normal; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }

.amount-positive { color: var(--income-color); font-weight: 600; }
.amount-negative { color: var(--expense-color); font-weight: 600; }

/* ── 徽章 ─────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 9999px;
  font-size: 11px; font-weight: 600;
  white-space: nowrap;
}
.badge-income   { background: #dcfce7; color: #166534; }
.badge-expense  { background: #fee2e2; color: #991b1b; }
.badge-transfer { background: #dbeafe; color: #1e40af; }
.badge-draft    { background: #f1f5f9; color: #64748b; }
.badge-confirmed{ background: #d1fae5; color: #065f46; }
.badge-locked   { background: #e0e7ff; color: #3730a3; }
.badge-twd      { background: #fef3c7; color: #92400e; }
.badge-usd      { background: #cffafe; color: #164e63; }

/* ── 按鈕 ─────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 8px; border: none;
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: all 0.15s; line-height: 1;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover  { background: #b91c1c; }
.btn-outline {
  background: transparent; color: var(--text);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: #f8fafc; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 7px; }
.btn-income   { background: #16a34a; color: #fff; }
.btn-income:hover  { background: #15803d; }
.btn-expense  { background: #dc2626; color: #fff; }
.btn-expense:hover { background: #b91c1c; }
.btn-transfer { background: #2563eb; color: #fff; }
.btn-transfer:hover { background: #1d4ed8; }

/* ── 表單 ─────────────────────────────────────────────── */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group.full { grid-column: 1 / -1; }
label { font-size: 13px; font-weight: 500; color: var(--text-muted); }
.required-star { color: var(--danger); margin-left: 2px; }
input, select, textarea {
  padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px; color: var(--text);
  background: #fff; width: 100%;
  transition: border-color 0.15s, box-shadow 0.15s;
  font-family: inherit;
}
input[type="checkbox"], input[type="radio"] {
  width: auto; padding: 0; margin: 0;
  accent-color: var(--primary);
  cursor: pointer;
  vertical-align: middle;
}
.checkbox-inline, .radio-inline {
  display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer;
  font-size: 13px;
  user-select: none;
}
.checkbox-inline input, .radio-inline input { flex: 0 0 auto; }
.form-group > label:has(> input[type="checkbox"]),
.form-group > label:has(> input[type="radio"]) {
  display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
textarea { resize: vertical; min-height: 70px; }
.form-hint { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

/* ── 可搜尋下拉 ──────────────────────────────────────── */
.sel2-wrap { position: relative; width: 100%; }
.sel2-btn {
  display: block; width: 100%;
  padding: 9px 32px 9px 12px;
  border-radius: 8px; border: 1px solid var(--border);
  background: #fff; color: var(--text);
  font-size: 14px; font-family: inherit; text-align: left;
  cursor: pointer; min-height: 40px;
  position: relative;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.sel2-btn::after {
  content: ''; position: absolute; right: 12px; top: 50%;
  width: 8px; height: 8px; margin-top: -6px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg);
}
.sel2-btn:hover { border-color: #cbd5e1; }
.sel2-btn:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }
.sel2-btn.is-placeholder { color: var(--text-muted); }
.sel2-panel {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 2000;
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.08);
  margin-top: 4px; max-height: 300px;
  display: flex; flex-direction: column;
}
.sel2-search {
  border: none !important; border-bottom: 1px solid var(--border) !important;
  border-radius: 8px 8px 0 0 !important;
  padding: 10px 12px; font-size: 13px; width: 100%;
}
.sel2-search:focus { box-shadow: none !important; border-bottom-color: var(--primary) !important; }
.sel2-list { overflow-y: auto; max-height: 250px; }
.sel2-opt {
  padding: 8px 12px; font-size: 13px; cursor: pointer;
  border-bottom: 1px solid #f1f5f9; color: var(--text);
}
.sel2-opt:last-child { border-bottom: none; }
.sel2-opt:hover, .sel2-opt.active { background: #f8fafc; }
.sel2-opt.selected { background: #eff6ff; color: var(--primary); font-weight: 600; }
.sel2-opt.disabled {
  color: var(--text-muted); cursor: not-allowed; background: #f8fafc;
  font-size: 11px; text-align: center; font-style: italic;
}
.sel2-opt.disabled:hover { background: #f8fafc; }

.form-section {
  background: #f8fafc; border-radius: 10px;
  padding: 16px 20px; margin-bottom: 4px;
}
.form-section-title {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--text-muted); margin-bottom: 14px;
}

/* ── 篩選列 ──────────────────────────────────────────── */
.filter-bar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 10px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}
.filter-bar input,
.filter-bar select {
  width: auto; min-width: 120px; padding: 7px 10px; font-size: 13px;
}
.filter-bar .btn { padding: 7px 14px; font-size: 13px; }

/* ── 帳戶分組 ─────────────────────────────────────────── */
.company-section { margin-bottom: 28px; }
.company-header {
  font-size: 13px; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  padding: 0 4px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 8px;
}
.company-header::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}
.accounts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.account-card {
  background: var(--card-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
  cursor: pointer;
}
.account-card:hover {
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(37,99,235,0.1);
}
.account-card-name { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.account-card-bank { font-size: 12px; color: var(--text-muted); margin-bottom: 10px; }
.account-card-balance { font-size: 20px; font-weight: 700; }
.account-card-balance.twd { color: var(--primary); }
.account-card-balance.usd { color: #0891b2; }
.account-type-icon {
  width: 36px; height: 36px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; margin-bottom: 10px;
}
.type-bank { background: #dbeafe; color: #2563eb; }
.type-cash { background: #d1fae5; color: #16a34a; }
.type-safe { background: #fef9c3; color: #854d0e; }

/* ── 明細帳 ──────────────────────────────────────────── */
.ledger-header {
  display: flex; align-items: center; gap: 16px;
  margin-bottom: 20px; flex-wrap: wrap;
}
.ledger-balance-box {
  padding: 10px 20px; background: var(--primary);
  color: #fff; border-radius: 10px; text-align: center;
}
.ledger-balance-label { font-size: 11px; opacity: 0.8; }
.ledger-balance-value { font-size: 24px; font-weight: 700; }

/* ── 登入頁 ──────────────────────────────────────────── */
.login-page {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1a2e4a 0%, #1d4ed8 100%);
}
.login-card {
  background: #fff; border-radius: 16px; padding: 40px;
  width: 100%; max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-logo {
  text-align: center; margin-bottom: 28px;
}
.login-logo i { font-size: 48px; color: var(--primary); }
.login-logo h1 { font-size: 22px; font-weight: 700; margin-top: 8px; }
.login-logo p  { font-size: 13px; color: var(--text-muted); }
.login-btn {
  width: 100%; padding: 12px;
  background: var(--primary); color: #fff; border: none;
  border-radius: 8px; font-size: 15px; font-weight: 600; cursor: pointer;
  transition: background 0.15s; margin-top: 8px;
}
.login-btn:hover { background: var(--primary-dark); }

/* ── 頁面標題列 ──────────────────────────────────────── */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; flex-wrap: wrap; gap: 12px;
}
.page-header h2 { font-size: 20px; font-weight: 700; }

/* ── 空狀態 ──────────────────────────────────────────── */
.empty-state {
  text-align: center; padding: 48px 20px; color: var(--text-muted);
}
.empty-state i { font-size: 48px; display: block; margin-bottom: 12px; opacity: 0.3; }

/* ── 行動按鈕群組 ─────────────────────────────────────── */
.action-group { display: flex; gap: 6px; }

/* ── 響應式 ──────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 20px rgba(0,0,0,0.3);
  }
  .sidebar-close { display: block; }
  .sidebar-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 190;
  }
  .sidebar-overlay.show { display: block; }
  .main-wrapper { margin-left: 0; }
  .menu-toggle { display: block; }
  .main-content { padding: 16px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid { grid-template-columns: 1fr; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar input, .filter-bar select { width: 100%; }
}

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr; }
  .accounts-grid { grid-template-columns: 1fr; }
  .login-card { margin: 16px; padding: 28px 24px; }
  .topbar { padding: 0 12px; gap: 8px; }
  .topbar-title { font-size: 15px; }
  /* 入帳表單 / 暫存表單的方法 radio：避免擠到中文直書 */
  .method-radio { flex-wrap: wrap; gap: 8px; }
  .method-radio label { white-space: nowrap; }
  /* daily report 的方法統計卡：橫排會擠，改直排 */
  .dr .method-card { grid-template-columns: 1fr !important; gap: 8px; }
}

/* ── 全站 method radio label 排版穩定（避免 emoji+中文被 break） ──── */
.method-radio label {
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap; cursor: pointer;
}
.method-radio label > span { white-space: nowrap; }

/* ── 工具 ─────────────────────────────────────────────── */
.text-muted    { color: var(--text-muted); }
.text-small    { font-size: 12px; }
.fw-600        { font-weight: 600; }
.mt-4          { margin-top: 16px; }
.mb-4          { margin-bottom: 16px; }
.d-flex        { display: flex; }
.align-center  { align-items: center; }
.gap-2         { gap: 8px; }
.ms-auto       { margin-left: auto; }
.divider       { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* ── Modal 浮動視窗 ───────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 1050;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
  animation: modalFade 0.15s ease-out;
}
@keyframes modalFade { from { opacity: 0 } to { opacity: 1 } }
.modal {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  width: 100%;
  max-width: 720px;
  max-height: calc(100vh - 80px);
  display: flex;
  flex-direction: column;
  animation: modalSlide 0.2s ease-out;
}
@keyframes modalSlide {
  from { transform: translateY(-20px); opacity: 0 }
  to   { transform: translateY(0);     opacity: 1 }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 17px; font-weight: 700; }
.modal-body {
  padding: 20px 22px;
  overflow-y: auto;
  flex: 1 1 auto;
}
.modal-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  background: #f9fafb;
  border-radius: 0 0 14px 14px;
}
.btn-close {
  background: transparent;
  border: none;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 4px;
}
.btn-close:hover { color: var(--text); }
body.modal-open { overflow: hidden; }
