:root {
  --bg: #0f172a;
  --bg-elevated: #1e293b;
  --card: #16213a;
  --border: #2a3958;
  --text: #e5edf9;
  --text-muted: #93a3c0;
  --accent: #38bdf8;
  --accent-2: #818cf8;
  --danger: #f87171;
  --success: #4ade80;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", sans-serif;
  overscroll-behavior-y: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 12px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 600; flex: 1; text-align: center; }
.icon-btn {
  background: none; border: none; color: var(--text); font-size: 20px;
  width: 36px; height: 36px; border-radius: 50%; cursor: pointer;
}
.icon-btn:active { background: var(--bg-elevated); }
.icon-btn.hidden { visibility: hidden; }

main#views {
  flex: 1;
  overflow-y: auto;
  padding-bottom: calc(24px + var(--safe-bottom));
}

.view { display: none; padding: 16px; animation: fadeIn .15s ease; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 8px; font-size: 15px; }
.muted { color: var(--text-muted); font-size: 13px; line-height: 1.6; }

.segmented {
  display: flex; background: var(--bg-elevated); border-radius: 999px; padding: 4px; margin-bottom: 12px;
}
.seg-btn {
  flex: 1; border: none; background: none; color: var(--text-muted); padding: 8px 0;
  border-radius: 999px; font-size: 13px; cursor: pointer; font-weight: 600;
}
.seg-btn.active { background: var(--accent); color: #0f172a; }

.search-row { display: flex; gap: 8px; }
#manualInput {
  flex: 1; background: var(--bg-elevated); border: 1px solid var(--border); color: var(--text);
  padding: 12px 14px; border-radius: 12px; font-size: 15px; width: 100%; margin-bottom: 10px;
}

.pill-btn {
  background: var(--accent); color: #0f172a; border: none; border-radius: 12px;
  padding: 12px 18px; font-weight: 700; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.pill-btn.full { width: 100%; }
.link-btn { background: none; border: none; color: var(--accent); font-size: 13px; cursor: pointer; }

.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.section-head h2 { font-size: 14px; color: var(--text-muted); margin: 0; font-weight: 600; }

.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-item {
  display: flex; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px; cursor: pointer;
}
.history-item img { width: 44px; height: 44px; object-fit: contain; background: #fff; border-radius: 8px; }
.history-item .hi-text { flex: 1; min-width: 0; }
.history-item .hi-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-item .hi-sub { font-size: 12px; color: var(--text-muted); }
.empty-hint { color: var(--text-muted); font-size: 13px; padding: 20px 0; text-align: center; }

.result-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.result-img { width: 100%; max-width: 260px; display: block; margin: 0 auto 16px; background: #fff; border-radius: 12px; }
.result-title { font-size: 18px; font-weight: 700; margin: 0 0 4px; }
.result-sub { color: var(--text-muted); font-size: 13px; margin-bottom: 14px; }
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.stat-box { background: var(--bg-elevated); border-radius: 10px; padding: 10px; text-align: center; }
.stat-box .num { font-size: 17px; font-weight: 700; color: var(--accent); }
.stat-box .lbl { font-size: 11px; color: var(--text-muted); }
.parts-list { display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow-y: auto; }
.part-row { display: flex; align-items: center; gap: 10px; background: var(--bg-elevated); border-radius: 10px; padding: 8px; }
.part-row img { width: 36px; height: 36px; object-fit: contain; background: #fff; border-radius: 6px; }
.part-row .pr-text { flex: 1; font-size: 12px; }
.part-row .pr-qty { font-size: 12px; color: var(--text-muted); }
.color-chip {
  display: inline-flex; align-items: center; gap: 6px; background: var(--bg-elevated);
  border-radius: 999px; padding: 4px 10px; font-size: 12px; margin: 3px 4px 0 0;
}
.color-dot { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(255,255,255,.3); }
.state-msg { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.state-msg .big { font-size: 40px; margin-bottom: 10px; }
.retry-btn { margin-top: 14px; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--bg-elevated); border: 1px solid var(--border); color: var(--text);
  padding: 10px 16px; border-radius: 999px; font-size: 13px; z-index: 100; max-width: 90%;
  box-shadow: 0 4px 20px rgba(0,0,0,.4);
}
.toast.hidden { display: none; }
.toast.error { border-color: var(--danger); color: var(--danger); }

.hidden { display: none !important; }

.account-bar {
  display: flex; align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; margin-bottom: 12px; min-height: 44px;
}
.account-bar .acc-user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.account-bar .acc-user img { width: 32px; height: 32px; border-radius: 50%; background: var(--bg-elevated); object-fit: cover; }
.account-bar .acc-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px; }
#googleSignInBtn { display: flex; justify-content: center; }

.stats-card {
  background: linear-gradient(135deg, #0ea5e9, #6366f1); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 20px; color: white;
}
.stats-label { font-size: 13px; opacity: .85; font-weight: 600; }
.stats-value { font-size: 30px; font-weight: 800; margin: 4px 0 2px; }
.stats-sub { font-size: 12px; opacity: .85; }

.collection-list { display: flex; flex-direction: column; gap: 8px; }
.collection-item {
  display: flex; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px; cursor: pointer;
}
.collection-item img { width: 44px; height: 44px; object-fit: contain; background: #fff; border-radius: 8px; }
.collection-item .ci-text { flex: 1; min-width: 0; }
.collection-item .ci-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.collection-item .ci-sub { font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.collection-item .ci-value { font-size: 12px; color: var(--accent); font-weight: 600; margin-top: 2px; }
.collection-item .ci-condition {
  display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 7px; border-radius: 999px;
}
.collection-item .ci-condition.new { background: rgba(74, 222, 128, .18); color: var(--success); }
.collection-item .ci-condition.used { background: rgba(129, 140, 248, .2); color: var(--accent-2); }
.collection-item .ci-qty { font-size: 12px; color: var(--text-muted); font-weight: 700; white-space: nowrap; }
.collection-item .ci-edit, .collection-item .ci-remove {
  background: none; border: none; font-size: 16px; cursor: pointer; padding: 6px;
}
.collection-item .ci-remove { color: var(--danger); }

.fav-btn {
  width: 100%; margin-top: 10px; padding: 12px 18px; border-radius: 12px; font-weight: 700; font-size: 14px;
  cursor: pointer; border: 1px solid var(--accent); background: transparent; color: var(--accent);
}
.fav-btn:active { background: var(--accent); color: #0f172a; }

/* ---------- Modals (加入收藏 / 編輯收藏) ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 200;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-overlay.hidden { display: none; }
.modal-sheet {
  width: 100%; max-width: 480px; background: var(--card); border: 1px solid var(--border);
  border-radius: 20px 20px 0 0; padding: 20px 20px calc(20px + var(--safe-bottom));
}
.modal-sheet h3 { margin: 0 0 6px; font-size: 17px; }

.condition-choice { display: flex; gap: 12px; margin: 16px 0; }
.condition-btn {
  flex: 1; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 14px;
  padding: 18px 8px; color: var(--text); display: flex; flex-direction: column; align-items: center;
  gap: 6px; cursor: pointer; font-size: 13px; font-weight: 700;
}
.condition-btn:active { border-color: var(--accent); background: rgba(56,189,248,.12); }
.condition-btn .cb-icon { font-size: 26px; }
.condition-btn .cb-label { line-height: 1.3; }

.edit-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 14px 0; }
.edit-row label { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.edit-row #editConditionSeg { width: 160px; margin-bottom: 0; }
.edit-row input[type="number"] {
  width: 130px; background: var(--bg-elevated); border: 1px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: 10px; font-size: 14px; text-align: right;
}
.qty-stepper { display: flex; align-items: center; gap: 12px; }
.step-btn {
  width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--border); background: var(--bg-elevated);
  color: var(--text); font-size: 16px; font-weight: 700; cursor: pointer;
}
.qty-stepper span { min-width: 24px; text-align: center; font-weight: 700; }
