:root {
  --navy: #2d3557;
  --navy-light: #3c4570;
  --cream: #f7efe6;
  --card: #ffffff;
  --green: #3fae72;
  --orange: #e2795a;
  --red: #d9534f;
  --gold: #e8b84b;
  --text-dark: #2b2b2b;
  --text-gray: #8b8b8b;
  --border: #ece3d6;
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Noto Sans Thai", "Sarabun", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--cream);
  color: var(--text-dark);
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  background: var(--cream);
  position: relative;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; }

a { color: inherit; }

.hidden { display: none !important; }

/* ---------- Top App Header ---------- */
.app-header {
  background: var(--navy);
  color: #fff;
  padding: 18px 20px 22px;
  border-radius: 0 0 26px 26px;
}

.app-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-header-brand .brand-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--navy-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.app-header-brand .brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.app-header-brand .brand-text p {
  margin: 2px 0 0;
  font-size: 11.5px;
  color: #cfd3e6;
}

.icon-btn {
  background: var(--navy-light);
  border: none;
  color: #fff;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  cursor: pointer;
}

.balance-block { margin-top: 18px; }
.balance-label { font-size: 12.5px; color: #cfd3e6; }
.balance-amount { font-size: 34px; font-weight: 800; margin: 4px 0 10px; letter-spacing: -0.5px; }
.balance-amount.negative { color: #ff9f8f; }

.balance-sub { display: flex; gap: 18px; font-size: 13px; color: #e6e9f5; }
.balance-sub span { display: flex; align-items: center; gap: 4px; }
.balance-sub .in { color: #7be0a8; }
.balance-sub .out { color: #ff9f8f; }

/* ---------- Page content ---------- */
.page {
  flex: 1;
  padding: 16px 16px calc(96px + var(--safe-bottom));
  overflow-y: auto;
}

.section-title {
  font-size: 20px;
  font-weight: 800;
  margin: 4px 0 2px;
}

.section-subtitle {
  font-size: 13px;
  color: var(--text-gray);
  margin: 0 0 14px;
}

.card {
  background: var(--card);
  border-radius: var(--radius-md);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 3px rgba(45, 53, 87, 0.06);
}

.card-navy {
  background: var(--navy);
  color: #fff;
  border-radius: var(--radius-md);
  padding: 18px;
  margin-bottom: 14px;
  text-align: center;
}

.card-navy .label { font-size: 13px; color: #cfd3e6; margin-bottom: 6px; }
.card-navy .amount { font-size: 26px; font-weight: 800; }
.card-navy .amount.negative { color: #ff9f8f; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.stat-card {
  background: var(--card);
  border-radius: var(--radius-md);
  padding: 14px;
  box-shadow: 0 1px 3px rgba(45,53,87,0.06);
}
.stat-card .label { font-size: 12.5px; color: var(--text-gray); margin-bottom: 4px; }
.stat-card .value { font-size: 19px; font-weight: 800; }
.stat-card .value.income { color: var(--green); }
.stat-card .value.expense { color: var(--orange); }

/* ---------- Group header (account row) ---------- */
.group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 4px;
  cursor: pointer;
}

.group-header-left { display: flex; align-items: center; gap: 10px; }

.group-icon {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
  background: #f1e9dd;
}

.group-title { font-weight: 700; font-size: 15px; }
.group-amount { font-weight: 700; font-size: 14.5px; }
.group-amount.positive { color: var(--green); }
.group-amount.negative { color: var(--orange); }
.group-toggle { color: var(--text-gray); font-size: 12px; margin-left: 6px; }

/* ---------- Transaction row ---------- */
.txn-row {
  background: var(--card);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 1px 3px rgba(45,53,87,0.05);
}

.txn-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  background: #f1e9dd;
}

.txn-body { flex: 1; min-width: 0; }
.txn-title { font-weight: 700; font-size: 14.5px; margin-bottom: 2px; }
.txn-meta { font-size: 11.5px; color: var(--text-gray); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.txn-amount-block { text-align: right; flex-shrink: 0; }
.txn-amount { font-weight: 700; font-size: 14.5px; white-space: nowrap; }
.txn-amount.income { color: var(--green); }
.txn-amount.expense { color: var(--orange); }
.txn-actions { display: flex; gap: 6px; margin-top: 4px; justify-content: flex-end; }
.txn-actions button {
  background: none; border: none; color: var(--text-gray); font-size: 13px; cursor: pointer; padding: 2px;
}

.empty-state {
  text-align: center;
  color: var(--text-gray);
  padding: 40px 20px;
  font-size: 13.5px;
}
.empty-state .emoji { font-size: 40px; display: block; margin-bottom: 10px; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 480px;
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 8px 4px calc(8px + var(--safe-bottom));
  z-index: 40;
}

.nav-btn {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 10.5px;
  color: var(--text-gray);
  cursor: pointer;
  flex: 1;
  padding: 4px 2px;
}
.nav-btn .nav-icon { font-size: 19px; }
.nav-btn.active { color: var(--navy); font-weight: 700; }

.fab {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(46px + var(--safe-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--navy);
  color: #fff;
  border: none;
  font-size: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 16px rgba(45,53,87,0.35);
  cursor: pointer;
  z-index: 41;
}

.fab-mic {
  position: fixed;
  left: calc(50% + 48px);
  bottom: calc(53px + var(--safe-bottom));
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--orange);
  color: #fff;
  border: none;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 14px rgba(226,121,90,0.35);
  cursor: pointer;
  z-index: 41;
}

.qa-mic-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.qa-mic-btn.listening {
  background: var(--orange);
  color: #fff;
  border-color: var(--orange);
}

/* ---------- Tabs (period selector) ---------- */
.tab-row {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  overflow-x: auto;
}
.tab-chip {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--text-dark);
  white-space: nowrap;
  cursor: pointer;
}
.tab-chip.active { background: var(--navy); color: #fff; border-color: var(--navy); }

/* ---------- Progress bar ---------- */
.progress-track {
  height: 8px;
  background: var(--border);
  border-radius: 6px;
  overflow: hidden;
  margin: 8px 0;
}
.progress-fill { height: 100%; background: var(--orange); border-radius: 6px; }
.progress-fill.green { background: var(--green); }

/* ---------- Section within cards ---------- */
.card-section-title {
  font-weight: 800;
  font-size: 15.5px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.card-section-title .add-btn {
  background: var(--navy);
  color: #fff;
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
}

.recurring-row, .debt-card, .tax-row {
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
}
.recurring-row:last-child, .tax-row:last-child { border-bottom: none; }

.recurring-top { display: flex; align-items: center; gap: 10px; }
.recurring-info { flex: 1; min-width: 0; }
.recurring-name { font-weight: 700; font-size: 14.5px; }
.recurring-freq { font-size: 11.5px; color: var(--text-gray); }
.recurring-amount { font-weight: 700; font-size: 14px; white-space: nowrap; }

.switch {
  width: 40px;
  height: 22px;
  border-radius: 12px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}
.switch.on { background: var(--green); }
.switch .knob {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  position: absolute;
  top: 2px;
  left: 2px;
  transition: left 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}
.switch.on .knob { left: 20px; }

.row-actions { display: flex; gap: 10px; align-items: center; }
.row-actions button { background: none; border: none; color: var(--text-gray); font-size: 15px; cursor: pointer; }
.quick-log-btn {
  background: #eef6f1;
  color: var(--green);
  border: none;
  border-radius: 14px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.debt-header { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.debt-name { font-weight: 700; font-size: 15px; flex: 1; }
.debt-sub { font-size: 11.5px; color: var(--text-gray); }
.debt-stats { display: flex; justify-content: space-between; font-size: 12.5px; margin-top: 6px; }
.debt-stats .remain { font-weight: 700; }
.debt-stats .remain.over { color: var(--red); }
.debt-totals { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-gray); margin-top: 4px; }
.log-payment-btn {
  width: 100%;
  margin-top: 10px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 12px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}
.log-payment-btn:disabled { background: #b7d9c4; cursor: default; }

.tax-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tax-info .tax-name { font-weight: 700; font-size: 14px; }
.tax-info .tax-due { font-size: 11.5px; color: var(--text-gray); }
.tax-amount { font-weight: 700; font-size: 14px; }
.tax-paid-btn {
  background: var(--navy);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}
.tax-row.paid { opacity: 0.5; }

/* ---------- Assets ---------- */
.asset-row {
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
}
.asset-row:last-child { border-bottom: none; }
.asset-name { font-weight: 700; font-size: 14px; }
.asset-meta { font-size: 11.5px; color: var(--text-gray); }
.asset-value { font-weight: 700; font-size: 14px; white-space: nowrap; }
.asset-gain { font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.asset-gain.positive { color: var(--green); }
.asset-gain.negative { color: var(--orange); }

/* ---------- Budget ---------- */
.budget-row { margin-bottom: 14px; }
.budget-row:last-child { margin-bottom: 0; }
.budget-top { display: flex; justify-content: space-between; font-size: 13.5px; margin-bottom: 6px; }
.budget-top .name { font-weight: 600; display: flex; gap: 6px; align-items: center; }
.budget-top .nums { color: var(--text-gray); }
.budget-top .nums.over { color: var(--red); font-weight: 700; }

/* ---------- Forms / Sheets ---------- */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 45, 0.45);
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-overlay.centered { align-items: center; padding: 20px; }

.sheet {
  background: var(--cream);
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  border-radius: 24px 24px 0 0;
  overflow-y: auto;
  padding-bottom: calc(20px + var(--safe-bottom));
}
.sheet-overlay.centered .sheet { border-radius: 20px; max-height: 88vh; }

.sheet-header {
  position: sticky;
  top: 0;
  background: var(--cream);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 10px;
  z-index: 2;
}
.sheet-header h2 { margin: 0; font-size: 17px; font-weight: 800; }
.sheet-close {
  background: #fff;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 15px;
  cursor: pointer;
  color: var(--text-gray);
}

.sheet-body { padding: 6px 18px 18px; }

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--text-dark);
}
.field input[type=text],
.field input[type=number],
.field input[type=date],
.field input[type=password],
.field select,
.field textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  background: #fff;
  border-radius: 12px;
  padding: 11px 13px;
  font-size: 14.5px;
  font-family: inherit;
  color: var(--text-dark);
}
.field textarea { resize: vertical; min-height: 60px; }
.field .hint { font-size: 11.5px; color: var(--text-gray); margin-top: 5px; }

.segmented {
  display: flex;
  background: #efe6d8;
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 16px;
}
.segmented button {
  flex: 1;
  border: none;
  background: none;
  padding: 9px 0;
  border-radius: 9px;
  font-weight: 700;
  font-size: 13.5px;
  color: var(--text-gray);
  cursor: pointer;
}
.segmented button.active.expense { background: var(--orange); color: #fff; }
.segmented button.active.income { background: var(--green); color: #fff; }

.icon-picker {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  max-height: 160px;
  overflow-y: auto;
}
.icon-picker button {
  border: 1.5px solid var(--border);
  background: #fff;
  border-radius: 10px;
  font-size: 18px;
  padding: 8px 0;
  cursor: pointer;
}
.icon-picker button.selected { border-color: var(--navy); background: #e9ebf5; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1.5px solid var(--border);
  background: #fff;
  border-radius: 14px;
  padding: 9px 13px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.chip.selected { background: var(--navy); color: #fff; border-color: var(--navy); }

.btn-primary {
  width: 100%;
  background: var(--navy);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 14px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}
.btn-primary:disabled { opacity: 0.5; }
.btn-secondary {
  width: 100%;
  background: #fff;
  color: var(--navy);
  border: 1.5px solid var(--navy);
  border-radius: 14px;
  padding: 13px;
  font-size: 14.5px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 10px;
}
.btn-danger {
  width: 100%;
  background: #fdecea;
  color: var(--red);
  border: none;
  border-radius: 14px;
  padding: 12px;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 10px;
}

.list-manage-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.list-manage-row:last-child { border-bottom: none; }
.list-manage-row .name { flex: 1; font-weight: 600; font-size: 14px; }

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.settings-item:last-child { border-bottom: none; }
.settings-item .left { display: flex; align-items: center; gap: 12px; }
.settings-item .left .emoji { font-size: 19px; }
.settings-item .title { font-weight: 700; font-size: 14.5px; }
.settings-item .sub { font-size: 11.5px; color: var(--text-gray); }
.settings-item .chevron { color: var(--text-gray); }

.toast {
  position: fixed;
  bottom: calc(110px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--navy);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 100;
  max-width: 90%;
  text-align: center;
  box-shadow: 0 4px 14px rgba(0,0,0,0.2);
}

.ocr-drop {
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 22px;
  text-align: center;
  color: var(--text-gray);
  font-size: 13px;
  cursor: pointer;
  background: #fff;
}
.ocr-preview-img {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 12px;
  margin-bottom: 12px;
  background: #eee;
}
.ocr-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-gray);
  padding: 10px 0;
}
.spinner {
  width: 16px;
  height: 16px;
  border: 2.5px solid var(--border);
  border-top-color: var(--navy);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.badge {
  display: inline-block;
  background: var(--gold);
  color: #4a3800;
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 6px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--navy);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}

/* ---------------------------- Lock screen / PIN --------------------------- */
.lock-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  background: var(--cream);
  text-align: center;
}
.lock-logo { width: 84px; height: 84px; border-radius: 22px; margin-bottom: 14px; }
.lock-screen h2 { margin: 0 0 4px; font-size: 20px; font-weight: 800; color: var(--navy); }
.lock-sub { color: var(--text-gray); font-size: 13.5px; margin: 0 0 22px; }

.pin-dots { display: flex; gap: 14px; margin-bottom: 18px; }
.pin-dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--navy); background: transparent; }
.pin-dot.filled { background: var(--navy); }

.lock-error { color: var(--red); font-size: 13px; margin-bottom: 14px; }

.pin-keypad {
  display: grid;
  grid-template-columns: repeat(3, 64px);
  gap: 14px;
  margin-bottom: 20px;
  justify-content: center;
}
.pin-keypad button {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: #fff;
  font-size: 20px;
  font-weight: 700;
  color: var(--text-dark);
  box-shadow: 0 1px 3px rgba(45, 53, 87, 0.08);
  cursor: pointer;
}
.pin-key-empty { background: none !important; box-shadow: none !important; cursor: default !important; }

.lock-forgot { margin-top: 6px; }
