/* ==================== Reset & Variables ==================== */
:root {
  --primary: #e07b6c;
  --primary-dark: #c96a5c;
  --primary-light: #f5e3de;
  --danger: #e74c3c;
  --success: #27ae60;
  --warning: #f39c12;
  --text: #4a3728;
  --text-secondary: #8c7a6b;
  --text-light: #b8a89c;
  --bg: #fffaf5;
  --card: #ffffff;
  --border: #f0e8e0;
  --shadow: 0 2px 12px rgba(74, 55, 40, 0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 50px;
  --tab-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

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

/* ==================== Header ==================== */
#header-bar {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}
#header-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  text-align: center;
}
.header-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--primary);
  cursor: pointer;
  border-radius: 50%;
  flex-shrink: 0;
}
.header-btn:active { background: var(--primary-light); }
.header-btn.hidden { visibility: hidden; pointer-events: none; }
#header-action { font-size: 14px; font-weight: 500; width: auto; padding: 0 8px; gap: 4px; white-space: nowrap; }

/* ==================== Main Content ==================== */
#main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}
.page {
  display: none;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  min-height: 100%;
}
.page.active { display: block; }

/* ==================== Tab Bar ==================== */
#tab-bar {
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  align-items: center;
  background: var(--card);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}
.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 100%;
  border: none;
  background: none;
  color: var(--text-light);
  cursor: pointer;
  font-size: 11px;
  padding: 0;
  transition: color 0.15s;
}
.tab-item.active { color: var(--primary); }
.tab-item:active { opacity: 0.7; }

/* ==================== Cards & Common ==================== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 12px;
}
.card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}
.list-item {
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin-bottom: 8px;
  box-shadow: 0 1px 4px rgba(74, 55, 40, 0.06);
  cursor: pointer;
  transition: transform 0.1s;
  border: 1px solid var(--border);
}
.list-item:active { transform: scale(0.98); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
  -webkit-user-select: none;
}
.btn:active { opacity: 0.8; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: transparent; color: var(--primary); border: 1.5px solid var(--primary); }
.btn-ghost { background: none; color: var(--text-secondary); padding: 6px 12px; }
.btn-sm { font-size: 13px; padding: 6px 14px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ==================== Form ==================== */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.form-input, .form-textarea, .form-select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  color: var(--text);
  background: #fff;
  outline: none;
  transition: border-color 0.15s;
  font-family: inherit;
}
.form-input:focus, .form-textarea:focus { border-color: var(--primary); }
.form-textarea { min-height: 80px; resize: vertical; }
.form-input-error { border-color: var(--danger) !important; }
.form-error { color: var(--danger); font-size: 12px; margin-top: 4px; }

