:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f1f4f9;
  --text: #0f172a;
  --text-2: #475569;
  --muted: #94a3b8;
  --border: #e5e9f0;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --green: #059669;
  --green-bg: #ecfdf5;
  --red: #dc2626;
  --red-bg: #fef2f2;
  --blue: #2563eb;
  --blue-bg: #eff6ff;
  --amber: #d97706;
  --amber-bg: #fffbeb;
  --hero-1: #1e1b4b;
  --hero-2: #4338ca;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 8px 24px -12px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 24px 64px -16px rgba(15, 23, 42, 0.35);
  --radius: 18px;
  --radius-sm: 12px;
  --ring: 0 0 0 4px rgba(79, 70, 229, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --surface: #131a2c;
    --surface-2: #1a2238;
    --text: #e8ecf5;
    --text-2: #a3aec4;
    --muted: #6b7690;
    --border: #232c44;
    --primary: #818cf8;
    --primary-ink: #0b1020;
    --green: #34d399;
    --green-bg: rgba(52, 211, 153, 0.12);
    --red: #f87171;
    --red-bg: rgba(248, 113, 113, 0.12);
    --blue: #60a5fa;
    --blue-bg: rgba(96, 165, 250, 0.12);
    --amber: #fbbf24;
    --amber-bg: rgba(251, 191, 36, 0.12);
    --hero-1: #1e1b4b;
    --hero-2: #3730a3;
    --shadow-sm: none;
    --shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset;
    --ring: 0 0 0 4px rgba(129, 140, 248, 0.25);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1120px; margin: 0 auto; padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; margin-right: auto; }
.lock-btn { flex: none; }
@media (max-width: 420px) { .brand-sub { display: none; } .month-label { min-width: 0; font-size: 13px; } }
.brand-mark {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--hero-2), var(--hero-1)); color: #fff;
}
.brand-name { font-weight: 700; letter-spacing: -0.01em; }
.brand-sub { font-size: 12px; color: var(--text-2); }

.month-switch {
  display: flex; align-items: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 3px;
  box-shadow: var(--shadow-sm);
}
.month-label {
  border: 0; background: none; cursor: pointer; font-weight: 600; font-size: 14px;
  padding: 6px 8px; min-width: 112px; text-align: center; border-radius: 999px;
}
.month-label:hover { background: var(--surface-2); }

.icon-btn {
  border: 0; background: transparent; width: 34px; height: 34px; border-radius: 999px;
  display: grid; place-items: center; cursor: pointer; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
.icon-btn svg { width: 18px; height: 18px; }

/* ---------- Layout ---------- */
.layout {
  max-width: 1120px; margin: 0 auto; padding: 16px 16px 96px;
  display: grid; gap: 16px;
}
.col { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); padding-top: 24px; gap: 20px; align-items: start; }
  .col { gap: 20px; }
  .history-card { position: sticky; top: 84px; max-height: calc(100vh - 108px); display: flex; flex-direction: column; }
  .history-card .history { overflow-y: auto; overflow-x: hidden; margin-right: -8px; padding-right: 8px; }
  .fab { display: none !important; }
}

/* ---------- Hero ---------- */
.hero {
  position: relative; overflow: hidden;
  color: #fff; border-radius: 22px; padding: 22px;
  background:
    radial-gradient(120% 140% at 100% 0%, rgba(255,255,255,0.16), transparent 50%),
    linear-gradient(135deg, var(--hero-2) 0%, var(--hero-1) 100%);
  box-shadow: 0 18px 40px -18px rgba(67, 56, 202, 0.6);
}
.hero::after {
  content: ""; position: absolute; right: -60px; bottom: -80px; width: 220px; height: 220px;
  border-radius: 50%; background: rgba(255,255,255,0.06);
}
.hero-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.hero-label { font-size: 13px; opacity: 0.8; font-weight: 500; }
.hero-balance {
  font-size: clamp(34px, 9vw, 44px); font-weight: 800; letter-spacing: -0.03em; line-height: 1.1;
  margin-top: 4px; font-variant-numeric: tabular-nums;
}
.hero-balance .cur { font-size: 0.45em; font-weight: 600; opacity: 0.75; margin-left: 6px; letter-spacing: 0; }
.hero-pill {
  font-size: 12px; font-weight: 600; padding: 5px 10px; border-radius: 999px; white-space: nowrap;
  background: rgba(255,255,255,0.14); border: 1px solid rgba(255,255,255,0.18);
}
.hero-pill.warn { background: rgba(251, 191, 36, 0.22); border-color: rgba(251, 191, 36, 0.4); }
.hero-pill.bad { background: rgba(248, 113, 113, 0.25); border-color: rgba(248, 113, 113, 0.45); }

