:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #e5e9f2;
  --text: #141b2d;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --tw-up: #dc2626;
  --tw-down: #16a34a;
  --income: #059669;
  --expense: #d97706;
  --liability: #e11d48;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.05);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1360px;
  margin: 0 auto;
  padding: 14px 20px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

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

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, #1e3a8a, #2563eb);
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  display: grid;
  place-items: center;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

.sync-status {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.month-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  background: var(--bg);
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.month-picker select {
  border: none;
  background: transparent;
  font-weight: 600;
  color: var(--text);
  font-size: 14px;
  outline: none;
  cursor: pointer;
}

.tabs {
  max-width: 1360px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  gap: 6px;
  overflow-x: auto;
}

.tab {
  border: none;
  background: transparent;
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.badge {
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 11px;
}

.container {
  max-width: 1360px;
  margin: 20px auto 48px;
  padding: 0 20px;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: 0.15s ease;
  border: 1px solid transparent;
}

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

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

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: #e9eef7;
}

.btn-outline {
  background: transparent;
  color: var(--primary);
  border-color: #bfdbfe;
}

.btn-outline:hover {
  background: var(--primary-soft);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.btn-full {
  width: 100%;
  padding: 12px;
  font-size: 15px;
}

.link-btn {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}

.kpi-card.highlight {
  background: linear-gradient(135deg, #1e3a8a, #2563eb);
  color: #fff;
  border: none;
}

.kpi-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.kpi-card.highlight .kpi-label,
.kpi-card.highlight .kpi-sub {
  color: rgba(255, 255, 255, 0.85);
}

.kpi-value {
  font-size: 24px;
  font-weight: 700;
  margin: 6px 0 4px;
  letter-spacing: -0.02em;
}

.kpi-sub {
  font-size: 12px;
  color: var(--muted);
}

.overview-grid {
  display: grid;
  grid-template-columns: 400px 1fr;
  gap: 20px;
  align-items: start;
}

@media (max-width: 980px) {
  .overview-grid {
    grid-template-columns: 1fr;
  }
}

.right-col {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.card-header h2 {
  margin: 0;
  font-size: 16px;
}

.hint {
  font-size: 12px;
  color: var(--muted);
}

.cat-pills,
.filter-pills {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.cat-pills {
  margin-bottom: 14px;
}

.pill {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 13px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

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

.entry-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
}

.field span {
  font-weight: 600;
  color: #334155;
}

.field input,
.field select {
  width: 100%;
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid #cbd5e1;
  font-size: 14px;
  background: #fff;
  color: var(--text);
  outline: none;
}

.field input:focus,
.field select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.radio-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  cursor: pointer;
  background: #fafbfd;
}

.radio-card input {
  margin: 0;
}

.radio-card span {
  display: flex;
  flex-direction: column;
  font-size: 13px;
}

.radio-card small {
  font-size: 11px;
  color: var(--muted);
}

.quick-chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.chip {
  border: 1px solid var(--border);
  background: #f8fafc;
  color: #334155;
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.chip:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: #bfdbfe;
}

.preview-box {
  background: #f8fafc;
  border: 1px dashed #cbd5e1;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  color: #334155;
  line-height: 1.5;
}

.checkbox-warn {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #b45309;
  background: #fffbeb;
  border: 1px solid #fde68a;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

/* Asset allocation bar */
.alloc-bar {
  height: 16px;
  border-radius: 999px;
  overflow: hidden;
  display: flex;
  background: #e2e8f0;
  margin-bottom: 14px;
}

.alloc-seg {
  height: 100%;
  transition: width 0.3s ease;
}

.alloc-legend {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

.alloc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.alloc-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Chart */
.chart-wrap {
  width: 100%;
  overflow-x: auto;
}

/* Tables */
.table-scroll {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th,
.data-table td,
.sheet-table th,
.sheet-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

.data-table th,
.sheet-table th {
  background: #f8fafc;
  color: #475569;
  font-weight: 700;
  position: sticky;
  top: 0;
}

.data-table th:first-child,
.data-table td:first-child,
.data-table th:nth-child(2),
.data-table td:nth-child(2),
.sheet-table th:first-child,
.sheet-table td:first-child,
.sheet-table th:nth-child(2),
.sheet-table td:nth-child(2) {
  text-align: left;
}

.sheet-table .sec-row td {
  background: #eef2ff;
  color: #1e3a8a;
  font-weight: 700;
  text-align: left;
}

.sheet-table .total-row td {
  background: #f8fafc;
  font-weight: 700;
}

.sheet-table .networth-row td {
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 700;
}

.cell-btn {
  border: none;
  background: transparent;
  font: inherit;
  color: inherit;
  cursor: pointer;
  padding: 3px 6px;
  border-radius: 6px;
  width: 100%;
  text-align: right;
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}

.cell-btn:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

.fx-tag {
  font-size: 10px;
  color: #6366f1;
  background: #eef2ff;
  padding: 0 4px;
  border-radius: 4px;
  font-weight: 700;
}

.pos {
  color: var(--tw-up);
  font-weight: 600;
}

.neg {
  color: var(--tw-down);
  font-weight: 600;
}

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.tag-收入 { background: #ecfdf5; color: #047857; }
.tag-負債 { background: #fff1f2; color: #be123c; }
.tag-支出 { background: #fffbeb; color: #b45309; }
.tag-資產市值 { background: #eff6ff; color: #1d4ed8; }

.ledger-filters {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.ledger-filters select,
.ledger-filters input {
  padding: 7px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  font-size: 13px;
}

.recent-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.recent-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
}

.recent-left {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.recent-meta {
  font-size: 11px;
  color: var(--muted);
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 24px 12px;
  font-size: 13px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
}

.modal {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 440px;
  padding: 18px 20px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-header h3 {
  margin: 0;
  font-size: 16px;
}

.icon-btn {
  border: none;
  background: #f1f5f9;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #0f172a;
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.25);
  z-index: 60;
}

.toast.error {
  background: #be123c;
}

.toast.success {
  background: #047857;
}

/* Login Overlay */
.login-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 20px;
}

.login-card {
  background: #fff;
  border-radius: 20px;
  width: 100%;
  max-width: 380px;
  padding: 28px 26px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}

.login-card h2 {
  margin: 0;
  font-size: 20px;
}

.login-error {
  color: #be123c;
  background: #fff1f2;
  border: 1px solid #fecdd3;
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 13px;
}
