/* === CSS Variables === */
:root {
  --c-bg: #f5f6fa;
  --c-surface: #ffffff;
  --c-primary: #4a6cf7;
  --c-primary-light: #e8edff;
  --c-success: #22c55e;
  --c-warning: #f59e0b;
  --c-danger: #ef4444;
  --c-text: #1e293b;
  --c-text-light: #64748b;
  --c-border: #e2e8f0;
  --c-income: #22c55e;
  --c-expense: #ef4444;
  --c-asset: #3b82f6;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
  --radius: 12px;
  --nav-w: 220px;
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  min-height: 100vh;
}

/* === Navigation — Desktop sidebar === */
#nav {
  position: fixed; top: 0; left: 0;
  width: var(--nav-w); height: 100vh;
  background: var(--c-surface);
  border-right: 1px solid var(--c-border);
  display: flex; flex-direction: column;
  padding: 20px 0;
  z-index: 100;
}
.nav-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 0 20px 24px; border-bottom: 1px solid var(--c-border);
  margin-bottom: 12px;
}
.nav-logo { font-size: 28px; }
.nav-title { font-size: 18px; font-weight: 700; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px; color: var(--c-text-light);
  text-decoration: none; transition: all .2s;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--c-primary-light); color: var(--c-primary); }
.nav-item.active {
  color: var(--c-primary); background: var(--c-primary-light);
  border-left-color: var(--c-primary); font-weight: 600;
}
.nav-icon { font-size: 20px; }
.nav-label { font-size: 15px; }

/* === Main content === */
#app {
  margin-left: var(--nav-w);
  padding: 28px 32px;
  min-height: 100vh;
}

/* === FAB (mobile only) === */
#fab {
  display: none;
  position: fixed; bottom: 80px; right: 20px;
  width: 56px; height: 56px;
  background: var(--c-primary); color: #fff;
  border-radius: 50%; font-size: 24px;
  text-decoration: none;
  align-items: center; justify-content: center;
  box-shadow: var(--shadow-md); z-index: 90;
}

/* === Cards === */
.card {
  background: var(--c-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  margin-bottom: 16px;
}
.card-title {
  font-size: 16px; font-weight: 700;
  margin-bottom: 12px; color: var(--c-text);
}

/* === Summary cards row === */
.summary-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px; margin-bottom: 24px;
}
.summary-card {
  background: var(--c-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px;
}
.summary-card .label { font-size: 13px; color: var(--c-text-light); margin-bottom: 4px; }
.summary-card .value { font-size: 28px; font-weight: 800; }
.summary-card .value.income { color: var(--c-income); }
.summary-card .value.expense { color: var(--c-expense); }
.summary-card .sub { font-size: 12px; color: var(--c-text-light); margin-top: 4px; }

/* === Badge === */
.badge {
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  padding: 2px 8px; border-radius: 99px;
  color: #fff;
}
.badge.danger { background: var(--c-danger); }
.badge.warning { background: var(--c-warning); }
.badge.success { background: var(--c-success); }

/* === Confidence dots === */
.conf-dot {
  display: inline-block; width: 10px; height: 10px;
  border-radius: 50%; margin-right: 4px;
}
.conf-dot.high { background: var(--c-success); }
.conf-dot.medium { background: var(--c-warning); }
.conf-dot.low { background: var(--c-danger); }

/* === Buttons === */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 20px; border-radius: 8px;
  font-size: 14px; font-weight: 600;
  border: none; cursor: pointer; transition: all .2s;
  text-decoration: none;
}
.btn-primary { background: var(--c-primary); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-outline {
  background: transparent; color: var(--c-primary);
  border: 1.5px solid var(--c-primary);
}
.btn-outline:hover { background: var(--c-primary-light); }
.btn-danger { background: var(--c-danger); color: #fff; }
.btn-sm { padding: 6px 14px; font-size: 13px; }

/* === Table === */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; padding: 10px 12px; color: var(--c-text-light); font-weight: 600; border-bottom: 2px solid var(--c-border); font-size: 13px; }
td { padding: 10px 12px; border-bottom: 1px solid var(--c-border); }
tr:hover td { background: #f8fafc; }
td.amount { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
td.amount.income { color: var(--c-income); }
td.amount.expense { color: var(--c-expense); }

/* === Form === */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; color: var(--c-text-light); margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 10px 12px;
  border: 1.5px solid var(--c-border); border-radius: 8px;
  font-size: 14px; color: var(--c-text);
  background: var(--c-surface); transition: border-color .2s;
}
.form-group input:focus, .form-group select:focus {
  outline: none; border-color: var(--c-primary);
}

/* === Upload zone === */
.upload-zone {
  border: 2.5px dashed var(--c-border);
  border-radius: var(--radius);
  padding: 48px 24px;
  text-align: center; cursor: pointer;
  transition: all .2s;
  background: #fafbfe;
}
.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--c-primary); background: var(--c-primary-light);
}
.upload-zone .icon { font-size: 48px; margin-bottom: 12px; }
.upload-zone .text { color: var(--c-text-light); font-size: 14px; }

