:root {
  --primary: #4A90D9;
  --primary-dark: #2C6FAC;
  --primary-light: #E8F2FC;
  --secondary: #27AE60;
  --secondary-light: #E8F8EF;
  --danger: #E74C3C;
  --danger-light: #FDEDEC;
  --warning: #F39C12;
  --warning-light: #FEF9E7;
  --bg: #F0F4F8;
  --card: #FFFFFF;
  --text: #1A2535;
  --text-secondary: #5A6A7E;
  --text-light: #8A99AA;
  --border: #DDE3EA;
  --nav-h: 68px;
  --header-h: 56px;
  --radius: 14px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

/* ── Header ── */
#app-header {
  position: fixed;
  top: 0; left: 50%;
  transform: translateX(-50%);
  width: 100%; max-width: 480px;
  height: var(--header-h);
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

#header-back {
  background: none; border: none; color: #fff;
  font-size: 22px; cursor: pointer; padding: 8px; margin-left: -8px;
  display: none;
}
#header-back.visible { display: flex; align-items: center; }

#header-title {
  flex: 1; font-size: 18px; font-weight: 600;
  text-align: center; margin: 0 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

#header-action {
  background: none; border: none; color: #fff;
  font-size: 22px; cursor: pointer; padding: 8px; margin-right: -8px;
  min-width: 40px; display: flex; align-items: center; justify-content: center;
}

/* ── Main content ── */
#main-content {
  position: fixed;
  top: var(--header-h);
  bottom: var(--nav-h);
  left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Bottom nav ── */
#bottom-nav {
  position: fixed;
  bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px;
  height: var(--nav-h);
  background: #fff;
  display: flex;
  border-top: 1px solid var(--border);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom);
}

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

.nav-btn svg { width: 24px; height: 24px; fill: currentColor; }

.nav-btn.active {
  color: var(--primary);
}

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

.card-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-body { padding: 16px; }

/* ── Child cards ── */
.child-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin: 12px 16px;
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  text-decoration: none;
  color: inherit;
}
.child-card:active { transform: scale(0.98); }

.child-avatar {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--primary-light);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700; color: var(--primary);
  flex-shrink: 0;
}

.child-info { flex: 1; min-width: 0; }
.child-name { font-size: 16px; font-weight: 600; margin-bottom: 3px; }
.child-meta { font-size: 13px; color: var(--text-secondary); }

.child-balance {
  text-align: right; flex-shrink: 0;
}
.balance-amount {
  font-size: 20px; font-weight: 700; color: var(--secondary);
}
.balance-amount.negative { color: var(--danger); }
.balance-label { font-size: 11px; color: var(--text-light); }

/* ── Section header ── */
.section-label {
  font-size: 12px; font-weight: 600; color: var(--text-light);
  text-transform: uppercase; letter-spacing: 0.8px;
  padding: 16px 16px 6px;
}

/* ── Transaction list ── */
.tx-list { padding: 0; }

.tx-item {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
}
.tx-item:last-child { border-bottom: none; }

