:root {
  --bg: #24232f;
  --sidebar: #383745;
  --card: #383745;
  --card-alt: #2c2b38;
  --border: #45445a;
  --text: #ffffff;
  --muted: #a9a8b8;
  --primary: #6c5dfc;
  --primary-hover: #5a4ae8;
  --pink: #ff6bac;
  --green: #2ee6a8;
  --danger: #ff6b7a;
  --radius: 10px;
  /* Chart series, validated for CVD separation on the card surface. */
  --c-needs: #8172ff;
  --c-wants: #cc4785;
  --c-savings: #16ad80;
  --c-power: #bf7d0c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Poppins", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: var(--primary); }

h1, h2, h3 { font-weight: 600; margin: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .7rem 1.4rem;
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font: inherit;
  font-size: .95rem;
  text-decoration: none;
  cursor: pointer;
  transition: background .15s;
}
.btn:hover { background: var(--primary-hover); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn-ghost { background: var(--card); }
.btn-ghost:hover { background: var(--border); }

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .3rem;
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}
.logo svg { width: 40px; height: 28px; fill: var(--primary); }

/* Landing */
.landing {
  min-height: 100vh;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 2rem 1rem;
}
.landing h1 { font-size: clamp(2.5rem, 8vw, 4.5rem); margin: 1.5rem 0 .75rem; }
.landing p { color: var(--muted); font-size: 1.15rem; margin: 0 0 2rem; }
.landing .actions { display: flex; gap: 1rem; justify-content: center; flex-wrap: wrap; }

/* Auth pages */
.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem 1rem;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 2rem;
}
.auth-card .logo { margin-bottom: 1.5rem; }
.auth-card h1 { font-size: 1.4rem; margin-bottom: 1.5rem; text-align: center; }
.field { display: flex; flex-direction: column; gap: .4rem; margin-bottom: 1.1rem; }
.field label { font-size: .9rem; color: var(--muted); }
.field input {
  padding: .75rem .9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-alt);
  color: var(--text);
  font: inherit;
}
.field input:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
.hint { font-size: .8rem; color: var(--muted); }
.auth-card .btn { width: 100%; margin-top: .5rem; }
.auth-switch { text-align: center; font-size: .9rem; color: var(--muted); margin: 1.25rem 0 0; }
.error {
  background: rgba(255, 107, 122, .12);
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 8px;
  padding: .65rem .9rem;
  font-size: .9rem;
  margin-bottom: 1rem;
}
.error[hidden] { display: none; }

