/* ══════════════════════════════════════════
   家庭記帳 App - 樣式
   手機優先設計，支援 iOS / Android
   ══════════════════════════════════════════ */

:root {
  --primary:    #4F7FFF;
  --primary-dk: #3A6AEF;
  --bg:         #F2F4F8;
  --card:       #FFFFFF;
  --text:       #1C1C1E;
  --text2:      #6E6E73;
  --border:     #E5E5EA;
  --green:      #34C759;
  --red:        #FF3B30;
  --orange:     #FF9500;
  --radius:     16px;
  --shadow:     0 2px 12px rgba(0,0,0,.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  font-family: -apple-system, 'PingFang TC', 'Microsoft JhengHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  overflow: hidden;
}

/* ─── 畫面切換 ─── */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
}
.screen.active {
  opacity: 1;
  pointer-events: auto;
}

/* ─── 上方標題列 ─── */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 56px 20px 12px;
  background: var(--card);
  box-shadow: 0 1px 0 var(--border);
  flex-shrink: 0;
}

.month-label, .top-bar-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
}

.month-nav {
  font-size: 22px;
  color: var(--primary);
  background: none;
  border: none;
  padding: 4px 12px;
  cursor: pointer;
  line-height: 1;
}
.month-nav:active { opacity: .5; }

.back-btn {
  font-size: 16px;
  color: var(--primary);
  background: none;
  border: none;
  padding: 4px 0;
  cursor: pointer;
  width: 60px;
}

/* ─── 滾動區域 ─── */
.scroll-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 16px 0;
  -webkit-overflow-scrolling: touch;
}

/* ─── 卡片 ─── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 14px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.card-title-row .card-title { margin-bottom: 0; }

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

/* ─── 登入畫面 ─── */
.login-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 32px;
  gap: 16px;
}

.login-logo { font-size: 64px; }
.login-title { font-size: 28px; font-weight: 700; }
.login-subtitle { font-size: 15px; color: var(--text2); margin-bottom: 8px; }

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

.form-group label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
}

.form-group input, .text-input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
}
.form-group input:focus, .text-input:focus {
  border-color: var(--primary);
}

.login-hint {
  font-size: 14px;
  color: var(--text2);
  margin-top: 8px;
}
.login-hint a { color: var(--primary); text-decoration: none; }

/* ─── 主要按鈕 ─── */
.btn-primary {
  width: 100%;
  padding: 16px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.btn-primary:active { background: var(--primary-dk); transform: scale(.98); }
.btn-record { margin: 0 16px 16px; width: calc(100% - 32px); }

.error-msg {
  color: var(--red);
  font-size: 14px;
  text-align: center;
  padding: 8px 16px;
}

/* ─── 類別可點選 ─── */
.cat-row-clickable {
  cursor: pointer;
  border-radius: 10px;
  transition: background .15s;
  padding: 4px 6px;
  margin: -4px -6px;
}
.cat-row-clickable:active { background: var(--bg); }

/* ─── 類別篩選標籤 ─── */
.category-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 10px 16px 0;
  padding: 6px 12px;
  background: rgba(79,127,255,.12);
  color: var(--primary);
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}
.category-chip button {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 15px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
  opacity: .7;
}
.category-chip button:active { opacity: 1; }

/* ─── 刪除按鈕 ─── */
.delete-btn {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 6px;
  opacity: 0.45;
  transition: opacity .15s;
  line-height: 1;
  display: block;
  margin-top: 4px;
}
.delete-btn:active { opacity: 1; }

/* ─── 首頁：總覽卡片 ─── */
.summary-card { text-align: center; }

.summary-total-label {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 6px;
}

.summary-total-amount {
  font-size: 42px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -1px;
  margin-bottom: 20px;
}

.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.summary-person {
  flex: 1;
  text-align: center;
}

.summary-divider {
  width: 1px;
  height: 40px;
  background: var(--border);
}

.person-name {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 4px;
}

.person-amount {
  font-size: 22px;
  font-weight: 700;
}

/* ─── 比較卡片 ─── */
.compare-card {
  display: flex;
  align-items: center;
  gap: 12px;
}
.compare-icon { font-size: 28px; }
.compare-text { font-size: 15px; line-height: 1.4; color: var(--text); }

/* ─── 類別清單 ─── */
.category-list { display: flex; flex-direction: column; gap: 10px; }

.cat-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cat-row-emoji { font-size: 20px; width: 28px; text-align: center; }

.cat-row-info { flex: 1; }
.cat-row-name { font-size: 14px; font-weight: 500; color: var(--text); }

.cat-bar-wrap {
  height: 4px;
  background: var(--bg);
  border-radius: 4px;
  margin-top: 4px;
  overflow: hidden;
}
.cat-bar { height: 100%; border-radius: 4px; background: var(--primary); transition: width .4s; }

