/* ============================================================
   SplitGo PWA — iOS 風格移動端樣式
   ============================================================ */

:root {
  --blue: #007AFF;
  --green: #34C759;
  --red: #FF3B30;
  --orange: #FF9500;
  --gray: #8E8E93;
  --gray-light: #C7C7CC;
  --gray-bg: #F2F2F7;
  --card: #FFFFFF;
  --text: #1C1C1E;
  --text-secondary: #6C6C70;
  --radius: 12px;
  --radius-lg: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --tab-height: 50px;
}

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

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: -apple-system, 'PingFang TC', 'PingFang SC', 'Microsoft JhengHei', Helvetica, sans-serif;
  background: var(--gray-bg);
  color: var(--text);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  overscroll-behavior: none;
  touch-action: pan-y;
}
#app { height: 100%; height: 100dvh; }
#app.is-swiping { box-shadow: -12px 0 32px rgba(0,0,0,0.18); will-change: transform; }

input, textarea, select {
  user-select: text;
  -webkit-user-select: text;
  font-size: 16px;
  touch-action: manipulation;
}

/* ====== PWA 安全區域 ====== */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-top: var(--safe-top);
  padding-bottom: calc(var(--tab-height) + var(--safe-bottom));
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* ====== Nav Bar ====== */
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 0.5px solid var(--gray-light);
  min-height: 44px;
  flex-shrink: 0;
}
.nav-bar .title {
  font-size: 17px;
  font-weight: 600;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  max-width: 52%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-bar .left-btn,
.nav-bar .right-btn {
  font-size: 15px;
  color: var(--blue);
  background: none;
  border: none;
  padding: 8px 10px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  z-index: 1;
  font-weight: 400;
  touch-action: manipulation;
}
.nav-bar .left-btn { margin-right: auto; }
.nav-bar .right-btn { margin-left: auto; }
.side-tabs { display: none; }

/* ====== Scroll Content ====== */
.scroll-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 0 0 8px 0;
  overscroll-behavior: contain;
}

/* ====== Tab Bar ====== */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: calc(var(--tab-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--card);
  border-top: 0.5px solid var(--gray-light);
  display: flex;
  z-index: 100;
}
.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--gray);
  cursor: pointer;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
  border: none;
  background: none;
  padding: 4px 0 2px;
  min-height: 48px;
  touch-action: manipulation;
}
.tab-item.active { color: var(--blue); }
.tab-item .tab-icon { font-size: 22px; line-height: 1; margin-bottom: 1px; }
.tab-item .tab-label { font-size: 10px; line-height: 1.2; }

/* ====== iOS 風格卡片 ====== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  margin: 8px 16px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.card-list {
  background: var(--card);
  border-radius: var(--radius);
  margin: 8px 16px;
  overflow: hidden;
}
.card-list .list-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 0.5px solid var(--gray-light);
  cursor: pointer;
  transition: background 0.15s;
}
.card-list .list-item:last-child { border-bottom: none; }
.card-list .list-item:active { background: var(--gray-bg); }
.list-item .icon { font-size: 22px; margin-right: 12px; width: 28px; text-align: center; }
.list-item .label { flex: 1; font-size: 15px; }
.list-item .value { font-size: 15px; color: var(--gray); }
.list-item .arrow { color: var(--gray-light); margin-left: 6px; font-size: 14px; }

/* ====== 表單 ====== */
.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  padding-left: 4px;
}
.form-input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--gray-light);
  border-radius: 10px;
  font-size: 16px;
  background: var(--card);
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
}
.form-input:focus { border-color: var(--blue); }
.form-select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--gray-light);
  border-radius: 10px;
  font-size: 16px;
  background: var(--card);
  outline: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%238E8E93' stroke-width='1.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