/* ==================== Tags / Badges ==================== */
.tag {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}
.tag-danger { background: #fde8e8; color: var(--danger); }
.tag-success { background: #e8f8ed; color: var(--success); }
.tag-warning { background: #fef3e0; color: var(--warning); }
.tag-primary { background: var(--primary-light); color: var(--primary); }

/* ==================== Toast ==================== */
#toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: 360px;
  width: 90%;
}
.toast {
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  animation: toastIn 0.25s ease-out;
  pointer-events: auto;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast-info { background: var(--text); color: #fff; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ==================== Modal ==================== */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#modal-overlay.hidden { display: none; }
#modal-content {
  background: var(--card);
  border-radius: var(--radius);
  width: 100%;
  max-width: 380px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 24px;
  animation: modalIn 0.2s ease-out;
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
#modal-body { margin-bottom: 20px; }
#modal-footer { display: flex; gap: 10px; justify-content: flex-end; }
#modal-footer .btn { flex: 1; }

/* ==================== Dashboard ==================== */
.summary-card {
  text-align: center;
  padding: 20px;
  margin-bottom: 16px;
}
.summary-amount {
  font-size: 42px;
  font-weight: 700;
  color: var(--danger);
  line-height: 1.2;
}
.summary-amount.all-paid { color: var(--success); }
.summary-label { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

.store-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.store-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  border-top: 3px solid var(--primary);
  cursor: pointer;
}
.store-card:active { transform: scale(0.97); }
.store-name { font-size: 16px; font-weight: 600; margin-bottom: 2px; }
.store-partner { font-size: 12px; color: var(--text-secondary); margin-bottom: 8px; }
.store-amount { font-size: 18px; font-weight: 700; color: var(--danger); }
.store-paid { color: var(--text-secondary); font-size: 12px; }
.store-progress { height: 4px; background: var(--border); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.store-progress-bar { height: 100%; border-radius: 2px; transition: width 0.3s; }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.section-title { font-size: 15px; font-weight: 600; }
.section-link { font-size: 13px; color: var(--primary); cursor: pointer; border: none; background: none; }

/* ==================== Purchase List ==================== */
.purchase-item {
  display: flex;
  align-items: center;
  gap: 14px;
}
.purchase-date {
  text-align: center;
  flex-shrink: 0;
  width: 48px;
}
.purchase-day { font-size: 20px; font-weight: 700; display: block; line-height: 1.2; }
.purchase-month { font-size: 11px; color: var(--text-light); }
.purchase-info { flex: 1; min-width: 0; }
.purchase-desc { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.purchase-meta { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.purchase-amount { font-size: 16px; font-weight: 700; flex-shrink: 0; }
.purchase-actions { display: flex; gap: 6px; margin-top: 8px; }
.month-divider {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 12px 0 6px 4px;
}

/* ==================== Allocation Wizard ==================== */
.wizard-steps {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  padding: 0 4px;
}
.wizard-dot {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  transition: background 0.2s;
}
.wizard-dot.active { background: var(--primary); }
.wizard-dot.done { background: var(--success); }

.wizard-step-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
}
.wizard-actions {
  margin-top: 24px;
  display: flex;
  gap: 12px;
}

.select-card {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.select-card:active { border-color: var(--primary); }
.select-card.selected { border-color: var(--primary); background: var(--primary-light); }
.select-card-title { font-size: 15px; font-weight: 600; }
.select-card-sub { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

/* ==================== Photo ==================== */
.photo-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s;
  margin-bottom: 12px;
}
.photo-area:active { border-color: var(--primary); }
.photo-area-icon { font-size: 40px; margin-bottom: 8px; }
.photo-area-text { font-size: 14px; color: var(--text-secondary); }
.photo-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.photo-thumb {
  width: 80px;
  height: 80px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  border: 1px solid var(--border);
  position: relative;
}
.photo-thumb-delete {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  border: 2px solid #fff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ==================== Payment ==================== */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.filter-bar::-webkit-scrollbar { display: none; }
.filter-chip {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  border: 1.5px solid var(--border);
  background: var(--card);
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.filter-chip.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}

.allocation-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}
.allocation-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 5px;
}
.allocation-status-dot.unpaid { background: var(--danger); }
.allocation-status-dot.paid { background: var(--success); }
.allocation-body { flex: 1; min-width: 0; }
.allocation-top { display: flex; justify-content: space-between; align-items: center; }
.allocation-date { font-size: 12px; color: var(--text-light); }
.allocation-amount { font-size: 16px; font-weight: 700; color: var(--danger); }
.allocation-amount.paid { color: var(--success); }
.allocation-desc { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.allocation-store { font-size: 12px; color: var(--text-secondary); }
.allocation-photo-count { font-size: 11px; color: var(--primary); }

/* ==================== Settings ==================== */
.settings-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;
}
.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.settings-item:last-child { border-bottom: none; }
.settings-item:active { background: var(--primary-light); }
.settings-label { font-size: 14px; font-weight: 500; }
.settings-value { font-size: 14px; color: var(--text-secondary); display: flex; align-items: center; gap: 6px; }

/* ==================== Empty State ==================== */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-light);
}
.empty-state-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state-text { font-size: 14px; }

/* ==================== Fullscreen Photo ==================== */
#photo-viewer {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 600;
  display: none;
  align-items: center;
  justify-content: center;
}
#photo-viewer.show { display: flex; }
#photo-viewer img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
#photo-viewer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  color: #fff;
  border: none;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ==================== Responsive ==================== */
@media (min-width: 480px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}
