:root {
  color-scheme: dark;
  --bg: #121212;
  --card: #1e1e1e;
  --input-bg: #262626;
  --text: #e8e8e8;
  --muted: #9a9a9a;
  --border: #333333;
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --danger: #f87171;
  --success: #4ade80;
  --kehendak: #fbbf24;
  --keperluan: #60a5fa;
  --simpanan: #4ade80;
  --radius: 12px;
  --success-bg: #143d24;
  --success-fg: #86efac;
  --danger-bg: #4c1d1d;
  --danger-fg: #fca5a5;
  --info-bg: #1c3654;
  --info-fg: #93c5fd;
  --warning-bg: #4a3208;
  --warning-fg: #fde68a;
}

/* Light theme override - same token set, different values. Dark (above)
   stays the unconditional default; light only applies when explicitly
   selected via the toggle (data-theme="light" on <html>), never through a
   prefers-color-scheme media query, so the app's own toggle is always the
   single source of truth. */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f5f7;
  --card: #ffffff;
  --input-bg: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --border: #dde1e6;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --kehendak: #b45309;
  --keperluan: #1d4ed8;
  --simpanan: #15803d;
  --success-bg: #e7f7ec;
  --success-fg: #157347;
  --danger-bg: #fdecec;
  --danger-fg: #b42318;
  --info-bg: #eaf2ff;
  --info-fg: #1d4ed8;
  --warning-bg: #fef6e7;
  --warning-fg: #92610a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Calibri, Candara, 'Segoe UI', Optima, Arial, sans-serif;
  font-size: 12px;
  line-height: 1.4;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

/* Theme-switch smoothing: color/background transitions only - no layout
   properties, so switching themes never causes a layout shift. */
body, .card, .btn, input, select, textarea, .tx-row {
  transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}

.excel-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  padding: 3px;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
}
.excel-btn:hover { background: var(--card); }
.excel-btn svg { display: block; }

.live-clock {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.clock-sep { color: var(--border); }

.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

/* Single-row enterprise-style header: brand identity (icon + name) pinned
   left, secondary metadata (clock, theme) pinned right - never a centered
   page-heading treatment. */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 10px 2px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex-shrink: 0;
}