.tx-icon {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.tx-icon.allowance { background: var(--secondary-light); }
.tx-icon.purchase { background: var(--danger-light); }
.tx-icon.manual { background: var(--warning-light); }

.tx-info { flex: 1; min-width: 0; }
.tx-desc { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-date { font-size: 12px; color: var(--text-light); margin-top: 2px; }
.tx-inv { font-size: 11px; color: var(--primary); margin-top: 2px; }

.tx-amount {
  font-size: 15px; font-weight: 600; flex-shrink: 0;
  text-align: right;
}
.tx-amount.credit { color: var(--secondary); }
.tx-amount.debit { color: var(--danger); }

/* ── Balance summary ── */
.balance-hero {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff; padding: 24px 20px; margin: 12px 16px;
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
}
.balance-hero-label { font-size: 13px; opacity: 0.85; margin-bottom: 4px; }
.balance-hero-amount { font-size: 40px; font-weight: 700; line-height: 1; }
.balance-hero-amount.negative { color: #FFBDBD; }
.balance-hero-sub {
  display: flex; gap: 20px; margin-top: 16px;
}
.balance-hero-sub div { flex: 1; }
.balance-hero-sub .lbl { font-size: 11px; opacity: 0.75; }
.balance-hero-sub .val { font-size: 16px; font-weight: 600; margin-top: 2px; }

/* ── FAB ── */
.fab {
  position: fixed;
  bottom: calc(var(--nav-h) + 16px);
  right: max(16px, calc(50% - 240px + 16px));
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none; cursor: pointer;
  box-shadow: 0 4px 16px rgba(74,144,217,0.45);
  font-size: 26px;
  display: flex; align-items: center; justify-content: center;
  z-index: 90;
  transition: transform 0.2s, box-shadow 0.2s;
}
.fab:active { transform: scale(0.93); }

/* ── Modal / Sheet ── */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  display: flex; align-items: flex-end;
  opacity: 0; transition: opacity 0.25s;
}
.overlay.open { opacity: 1; }

.sheet {
  background: var(--card);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 480px;
  margin: 0 auto;
  max-height: 92vh;
  overflow-y: auto;
  padding-bottom: env(safe-area-inset-bottom);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}
.overlay.open .sheet { transform: translateY(0); }

.sheet-handle {
  width: 40px; height: 4px;
  background: var(--border); border-radius: 2px;
  margin: 12px auto 4px;
}

.sheet-title {
  font-size: 18px; font-weight: 700;
  padding: 8px 20px 16px;
  display: flex; align-items: center; justify-content: space-between;
}

.sheet-close {
  background: var(--bg); border: none;
  width: 30px; height: 30px; border-radius: 50%;
  cursor: pointer; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
}

.sheet-body { padding: 0 20px 24px; }

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

.form-label {
  display: block;
  font-size: 13px; font-weight: 600; color: var(--text-secondary);
  margin-bottom: 7px; text-transform: uppercase; letter-spacing: 0.5px;
}

.form-input {
  display: block; width: 100%;
  background: var(--bg); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 16px; color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}
.form-input:focus { border-color: var(--primary); }

.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }

.form-hint { font-size: 12px; color: var(--text-light); margin-top: 5px; }

/* Toggle switch */
.toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 0;
}
.toggle-label { font-size: 15px; font-weight: 500; }
.toggle-hint { font-size: 12px; color: var(--text-light); }

.toggle {
  position: relative; width: 50px; height: 28px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--border); border-radius: 14px;
  cursor: pointer; transition: background 0.2s;
}
.toggle-slider:before {
  content: ''; position: absolute;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff; left: 3px; top: 3px;
  transition: transform 0.2s;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.toggle input:checked + .toggle-slider { background: var(--secondary); }
.toggle input:checked + .toggle-slider:before { transform: translateX(22px); }

/* ── Buttons ── */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 14px 20px;
  border-radius: var(--radius-sm);
  border: none; cursor: pointer;
  font-size: 16px; font-weight: 600;
  transition: opacity 0.2s, transform 0.15s;
  font-family: inherit;
  width: 100%;
}
.btn:active { transform: scale(0.97); opacity: 0.85; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1.5px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--secondary); color: #fff; }
.btn-sm {
  padding: 9px 16px; font-size: 14px; width: auto;
}

.btn-row { display: flex; gap: 10px; margin-top: 8px; }
.btn-row .btn { flex: 1; }

/* ── Empty state ── */
.empty-state {
  text-align: center; padding: 48px 24px;
  color: var(--text-secondary);
}
.empty-state .icon { font-size: 52px; margin-bottom: 16px; }
.empty-state h3 { font-size: 18px; font-weight: 600; margin-bottom: 8px; color: var(--text); }
.empty-state p { font-size: 14px; line-height: 1.6; }

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

.settings-item {
  background: var(--card); display: flex;
  align-items: center; padding: 14px 16px;
  border-bottom: 1px solid var(--border); cursor: pointer;
  transition: background 0.15s;
}
.settings-item:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.settings-item:last-child { border-radius: 0 0 var(--radius) var(--radius); border-bottom: none; }
.settings-item:only-child { border-radius: var(--radius); }
.settings-item:active { background: var(--bg); }

.settings-icon {
  width: 36px; height: 36px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; margin-right: 12px; flex-shrink: 0;
}
.settings-text { flex: 1; }
.settings-title { font-size: 15px; font-weight: 500; }
.settings-value { font-size: 13px; color: var(--text-light); margin-top: 2px; }
.settings-chevron { color: var(--text-light); font-size: 18px; }

/* ── Month group ── */
.month-group-header {
  padding: 14px 16px 6px;
  font-size: 13px; font-weight: 700;
  color: var(--text-secondary); text-transform: uppercase;
  letter-spacing: 0.6px; background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
}
.month-group-total { font-size: 13px; font-weight: 600; color: var(--primary); }