/* ====== 按鈕 ====== */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
  min-height: 44px;
  touch-action: manipulation;
}
.btn:active { opacity: 0.7; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-success { background: var(--green); color: #fff; }
.btn-danger { background: var(--red); color: #fff; }
.btn-secondary { background: var(--gray-bg); color: var(--blue); }
.btn-block { width: 100%; }
.btn[disabled] { opacity: 0.4; pointer-events: none; }

/* ====== 場景選擇器 ====== */
.scene-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.scene-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 4px;
  border: 2px solid var(--gray-light);
  border-radius: 10px;
  background: var(--card);
  cursor: pointer;
  transition: all 0.15s;
  font-size: 11px;
  color: var(--text-secondary);
  -webkit-tap-highlight-color: transparent;
}
.scene-btn .emoji { font-size: 24px; margin-bottom: 4px; }
.scene-btn.active { border-color: var(--blue); background: #E8F0FE; color: var(--blue); font-weight: 600; }

/* ====== 帳本卡片 ====== */
.book-card {
  background: var(--card);
  border-radius: var(--radius-lg);
  margin: 8px 16px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  cursor: pointer;
  transition: transform 0.15s;
}
.book-card:active { transform: scale(0.98); }
.book-card .book-top { display: flex; align-items: center; margin-bottom: 8px; }
.book-card .book-icon { font-size: 28px; margin-right: 12px; }
.book-card .book-name { font-size: 17px; font-weight: 600; flex: 1; }
.book-card .book-status { font-size: 11px; padding: 3px 8px; border-radius: 10px; }
.book-status.active { background: #E8F5E9; color: var(--green); }
.book-status.settled { background: #F3E5F5; color: #7B1FA2; }
.book-card .book-meta { font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.book-card .book-amount { font-size: 15px; font-weight: 600; }
.book-card .progress-bar {
  height: 4px; background: var(--gray-bg); border-radius: 2px; margin-top: 8px; overflow: hidden;
}
.book-card .progress-fill { height: 100%; border-radius: 2px; background: var(--green); }

/* ====== 收支統計 ====== */
.stats-row {
  display: flex;
  justify-content: space-around;
  padding: 4px 0;
}
.stat-item { text-align: center; }
.stat-num { font-size: 22px; font-weight: 700; }
.stat-label { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }
.stat-green { color: var(--green); }
.stat-red { color: var(--red); }

/* ====== 快速分類（記帳頁） ====== */
.cat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.cat-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 2px;
  border: 2px solid var(--gray-light);
  border-radius: 10px;
  background: var(--card);
  cursor: pointer;
  transition: all 0.15s;
  font-size: 11px;
  color: var(--text-secondary);
}
.cat-btn .emoji { font-size: 22px; margin-bottom: 3px; }
.cat-btn.active { border-color: var(--blue); background: #E8F0FE; color: var(--blue); font-weight: 600; }

/* ====== 分段控制器 ====== */
.segment {
  display: flex;
  background: var(--gray-bg);
  border-radius: 8px;
  padding: 2px;
  margin-bottom: 12px;
}
.segment-item {
  flex: 1;
  text-align: center;
  padding: 8px 4px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  color: var(--text-secondary);
}
.segment-item.active {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

/* ====== 結算單位 ====== */
.settle-unit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 0.5px solid var(--gray-light);
}
.settle-unit:last-child { border-bottom: none; }
.settle-unit .unit-name { font-weight: 600; font-size: 15px; }
.settle-unit .unit-members { font-size: 12px; color: var(--text-secondary); }
.settle-unit .unit-net { font-weight: 700; font-size: 17px; }

/* ====== 支出項目 ====== */
.expense-row {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 0.5px solid var(--gray-light);
}
.expense-row:last-child { border-bottom: none; }
.expense-row .exp-icon { font-size: 20px; margin-right: 10px; width: 24px; text-align: center; }
.expense-row .exp-info { flex: 1; }
.expense-row .exp-desc { font-size: 15px; }
.expense-row .exp-meta { font-size: 12px; color: var(--text-secondary); }
.expense-row .exp-amt { text-align: right; }
.expense-row .exp-amt .orig { font-weight: 600; font-size: 15px; }
.expense-row .exp-amt .home { font-size: 11px; color: var(--text-secondary); }

/* ====== 轉帳條 ====== */
.transfer-row {
  background: var(--gray-bg);
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
}
.transfer-row .transfer-amt { font-weight: 700; color: var(--blue); }

/* ====== 空狀態 ====== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--text-secondary);
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state .empty-text { font-size: 15px; }

/* ====== 智能輸入框 ====== */
.smart-input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.smart-input-row input {
  flex: 1;
  padding: 14px 16px;
  border: 2px solid var(--gray-light);
  border-radius: 12px;
  font-size: 17px;
  background: var(--gray-bg);
  outline: none;
}
.smart-input-row input:focus { border-color: var(--blue); background: var(--card); }
.smart-input-row .voice-btn {
  width: 48px;
  border: 2px solid var(--gray-light);
  border-radius: 12px;
  background: var(--card);
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ====== Toast ====== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.8);
  color: #fff;
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  backdrop-filter: blur(10px);
}
.toast.show { opacity: 1; }
.toast.success { background: rgba(52,199,89,0.9); }
.toast.error { background: rgba(255,59,48,0.9); }

/* ====== 載入中 ====== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px;
}
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--gray-light);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ====== 對話框（半屏） ====== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 300;
  display: none;
  align-items: flex-end;
  justify-content: center;
  touch-action: pan-y;
}
.overlay.show { display: flex; }
.overlay-content {
  background: var(--card);
  width: 100%;
  max-width: 480px;
  max-height: 85dvh;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px calc(16px + var(--safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: slideUp 0.3s ease;
  touch-action: pan-y;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.overlay-handle {
  width: 36px;
  height: 4px;
  background: var(--gray-light);
  border-radius: 2px;
  margin: 0 auto 16px;
  padding: 10px 0;
  box-sizing: content-box;
  background-clip: content-box;
}
.overlay-title {
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  margin-bottom: 16px;
}

/* ====== 登入/註冊頁 ====== */
.auth-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 40px 32px;
  text-align: center;
}
.auth-page .auth-logo { font-size: 64px; margin-bottom: 12px; }
.auth-page .auth-title { font-size: 24px; font-weight: 700; margin-bottom: 4px; }
.auth-page .auth-sub { font-size: 14px; color: var(--text-secondary); margin-bottom: 32px; }
.auth-page .auth-form { width: 100%; max-width: 320px; }
.auth-page .auth-link { font-size: 14px; color: var(--blue); margin-top: 16px; cursor: pointer; }

/* ====== 錯誤頁 ====== */
.error-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
  color: var(--text-secondary);
}

/* ====== 彈性元組件 ====== */
.ml-auto { margin-left: auto; }
.text-center { text-align: center; }
.flex { display: flex; }
.flex-1 { flex: 1; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }

/* ====== 純文字提示 ====== */
.hint {
  font-size: 13px;
  color: var(--text-secondary);
  padding: 4px 0;
}
.hint-blue { color: var(--blue); }

/* ====== 桌面版響應式（PC 自動偵測） ====== */
@media (min-width: 768px) {
  html, body { overflow: auto; }
  .app-shell {
    max-width: none;
    min-height: 100vh;
    flex-direction: row;
    align-items: flex-start;
    padding-bottom: var(--safe-bottom);
  }
  .app-shell .nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 220px;
    height: 100vh;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    padding: 20px 16px;
    border-bottom: none;
    border-right: 0.5px solid var(--gray-light);
    z-index: 100;
  }
  .app-shell .nav-bar .title {
    position: static;
    transform: none;
    font-size: 20px;
    margin-bottom: 24px;
    margin-left: 4px;
  }
  .app-shell .nav-bar .left-btn,
  .app-shell .nav-bar .right-btn {
    width: 100%;
    text-align: left;
    padding: 12px;
    margin: 4px 0;
    border-radius: 10px;
    font-size: 15px;
  }
  .app-shell .nav-bar .left-btn:hover,
  .app-shell .nav-bar .right-btn:hover { background: var(--gray-bg); }
  .app-shell .side-tabs {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin-top: 16px;
    gap: 4px;
  }
  .app-shell .side-tab {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    text-align: left;
    padding: 12px;
    border: none;
    border-radius: 10px;
    background: none;
    color: var(--gray);
    font-size: 15px;
    cursor: pointer;
  }
  .app-shell .side-tab .tab-icon { font-size: 18px; }
  .app-shell .side-tab .tab-label { font-size: 15px; }
  .app-shell .side-tab:hover { background: var(--gray-bg); }
  .app-shell .side-tab.active { background: var(--gray-bg); color: var(--blue); font-weight: 600; }
  .app-shell .tab-bar {
    display: none;
  }
  .app-shell .scroll-content {
    margin-left: 220px;
    width: calc(100% - 220px);
    min-height: 100vh;
    padding: 20px;
  }
  .desktop .scroll-content { padding-bottom: 24px; }
  .overlay {
    align-items: center;
  }
  .overlay-content {
    position: relative;
    max-width: 500px;
    max-height: 80vh;
    border-radius: 16px;
    margin-bottom: 0;
    animation: fadeIn 0.2s ease;
  }
  @keyframes fadeIn { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
  .book-card { border-radius: var(--radius-lg); }
  .cat-grid { grid-template-columns: repeat(4, 1fr); }
  .scene-grid { grid-template-columns: repeat(8, 1fr); }
  .auth-page { padding-left: 220px; }
}
@media (min-width: 1024px) {
  .app-shell { max-width: none; }
  .app-shell .nav-bar { width: 240px; }
  .app-shell .scroll-content {
    margin-left: 240px;
    width: calc(100% - 240px);
    max-width: 900px;
    padding: 24px;
  }
  .card, .card-list, .book-card { margin: 12px 0; }
  .desktop .stats-row { justify-content: flex-start; gap: 32px; }
  .book-card, .card-list { max-width: 680px; }
  .auth-page { padding-left: 240px; max-width: 720px; margin: 0 auto; }
}
@media (min-width: 1400px) {
  .app-shell .scroll-content {
    display: grid;
    grid-template-columns: 1fr 380px;
    gap: 24px;
    align-content: start;
    max-width: 1200px;
  }
  .app-shell .scroll-content > .card:first-child,
  .app-shell .scroll-content > .card:nth-child(2) {
    grid-column: 1 / 2;
  }
  .app-shell .scroll-content > .card:nth-child(3),
  .app-shell .scroll-content > .card:nth-child(4),
  .app-shell .scroll-content > .card:nth-child(5) {
    grid-column: 2 / 3;
  }
  .book-card, .card-list { max-width: none; }
}

/* === 智能偵測卡片 === */
.detect-result { margin: 8px 0; }
.detect-card {
  display: flex; align-items: center; gap: 12px;
  background: var(--gray-bg); border-radius: 12px; padding: 12px;
  border: 1.5px solid var(--blue); margin-bottom: 8px;
}
.detect-icon { font-size: 28px; }
.detect-info { flex: 1; }
.detect-title { font-size: 15px; font-weight: 600; color: var(--text); }
.detect-meta { font-size: 12px; color: var(--gray); }

/* === 成員標籤（chips） === */
.member-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 36px;
  padding: 6px 12px; border-radius: 20px;
  background: var(--gray-bg); border: 1.5px solid var(--gray-light);
  font-size: 13px; cursor: pointer; transition: .15s;
  touch-action: manipulation;
}
.chip:hover { border-color: var(--blue); }
.chip.selected { background: var(--blue); color: #fff; border-color: var(--blue); }
.chip-all { border-color: var(--green); color: var(--green); }
.chip-all.selected { background: var(--green); color: #fff; }

/* === 分攤對象選擇 === */
.split-members { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.split-members .chip.disabled { opacity: 0.45; background: var(--gray-bg); color: var(--gray); border-color: var(--gray-light); }
.split-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.split-row label { flex: 1; font-size: 14px; }
.split-row input { width: 90px; padding: 8px; border: 1.5px solid var(--gray-light); border-radius: 8px; font-size: 14px; text-align: right; }

/* === 支出操作選單 === */
.exp-actions { display: flex; gap: 8px; margin-left: 8px; }
.exp-actions button { border: none; background: var(--gray-bg); border-radius: 8px; padding: 6px 10px; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.exp-actions button:hover { background: var(--gray-light); }

/* === 分攤明細 === */
.split-detail { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }
