/* ─── Reset & Variables ─── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:       #4F7FE8;
  --primary-dark:  #3868D1;
  --success:       #27AE60;
  --danger:        #E53935;
  --warning:       #F59E0B;
  --bg:            #F7F8FC;
  --surface:       #FFFFFF;
  --text:          #1A2233;
  --text-2:        #5A6478;
  --text-3:        #9AA3B5;
  --border:        #E4E8F0;
  --shadow:        0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg:     0 8px 32px rgba(0,0,0,0.12);
  --radius:        16px;
  --radius-sm:     10px;
  --nav-h:         64px;
  --header-h:      56px;
  --safe-bottom:   env(safe-area-inset-bottom, 0px);
  --safe-top:      env(safe-area-inset-top, 0px);
}

html { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

/* ─── Utility ─── */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ─── Setup Screen ─── */
#setup-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 32px 24px;
  background: linear-gradient(160deg, #4F7FE8 0%, #6B9BFF 100%);
}

.setup-card {
  background: var(--surface);
  border-radius: 24px;
  padding: 40px 28px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.setup-logo {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #4F7FE8, #6B9BFF);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  margin: 0 auto 20px;
}

.setup-card h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.setup-card p {
  font-size: 14px;
  color: var(--text-2);
  margin-bottom: 28px;
  line-height: 1.5;
}

/* ─── App Shell ─── */
#main-app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ─── Header ─── */
.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--header-h) + var(--safe-top));
  padding: calc(var(--safe-top) + 8px) 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.header-back {
  width: 40px;
  height: 40px;
  border: none;
  background: var(--bg);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary);
  font-size: 20px;
  flex-shrink: 0;
  transition: background 0.15s;
}
.header-back:active { background: var(--border); }

