:root {
  --bg: #0b1120;
  --bg-2: #111a2e;
  --card: #131c31;
  --card-2: #1a2540;
  --border: rgba(148, 163, 184, 0.14);
  --text: #e2e8f0;
  --muted: #8b9ab3;
  --primary: #6366f1;
  --primary-2: #818cf8;
  --green: #22c55e;
  --red: #f87171;
  --amber: #fbbf24;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: radial-gradient(1200px 600px at 10% -10%, #1e1b4b 0%, transparent 60%), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: 0.35; cursor: not-allowed; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px clamp(14px, 3vw, 32px);
  background: rgba(11, 17, 32, 0.82);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; letter-spacing: 0.2px; }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-label {
  position: relative; min-width: 170px; text-align: center;
  font-weight: 600; font-size: 1.05rem; padding: 6px 12px; border-radius: 10px; cursor: pointer;
}
.month-label:hover { background: var(--card-2); }
.month-label input {
  position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%;
}
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--card-2);
  padding: 8px 14px; border-radius: 10px; font-weight: 600; font-size: 0.9rem;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-2); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--card-2); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--card); font-size: 1.3rem; line-height: 1; display: inline-grid; place-items: center;
}
.icon-btn:hover:not(:disabled) { background: var(--card-2); }
.icon-btn.small { width: 30px; height: 30px; font-size: 0.85rem; color: var(--muted); background: transparent; border-color: transparent; }
.icon-btn.small:hover { color: var(--red); background: rgba(248, 113, 113, 0.1); }

/* ---------- Layout ---------- */
main {
  max-width: 1200px; margin: 0 auto;
  padding: 20px clamp(12px, 3vw, 32px) 60px;
  display: flex; flex-direction: column; gap: 18px;
}
.grid.two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.card {
  background: linear-gradient(180deg, var(--card) 0%, var(--bg-2) 100%);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 1.02rem; font-weight: 650; }
.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- Banner ---------- */
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 18px; border-radius: var(--radius);
  background: rgba(99, 102, 241, 0.12); border: 1px solid rgba(99, 102, 241, 0.35);
}
.banner.warn { background: rgba(248, 113, 113, 0.1); border-color: rgba(248, 113, 113, 0.4); }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.kpi {
  background: linear-gradient(180deg, var(--card) 0%, var(--bg-2) 100%);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px;
  display: flex; flex-direction: column; gap: 4px; box-shadow: var(--shadow);
}
.kpi-label { color: var(--muted); font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.6px; }
.kpi strong { font-size: clamp(1.25rem, 2.2vw, 1.7rem); font-weight: 750; font-variant-numeric: tabular-nums; }
.kpi small { color: var(--muted); font-size: 0.82rem; min-height: 1em; }
.pos { color: var(--green); }
.neg { color: var(--red); }

/* ---------- Progress ---------- */
.progress-card { padding: 14px 18px; }
.progress-head { display: flex; justify-content: space-between; font-size: 0.9rem; color: var(--muted); margin-bottom: 8px; }
.progress { height: 10px; background: var(--card-2); border-radius: 99px; overflow: hidden; }
.progress > div {
  height: 100%; width: 0; border-radius: 99px;
  background: linear-gradient(90deg, var(--primary), var(--green));
  transition: width 0.4s ease;
}

/* ---------- Segmented ---------- */
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 7px; font-size: 0.82rem; color: var(--muted); }
.seg button.active { background: var(--card-2); color: var(--text); }

/* ---------- Charts ---------- */
.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 320px; }
.empty-chart {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  color: var(--muted); padding: 20px;
}

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.02);
  border: 1px solid transparent; transition: background 0.15s, border-color 0.15s;
}
.item:hover { background: var(--card-2); border-color: var(--border); }
.item-main { flex: 1; min-width: 0; cursor: pointer; }
.item-title-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge-installment {
  display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 99px;
  font-size: 0.74rem; font-weight: 650; font-variant-numeric: tabular-nums;
  background: rgba(99, 102, 241, 0.16); color: #a5b4fc; border: 1px solid rgba(99, 102, 241, 0.35);
}
.item.paid .badge-installment {
  background: rgba(34, 197, 94, 0.14); color: #86efac; border-color: rgba(34, 197, 94, 0.32);
}
.item-meta { display: flex; gap: 10px; flex-wrap: wrap; font-size: 0.78rem; color: var(--muted); margin-top: 3px; }
.item-value { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; cursor: pointer; }
.item-value small { display: block; font-weight: 500; font-size: 0.75rem; color: var(--amber); }
.item.paid .item-value small { color: var(--green); }
.item.paid .item-title { color: var(--muted); text-decoration: line-through; text-decoration-color: rgba(139, 154, 179, 0.5); }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
}
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c, var(--muted)); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }

.check {
  width: 28px; height: 28px; border-radius: 8px; flex: none;
  border: 2px solid rgba(148, 163, 184, 0.45); background: transparent;
  color: transparent; font-weight: 800; font-size: 0.9rem; display: grid; place-items: center;
  transition: all 0.15s;
}
.check:hover { border-color: var(--green); }
.item.paid .check { background: var(--green); border-color: var(--green); color: #052e16; }

.list-empty { color: var(--muted); padding: 18px 6px; text-align: center; font-size: 0.9rem; }
.list-total {
  display: flex; justify-content: space-between; padding: 10px 12px 0; margin-top: 6px;
  border-top: 1px dashed var(--border); font-weight: 700; color: var(--muted);
}
.hint { color: var(--muted); font-size: 0.85rem; margin: 12px 4px 0; }

/* ---------- Modal ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 0;
  background: var(--card); color: var(--text);
  width: min(520px, calc(100vw - 24px)); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}
dialog::backdrop { background: rgba(2, 6, 23, 0.7); backdrop-filter: blur(3px); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
dialog h3 { margin: 0; font-size: 1.1rem; }
#modalBody { display: flex; flex-direction: column; gap: 12px; max-height: 65vh; overflow: auto; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 0.82rem; color: var(--muted); }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: inherit; outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); }
.field textarea { min-height: 150px; resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 0.85rem; }
.field.check-field { flex-direction: row; align-items: center; gap: 10px; cursor: pointer; }
.field.check-field input { width: 18px; height: 18px; accent-color: var(--primary); }
.field.check-field > span { color: var(--text); font-size: 0.92rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.installment-box {
  padding: 12px; border-radius: 12px;
  background: rgba(99, 102, 241, 0.08); border: 1px solid rgba(99, 102, 241, 0.25);
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.preview { font-size: 0.85rem; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.preview table { width: 100%; border-collapse: collapse; }
.preview th, .preview td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--border); }
.preview th { color: var(--muted); font-weight: 600; background: var(--bg); }
.preview td.num { text-align: right; font-variant-numeric: tabular-nums; }
.tag { font-size: 0.72rem; padding: 2px 7px; border-radius: 99px; background: var(--card-2); }
.tag.incomes { background: rgba(56, 189, 248, 0.15); color: #7dd3fc; }
.tag.expenses { background: rgba(248, 113, 113, 0.15); color: #fca5a5; }
.tag.allocations { background: rgba(167, 139, 250, 0.15); color: #c4b5fd; }

.suggest-row { display: grid; grid-template-columns: 1fr 80px 110px; gap: 8px; align-items: center; }
.suggest-row input { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; width: 100%; }
.suggest-row .amt { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Auth ---------- */
.auth-screen {
  min-height: 100vh; display: grid; place-items: center; padding: 20px;
}
.auth-card {
  width: min(420px, 100%); padding: 24px; display: flex; flex-direction: column; gap: 16px;
}
.auth-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.auth-tabs { width: 100%; }
.auth-tabs button { flex: 1; padding: 8px 12px; font-weight: 600; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-submit { width: 100%; padding: 11px; margin-top: 4px; font-size: 0.95rem; }
.link-btn {
  border: 0; background: transparent; color: var(--muted); font-size: 0.85rem;
  padding: 4px; align-self: center; text-decoration: underline; text-underline-offset: 3px;
}
.link-btn:hover { color: var(--text); }
.user-badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 10px; font-size: 0.84rem; font-weight: 600;
  background: rgba(99, 102, 241, 0.14); color: #c7d2fe; border: 1px solid rgba(99, 102, 241, 0.3);
}
button.user-badge { cursor: pointer; font: inherit; font-size: 0.84rem; font-weight: 600; }
button.user-badge:hover, button.user-badge[aria-expanded="true"] { background: rgba(99, 102, 241, 0.24); }
.user-badge .caret { font-size: 0.7rem; opacity: 0.8; transition: transform 0.15s; }
button.user-badge[aria-expanded="true"] .caret { transform: rotate(180deg); }

.user-menu { position: relative; display: inline-flex; }
.user-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 50; min-width: 210px;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45); padding: 6px;
}
.user-dropdown-head {
  display: flex; flex-direction: column; gap: 2px; padding: 8px 10px 10px;
  border-bottom: 1px solid var(--border); margin-bottom: 6px;
}
.user-dropdown-head strong { font-size: 0.9rem; word-break: break-all; }
.dropdown-item {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 10px; border-radius: 8px; font-size: 0.88rem; font-weight: 600;
}
.dropdown-item:hover { background: var(--card-2); }
.dropdown-item.danger { color: var(--red); }
.dropdown-item.danger:hover { background: rgba(248, 113, 113, 0.12); }

.btn.danger { background: #dc2626; border-color: #dc2626; color: #fff; }
.btn.danger:hover { background: #ef4444; border-color: #ef4444; }

.confirm-modal { width: min(440px, calc(100vw - 24px)); }
.confirm-modal form { align-items: stretch; }
.confirm-modal h3 { text-align: center; }
.confirm-modal p { margin: 0; font-size: 0.9rem; line-height: 1.5; }
.confirm-icon {
  width: 52px; height: 52px; margin: 0 auto; border-radius: 50%; display: grid; place-items: center;
  font-size: 1.5rem; background: rgba(248, 113, 113, 0.14); border: 1px solid rgba(248, 113, 113, 0.35);
}
.confirm-list { margin: 0; padding-left: 20px; color: var(--muted); font-size: 0.88rem; line-height: 1.7; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: var(--card-2); border: 1px solid var(--border); padding: 10px 16px; border-radius: 12px;
  opacity: 0; pointer-events: none; transition: all 0.25s; z-index: 100; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: rgba(248, 113, 113, 0.6); color: #fecaca; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .grid.two { grid-template-columns: 1fr; }
  .topbar { justify-content: center; }
  .topbar .brand { display: none; }
  .top-actions { width: 100%; justify-content: center; }
  .top-actions .btn { flex: 1; padding: 8px 8px; font-size: 0.8rem; }
  .chart-box { height: 340px; }
}
@media (max-width: 420px) {
  .kpi { padding: 12px 14px; }
  .item { gap: 8px; padding: 9px 8px; }
  .row { grid-template-columns: 1fr; }
}