.brand-logo {
  display: block;
  width: 33px;
  height: 28px;
  flex-shrink: 0;
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
  white-space: nowrap;
  background-image: linear-gradient(100deg, #60a5fa, #a78bfa, #34d399, #60a5fa);
  background-size: 300% auto;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: brandGradientWave 3.5s ease-in-out infinite;
}

@keyframes brandGradientWave {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

@media (prefers-reduced-motion: reduce) {
  .brand-title { animation: none; }
}

/* Visually secondary, next to the (primary) live clock - smaller, no pill
   background, muted by default, only gaining emphasis on hover/focus. */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 11px;
  font-family: inherit;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
}
.theme-toggle:hover { background: var(--card); color: var(--text); }
.theme-toggle-icon { font-size: 12px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: 12px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  touch-action: manipulation;
  /* .btn is applied to real <button> elements everywhere else (which never
     have a native underline), but the new owner-warning "Connect Microsoft
     Account" link is an <a> - browsers underline anchors by default, so this
     is needed for it to look like an actual button, not a text link. */
  text-decoration: none;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

/* Destructive action - visually distinct but restrained (outline, not a
   loud solid block) until the confirmation step, where it becomes solid. */
.btn-danger-outline {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 6px 12px;
  font-size: 11px;
  min-height: auto;
}
.btn-danger-outline:hover { background: var(--danger); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:active { filter: brightness(0.9); }

.reset-confirm {
  margin-top: 10px;
  border-color: var(--danger);
}
.reset-confirm-title { margin: 0 0 4px; font-weight: 700; }
.reset-confirm-body { margin: 0 0 12px; color: var(--muted); font-size: 12px; }

/* Quick search over the already-loaded All Transactions list - filters
   client-side only, no separate fetch (see filterAndRenderTx in app.js). */
.tx-search {
  position: relative;
  margin-bottom: 10px;
}
.tx-search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  opacity: 0.6;
  pointer-events: none;
}
.tx-search-input { padding-left: 34px; }

/* Keperluan pie chart: centered, sensible max-width so it never dominates
   the page on desktop, scales down cleanly on mobile via the SVG's own
   viewBox + width:100% (no fixed pixel width to overflow the viewport). */
.pie-chart-card {
  max-width: 460px;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}
.chart-card-title {
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text);
}
/* One-sentence dynamic summary at the bottom of each chart card - always
   generated from the same live numbers already rendered above it, never a
   second/independent figure. */
.chart-summary {
  margin: 14px 0 0;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  text-align: left;
}
.pie-chart-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.pie-chart-svg {
  width: 100%;
  max-width: 420px;
  height: auto;
}

/* Direct slice labels + leader lines (replaces the old separate legend
   list) - drawn inside the same <svg> as the pie itself, so hover/tap
   highlighting can link a slice to its label via a shared data-idx without
   any extra layout code. */
.pie-slice {
  stroke: var(--card);
  stroke-width: 1.5;
  cursor: pointer;
  transition: opacity 0.15s ease, filter 0.15s ease;
}
.pie-slice.is-active { filter: brightness(1.12); }
.pie-slice.is-dimmed { opacity: 0.45; }
.pie-leader {
  stroke: var(--muted);
  stroke-width: 1;
  opacity: 0.6;
  transition: opacity 0.15s ease, stroke 0.15s ease;
}
.pie-label-group { cursor: pointer; transition: opacity 0.15s ease; }
.pie-label-group.is-dimmed { opacity: 0.45; }
.pie-label-group.is-active .pie-leader { stroke: var(--text); opacity: 1; }
.pie-label-text {
  font-size: 10.5px;
  fill: var(--text);
  paint-order: stroke;
  pointer-events: none;
}
.pie-label-inside .pie-label-text {
  fill: #fff;
  stroke: rgba(0, 0, 0, 0.4);
  stroke-width: 3px;
  stroke-linejoin: round;
}
.pie-label-name { font-weight: 700; }
.pie-label-value { font-weight: 500; }
.pie-label-pct { font-weight: 700; }
.pie-label-group.is-active .pie-label-name { text-decoration: underline; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 16px;
  margin-bottom: 14px;
}

/* --- Spending Overview: compact budget dashboard ---
   One card, high information density: monthly budget bar, three category
   rows (Kehendak/Keperluan/Simpanan against the same RM1,800 ceiling - no
   separate per-category budgets), then a four-period summary row. Values
   come entirely from /api/summary (todayTotal/weekTotal/monthTotal/
   yearTotal + *ByCategory) - no calculation duplicated here. */
.budget-dashboard { padding: 16px 18px; }

.budget-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 12px;
}

.budget-month {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-transform: uppercase;
}

.budget-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 12px;
  color: var(--muted);
}
.budget-stat strong { color: var(--text); font-weight: 700; margin-right: 3px; }
.budget-stat.is-over strong { color: var(--danger); }