/* === OCR result preview === */
.ocr-preview { display: flex; gap: 20px; margin-top: 20px; }
.ocr-preview .invoice-img {
  width: 200px; max-height: 300px;
  object-fit: contain; border-radius: 8px;
  border: 1px solid var(--c-border);
}
.ocr-preview .fields { flex: 1; }

/* === Report section === */
.report-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.report-table td, .report-table th { padding: 8px 12px; }
.report-table .section-header { font-weight: 700; background: #f1f5f9; }
.report-table .subtotal { font-weight: 700; border-top: 2px solid var(--c-text); }
.report-table .grand-total { font-weight: 800; border-top: 3px double var(--c-text); font-size: 16px; }
.report-table .indent { padding-left: 28px; }
.report-table .amount { text-align: right; font-variant-numeric: tabular-nums; }
.report-table .negative { color: var(--c-danger); }

/* === Tabs === */
.tabs {
  display: flex; gap: 0; border-bottom: 2px solid var(--c-border);
  margin-bottom: 20px;
}
.tab-btn {
  padding: 10px 20px; font-size: 14px; font-weight: 600;
  background: none; border: none; cursor: pointer;
  color: var(--c-text-light); border-bottom: 2px solid transparent;
  margin-bottom: -2px; transition: all .2s;
}
.tab-btn.active { color: var(--c-primary); border-bottom-color: var(--c-primary); }

/* === Filter bar === */
.filter-bar {
  display: flex; gap: 12px; flex-wrap: wrap;
  align-items: center; margin-bottom: 20px;
}
.filter-bar select, .filter-bar input {
  padding: 8px 12px; border: 1.5px solid var(--c-border);
  border-radius: 8px; font-size: 13px;
}

/* === Toast === */
#toast-container {
  position: fixed; top: 20px; right: 20px;
  z-index: 9999; display: flex; flex-direction: column; gap: 8px;
}
.toast {
  padding: 12px 20px; border-radius: 8px;
  font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toast-in .3s ease-out;
  color: #fff;
}
.toast.success { background: var(--c-success); }
.toast.error { background: var(--c-danger); }
.toast.info { background: var(--c-primary); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-12px); } to { opacity: 1; transform: none; } }

/* === Spinner === */
.spinner {
  display: inline-block; width: 20px; height: 20px;
  border: 3px solid var(--c-border); border-top-color: var(--c-primary);
  border-radius: 50%; animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* === Page header === */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 24px;
}
.page-header h1 { font-size: 24px; font-weight: 800; }

