/* === 拍照記帳 — 全域樣式 === */
* { box-sizing: border-box; }
:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #5b6cff;
  --primary-hover: #4452e0;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #10b981;
  --shadow: 0 4px 14px rgba(20, 28, 60, 0.06);
  --radius: 14px;
  --radius-sm: 8px;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 15px;
}

/* === topbar === */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 700; }
.logo { font-size: 24px; }

/* === container === */
.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 20px 80px;
}

/* === summary === */
.summary {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
  margin-bottom: 24px;
}
.sum-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.sum-label { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.sum-value { font-size: 32px; font-weight: 700; }
.sum-card.total .sum-value { color: var(--primary); }
.sum-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.cat-list { list-style: none; padding: 0; margin: 0; }
.cat-list li {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px dashed var(--line);
  font-size: 14px;
}
.cat-list li:last-child { border-bottom: none; }
.cat-list .cat-amt { font-weight: 600; color: var(--text); }

/* === summary tabs === */
.sum-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  background: #f3f4f6;
  border-radius: 8px;
  padding: 3px;
}
.sum-tab {
  flex: 1;
  padding: 6px 8px;
  background: transparent;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s;
  -webkit-tap-highlight-color: rgba(91, 108, 255, 0.15);
}
.sum-tab:hover { color: var(--text); }
.sum-tab.active {
  background: var(--card);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* === upload === */
.upload-card { margin-bottom: 24px; }
.dropzone {
  background: var(--card);
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 36px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
}
.dropzone:hover, .dropzone.drag {
  border-color: var(--primary);
  background: #f1f3ff;
}
.dz-icon { font-size: 48px; margin-bottom: 8px; }
.dz-text { font-size: 16px; margin-bottom: 6px; }
.dz-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 14px 0 10px;
}
.dz-buttons .btn {
  min-width: 140px;
  justify-content: center;
}
.dz-hint { color: var(--muted); font-size: 12px; }
.link { color: var(--primary); text-decoration: underline; cursor: pointer; }
.parse-status {
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: #eef2ff;
  color: var(--primary);
  font-size: 14px;
}
.parse-status.error { background: #fef2f2; color: var(--danger); }

/* === list === */
.list-section { margin-top: 12px; }
.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.list-header h2 { margin: 0; font-size: 18px; }
.filters { display: flex; gap: 8px; }
.filters select, .filters input {
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
  font-size: 13px;
}

.receipts-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.receipt {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: transform 0.1s;
}
.receipt:hover { transform: translateY(-2px); }
.receipt img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  background: #e5e7eb;
}
.receipt-body { padding: 12px 14px; flex: 1; }
.receipt-store { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.receipt-meta { color: var(--muted); font-size: 12px; }
.receipt-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
}
.receipt-total { font-size: 18px; font-weight: 700; color: var(--primary); }
.receipt-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.receipt-actions .readonly-hint {
  font-size: 12px;
  color: var(--muted);
  padding: 4px 8px;
  background: #f3f4f6;
  border-radius: 999px;
}
.btn-edit {
  background: #f3f4f6;
  color: var(--text);
  border: 1px solid var(--line);
}
.btn-edit:hover {
  background: #e5e7eb;
}
.tag {
  display: inline-block;
  padding: 2px 8px;
  background: #eef2ff;
  color: var(--primary);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.tag.user {
  background: #f0fdf4;
  color: #15803d;
  margin-left: 4px;
}
.empty-hint {
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
  font-size: 14px;
}

/* === button === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.1s;
  border: 1px solid var(--line);
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { background: transparent; }
.btn.icon { padding: 6px 10px; font-size: 16px; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger:hover { background: #fef2f2; }

/* === modal === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-inner {
  background: var(--card);
  border-radius: var(--radius);
  max-width: 880px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  overflow: hidden;
}
.modal-left {
  background: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: auto;
}
.preview {
  max-width: 100%;
  max-height: 60vh;
  border-radius: var(--radius-sm);
}
.modal-right {
  padding: 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-right label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}
.modal-right input, .modal-right select, .modal-right textarea {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text);
  font-family: inherit;
}
.modal-right input:focus, .modal-right select:focus, .modal-right textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(91, 108, 255, 0.15);
}
.items-box {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px;
  margin: 0;
}
.items-box legend { font-size: 12px; color: var(--muted); padding: 0 6px; }
.item-row {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
.item-row input.it-name { flex: 1; min-width: 0; }
.item-row input.it-qty { width: 56px; flex: 0 0 56px; }
.item-row input.it-price { width: 80px; flex: 0 0 80px; }
.item-row input {
  padding: 10px 8px;
  font-size: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
}
.item-row .remove {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--danger);
  cursor: pointer;
  font-size: 18px;
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  -webkit-tap-highlight-color: rgba(239, 68, 68, 0.2);
  /* iOS Safari 有時對 button 在 flex/grid 內 click target 計算有 bug，
     強制設 explicit position 繞過 */
  position: relative;
  z-index: 2;
}
.item-row .remove:active {
  background: #fef2f2;
  border-color: var(--danger);
}