.budget-bar-track {
  margin-top: 10px;
  height: 8px;
  border-radius: 5px;
  background: var(--input-bg);
  overflow: hidden;
}
.budget-bar-fill {
  height: 100%;
  border-radius: 5px;
  background: var(--primary);
  transition: width 0.4s ease, background-color 0.3s ease;
  min-width: 2px;
}
.budget-bar-fill.warning { background: #f59e0b; }
.budget-bar-fill.danger { background: var(--danger); }

.budget-caption {
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
}

.category-breakdown {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.category-row {
  padding: 4px 6px;
  border-radius: 8px;
  transition: background-color 0.15s ease;
}
.category-row:hover,
.category-row:focus-within { background: var(--input-bg); }

.category-row-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 12px;
  gap: 8px;
}
.category-row-label {
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.category-row-amount { font-weight: 700; white-space: nowrap; }

.category-row-track {
  margin-top: 5px;
  height: 6px;
  border-radius: 4px;
  background: var(--input-bg);
  overflow: hidden;
}
.category-row-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.4s ease;
  min-width: 2px;
}
.category-kehendak { background: var(--kehendak); }
.category-keperluan { background: var(--keperluan); }
.category-simpanan { background: var(--simpanan); }

.category-row-pct {
  margin-top: 3px;
  font-size: 11px;
  color: var(--muted);
  transition: color 0.15s ease;
}
.category-row:hover .category-row-pct,
.category-row:focus-within .category-row-pct {
  color: var(--text);
  font-weight: 600;
}

@media (prefers-reduced-motion: reduce) {
  .budget-bar-fill, .category-row-fill, .category-row-pct { transition: none; }
}

.period-row {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.period-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.period-label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.period-value {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 480px) {
  .period-row { grid-template-columns: repeat(2, 1fr); gap: 10px 14px; }
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 8px;
}

.section-title h2 {
  font-size: 14px;
  margin: 0;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tx-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  /* Sized in JS (fitTxListToFiveRows in app.js) to exactly 5 rows' worth of
     height once real row heights are known; this is just the pre-render
     fallback so the list isn't unbounded before that runs. */
  max-height: 380px;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding-right: 2px;
}

.tx-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  gap: 10px;
  flex-shrink: 0;
  cursor: pointer;
  touch-action: manipulation;
}
.tx-row:hover { background: var(--input-bg); border-color: var(--primary); }
.tx-row:active { filter: brightness(0.95); transform: scale(0.995); }

/* Compact Edit/Delete popup for a clicked transaction row - position:fixed
   so it escapes the .tx-list scroll container instead of being clipped by
   its overflow-y:auto, and is placed dynamically in JS next to the row. */
.tx-action-menu {
  position: fixed;
  z-index: 40;
  display: flex;
  flex-direction: column;
  min-width: 140px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}
.tx-action-item {
  padding: 10px 14px;
  border: none;
  background: none;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}
.tx-action-item:hover { background: var(--input-bg); }
.tx-action-item.tx-action-danger { color: var(--danger); }
.tx-action-item + .tx-action-item { border-top: 1px solid var(--border); }

.tx-row .tx-main { min-width: 0; }
.tx-row .tx-id { font-size: 11px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.tx-row .tx-catatan { font-size: 12px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-row .tx-right { text-align: right; flex-shrink: 0; }
.tx-row .tx-amount { font-weight: 700; }
.tx-row .tx-date { font-size: 12px; color: var(--muted); }

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  color: #0a0a0a;
  margin-top: 4px;
}
.badge-kehendak { background: var(--kehendak); }
.badge-keperluan { background: var(--keperluan); }
.badge-simpanan { background: var(--simpanan); }

.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--muted);
}

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  margin: 0 0 4px;
}
label.required::after { content: ' *'; color: var(--danger); }

input, select, textarea {
  width: 100%;
  height: 34px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 12px;
  background: var(--input-bg);
  color: var(--text);
  font-family: inherit;
}
textarea { height: auto; resize: vertical; min-height: 72px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.field-error {
  color: var(--danger);
  font-size: 11px;
  margin-top: 3px;
  min-height: 14px;
}

/* Compact, space-efficient data-entry form: a 3-column grid on desktop
   (Date/Amount/Category, Payment Type/Payment Method/Email), collapsing to
   a single column on narrow screens. Catatan and the action row span the
   full width since Catatan holds the cascading selectors. */
.entry-card { padding: 16px 18px; }

.entry-form {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px 16px;
}

.field { min-width: 0; }
.field-wide { grid-column: 1 / -1; }

.catatan-builder {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.catatan-builder select,
.catatan-builder input {
  width: auto;
  flex: 1 1 180px;
  margin-top: 0;
}

@media (max-width: 860px) {
  .entry-form { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  .entry-form { grid-template-columns: 1fr; }
  .catatan-builder select,
  .catatan-builder input { flex-basis: 100%; }
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 4px;
}
.form-actions .btn { min-width: 140px; }

.status-banner {
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 12px;
  margin-bottom: 12px;
}
.status-banner.success { background: var(--success-bg); color: var(--success-fg); }
.status-banner.error { background: var(--danger-bg); color: var(--danger-fg); }
.status-banner.info { background: var(--info-bg); color: var(--info-fg); }

/* Owner-connection warning + the edit-mode notice below share this layout:
   message + action button side by side on wide screens, wrapping to stack
   (not stretching taller) on narrow ones. Scoped to these two banners
   specifically so other plain-text .status-banner uses (form save/error
   messages) are unaffected. */
#ownerWarning, #editModeNotice {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.owner-warning-text { flex: 1 1 auto; min-width: 200px; }
.owner-warning-btn {
  flex-shrink: 0;
  white-space: nowrap;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
}
@keyframes spin { to { transform: rotate(360deg); } }

.link-row { margin-top: 10px; }
.link-row a { color: var(--primary); font-weight: 600; text-decoration: none; }

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

/* Still used by edit.html's "Current Catatan" display. */
.chart-meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
}
