/* AlvinTax 傳統風格樣式：方形按鈕、清晰框線、簡約配色 */
:root {
  --navy: #24425f;
  --navy-dark: #1a3046;
  --blue: #2c5f8a;
  --blue-dark: #204863;
  --bg: #eceae4;
  --panel: #ffffff;
  --line: #b3b3ad;
  --line-dark: #83837d;
  --text: #22262b;
  --muted: #666b70;
  --income: #1d6b3a;
  --expense: #a4291f;
  --warn-bg: #fbf3d4;
  --warn-line: #d8c46a;
  --err-bg: #fbe3e1;
  --err-line: #d99a94;
  --ok-bg: #e2f0e5;
  --ok-line: #8fbf9e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft JhengHei", "PingFang TC", "Heiti TC", "Segoe UI", Tahoma, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--blue-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- 頂部列 ---------- */
.topbar {
  background: var(--navy);
  border-bottom: 3px solid var(--navy-dark);
}
.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  display: inline-block;
  width: 32px; height: 32px;
  line-height: 32px;
  text-align: center;
  background: #e9e4d4;
  color: var(--navy);
  font-weight: 700;
  border: 1px solid #ffffff;
}
.brand-name { color: #f2f2ef; font-size: 17px; font-weight: 700; letter-spacing: .5px; }
.nav { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.nav-link {
  color: #dfe6ec;
  padding: 6px 12px;
  border: 1px solid transparent;
}
.nav-link:hover {
  background: var(--navy-dark);
  text-decoration: none;
  border: 1px solid #4a6b88;
}
.nav-user { color: #a9bccb; padding: 0 8px; font-size: 14px; }

/* ---------- 版面 ---------- */
.container {
  max-width: 1180px;
  margin: 20px auto;
  padding: 0 18px 40px;
}
.page-title {
  font-size: 21px;
  margin: 0 0 4px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--line-dark);
}
.page-sub { color: var(--muted); margin: 0 0 18px; font-size: 14px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  margin-bottom: 20px;
}
.panel-head {
  background: #f2f1ec;
  border-bottom: 1px solid var(--line);
  padding: 9px 14px;
  font-weight: 700;
  color: var(--navy);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.panel-body { padding: 14px; }
.panel-body.flush { padding: 0; }

.grid { display: grid; gap: 20px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-side { grid-template-columns: 320px 1fr; align-items: start; }
@media (max-width: 900px) {
  .grid-2, .grid-3, .grid-side { grid-template-columns: 1fr; }
}

/* ---------- 表單 ---------- */
.field { margin-bottom: 12px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 700;
  color: #3a4047;
  margin-bottom: 4px;
}
input[type="text"], input[type="password"], input[type="number"], input[type="date"],
input[type="time"], input[type="url"], input[type="file"], select, textarea {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--line-dark);
  border-radius: 0;
  background: #fff;
  font-family: inherit;
  font-size: 14px;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--blue);
  outline-offset: -1px;
}
textarea { resize: vertical; min-height: 60px; }
.form-row { display: flex; gap: 10px; flex-wrap: wrap; }
.form-row > .field { flex: 1 1 140px; }

/* ---------- 按鈕（方形） ---------- */
.btn {
  display: inline-block;
  padding: 8px 16px;
  border: 1px solid var(--blue-dark);
  border-radius: 0;
  background: var(--blue);
  color: #fff;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}
.btn:hover { background: var(--blue-dark); text-decoration: none; }
.btn:active { background: #17364c; }
.btn-ghost {
  background: transparent;
  color: #e7eef4;
  border-color: #5d7c96;
}
.btn-ghost:hover { background: var(--navy-dark); }
.btn-secondary {
  background: #e8e7e1;
  color: #2b3138;
  border-color: var(--line-dark);
}
.btn-secondary:hover { background: #dcdbd4; }
.btn-danger {
  background: #97331f;
  border-color: #7a2917;
}
.btn-danger:hover { background: #7a2917; }
.btn-small { padding: 4px 9px; font-size: 12.5px; }
.btn-block { display: block; width: 100%; }
button:disabled { opacity: .5; cursor: not-allowed; }
.inline { display: inline; }

/* ---------- 表格 ---------- */
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  background: #fff;
}
table.data th, table.data td {
  border: 1px solid var(--line);
  padding: 7px 9px;
  text-align: left;
  vertical-align: middle;
}
table.data thead th {
  background: #e7e6e0;
  color: #2b3138;
  font-weight: 700;
  white-space: nowrap;
}
table.data tbody tr:nth-child(even) { background: #fafaf7; }
table.data tbody tr:hover { background: #eef4f8; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.income { color: var(--income); font-weight: 700; }
.expense { color: var(--expense); font-weight: 700; }
.tag {
  display: inline-block;
  padding: 1px 7px;
  border: 1px solid var(--line-dark);
  font-size: 12px;
  background: #f0efe9;
}
.tag-income { border-color: #8fbf9e; background: #e2f0e5; color: #1d6b3a; }
.tag-expense { border-color: #d99a94; background: #fbe3e1; color: #a4291f; }

/* ---------- 統計卡 ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
@media (max-width: 900px) { .stats { grid-template-columns: 1fr 1fr; } }
.stat {
  background: #fff;
  border: 1px solid var(--line);
  border-top: 3px solid var(--blue);
  padding: 12px 14px;
}
.stat .stat-label { font-size: 13px; color: var(--muted); }
.stat .stat-value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat.income-card { border-top-color: var(--income); }
.stat.expense-card { border-top-color: var(--expense); }

/* ---------- 圖表 ---------- */
.chart-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chart-tab {
  padding: 5px 12px;
  border: 1px solid var(--line-dark);
  background: #e8e7e1;
  color: #2b3138;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
}
.chart-tab.active { background: var(--blue); color: #fff; border-color: var(--blue-dark); }
.chart-wrap { position: relative; height: 320px; }
.chart-wrap.hidden { display: none; }

/* ---------- Flash ---------- */
.flash-area { margin-bottom: 16px; }
.flash {
  padding: 9px 14px;
  border: 1px solid;
  margin-bottom: 8px;
}
.flash-success { background: var(--ok-bg); border-color: var(--ok-line); }
.flash-error { background: var(--err-bg); border-color: var(--err-line); }
.flash-warning { background: var(--warn-bg); border-color: var(--warn-line); }

/* ---------- 篩選列 ---------- */
.filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
}
.filter-bar .field { margin-bottom: 0; min-width: 120px; }

/* ---------- 卡片清單 ---------- */
.card-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.project-card {
  background: #fff;
  border: 1px solid var(--line);
  padding: 14px;
  display: block;
  color: inherit;
}
.project-card:hover { border-color: var(--blue); background: #f7fafc; text-decoration: none; }
.project-card h3 { margin: 0 0 4px; font-size: 16px; color: var(--navy); }
.project-card .meta { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.project-card .amounts { font-size: 13px; }
.project-card .amounts div { display: flex; justify-content: space-between; border-top: 1px dotted #ccc; padding: 3px 0; }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.right { text-align: right; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.hidden { display: none !important; }
.mono { font-family: Consolas, "Courier New", monospace; }
.section-note { background: #f2f1ec; border: 1px solid var(--line); padding: 9px 12px; font-size: 13px; color: #4a4f55; }

/* 收據縮圖 */
.receipt-thumb { max-width: 60px; max-height: 60px; border: 1px solid var(--line-dark); display: block; }

/* 登入頁 */
.auth-wrap { max-width: 420px; margin: 60px auto; }
.auth-brand { text-align: center; margin-bottom: 20px; }
.auth-brand .brand-mark { width: 48px; height: 48px; line-height: 48px; font-size: 24px; background: var(--navy); color: #fff; }

/* 對話框 / 總結框 */
.summary-box {
  border: 1px dashed var(--line-dark);
  background: #fbfbf7;
  padding: 12px;
  white-space: pre-wrap;
  font-size: 14px;
  min-height: 40px;
}

/* 模態（純 CSS 顯示） */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20,25,30,.5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 50;
}
.modal {
  background: #fff; border: 1px solid var(--line-dark);
  max-width: 520px; width: 100%; max-height: 88vh; overflow: auto;
}
.modal-head { background: var(--navy); color: #fff; padding: 10px 14px; font-weight: 700; }
.modal-body { padding: 14px; }
.modal-foot { padding: 12px 14px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }

.footer {
  border-top: 1px solid var(--line);
  background: #e4e2db;
  color: var(--muted);
  text-align: center;
  padding: 12px;
  font-size: 13px;
}
.footer-meta { margin-top: 4px; font-size: 12.5px; }
.footer-meta .sep { margin: 0 6px; color: #b3b3ad; }

/* ---------- 選單切換按鈕（預設隱藏，手機才顯示） ---------- */
.nav-toggle {
  display: none;
  padding: 8px 14px;
  border: 1px solid #5d7c96;
  background: transparent;
  color: #e7eef4;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.nav-toggle:hover { background: var(--navy-dark); }

/* ---------- 更新內容彈窗 ---------- */
.update-entry { margin-bottom: 16px; }
.update-entry:last-child { margin-bottom: 0; }
.update-entry h4 { margin: 0 0 4px; color: var(--navy); font-size: 15px; }
.update-title { margin: 0 0 6px; font-weight: 700; }
.update-entry ul { margin: 0; padding-left: 20px; }
.update-entry ul li { margin-bottom: 3px; }

/* ---------- 手機快速新增按鈕 ---------- */
.fab-mobile {
  display: none;
  position: fixed;
  right: 14px;
  bottom: 72px;   /* 疊在 AI 助理按鈕之上 */
  z-index: 40;
  padding: 12px 18px;
  background: var(--blue);
  color: #fff;
  border: 1px solid var(--blue-dark);
  font-weight: 700;
  box-shadow: 2px 2px 0 rgba(0, 0, 0, .25);
}
.fab-mobile:hover { background: var(--blue-dark); text-decoration: none; }

/* 讓浮動按鈕的錨點跳轉留有適當間距 */
#add-record { scroll-margin-top: 12px; }

/* =====================================================================
   AI 聊天助理
   ===================================================================== */
.ai-chat {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 45;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}
.ai-chat-launcher {
  padding: 11px 16px;
  background: var(--navy);
  color: #fff;
  border: 1px solid var(--navy-dark);
  border-radius: 0;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(0, 0, 0, .25);
}
.ai-chat-launcher:hover { background: var(--navy-dark); }

.ai-chat-panel {
  width: 370px;
  max-width: calc(100vw - 28px);
  height: 500px;
  max-height: calc(100vh - 120px);
  background: var(--panel);
  border: 1px solid var(--line-dark);
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .28);
}
.ai-chat-head {
  background: var(--navy);
  color: #fff;
  padding: 8px 12px;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.ai-chat-close {
  background: transparent;
  border: 1px solid #5d7c96;
  color: #e7eef4;
  font-family: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 3px 8px;
  cursor: pointer;
}
.ai-chat-close:hover { background: var(--navy-dark); }

.ai-chat-mode {
  display: flex;
  background: #e7e6e0;
  border-bottom: 1px solid var(--line);
}
.ai-chat-mode button {
  flex: 1;
  padding: 7px 10px;
  border: none;
  border-right: 1px solid var(--line);
  background: transparent;
  color: #3a4047;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.ai-chat-mode button:last-child { border-right: none; }
.ai-chat-mode button.active {
  background: var(--blue);
  color: #fff;
}

.ai-chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background: #fafaf7;
}
.ai-msg { margin-bottom: 10px; }
.ai-msg-who {
  font-size: 12px;
  font-weight: 700;
  color: #666b70;
  margin-bottom: 2px;
}
.ai-msg-body {
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: #fff;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.5;
}
.ai-msg-user { text-align: right; }
.ai-msg-user .ai-msg-who { text-align: right; color: var(--blue-dark); }
.ai-msg-user .ai-msg-body {
  background: #eef4f8;
  border-color: #a8c4d8;
  display: inline-block;
  text-align: left;
}
.ai-msg-pending .ai-msg-body { color: #888; font-style: italic; }
.ai-msg-error .ai-msg-body { background: var(--err-bg); border-color: var(--err-line); }

.ai-chat-foot {
  border-top: 1px solid var(--line);
  padding: 8px;
  display: flex;
  gap: 6px;
  background: #f2f1ec;
}
.ai-chat-foot textarea {
  flex: 1;
  height: 46px;
  min-height: 46px;
  resize: none;
  font-size: 14px;
}
.ai-chat-foot .btn { padding: 8px 14px; }

.ai-chat-note {
  font-size: 11.5px;
  color: #6b7075;
  padding: 0 10px 8px;
  background: #f2f1ec;
}

/* =====================================================================
   手機 / 平板優化
   ===================================================================== */
@media (max-width: 700px) {
  body { font-size: 15px; }

  .topbar-inner { padding: 8px 12px; gap: 8px; }
  .brand-name { font-size: 15px; }
  .brand-mark { width: 28px; height: 28px; line-height: 28px; }

  /* 可收合導覽 */
  .nav-toggle { display: inline-block; }
  .nav {
    display: none;
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding-top: 8px;
    border-top: 1px solid #3d5b76;
  }
  .nav.open { display: flex; }
  .nav-link {
    padding: 11px 12px;
    border: 1px solid #3d5b76;
    font-size: 15px;
  }
  .nav-user { padding: 8px 12px; }
  .nav form.inline { display: block; }
  .nav form.inline .btn { width: 100%; }

  .container { margin: 14px auto; padding: 0 12px 40px; }
  .page-title { font-size: 18px; }

  /* 統計卡：兩欄 */
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .stat-label { font-size: 12px; }
  .stat .stat-value { font-size: 17px; }

  /* 表單：單欄、加大輸入框（16px 避免 iOS 自動縮放） */
  input[type="text"], input[type="password"], input[type="number"],
  input[type="date"], input[type="time"], input[type="url"], input[type="file"],
  select, textarea {
    font-size: 16px;
    padding: 10px 10px;
  }
  .form-row { flex-direction: column; gap: 0; }
  .form-row > .field { flex: 1 1 auto; width: 100%; }

  /* 按鈕：加大、易按 */
  .btn { padding: 11px 16px; font-size: 15px; }
  .btn-small { padding: 8px 12px; font-size: 13.5px; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar > .field, .filter-bar > .btn, .filter-bar > a.btn { width: 100%; }
  .filter-bar .field { min-width: 0; }

  /* 卡片清單單欄 */
  .card-list { grid-template-columns: 1fr; }
  .project-card .amounts div { padding: 5px 0; }

  /* 圖表 */
  .chart-tabs { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .chart-tab { padding: 8px 12px; white-space: nowrap; }
  .chart-wrap { height: 240px; }

  /* 彈窗：底部按鈕全寬 */
  .modal { max-width: 100%; }
  .modal-body { padding: 12px; }
  .modal-foot { flex-direction: column-reverse; }
  .modal-foot .btn { width: 100%; }

  /* 手機快速新增 */
  .fab-mobile { display: inline-block; }

  /* 交易記錄：表格轉卡片 */
  table.data.responsive thead { display: none; }
  table.data.responsive,
  table.data.responsive tbody,
  table.data.responsive tr,
  table.data.responsive td {
    display: block;
    width: 100%;
  }
  table.data.responsive tr {
    border: 1px solid var(--line);
    border-left: 3px solid var(--blue);
    margin-bottom: 10px;
    background: #fff !important;
  }
  table.data.responsive td {
    border: none;
    border-bottom: 1px dotted #c9c9c2;
    padding: 8px 10px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    text-align: right;
  }
  table.data.responsive td:last-child { border-bottom: none; }
  table.data.responsive td::before {
    content: attr(data-label);
    font-weight: 700;
    color: #555b61;
    text-align: left;
    flex: 0 0 34%;
  }
  table.data.responsive td.num { text-align: right; }
  table.data.responsive td .receipt-thumb { max-width: 90px; max-height: 90px; }
  table.data.responsive td[data-label="操作"] { justify-content: flex-end; }
  table.data.responsive td[data-label="操作"]::before { flex: 0 0 auto; }
  table.data.responsive td.empty-cell { display: block; text-align: center; }
  table.data.responsive td.empty-cell::before { content: none; }

  .auth-wrap { margin: 30px auto; }

  /* AI 聊天：面板幾乎全寬 */
  .ai-chat { right: 12px; bottom: 12px; }
  .ai-chat-panel {
    width: calc(100vw - 24px);
    height: calc(100vh - 130px);
    max-height: 560px;
  }
  .ai-chat-launcher { padding: 10px 14px; font-size: 13.5px; }
  .ai-chat-foot textarea { font-size: 16px; }  /* 避免 iOS 自動縮放 */
  .ai-chat-foot .btn { padding: 10px 16px; }
}

/* 極窄螢幕：統計卡改單欄 */
@media (max-width: 380px) {
  .stats { grid-template-columns: 1fr; }
}