/* App layout */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  padding: 1.75rem 0 1.5rem;
}
.sidebar .logo { margin-bottom: 2.5rem; }
.nav { display: flex; flex-direction: column; gap: .25rem; }
.nav a {
  display: flex;
  align-items: center;
  gap: .85rem;
  padding: .75rem 1.5rem;
  color: var(--muted);
  text-decoration: none;
  font-size: .95rem;
}
.nav a svg { width: 20px; height: 20px; fill: currentColor; }
.nav a.active { color: var(--primary); background: rgba(108, 93, 252, .12); border-left: 3px solid var(--primary); padding-left: calc(1.5rem - 3px); }
.sidebar-footer {
  margin-top: auto;
  padding: 0 1.25rem;
  display: flex;
  align-items: center;
  gap: .75rem;
}
.avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--primary);
  display: grid; place-items: center;
  font-weight: 600;
  flex-shrink: 0;
}
.user-email { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.logout {
  background: none; border: 0; color: var(--muted); cursor: pointer; padding: .4rem; border-radius: 6px;
}
.logout:hover { color: var(--text); background: var(--border); }
.logout svg { width: 20px; height: 20px; fill: currentColor; display: block; }

.main { flex: 1; padding: 2rem 2.25rem; min-width: 0; }
.main-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 2rem; flex-wrap: wrap; }
.main-header h1 { font-size: 1.6rem; }
.month-chip { background: var(--primary); padding: .6rem 1.2rem; border-radius: 8px; font-size: .95rem; text-transform: capitalize; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1.25rem; margin-bottom: 1.5rem; }
.stat {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1.4rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}
.stat-icon { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; flex-shrink: 0; }
.stat-icon svg { width: 22px; height: 22px; fill: currentColor; }
.stat-value { font-size: 1.4rem; font-weight: 600; }
.stat-label { font-size: .85rem; color: var(--muted); }
.i-purple { background: rgba(129, 114, 255, .25); color: var(--c-needs); }
.i-pink { background: rgba(204, 71, 133, .25); color: #e46aa4; }
.i-green { background: rgba(22, 173, 128, .22); color: #2ccf9a; }

.card { background: var(--card); border-radius: var(--radius); padding: 1.5rem; }
.card h2 { font-size: 1.1rem; margin-bottom: .75rem; }
.card p { color: var(--muted); margin: 0; }

.grid-top { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; margin-bottom: 1.25rem; }

.salary-input { position: relative; max-width: 320px; }
.salary-input input {
  width: 100%;
  padding: .8rem 2.5rem .8rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-alt);
  color: var(--text);
  font: inherit;
  font-size: 1.6rem;
  font-weight: 600;
}
.salary-input input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.salary-input input[aria-invalid="true"] { border-color: var(--danger); }
.salary-input .currency { position: absolute; right: 1rem; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 1.3rem; }
.card .salary-msg { min-height: 1.4em; margin-top: .6rem; font-size: .85rem; }
.salary-msg.is-error { color: var(--danger); }
.salary-msg.is-prefill { color: var(--pink); }
.salary-msg.is-saved { color: var(--green); }

.split-body { display: flex; align-items: center; gap: 2rem; }
.donut { width: 130px; height: 130px; transform: rotate(-90deg); flex-shrink: 0; }
.donut circle { fill: none; stroke-width: 6; }
.donut-track { stroke: var(--card-alt); }
.seg-needs { stroke: var(--c-needs); }
.seg-wants { stroke: var(--c-wants); }
.seg-savings { stroke: var(--c-savings); }
.legend { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .7rem; color: var(--muted); }
.legend b { color: var(--text); font-weight: 600; margin-left: .3rem; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 4px; margin-right: .6rem; vertical-align: -1px; }
.d-needs { background: var(--c-needs); }
.d-wants { background: var(--c-wants); }
.d-savings { background: var(--c-savings); }
.d-power { background: var(--c-power); }

/* Charts */
.grid-charts { display: grid; grid-template-columns: minmax(300px, 1fr) 2fr; gap: 1.25rem; margin-bottom: 1.25rem; }
#sankey { overflow-x: auto; }
@media (max-width: 1250px) {
  .grid-charts { grid-template-columns: 1fr; }
  .grid-charts .pie-body { flex-direction: row; align-items: center; gap: 2rem; }
  .grid-charts .pie-body .chart-wrap { flex-shrink: 0; }
}
@media (max-width: 560px) {
  .grid-charts .pie-body { flex-direction: column; }
}
.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }
.pie-slice { stroke: var(--card); stroke-width: 2; cursor: pointer; transition: transform .15s; transform-origin: 100px 100px; }
.pie-slice:hover, .pie-slice.is-active { transform: scale(1.04); }
.pie-label { fill: #fff; font-size: 11px; font-weight: 600; pointer-events: none; }
.pie-body { display: flex; flex-direction: column; align-items: center; gap: 1rem; }
.pie-body[hidden], .chart-empty[hidden] { display: none; }
.pie-body .chart-wrap { width: 200px; max-width: 100%; }
.chart-legend { list-style: none; margin: 0; padding: 0; width: 100%; display: flex; flex-direction: column; gap: .45rem; font-size: .88rem; }
.chart-legend li { display: grid; grid-template-columns: auto 1fr auto auto; gap: .6rem; align-items: center; color: var(--muted); }
.chart-legend b { color: var(--text); font-weight: 600; text-align: right; }
.chart-legend .pct { color: var(--muted); min-width: 3.2em; text-align: right; }
.chart-legend .dot { margin-right: 0; }
.chart-tip {
  position: absolute;
  pointer-events: none;
  background: #1c1b25;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .45rem .7rem;
  font-size: .82rem;
  white-space: nowrap;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .35);
  transform: translate(-50%, calc(-100% - 12px));
  z-index: 5;
}
.chart-tip[hidden] { display: none; }
.chart-tip b { display: block; font-weight: 600; }
.chart-tip span { color: var(--muted); }
.sankey-link { fill-opacity: .35; cursor: pointer; transition: fill-opacity .15s; }
.sankey-link:hover, .sankey-link.is-active { fill-opacity: .7; }
.sankey-node { rx: 2; }
.sankey-label { fill: var(--text); font-size: 11px; }
.sankey-label tspan.amount { fill: var(--muted); }
.sankey-label.lvl1 { font-weight: 600; paint-order: stroke; stroke: var(--card); stroke-width: 3px; }
.chart-empty { color: var(--muted); font-size: .9rem; margin: 1rem 0 0; }

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }
.btn-small { padding: .45rem .9rem; font-size: .85rem; }
.card .btn-ghost { background: var(--card-alt); }
.card .btn-ghost:hover { background: var(--border); }