/* ── Inventory ── */
.inv-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.inv-item:last-child { border-bottom: none; }
.inv-num {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--primary-light); color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; flex-shrink: 0;
}
.inv-info { flex: 1; min-width: 0; }
.inv-name { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inv-date { font-size: 12px; color: var(--text-light); margin-top: 2px; }
.inv-amount { font-size: 14px; font-weight: 600; color: var(--danger); flex-shrink: 0; }

/* ── Child selector tabs ── */
.child-tabs {
  display: flex; overflow-x: auto; padding: 12px 16px;
  gap: 8px; scrollbar-width: none;
}
.child-tabs::-webkit-scrollbar { display: none; }

.child-tab {
  flex-shrink: 0; padding: 8px 16px;
  border-radius: 20px; border: 1.5px solid var(--border);
  background: var(--card); cursor: pointer;
  font-size: 14px; font-weight: 500; color: var(--text-secondary);
  transition: all 0.2s; white-space: nowrap;
}
.child-tab.active {
  background: var(--primary); border-color: var(--primary); color: #fff;
}

/* ── Export / PDF view ── */
.export-summary {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); margin: 12px 16px; overflow: hidden;
}
.export-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.export-row:last-child { border-bottom: none; }
.export-row.total {
  font-weight: 700; font-size: 15px;
  background: var(--primary-light);
}
.export-row .label { color: var(--text-secondary); }
.export-row .value { font-weight: 600; }
.export-row .value.credit { color: var(--secondary); }
.export-row .value.debit { color: var(--danger); }

/* ── Signature pad ── */
.sig-container {
  margin-bottom: 20px;
}
.sig-label {
  font-size: 13px; font-weight: 600; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px;
}
.sig-pad-wrap {
  background: var(--bg); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); overflow: hidden; position: relative;
}
.sig-canvas {
  display: block; width: 100%; height: 120px; touch-action: none;
  cursor: crosshair;
}
.sig-placeholder {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  color: var(--text-light); font-size: 13px; pointer-events: none;
  text-align: center; line-height: 1.4;
  transition: opacity 0.2s;
}
.sig-actions {
  display: flex; justify-content: flex-end; margin-top: 6px;
}
.sig-clear {
  background: none; border: none; cursor: pointer;
  font-size: 12px; color: var(--primary); font-weight: 600; padding: 4px 8px;
}

/* ── Select month ── */
.month-select-row {
  display: flex; gap: 10px; padding: 12px 16px 4px;
  align-items: center;
}
.month-select-row select {
  flex: 1; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  background: var(--card); font-size: 15px; color: var(--text);
  font-family: inherit; outline: none;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%235A6A7E' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

/* ── Chip / tag ── */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 12px;
  font-size: 12px; font-weight: 600;
}
.chip-success { background: var(--secondary-light); color: var(--secondary); }
.chip-danger { background: var(--danger-light); color: var(--danger); }
.chip-info { background: var(--primary-light); color: var(--primary); }
.chip-warning { background: var(--warning-light); color: var(--warning); }

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

/* ── Page padding ── */
.page { padding-bottom: 16px; }

/* ── Confirm dialog ── */
.dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  z-index: 300; display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.dialog {
  background: var(--card); border-radius: var(--radius);
  padding: 24px; width: 100%; max-width: 320px;
  box-shadow: var(--shadow-lg);
}
.dialog h3 { font-size: 17px; font-weight: 700; margin-bottom: 10px; }
.dialog p { font-size: 14px; color: var(--text-secondary); line-height: 1.5; margin-bottom: 20px; }

/* ── Info banner ── */
.info-banner {
  margin: 0 16px 0;
  background: var(--primary-light); border-radius: var(--radius-sm);
  padding: 10px 14px; font-size: 13px; color: var(--primary);
  display: flex; align-items: flex-start; gap: 8px;
}

/* Inactive badge */
.inactive-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: var(--bg); color: var(--text-light);
  font-weight: 600; margin-left: 6px;
}

/* ── Swipe to delete ── */
.swipeable { overflow: hidden; position: relative; }
.delete-bg {
  position: absolute; right: 0; top: 0; bottom: 0;
  width: 80px; background: var(--danger);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px; font-weight: 600;
}

/* ── Loading ── */
.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive tweaks ── */
@media (max-width: 360px) {
  .balance-hero-amount { font-size: 34px; }
  .child-name { font-size: 15px; }
}

/* iOS safe area */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #bottom-nav { height: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
  #main-content { bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
  .fab { bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); }
  #toast { bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); }
}