/* === Modal === */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal {
  background: var(--c-surface);
  border-radius: var(--radius);
  padding: 28px;
  width: min(90vw, 520px);
  max-height: 80vh; overflow-y: auto;
  box-shadow: var(--shadow-md);
}
.modal h2 { font-size: 18px; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* === Empty state === */
.empty-state {
  text-align: center; padding: 60px 20px;
  color: var(--c-text-light);
}
.empty-state .icon { font-size: 64px; margin-bottom: 16px; }
.empty-state .text { font-size: 16px; margin-bottom: 20px; }

/* === Item card (upload quick-select) === */
.item-card {
  border: 1.5px solid var(--c-border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 10px;
  transition: border-color .2s;
}
.item-card:hover { border-color: var(--c-primary); }

.item-card-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 10px;
}
.item-card-left {
  display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0;
}
.item-card-left .item-name {
  flex: 1; min-width: 0;
  padding: 6px 10px; border: 1.5px solid var(--c-border);
  border-radius: 6px; font-size: 14px; font-weight: 600;
}
.item-card-left .item-name:focus { outline: none; border-color: var(--c-primary); }

.item-card-right {
  display: flex; align-items: center; gap: 4px;
  font-size: 13px; flex-shrink: 0;
}
.item-card-right input {
  padding: 6px 8px; border: 1.5px solid var(--c-border);
  border-radius: 6px; font-size: 13px; text-align: right;
}
.item-card-right input:focus { outline: none; border-color: var(--c-primary); }
.item-amount { font-weight: 700; font-size: 14px; margin-left: 4px; white-space: nowrap; }

.item-card-account { margin-top: 4px; }
.item-current-account {
  font-size: 14px; color: var(--c-text);
  margin-bottom: 8px;
  display: flex; align-items: center; flex-wrap: wrap;
}

.quick-btns {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.qbtn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 12px; border-radius: 20px;
  font-size: 13px; font-weight: 500;
  background: var(--c-bg); color: var(--c-text);
  border: 1.5px solid var(--c-border);
  cursor: pointer; transition: all .15s;
  white-space: nowrap;
}
.qbtn:hover {
  border-color: var(--c-primary); background: var(--c-primary-light);
  color: var(--c-primary);
}
.qbtn.active {
  background: var(--c-primary); color: #fff;
  border-color: var(--c-primary);
}
.qbtn-more {
  background: transparent; border-style: dashed;
  color: var(--c-text-light);
}
.qbtn-more:hover {
  border-color: var(--c-primary); color: var(--c-primary);
  background: var(--c-primary-light);
}

/* === Swipe row (ledger mobile) === */
.swipe-row {
  position: relative; overflow: hidden;
  touch-action: pan-y;
}
.swipe-row-inner {
  transition: transform .25s ease;
}
.swipe-row .swipe-del {
  position: absolute; right: 0; top: 0; bottom: 0; width: 80px;
  background: var(--c-danger); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700;
}
.swipe-row.swiped .swipe-row-inner { transform: translateX(-80px); }

/* === Bottom sheet (mobile modal) === */
@media (max-width: 768px) {
  .modal-overlay { align-items: flex-end; }
  .modal {
    width: 100vw; max-height: 85vh;
    border-radius: 16px 16px 0 0;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
    animation: sheet-up .3s ease-out;
  }
  @keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
}

/* === Sticky filter bar (mobile) === */
@media (max-width: 768px) {
  .filter-bar {
    position: sticky; top: 0; z-index: 50;
    background: var(--c-bg); padding-top: 8px; padding-bottom: 12px;
  }
}

/* === Mobile === */
@media (max-width: 768px) {
  #nav {
    position: fixed; bottom: 0; left: 0; right: 0;
    width: 100%; height: auto;
    flex-direction: row; justify-content: space-around;
    padding: 0; border-right: none;
    border-top: 1px solid var(--c-border);
  }
  .nav-brand { display: none; }
  .nav-item {
    flex-direction: column; gap: 2px;
    padding: 8px 4px; border-left: none;
    font-size: 12px; flex: 1; text-align: center;
    justify-content: center;
    border-top: 2px solid transparent;
  }
  .nav-item.active { border-left-color: transparent; border-top-color: var(--c-primary); }
  .nav-icon { font-size: 18px; }
  .nav-label { font-size: 11px; }
  #app { margin-left: 0; padding: 16px 12px 90px; }
  #fab { display: flex; }
  .summary-row { grid-template-columns: repeat(2, 1fr); }
  .ocr-preview { flex-direction: column; }
  .ocr-preview .invoice-img { width: 100%; max-height: 200px; }
  .page-header h1 { font-size: 20px; }
  .chart-row { grid-template-columns: 1fr !important; }
  .item-card-header { flex-direction: column; align-items: stretch; gap: 8px; }
  .item-card-right { justify-content: flex-end; }
  /* 加大觸控區 */
  .qbtn { padding: 10px 14px; font-size: 14px; min-height: 40px; }
  .btn-sm { padding: 10px 16px; font-size: 14px; min-height: 40px; }
}

/* === AR/AP overdue row === */
.overdue-row td { background: #fef2f2; }
.overdue-row:hover td { background: #fee2e2; }

/* === Dark mode === */
@media (prefers-color-scheme: dark) {
  :root {
    --c-bg: #0f172a;
    --c-surface: #1e293b;
    --c-primary: #6d8cff;
    --c-primary-light: #1e2a4a;
    --c-text: #e2e8f0;
    --c-text-light: #94a3b8;
    --c-border: #334155;
    --c-income: #4ade80;
    --c-expense: #f87171;
    --c-asset: #60a5fa;
    --shadow-sm: 0 1px 3px rgba(0,0,0,.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,.4);
  }
  body { background: var(--c-bg); }
  .upload-zone { background: #1a2332; }
  tr:hover td { background: #263347; }
  .overdue-row td { background: #3b1c1c; }
  .overdue-row:hover td { background: #4a2020; }
  .form-group input, .form-group select, .form-group textarea {
    background: var(--c-surface); color: var(--c-text);
  }
  .item-card-right input { background: var(--c-surface); color: var(--c-text); }
  .item-card-left .item-name { background: var(--c-surface); color: var(--c-text); }
  .filter-bar select, .filter-bar input { background: var(--c-surface); color: var(--c-text); }
}