.header-title {
  flex: 1;
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-action {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
  color: var(--text-2);
  transition: background 0.15s;
}
.header-action:active { background: var(--border); }

/* ─── Pages ─── */
.pages {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.page {
  display: none;
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.page.active { display: block; }

.page-content {
  padding: 16px 16px calc(var(--nav-h) + var(--safe-bottom) + 16px);
  max-width: 600px;
  margin: 0 auto;
}

/* ─── Bottom Nav ─── */
.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  height: calc(var(--nav-h) + var(--safe-bottom));
  position: sticky;
  bottom: 0;
  z-index: 100;
}

.nav-btn {
  flex: 1;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-3);
  font-size: 11px;
  font-weight: 500;
  padding: 8px 4px;
  transition: color 0.15s;
}

.nav-btn .nav-icon { font-size: 24px; line-height: 1; }
.nav-btn.active { color: var(--primary); }
.nav-btn:active { opacity: 0.7; }

/* ─── Cards ─── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-3);
  margin-bottom: 12px;
}

/* ─── Balance Card ─── */
.balance-card {
  background: linear-gradient(135deg, #4F7FE8 0%, #6B9BFF 100%);
  color: white;
  border-radius: var(--radius);
  padding: 24px 20px;
  margin-bottom: 12px;
  box-shadow: 0 4px 20px rgba(79,127,232,0.35);
}

.balance-card.negative {
  background: linear-gradient(135deg, #E53935 0%, #EF5350 100%);
  box-shadow: 0 4px 20px rgba(229,57,53,0.35);
}

.balance-label {
  font-size: 13px;
  font-weight: 600;
  opacity: 0.8;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.balance-amount {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: -1px;
  line-height: 1;
  margin-bottom: 12px;
}

.balance-meta {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.balance-meta-item {
  font-size: 12px;
  opacity: 0.85;
}

.balance-meta-item strong { font-weight: 700; }

/* ─── Child Cards (Dashboard) ─── */
.child-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
  border: 1px solid var(--border);
  text-align: left;
  width: 100%;
}

.child-card:active {
  transform: scale(0.98);
  box-shadow: none;
}

.child-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4F7FE8, #6B9BFF);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.child-avatar.inactive {
  background: linear-gradient(135deg, #9AA3B5, #C0C8D8);
}

.child-info { flex: 1; min-width: 0; }

.child-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.child-sub {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 2px;
}

.child-balance {
  font-size: 18px;
  font-weight: 800;
  color: var(--success);
  flex-shrink: 0;
}

.child-balance.negative { color: var(--danger); }

.status-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 20px;
  margin-left: 6px;
}

.badge-active { background: #DCFCE7; color: #166534; }
.badge-left   { background: #F3F4F6; color: #6B7280; }

/* ─── Section Header ─── */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  margin-top: 4px;
}

.section-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

/* ─── Transaction Rows ─── */
.txn-list { display: flex; flex-direction: column; gap: 0; }

.txn-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.txn-row:last-child { border-bottom: none; }

.txn-dot {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  margin-top: 1px;
}

.txn-dot.purchase { background: #FEF2F2; }
.txn-dot.allowance { background: #F0FDF4; }

.txn-info { flex: 1; min-width: 0; }

.txn-desc {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.txn-date {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 2px;
}

.txn-amount {
  font-size: 15px;
  font-weight: 700;
  flex-shrink: 0;
}

.txn-amount.credit { color: var(--success); }
.txn-amount.debit  { color: var(--danger); }

.txn-delete {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.txn-delete:active { background: #FEF2F2; color: var(--danger); }

/* ─── Forms ─── */
.form-group { margin-bottom: 16px; }

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  height: 48px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 14px;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  transition: border-color 0.15s;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.form-input:focus { border-color: var(--primary); background: white; }

.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.input-prefix {
  position: absolute;
  left: 14px;
  font-size: 16px;
  color: var(--text-2);
  pointer-events: none;
}

.input-wrap .form-input { padding-left: 28px; }

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 48px;
  padding: 0 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active { opacity: 0.8; transform: scale(0.98); }
.btn-full { width: 100%; }

.btn-primary   { background: var(--primary); color: white; }
.btn-success   { background: var(--success); color: white; }
.btn-danger    { background: var(--danger);  color: white; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1.5px solid var(--border); }
.btn-ghost     { background: transparent; color: var(--text-2); }
.btn-ghost-danger { background: transparent; color: var(--danger); }
.btn-sm { height: 36px; padding: 0 14px; font-size: 13px; }

/* ─── Action Grid ─── */
.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 70px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  transition: all 0.15s;
}

.action-btn:active { transform: scale(0.96); background: var(--bg); }
.action-btn .action-icon { font-size: 22px; }

.action-btn.primary { border-color: #DBEAFE; background: #EFF6FF; color: var(--primary); }
.action-btn.success { border-color: #DCFCE7; background: #F0FDF4; color: var(--success); }
.action-btn.danger  { border-color: #FEE2E2; background: #FFF5F5; color: var(--danger); }
.action-btn.warning { border-color: #FEF3C7; background: #FFFBEB; color: #B45309; }

/* ─── Signature Pad ─── */
.sig-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 8px;
}

.sig-wrap {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  overflow: hidden;
  touch-action: none;
  position: relative;
}

.sig-canvas {
  display: block;
  width: 100%;
  cursor: crosshair;
}

.sig-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 13px;
  color: var(--text-3);
  pointer-events: none;
  text-align: center;
  white-space: nowrap;
}

.sig-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 6px;
}

/* ─── Empty State ─── */
.empty-state {
  text-align: center;
  padding: 48px 24px;
}

.empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 15px; color: var(--text-2); margin-bottom: 20px; }

/* ─── Inventory Items ─── */
.inv-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.inv-item:last-child { border-bottom: none; }

.inv-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: #EFF6FF;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.inv-info { flex: 1; min-width: 0; }

.inv-desc {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inv-meta {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 2px;
}

.inv-amount {
  font-size: 14px;
  font-weight: 700;
  color: var(--danger);
  flex-shrink: 0;
}

/* ─── Export Preview ─── */
.preview-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.preview-row:last-child { border-bottom: none; }
.preview-row .label { color: var(--text-2); }
.preview-row .value { font-weight: 600; }
.preview-row .value.credit { color: var(--success); }
.preview-row .value.debit  { color: var(--danger); }

/* ─── Filter Bar ─── */
.filter-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
}

.filter-chip {
  display: inline-flex;
  align-items: center;
  height: 34px;
  padding: 0 14px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
  flex-shrink: 0;
}

.filter-chip.active { background: var(--primary); border-color: var(--primary); color: white; }

/* ─── Modal ─── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 999;
  padding-bottom: var(--safe-bottom);
}

.modal {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 24px 20px;
  width: 100%;
  max-width: 500px;
}

.modal-title {
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 8px;
}

.modal-message {
  font-size: 14px;
  color: var(--text-2);
  margin-bottom: 20px;
  line-height: 1.5;
}

.modal-form { margin-bottom: 20px; }

.modal-actions {
  display: flex;
  gap: 10px;
}

.modal-actions .btn { flex: 1; }

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: #1A2233;
  color: white;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  z-index: 1000;
  box-shadow: var(--shadow-lg);
  white-space: nowrap;
  transition: opacity 0.3s;
}

/* ─── Settings ─── */
.settings-section { margin-bottom: 20px; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child { border-bottom: none; }
.settings-key { font-size: 15px; color: var(--text); }
.settings-val { font-size: 15px; color: var(--text-2); }

/* ─── Danger Zone ─── */
.danger-zone {
  background: #FFF5F5;
  border: 1px solid #FEE2E2;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.danger-zone-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--danger);
  margin-bottom: 12px;
}

/* ─── Dashboard header row ─── */
.dash-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-top: 4px;
}

.dash-family {
  font-size: 22px;
  font-weight: 800;
  color: var(--text);
  line-height: 1.2;
}

.dash-sub {
  font-size: 13px;
  color: var(--text-2);
  margin-top: 2px;
}

/* ─── Child detail date info ─── */
.info-pills {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.info-pill {
  background: rgba(255,255,255,0.25);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 600;
}

/* ─── Scrollable transaction list ─── */
.txn-scroll {
  max-height: 340px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ─── Month label ─── */
.month-group-label {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-3);
  padding: 12px 0 6px;
}

/* ─── Animations ─── */
@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.page.active .page-content {
  animation: fadeSlideIn 0.22s ease;
}

@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

.toast { animation: toastIn 0.2s ease; }