.cat-row-amount { font-size: 15px; font-weight: 700; color: var(--text); min-width: 70px; text-align: right; }

/* ─── 花費清單 ─── */
.expense-list { display: flex; flex-direction: column; gap: 0; }

.expense-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.expense-item:last-child { border-bottom: none; }

.expense-emoji {
  font-size: 24px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border-radius: 10px;
  flex-shrink: 0;
}

.expense-info { flex: 1; min-width: 0; }
.expense-cat { font-size: 15px; font-weight: 600; color: var(--text); }
.expense-meta { font-size: 12px; color: var(--text2); margin-top: 2px; }

.expense-right { text-align: right; flex-shrink: 0; }
.expense-amount { font-size: 17px; font-weight: 700; color: var(--text); }
.expense-amount.mine { color: var(--red); }
.expense-amount.partner { color: var(--text2); }
.expense-amount.private { color: var(--text2); font-style: italic; }

.loading-placeholder { color: var(--text2); font-size: 14px; text-align: center; padding: 20px 0; }
.empty-placeholder { color: var(--text2); font-size: 14px; text-align: center; padding: 32px 0; }

/* ─── 底部導航 ─── */
.bottom-nav {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  background: var(--card);
  box-shadow: 0 -1px 0 var(--border);
  flex-shrink: 0;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 11px;
  color: var(--text2);
  padding: 4px 0;
  transition: color .15s;
}
.nav-btn.active { color: var(--primary); }
.nav-icon { font-size: 24px; }

.add-center-btn {
  position: relative;
  top: -16px;
}
.nav-add {
  width: 56px;
  height: 56px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: 300;
  box-shadow: 0 4px 16px rgba(79,127,255,.4);
  line-height: 1;
}

/* ─── 記帳畫面 ─── */
.category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.cat-btn {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border: 2px solid transparent;
  border-radius: 14px;
  font-size: 11px;
  color: var(--text);
  cursor: pointer;
  padding: 8px 4px;
  line-height: 1.4;
  transition: border-color .15s, background .15s;
}
.cat-btn:active { opacity: .7; }
.cat-btn.selected {
  border-color: var(--primary);
  background: rgba(79,127,255,.08);
  color: var(--primary);
}

.amount-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
}
.amount-input-wrap:focus-within { border-color: var(--primary); }

.currency-symbol { font-size: 22px; font-weight: 700; color: var(--text2); }

.amount-input {
  flex: 1;
  font-size: 32px;
  font-weight: 800;
  border: none;
  background: none;
  outline: none;
  color: var(--text);
  width: 100%;
}
.amount-input::placeholder { color: var(--border); }

/* ─── 私帳開關 ─── */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.toggle-title { font-size: 15px; font-weight: 600; color: var(--text); }
.toggle-desc { font-size: 12px; color: var(--text2); margin-top: 3px; }

.toggle-switch { position: relative; display: inline-block; width: 52px; height: 30px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 30px;
  cursor: pointer;
  transition: background .2s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 24px; height: 24px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform .2s;
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
.toggle-switch input:checked + .toggle-slider { background: var(--primary); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(22px); }

/* ─── 統計畫面 ─── */
.person-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}
.ptab {
  flex: 1;
  padding: 8px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  cursor: pointer;
  transition: all .15s;
}
.ptab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.chart-wrap {
  position: relative;
  max-width: 220px;
  margin: 0 auto 20px;
}

.chart-center-text {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.chart-center-amount { font-size: 22px; font-weight: 800; color: var(--text); }
.chart-center-label { font-size: 12px; color: var(--text2); }

.category-breakdown { display: flex; flex-direction: column; gap: 12px; }

.bar-chart-wrap { height: 180px; }

/* ─── 歷史記錄 ─── */
.filter-tabs {
  display: flex;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.ftab {
  flex: 1;
  padding: 12px;
  border: none;
  background: none;
  font-size: 15px;
  color: var(--text2);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  font-weight: 500;
  transition: all .15s;
}
.ftab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 700;
}

.history-area { padding-top: 0; }
.history-area .card { border-radius: 0; margin-bottom: 0; box-shadow: none; border-bottom: 1px solid var(--border); }

/* ─── 日期分組標題 ─── */
.date-header {
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  padding: 12px 16px 6px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 1;
}

/* ─── 設定初始化頁面 ─── */
.setup-container {
  max-width: 480px;
  margin: 0 auto;
  padding: 40px 24px;
}
.setup-title { font-size: 22px; font-weight: 700; margin-bottom: 8px; }
.setup-subtitle { font-size: 14px; color: var(--text2); margin-bottom: 32px; }
.setup-section { margin-bottom: 24px; }
.setup-section h3 { font-size: 16px; font-weight: 600; margin-bottom: 12px; }
.setup-url-input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  font-family: monospace;
}
.setup-url-input:focus { border-color: var(--primary); }
.setup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