.split-form[hidden], .split-body[hidden], .btn[hidden] { display: none; }
.pct-fields { display: flex; flex-direction: column; gap: .55rem; }
.pct-fields label { display: flex; align-items: center; color: var(--muted); font-size: .95rem; }
.pct-input { margin-left: auto; display: inline-flex; align-items: center; gap: .4rem; color: var(--muted); }
.pct-input input {
  width: 72px;
  padding: .4rem .6rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--card-alt);
  color: var(--text);
  font: inherit;
  text-align: right;
}
.pct-input input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.card .split-total { margin: .75rem 0 .2rem; font-size: .9rem; font-weight: 500; }
.split-total.is-ok { color: var(--green); }
.split-total.is-error { color: var(--danger); }
.form-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: .9rem; }

.table-wrap { overflow-x: auto; margin-top: .5rem; }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { padding: .85rem 1rem; text-align: right; white-space: nowrap; }
.table th:first-child, .table td:first-child { text-align: left; }
.table thead th { color: var(--muted); font-weight: 500; font-size: .82rem; border-bottom: 1px solid var(--border); }
.table tbody tr:nth-child(odd) { background: var(--card-alt); }
.table tfoot th, .table tfoot td { font-weight: 600; border-top: 1px solid var(--border); }
.pos { color: var(--danger); font-weight: 600; }
.neg { color: var(--green); font-weight: 600; }

.grid-power { display: grid; grid-template-columns: minmax(240px, 1fr) 2fr; gap: 1.25rem; margin-bottom: 1.25rem; }
.power-card { display: flex; flex-direction: column; }
.power-value { font-size: 2.4rem; font-weight: 600; color: var(--green); margin: .4rem 0 .2rem; }
.power-value.is-negative { color: var(--danger); }
.card .power-detail { font-size: .85rem; }
.alert, .notice { border-radius: 8px; padding: .65rem .9rem; font-size: .88rem; margin-top: 1rem; }
.alert { background: rgba(255, 107, 122, .12); border: 1px solid var(--danger); color: var(--danger); }
.notice { background: rgba(108, 93, 252, .12); border: 1px solid var(--primary); color: #c9c3ff; margin: 0 0 .9rem; }
.notice a { color: #fff; }
.alert[hidden], .notice[hidden] { display: none; }

.transfer-list { list-style: none; margin: .5rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.transfer { display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: .75rem; padding: .7rem .9rem; border-radius: 8px; background: var(--card-alt); }
.transfer input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--green); cursor: pointer; }
.transfer-name { font-weight: 500; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.transfer-detail { display: block; color: var(--muted); font-size: .8rem; margin-top: .15rem; }
.transfer-amount { font-weight: 600; white-space: nowrap; }
.transfer.is-done .transfer-name, .transfer.is-done .transfer-amount { color: var(--muted); text-decoration: line-through; }
.transfer.is-main { background: transparent; border: 1px dashed var(--border); }
.transfer-icon { width: 20px; height: 20px; fill: var(--muted); }
.transfer.is-total { background: transparent; font-weight: 600; border-top: 1px solid var(--border); border-radius: 0; margin-top: .2rem; }

.head-actions { display: flex; gap: .6rem; }

.month-nav { display: flex; align-items: center; gap: .4rem; }
.month-nav .icon-btn svg { width: 22px; height: 22px; }
.month-chip { min-width: 150px; text-align: center; }
.month-nav .btn[hidden] { display: none; }
.month-actions { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin: -1rem 0 1.5rem; }
.month-actions[hidden] { display: none; }
.page-intro { color: var(--muted); margin: -1rem 0 1.5rem; max-width: 640px; }
.grid-top .power-value { color: var(--text); }
.month-actions .btn { background: var(--card); }
.month-actions .btn:hover { background: var(--border); }
.month-actions .salary-msg { min-height: 0; margin: 0; font-size: .85rem; }
.stats + .card, .card + .card { margin-top: 1.25rem; }

.exp-group { padding: 1rem 0; border-top: 1px solid var(--border); }
.exp-group:first-child { border-top: 0; padding-top: .25rem; }
.exp-group-head { display: flex; align-items: center; gap: .1rem; margin-bottom: .6rem; }
.exp-group-head h3 { font-size: .98rem; font-weight: 500; }
.exp-total { margin-left: auto; font-weight: 600; }
.exp-list { list-style: none; margin: 0; padding: 0; }
.exp-list li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 1rem;
  align-items: center;
  padding: .6rem .9rem;
  border-radius: 6px;
  font-size: .92rem;
}
.exp-list li:nth-child(odd) { background: var(--card-alt); }
.exp-account { color: var(--muted); font-size: .82rem; }
.exp-amount { min-width: 100px; text-align: right; font-weight: 500; }
.card .exp-empty { font-size: .88rem; padding: .3rem .9rem; }

.exp-edit-list { display: flex; flex-direction: column; gap: .5rem; }
.exp-edit-row { display: grid; grid-template-columns: 1fr 130px 180px auto; gap: .5rem; align-items: center; }
.exp-edit-row input, .exp-edit-row select, .field-inline input, .field-inline select {
  width: 100%;
  padding: .55rem .7rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--card-alt);
  color: var(--text);
  font: inherit;
  font-size: .9rem;
}
.exp-in-amount { text-align: right; }
.exp-edit-row :focus, .field-inline :focus { outline: 2px solid var(--primary); border-color: transparent; }
[aria-invalid="true"] { border-color: var(--danger) !important; }
.exp-add { margin-top: .6rem; }

