:root {
  --primary: #4CAF50;
  --primary-dark: #388E3C;
  --secondary: #FF9800;
  --bg: #f5f5f5;
  --card: #ffffff;
  --text: #212121;
  --text-secondary: #757575;
  --border: #e0e0e0;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --cat1: #66BB6A;
  --cat2: #FFA726;
  --cat3: #EF5350;
  --cat4: #AB47BC;
  --cat5: #26C6DA;
  --cat6: #78909C;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text);
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
  padding-bottom: calc(64px + var(--safe-bottom));
}

/* ── Top Bar ── */
.topbar {
  background: var(--primary);
  color: white;
  padding: 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.topbar h1 { font-size: 18px; font-weight: 700; flex: 1; }
.topbar-btn {
  background: none;
  border: none;
  color: white;
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  display: flex;
  align-items: center;
}
.topbar-btn:active { background: rgba(255,255,255,0.2); }

/* ── Bottom Nav ── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 430px;
  background: white;
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
  padding-bottom: var(--safe-bottom);
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0 6px;
  gap: 2px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 10px;
  font-family: inherit;
  transition: color 0.2s;
}
.nav-item .nav-icon { font-size: 22px; line-height: 1; }
.nav-item.active { color: var(--primary); }
.nav-item:active { background: var(--bg); }

/* ── Pages ── */
.page { display: none; padding: 16px; }
.page.active { display: block; }

/* ── Cards ── */
.card {
  background: var(--card);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}
.card-title {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ── Month Selector ── */
.month-selector {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: center;
  margin-bottom: 16px;
}
.month-btn {
  background: white;
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.month-btn:active { background: var(--bg); }
.month-label { font-size: 18px; font-weight: 700; }

/* ── Summary Cards ── */
.summary-total {
  text-align: center;
  padding: 8px 0;
}
.summary-total .amount {
  font-size: 36px;
  font-weight: 800;
  color: var(--primary);
}
.summary-total .label { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.summary-row:last-child { border-bottom: none; }
.cat-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
}
.cat-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.summary-amount { font-size: 15px; font-weight: 700; }

/* ── Progress Bar ── */
.progress-bar {
  display: flex;
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
  margin: 12px 0 4px;
  gap: 1px;
}
.progress-seg { height: 100%; transition: width 0.4s ease; }
.progress-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-secondary);
}

/* ── Scan Page ── */
.scan-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 16px 0;
}
.camera-btn-wrap { position: relative; }
#camera-input,
#file-input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.camera-btn {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: var(--primary);
  border: none;
  color: white;
  font-size: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(76,175,80,0.4);
  cursor: pointer;
  transition: transform 0.1s;
}
.camera-btn:active { transform: scale(0.96); }

.scan-hint { color: var(--text-secondary); font-size: 14px; text-align: center; }

#preview-area { width: 100%; display: none; }
#receipt-preview {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: 12px;
  border: 2px solid var(--border);
}

.analyze-btn {
  width: 100%;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 14px;
  padding: 16px;
  font-size: 16px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 3px 12px rgba(76,175,80,0.35);
  transition: opacity 0.2s;
}
.analyze-btn:disabled { opacity: 0.5; }
.analyze-btn:active:not(:disabled) { opacity: 0.85; }

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255,255,255,0.4);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Result Items ── */
.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.store-name { font-size: 16px; font-weight: 700; }
.receipt-date { font-size: 13px; color: var(--text-secondary); }

.item-list { display: flex; flex-direction: column; gap: 6px; }
.item-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 10px;
}
.item-row.highlight { background: #fff3e0; }

.item-cat-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.item-name { flex: 1; font-size: 14px; line-height: 1.3; }
.item-price { font-size: 14px; font-weight: 700; white-space: nowrap; }
.item-cat-select {
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  font-family: inherit;
  padding: 4px 6px;
  color: var(--text);
  cursor: pointer;
}
.item-tax { font-size: 11px; color: var(--text-secondary); padding: 2px 6px; background: white; border-radius: 6px; }

.total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 4px;
  border-top: 2px solid var(--border);
  margin-top: 8px;
  font-weight: 700;
  font-size: 16px;
}
.save-btn {
  width: 100%;
  background: var(--secondary);
  color: white;
  border: none;
  border-radius: 14px;
  padding: 16px;
  font-size: 16px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  margin-top: 12px;
  box-shadow: 0 3px 12px rgba(255,152,0,0.35);
}
.save-btn:active { opacity: 0.85; }

/* ── History Page ── */
.receipt-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: white;
  border-radius: 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.1s;
  border: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}
.receipt-card:active { transform: scale(0.98); }
.receipt-icon { font-size: 28px; }
.receipt-info { flex: 1; }
.receipt-store { font-size: 15px; font-weight: 700; margin-bottom: 2px; }
.receipt-meta { font-size: 12px; color: var(--text-secondary); }
.receipt-total { font-size: 16px; font-weight: 800; color: var(--primary); }

.delete-btn {
  background: none;
  border: none;
  color: #ef5350;
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-secondary);
}
.empty-icon { font-size: 64px; margin-bottom: 12px; }
.empty-text { font-size: 16px; }
.empty-sub { font-size: 13px; margin-top: 6px; }

/* ── Settings ── */
.settings-group { margin-bottom: 20px; }
.settings-label { font-size: 13px; color: var(--text-secondary); font-weight: 600; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.05em; }
.settings-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  font-size: 15px;
  font-family: inherit;
  background: white;
  color: var(--text);
  outline: none;
}
.settings-input:focus { border-color: var(--primary); }
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: white;
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}
.settings-row-label { font-size: 15px; }
.settings-row-sub { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

.save-settings-btn {
  width: 100%;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 14px;
  padding: 14px;
  font-size: 15px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  margin-top: 8px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: white;
  border-radius: 24px 24px 0 0;
  width: 100%;
  max-width: 430px;
  padding: 20px 16px calc(20px + var(--safe-bottom));
  max-height: 90vh;
  overflow-y: auto;
}
.modal-title { font-size: 17px; font-weight: 700; margin-bottom: 16px; }
.modal-close {
  float: right;
  background: var(--bg);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #323232;
  color: white;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  z-index: 300;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── API Key Warning ── */
.api-warning {
  background: #fff8e1;
  border: 1px solid #ffe082;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  color: #f57f17;
  margin-bottom: 14px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.api-warning.hidden { display: none; }

/* ── Chart ── */
.donut-wrap {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 8px 0;
}
.donut-wrap svg { flex-shrink: 0; }
.donut-legend { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.donut-legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.donut-legend-color { width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0; }
.donut-legend-label { flex: 1; color: var(--text-secondary); }
.donut-legend-val { font-weight: 600; font-size: 13px; }

/* ── Responsive ── */
@media (min-width: 430px) {
  .bottom-nav { left: 50%; }
}
