:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1b1f27;
  --muted: #69707d;
  --faint: #98a0ad;
  --border: #e8eaef;
  --border-strong: #dcdfe6;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --accent-soft: #eef0fe;
  --accent-soft-text: #4338ca;
  --success: #15803d;
  --danger: #dc2626;
  --warn: #b45309;
  --track: #eceef3;
  --shadow: 0 1px 2px rgba(17, 24, 39, .04), 0 8px 24px rgba(17, 24, 39, .06);
  --radius: 16px;
  --radius-sm: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1013;
    --card: #181b21;
    --text: #e9ebef;
    --muted: #9aa1ad;
    --faint: #6c7480;
    --border: #262a31;
    --border-strong: #323843;
    --accent: #818cf8;
    --accent-text: #10121a;
    --accent-soft: #20233a;
    --accent-soft-text: #b6bcff;
    --success: #4ade80;
    --danger: #f87171;
    --warn: #fbbf24;
    --track: #242830;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  line-height: 1.5;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 20px calc(48px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

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

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  background: var(--accent-soft);
  color: var(--accent-soft-text);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-title { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
.brand-sub { font-size: 12px; color: var(--muted); }

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

.summary { position: relative; }
.info-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--faint);
  border-radius: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s ease, background .15s ease;
}
.info-btn:hover { color: var(--accent); background: var(--bg); }

.month-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-bottom: 16px;
}

.month-label {
  font-size: 14px;
  font-weight: 600;
  min-width: 130px;
  text-align: center;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.metric {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  min-width: 0;
}

.metric-label { font-size: 11.5px; color: var(--muted); margin-bottom: 3px; }
.metric-value {
  font-size: clamp(15px, 3.4vw, 17px);
  font-weight: 650;
  letter-spacing: -.02em;
  white-space: nowrap;
}
.metric-value.danger { color: var(--danger); }
.metric-note { font-size: 12px; color: var(--danger); margin-top: 4px; white-space: normal; }

.progress-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
}

.progress-track {
  height: 9px;
  background: var(--track);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--success);
  border-radius: 999px;
  transition: width .35s ease, background .25s ease;
}
.progress-fill.warn { background: var(--warn); }
.progress-fill.over { background: var(--danger); }

.month-status {
  margin-top: 14px;
  font-size: 12.5px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--muted);
}
.month-status.over { color: var(--danger); }
.month-status.lost { color: var(--warn); }

.card-title { font-size: 13.5px; font-weight: 600; margin-bottom: 13px; }
.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.card-title-row .card-title { margin-bottom: 0; }
.count { font-size: 12.5px; color: var(--muted); }

.add-form {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  align-items: end;
}
.shop-field { grid-column: 1 / -1; }
.notes-field { grid-column: 1 / -1; }
.add-btn { grid-column: 1 / -1; width: 100%; }

.field { display: flex; flex-direction: column; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.optional { color: var(--faint); font-weight: 400; }

input[type="text"], input[type="number"], input[type="date"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  width: 100%;
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
input[type="text"], input[type="number"], input[type="date"], select {
  height: 44px;
  padding: 0 12px;
}
textarea {
  padding: 10px 12px;
  resize: vertical;
  min-height: 44px;
  line-height: 1.4;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder, textarea::placeholder { color: var(--faint); }
input[type="date"] { -webkit-appearance: none; appearance: none; }
select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2398a0ad' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.amount-input { position: relative; }
.amount-input .cur {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 15px;
  pointer-events: none;
}
.amount-input input { padding-left: 26px; }

.btn {
  font: inherit;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 44px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background .15s ease, border-color .15s ease, transform .05s ease, opacity .15s ease;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: scale(.98); }
.btn.ghost { height: 38px; font-weight: 500; font-size: 13px; color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.btn.primary:hover { background: var(--accent); opacity: .9; }

.iconbtn {
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--card);
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background .15s ease, color .15s ease, opacity .15s ease;
}
.iconbtn:hover:not(:disabled) { background: var(--bg); color: var(--text); }
.iconbtn:disabled { opacity: .35; cursor: not-allowed; }

.form-error { margin: 12px 0 0; font-size: 12.5px; color: var(--danger); }

.expense-list { display: flex; flex-direction: column; }

.expense-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 0;
  border-top: 1px solid var(--border);
}
.expense-row:first-child { border-top: none; }

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-soft-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}

.expense-info { flex: 1; min-width: 0; }
.expense-shop {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.expense-date { font-size: 12px; color: var(--faint); }
.expense-notes {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  white-space: normal;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.expense-amount { font-size: 14px; font-weight: 600; white-space: nowrap; }

.expense-actions { display: flex; gap: 2px; flex-shrink: 0; }
.icon-action {
  border: none;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  display: inline-flex;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s ease, background .15s ease;
}
.icon-action:hover { background: var(--bg); }
.icon-action.edit-btn:hover { color: var(--accent); }
.icon-action.del-btn:hover { color: var(--danger); }

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 26px 0 16px;
  color: var(--faint);
  font-size: 13.5px;
}

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 22px;
  width: min(92vw, 360px);
  max-height: 85vh;
  overflow-y: auto;
}
dialog::backdrop { background: rgba(15, 18, 24, .45); }
dialog h2 { margin: 0 0 4px; font-size: 16px; }
.dialog-sub { margin: 0 0 16px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.dialog-field { display: flex; flex-direction: column; margin-bottom: 14px; }
.dialog-field label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

.info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 0;
  font-size: 13.5px;
  border-bottom: 1px solid var(--border);
}
.info-row span { color: var(--muted); }
.info-row strong { font-weight: 650; font-size: 15px; }
.info-row strong.debt { color: var(--danger); }
.info-row strong.lost { color: var(--warn); }

.cat-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
  flex-shrink: 0;
}

.info-row strong.over { color: var(--danger); }

.bd-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; }
.bd-name { display: flex; align-items: center; width: 104px; flex-shrink: 0; font-size: 13px; }
.bd-bar-wrap { flex: 1; height: 8px; background: var(--track); border-radius: 999px; overflow: hidden; }
.bd-bar { height: 100%; border-radius: 999px; }
.bd-amt { font-size: 12.5px; font-weight: 600; min-width: 86px; text-align: right; white-space: nowrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 16px;
  background: #1f2430;
  color: #e9ebef;
  padding: 12px 16px;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .35);
  font-size: 13.5px;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast button {
  background: transparent;
  border: none;
  color: #a5b4fc;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 2px 4px;
}

.cat-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.cat-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 6px 5px 11px;
  font-size: 13px;
}
.cat-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  border-radius: 50%;
  padding: 0;
}
.cat-x:hover { background: var(--border-strong); color: var(--text); }
.cat-add { display: flex; gap: 8px; }
.cat-add input { flex: 1; }
.cat-add .btn { flex-shrink: 0; }

@media (max-width: 560px) {
  .app { padding: max(16px, env(safe-area-inset-top)) 14px calc(40px + env(safe-area-inset-bottom)); }
  .card { padding: 15px; border-radius: 14px; }
  .metrics { gap: 8px; }
  .add-form { grid-template-columns: 1fr 1fr; }
  .shop-field { grid-column: 1 / -1; }
  .date-field { grid-column: 1 / -1; }
}

@media (max-width: 380px) {
  .brand-sub { display: none; }
  .btn.ghost .btn-text { display: none; }
  .btn.ghost { padding: 0 12px; }
}