.hero-progress { margin-top: 20px; position: relative; z-index: 1; }
.hero-progress-row { display: flex; justify-content: space-between; font-size: 13px; opacity: 0.9; margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 999px; background: rgba(255,255,255,0.18); overflow: hidden; }
.bar-fill { height: 100%; width: 0; border-radius: inherit; background: #fff; transition: width 0.6s cubic-bezier(.2,.8,.2,1); }
.bar-fill.warn { background: #fbbf24; }
.bar-fill.bad { background: #f87171; }

.hero-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; position: relative; z-index: 1; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: var(--radius-sm); padding: 12px 16px; font-weight: 600; cursor: pointer;
  transition: transform 0.08s, filter 0.15s, background 0.15s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn svg { width: 18px; height: 18px; }
.btn-block { width: 100%; padding: 14px; font-size: 16px; }
.btn-light { background: #fff; color: var(--hero-1); }
.btn-light:hover { filter: brightness(0.96); }
.btn-ghost-light { background: rgba(255,255,255,0.12); color: #fff; border: 1px solid rgba(255,255,255,0.22); }
.btn-ghost-light:hover { background: rgba(255,255,255,0.2); }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-success { background: var(--green); color: #fff; }
.btn-success:hover { filter: brightness(1.08); }
.btn-danger-ghost { background: var(--red-bg); color: var(--red); margin-top: 12px; }
.btn-danger-ghost.confirm { background: var(--red); color: #fff; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 560px) and (max-width: 899px), (min-width: 1100px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 14px;
  box-shadow: var(--shadow); min-width: 0;
}
.stat-icon { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; margin-bottom: 10px; }
.stat-icon svg { width: 16px; height: 16px; }
.stat-icon.red { background: var(--red-bg); color: var(--red); }
.stat-icon.green { background: var(--green-bg); color: var(--green); }
.stat-icon.blue { background: var(--blue-bg); color: var(--blue); }
.stat-icon.amber { background: var(--amber-bg); color: var(--amber); }
.stat-label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.stat-value { font-size: 17px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-value small { font-size: 11px; color: var(--muted); font-weight: 600; margin-left: 3px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card h2 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.card-sub { margin: 2px 0 0; font-size: 13px; color: var(--text-2); }

/* ---------- Chart ---------- */
.chart { position: relative; height: 180px; display: flex; align-items: flex-end; gap: 10px; padding-top: 22px; }
.chart-bar-wrap { flex: 1; min-width: 0; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 6px; }
.chart-bar {
  width: 100%; max-width: 46px; border-radius: 8px 8px 4px 4px; position: relative;
  background: linear-gradient(180deg, var(--primary), color-mix(in srgb, var(--primary) 55%, transparent));
  min-height: 4px; transition: height 0.5s cubic-bezier(.2,.8,.2,1);
}
.chart-bar span {
  position: absolute; bottom: 100%; left: 50%; transform: translate(-50%, -4px);
  font-size: 11px; font-weight: 600; color: var(--text-2); white-space: nowrap; font-variant-numeric: tabular-nums;
}
.chart-x { font-size: 11px; color: var(--muted); font-weight: 500; white-space: nowrap; }
.chart-line {
  position: absolute; left: 0; right: 0; border-top: 1.5px dashed var(--muted); opacity: 0.6; pointer-events: none;
}
.chart-line span { position: absolute; right: 0; top: -18px; font-size: 11px; color: var(--muted); font-weight: 600; }

/* ---------- History ---------- */
.history-tools { display: flex; flex-direction: column; gap: 10px; margin-bottom: 8px; }
@media (min-width: 480px) { .history-tools { flex-direction: row; } .search { flex: 1; } }
.search {
  display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid transparent; border-radius: var(--radius-sm); color: var(--muted);
}
.search:focus-within { border-color: var(--primary); box-shadow: var(--ring); }
.search svg { width: 17px; height: 17px; }
.search input { border: 0; background: transparent; outline: 0; padding: 10px 0; width: 100%; font: inherit; color: var(--text); }

.segmented { display: flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.segmented button {
  flex: 1; border: 0; background: transparent; padding: 7px 12px; border-radius: 9px;
  font-size: 13px; font-weight: 600; color: var(--text-2); cursor: pointer; white-space: nowrap;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }

.history { min-height: 120px; }
.day-head {
  display: flex; justify-content: space-between; font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em; padding: 16px 4px 6px;
}
.row {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 10px 8px; margin: 0;
  border: 0; background: transparent; text-align: left; border-radius: 14px; cursor: pointer;
}
.row:hover { background: var(--surface-2); }
.row:focus-visible { outline: 0; box-shadow: var(--ring); }
.thumb {
  width: 46px; height: 46px; border-radius: 12px; flex: none; object-fit: cover; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--border);
}
.thumb.topup { background: var(--green-bg); color: var(--green); border-color: transparent; }
.thumb.order { background: var(--blue-bg); color: var(--blue); border-color: transparent; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; line-height: 1.3; }
.row-sub { font-size: 12.5px; color: var(--text-2); }
.row-amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-amount.expense { color: var(--text); }
.row-amount.budget { color: var(--green); }

.empty { text-align: center; padding: 36px 12px; color: var(--text-2); }
.empty-icon { width: 52px; height: 52px; border-radius: 16px; margin: 0 auto 12px; display: grid; place-items: center; background: var(--surface-2); color: var(--muted); }
.empty-title { font-weight: 600; color: var(--text); }
.empty p { margin: 4px 0 0; font-size: 13px; }

.skeleton {
  display: inline-block; height: 1em; border-radius: 6px; background: currentColor; opacity: 0.12;
  animation: pulse 1.2s ease-in-out infinite;
}
.w-40 { width: 160px; }
@keyframes pulse { 50% { opacity: 0.22; } }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 15;
  width: 58px; height: 58px; border-radius: 18px; border: 0; cursor: pointer;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--hero-2), var(--hero-1));
  box-shadow: 0 12px 28px -8px rgba(67, 56, 202, 0.7);
  opacity: 0; transform: translateY(16px) scale(0.9); pointer-events: none; transition: 0.2s;
}
.fab.show { opacity: 1; transform: none; pointer-events: auto; }
.fab svg { width: 26px; height: 26px; }

/* ---------- Sheets (dialogs) ---------- */
dialog { color: var(--text); }
.sheet {
  border: 0; padding: 0; background: var(--surface); width: 100%; max-width: 100%;
  margin: auto auto 0; border-radius: 22px 22px 0 0; max-height: 92vh; box-shadow: var(--shadow-lg);
}
.sheet[open] { animation: sheet-up 0.28s cubic-bezier(.2,.9,.3,1); }
.sheet::backdrop { background: rgba(2, 6, 23, 0.55); backdrop-filter: blur(2px); }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
@media (min-width: 640px) {
  .sheet { max-width: 480px; margin: auto; border-radius: 22px; }
  .sheet[open] { animation: pop 0.2s ease-out; }
  @keyframes pop { from { transform: scale(0.96); opacity: 0; } }
}
.sheet-body { padding: 18px 20px calc(22px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 16px; }
.sheet-body::before {
  content: ""; width: 40px; height: 4px; border-radius: 4px; background: var(--border); align-self: center; margin: -6px 0 -4px;
}
@media (min-width: 640px) { .sheet-body::before { display: none; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h3 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--text-2); }
.field-hint { font-size: 12.5px; color: var(--text-2); min-height: 1em; }
.field-hint.bad { color: var(--red); }
.field input, .field textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid transparent; border-radius: var(--radius-sm); padding: 12px 14px; width: 100%; resize: vertical;
}
.field input:focus, .field textarea:focus { outline: 0; border-color: var(--primary); box-shadow: var(--ring); background: var(--surface); }
.field .invalid { border-color: var(--red) !important; }

.amount-input { position: relative; }
.amount-input input { font-size: 26px; font-weight: 700; padding-right: 56px; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.amount-input .suffix { position: absolute; right: 16px; top: 50%; transform: translateY(-50%); font-weight: 700; color: var(--muted); }

.quick-amounts { display: flex; gap: 8px; flex-wrap: wrap; margin-top: -6px; }
.quick-amounts button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px;
  font-size: 13px; font-weight: 600; cursor: pointer; color: var(--text-2); font-variant-numeric: tabular-nums;
}
.quick-amounts button:hover { border-color: var(--primary); color: var(--primary); }

.dropzone {
  border: 2px dashed var(--border); border-radius: 16px; background: var(--surface-2);
  cursor: pointer; transition: border-color 0.15s, background 0.15s; overflow: hidden; outline: 0;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.drag { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 6%, var(--surface-2)); }
.dropzone.invalid { border-color: var(--red); }
.dropzone-empty { padding: 26px 16px; text-align: center; }
.dropzone-icon { width: 46px; height: 46px; border-radius: 14px; margin: 0 auto 10px; display: grid; place-items: center; background: var(--surface); color: var(--primary); box-shadow: var(--shadow-sm); }
.dropzone-title { font-weight: 600; }
.dropzone-hint { font-size: 12.5px; color: var(--text-2); margin-top: 2px; }
.dropzone-preview { position: relative; display: flex; justify-content: center; background: #0b1020; }
.dropzone-preview img { display: block; max-height: 240px; max-width: 100%; object-fit: contain; }
.chip-btn {
  position: absolute; right: 10px; bottom: 10px; border: 0; border-radius: 999px; padding: 6px 12px;
  background: rgba(15, 23, 42, 0.75); color: #fff; font-size: 12.5px; font-weight: 600; cursor: pointer; backdrop-filter: blur(6px);
}

/* ---------- Detail ---------- */
.detail-amount { font-size: 32px; font-weight: 800; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.detail-amount.budget { color: var(--green); }
.detail-meta { display: flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 13.5px; margin-top: 2px; }
.detail-meta svg { width: 15px; height: 15px; }
.detail-desc { margin-top: 14px; padding: 12px 14px; background: var(--surface-2); border-radius: var(--radius-sm); white-space: pre-wrap; overflow-wrap: anywhere; }
.detail-receipt { position: relative; margin-top: 14px; border-radius: 14px; overflow: hidden; background: #0b1020; cursor: zoom-in; display: flex; justify-content: center; }
.detail-receipt img { display: block; max-width: 100%; max-height: 46vh; object-fit: contain; }
.detail-receipt .chip-btn { display: inline-flex; gap: 6px; align-items: center; pointer-events: none; }
.detail-receipt .chip-btn svg { width: 14px; height: 14px; }

/* ---------- Viewer ---------- */
.viewer { border: 0; padding: 0; background: transparent; max-width: 100vw; max-height: 100vh; width: 100vw; height: 100vh; }
.viewer::backdrop { background: rgba(2, 6, 23, 0.92); }
.viewer[open] { display: grid; place-items: center; }
.viewer img { max-width: 96vw; max-height: 92vh; object-fit: contain; border-radius: 8px; }
.viewer-close { position: fixed; top: 14px; right: 14px; background: rgba(255,255,255,0.12); color: #fff; width: 42px; height: 42px; }
.viewer-close:hover { background: rgba(255,255,255,0.22); color: #fff; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; left: 50%; top: 14px; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 380px); pointer-events: none; }
.toast {
  background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 14px; font-weight: 600; font-size: 14px;
  box-shadow: var(--shadow-lg); animation: toast-in 0.25s ease-out; display: flex; gap: 10px; align-items: center;
}
.toast.error { background: var(--red); color: #fff; }
.toast.out { opacity: 0; transform: translateY(-8px); transition: 0.25s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-10px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