.icon-btn { background: none; border: 0; color: var(--muted); cursor: pointer; padding: .45rem; border-radius: 6px; display: inline-grid; place-items: center; }
.icon-btn:hover { color: var(--text); background: var(--border); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; }

/* Accounts page */
.narrow { max-width: 640px; }
.field-inline { display: flex; gap: .6rem; }
.field-inline input { flex: 1; }
.acc-list { list-style: none; margin: 1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.acc-item { display: flex; align-items: center; gap: .75rem; padding: .7rem .9rem; border-radius: 8px; background: var(--card-alt); }
.acc-name { font-weight: 500; }
.acc-count { color: var(--muted); font-size: .82rem; margin-left: auto; }
.acc-item form { display: flex; gap: .5rem; flex: 1; align-items: center; flex-wrap: wrap; }
.acc-item form input, .acc-item form select { flex: 1; min-width: 140px; }
.badge { font-size: .72rem; padding: .15rem .55rem; border-radius: 999px; background: rgba(108, 93, 252, .25); color: #b9b1ff; }
.badge-pink { background: rgba(255, 107, 172, .2); color: var(--pink); }
.roles { display: flex; flex-direction: column; gap: 1rem; margin-top: 1.1rem; }
.role-field { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.role-field span { flex: 1; min-width: 200px; font-weight: 500; }
.role-field small { display: block; color: var(--muted); font-weight: 400; font-size: .8rem; }
.role-field select, .acc-item form select {
  padding: .55rem .7rem; border-radius: 6px; border: 1px solid var(--border);
  background: var(--card-alt); color: var(--text); font: inherit; font-size: .9rem; min-width: 200px;
}
.card.narrow + .card.narrow { margin-top: 1.25rem; }
.acc-reassign { flex-basis: 100%; color: var(--muted); font-size: .85rem; margin: 0; }

@media (max-width: 900px) {
  .grid-top, .grid-power, .grid-charts { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; align-items: center; padding: .75rem 1rem; gap: 1rem; }
  .sidebar .logo { flex-direction: row; margin: 0; font-size: 1.1rem; }
  .sidebar .logo svg { width: 28px; height: 20px; }
  .nav { display: none; }
  .sidebar-footer { margin: 0 0 0 auto; padding: 0; }
  .user-email { display: none; }
  .main { padding: 1.5rem 1rem; }
  .nav { display: flex; flex-direction: row; gap: 0; }
  .nav a { padding: .5rem .6rem; font-size: 0; }
  .nav a.active { border-left: 0; padding-left: .6rem; }
  .exp-edit-row { grid-template-columns: 1fr 1fr auto; }
  .exp-in-label { grid-column: 1 / -1; }
  .exp-list li { grid-template-columns: 1fr auto; }
  .exp-account { grid-row: 2; }
}