/* 唯讀模式：隱藏所有編輯按鈕 */
.items-box.readonly .remove,
.items-box.readonly #btnAddItem {
  display: none;
}
.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* === 響應式 === */
@media (max-width: 720px) {
  .summary { grid-template-columns: 1fr; }
  .receipts-list { grid-template-columns: 1fr; }
  .topbar { padding: 12px 16px; }
  .container { padding: 16px 12px 60px; }

  /* 手機版隱藏「拖進來」提示（手機沒拖檔功能）*/
  .dz-text { display: none; }
  /* 手機版也隱藏大 📷 icon（按鈕內的 📷 還在）*/
  .dz-icon { display: none; }
  /* 拍照區更緊湊 */
  .dropzone { padding: 20px 16px; }
  .dz-buttons { margin: 4px 0 6px; }
  .dz-hint { font-size: 11px; }

  /* 手機版 modal：全螢幕（避開鍵盤擠壓） */
  .modal {
    padding: 0;
    align-items: stretch;
  }
  .modal-inner {
    max-width: 100%;
    /* dvh 自動扣鍵盤高度；舊瀏覽器 fallback vh */
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
  }
  .modal-body {
    grid-template-columns: 1fr;
    grid-template-rows: 32vh 1fr;
    min-height: 0;  /* 讓 grid item 可縮 */
  }
  .modal-left {
    padding: 8px;
    max-height: 32vh;
  }
  .preview { max-height: 30vh; }
  .modal-right {
    padding: 14px 16px 16px;
    padding-bottom: calc(84px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
    overflow-y: auto;  /* 表單內容可捲 */
    -webkit-overflow-scrolling: touch;
  }
  .modal-right input,
  .modal-right select,
  .modal-right textarea {
    font-size: 16px;  /* iOS 不會自動 zoom-in */
    padding: 10px 12px;
  }
  .items-box {
    padding: 8px;
  }
  .item-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 56px 80px 44px;
    gap: 6px;
    align-items: center;
  }
  .item-row input.it-name,
  .item-row input.it-qty,
  .item-row input.it-price,
  .item-row .remove {
    width: 100%;
  }
  .item-row input.it-qty,
  .item-row input.it-price,
  .item-row .remove {
    flex: initial;
  }
  /* footer 黏在 modal-inner 底部，鍵盤彈出時 dvh 會自動調整 */
  .modal-footer {
    position: sticky;
    bottom: 0;
    background: var(--card);
    padding: 10px 16px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--line);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
    z-index: 1;
  }
}

/* === 登入畫面 === */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f6f7fb 0%, #e8ebff 100%);
  padding: 20px;
}
.login-screen[hidden] { display: none; }
.login-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 48px 36px;
  box-shadow: 0 10px 40px rgba(20, 28, 60, 0.08);
  text-align: center;
  max-width: 380px;
  width: 100%;
}
.login-logo { font-size: 56px; margin-bottom: 12px; }
.login-card h1 { margin: 0 0 8px; font-size: 24px; }
.login-sub { color: var(--muted); margin: 0 0 28px; font-size: 14px; }
.login-btn {
  width: 100%;
  padding: 12px 16px;
  font-size: 15px;
  justify-content: center;
  margin-bottom: 16px;
}
.g-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  background: white;
  color: #4285f4;
  border-radius: 50%;
  font-weight: 700;
  font-size: 13px;
  margin-right: 6px;
}
.login-hint { color: var(--muted); font-size: 12px; margin: 0; }

/* === user badge === */
.user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 12px;
  background: #f3f4f6;
  border-radius: 999px;
  font-size: 13px;
}
.user-badge[hidden] { display: none; }
.user-email { color: var(--text); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* === view 切換：回到 hidden 屬性切換 === */
#monthView, #tripsView, #tripDetailView { display: block; }
#tripsView[hidden], #tripDetailView[hidden], #monthView[hidden] { display: none; }

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
}
.view-header h2 { margin: 0; font-size: 20px; }
.trip-detail-actions { display: flex; gap: 8px; }

/* === trips list === */
.trips-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.trip-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.1s;
  border: 1px solid var(--line);
}
.trip-card:hover { transform: translateY(-2px); border-color: var(--primary); }
.trip-card-name { font-size: 16px; font-weight: 700; margin-bottom: 6px; }
.trip-card-dates { color: var(--muted); font-size: 13px; margin-bottom: 12px; }
.trip-card-stats { display: flex; justify-content: space-between; padding-top: 10px; border-top: 1px dashed var(--line); }
.trip-card-count { font-size: 12px; color: var(--muted); }
.trip-card-total { font-size: 16px; font-weight: 700; color: var(--primary); }

.trip-summary-cards {
  display: grid;
  grid-template-columns: 240px 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
@media (max-width: 720px) {
  .trip-summary-cards { grid-template-columns: 1fr; }
}

/* === user menu (下拉選單) === */
.user-menu { position: relative; }
.user-menu-btn {
  font-size: 18px;
  padding: 6px 12px;
  cursor: pointer;
}
.user-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 220px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  padding: 8px 0;
  z-index: 50;
}
.user-menu-dropdown[hidden] { display: none; }
.user-menu-info {
  padding: 8px 16px 12px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 6px;
}
.user-menu-email {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
  word-break: break-all;
}
.user-menu-role {
  font-size: 11px;
  color: var(--primary);
  margin-top: 2px;
  font-weight: 600;
}
.user-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 16px;
  background: transparent;
  border: none;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: rgba(91, 108, 255, 0.1);
}
.user-menu-item:hover { background: #f3f4f6; }
.user-menu-item:active { background: #e5e7eb; }

/* === main content === */
#mainContent[hidden] { display: none; }
