* { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
body { font-family: system-ui, sans-serif; margin: 0; background: #f4f5f7; color: #1c1e21; }
h1 { font-size: 1.3rem; margin: 0; }

.login { max-width: 320px; margin: 15vh auto; background: #fff; padding: 2rem;
  border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,.08); display: flex;
  flex-direction: column; gap: .75rem; }
.login input, .login button { padding: .6rem; font-size: 1rem; border-radius: 6px;
  border: 1px solid #ccc; }
.login button { background: #2d6cdf; color: #fff; border: none; cursor: pointer; }
.error { color: #c0392b; margin: 0; }

.topbar { display: flex; justify-content: space-between; align-items: center;
  gap: 1rem; padding: .8rem 1.5rem; background: #fff; border-bottom: 1px solid #e2e4e8; }
.logout { color: #2d6cdf; text-decoration: none; white-space: nowrap; }

main { max-width: 1120px; margin: 1rem auto; padding: 0 1rem;
  display: flex; flex-direction: column; gap: 1rem; }

.layout { display: grid; gap: 1.25rem; grid-template-columns: 1fr; }
.col-main, .col-side { display: flex; flex-direction: column; gap: 1rem; min-width: 0; }
@media (min-width: 900px) {
  .layout { grid-template-columns: 1.6fr 1fr; align-items: start; }
  .col-side { position: sticky; top: 1rem; }
}
.card { background: #fff; border-radius: 10px; padding: 1rem;
  box-shadow: 0 1px 4px rgba(0,0,0,.06); }

#filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
#add-form { display: flex; flex-direction: column; gap: .6rem; align-items: stretch; }
.af-row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
#add-form #category { flex: 1; min-width: 140px; }
select, select option { font-size: .95rem; }
#category, #category option, #f-category, #f-category option,
.cat-edit, .cat-edit option { font-size: 1rem; }
#add-form input, #add-form select, #filters input, #filters select {
  padding: .5rem; border: 1px solid #ccc; border-radius: 6px; font-size: .95rem; }
#amount { width: 110px; }
button { cursor: pointer; }
#add-form button[type=submit] { background: #27ae60; color: #fff; border: none;
  padding: .5rem 1rem; border-radius: 6px; }
#new-cat, #new-acct { background: #eef0f3; color: #555; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .45rem .6rem; font-size: .85rem; }
#new-cat:hover, #new-acct:hover { background: #e2e5ea; }

.account-toggle { display: inline-flex; flex-wrap: wrap; gap: .35rem; }
.acct { background: #fff; color: #2d6cdf; border: 1px solid #2d6cdf;
  border-radius: 6px; padding: .5rem .9rem; display: inline-flex; align-items: center; gap: .4rem; }
.acct.active { background: #2d6cdf; color: #fff; }
.acct-del { font-size: 1rem; line-height: 1; opacity: 0; transition: opacity .15s; }
.acct:hover .acct-del { opacity: .55; }
.acct-del:hover { opacity: 1; color: #c0392b; }
.acct.active .acct-del:hover { color: #ffd2cc; }
.topbar-right { display: flex; align-items: center; gap: 1rem; white-space: nowrap; }
.uname { color: #2d6cdf; font-size: .9rem; text-decoration: none;
  border: 1px solid #cdd9f2; background: #f3f7ff; padding: .28rem .7rem;
  border-radius: 999px; display: inline-flex; align-items: center; gap: .35rem; }
.uname::before { content: "👤"; font-size: .85em; }
.uname:hover { background: #e6efff; border-color: #aebfe8; }
.uname.active { border-color: #2d6cdf; }

.empty { text-align: center; color: #999; padding: 1.5rem; margin: 0; }
.empty-row td { text-align: center !important; color: #999; padding: 1.5rem; }

.msg { color: #27ae60; font-size: .9rem; }

.charts { display: grid; grid-template-columns: 1fr; gap: 1rem; }
.chart-box { position: relative; height: 260px; min-width: 0; }

.table-wrap { overflow-x: auto; }
#list { width: 100%; border-collapse: collapse; }
#list th, #list td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid #eee; }
#list .amt { text-align: right; font-variant-numeric: tabular-nums; }
.del { background: none; border: none; color: #c0392b; font-size: 1.1rem; }
.cat-edit { padding: .25rem; border: 1px solid transparent; border-radius: 4px;
  background: transparent; font: inherit; color: inherit; cursor: pointer; }
.cat-edit:hover, .cat-edit:focus { border-color: #ccc; background: #fff; }

/* category colors */
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%;
  vertical-align: middle; margin-right: .4rem; cursor: pointer; }
.cat-cell { display: flex; align-items: center; }

.topbar-left { display: flex; align-items: center; gap: 1.3rem; }
.brand { display: inline-flex; align-items: center; line-height: 0; }
.brand-logo { width: 30px; height: 30px; display: block; }
.navlink { color: #2d6cdf; text-decoration: none; white-space: nowrap; font-size: .95rem; }
.navlink.active { color: #1c1e21; font-weight: 600; }

/* categories page */
.palette { display: inline-flex; flex-wrap: wrap; gap: .3rem; }
.swatch { width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  border: 2px solid transparent; }
.swatch.sel { border-color: #1c1e21; }
#cat-list .cat-name-cell { font-weight: 500; }
#cat-list .rename { background: #eef0f3; color: #555; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .25rem .5rem; font-size: .8rem; }
.popover { position: absolute; z-index: 10; background: #fff; padding: .5rem;
  border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.18);
  display: inline-flex; flex-wrap: wrap; gap: .3rem; max-width: 180px; margin-top: .3rem; }
#cat-list td:first-child { position: relative; }

/* budgets panel */
.card-title { margin: 0 0 .6rem; font-size: 1rem; }
.budget-row { margin: .55rem 0; }
.budget-head { display: flex; align-items: center; gap: .4rem; font-size: .9rem; }
.budget-name { font-weight: 500; }
.budget-nums { margin-left: auto; color: #555; font-variant-numeric: tabular-nums; }
.bar { background: #eef0f3; border-radius: 6px; height: 8px; margin-top: .25rem; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; transition: width .2s; }
.bar-fill.ok   { background: #27ae60; }
.bar-fill.warn { background: #f1c40f; }
.bar-fill.over { background: #c0392b; }
.budget-totals { font-size: .88rem; color: #555; margin-bottom: .6rem; }
.budget-nums .left, .budget-totals .left { color: #27ae60; }
.budget-nums .over, .budget-totals .over { color: #c0392b; }
.budget-input { width: 90px; padding: .3rem; border: 1px solid #ccc; border-radius: 4px; }

#cat-list .sub-row .cat-name-cell { padding-left: 1.4rem; color: #444; }
#cat-parent { padding: .5rem; border: 1px solid #ccc; border-radius: 6px; font-size: .95rem; }

/* categories page polish */
.page-hint { color: #777; font-size: .85rem; margin: .2rem 0 .2rem; }

#cat-list th, #cat-list td { padding: .55rem .65rem; }
#cat-list tbody tr:hover { background: #f7f9fc; }
#cat-list .dot { transition: box-shadow .15s; }
#cat-list .dot:hover { box-shadow: 0 0 0 3px rgba(45,108,223,.25); }

.budget-cell { white-space: nowrap; text-align: right; }
.budget-cell .eur { color: #999; margin-right: .15rem; }
.budget-input { width: 78px; text-align: right; }
.count-cell { text-align: right; }
.count-cell.zero { color: #bbb; }

.cat-actions { white-space: nowrap; text-align: right; opacity: .35; transition: opacity .15s; }
#cat-list tbody tr:hover .cat-actions { opacity: 1; }
.cat-actions button { background: #eef0f3; color: #555; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .25rem .5rem; font-size: .85rem; cursor: pointer; }
.cat-actions .del { color: #c0392b; }
.cat-actions .addsub { color: #2e7d32; }
.cat-actions button:hover { background: #e2e5ea; }

/* inline add */
.add-cat-row td { padding: .4rem .65rem; }
.add-cat-row .add-toggle { background: #eef6ee; color: #2e7d32; border: 1px dashed #b6d6b6;
  border-radius: 6px; padding: .4rem .8rem; font-size: .9rem; cursor: pointer; }
.add-cat-row .add-toggle:hover { background: #e3f0e3; }
.add-edit-row .add-name { padding: .4rem; border: 1px solid #bcd0f5; border-radius: 6px;
  font-size: .95rem; width: 100%; max-width: 240px; }
.add-edit-row .add-swatch { cursor: pointer; box-shadow: 0 0 0 2px rgba(45,108,223,.35); }
.add-edit-row .save { background: #27ae60; color: #fff; border: none;
  border-radius: 6px; padding: .3rem .7rem; cursor: pointer; }
.add-edit-row .cancel { background: #eef0f3; color: #777; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .3rem .55rem; cursor: pointer; }

/* account page */
.account-form { display: flex; flex-direction: column; gap: .6rem; max-width: 320px; }
.account-form input { padding: .55rem; border: 1px solid #ccc; border-radius: 6px; font-size: .95rem; }
.account-form button { background: #2d6cdf; color: #fff; border: none; border-radius: 6px;
  padding: .55rem; font-size: .95rem; cursor: pointer; }
.msg.err { color: #c0392b; }

/* accounts manager (Account page) */
.acct-manage { list-style: none; padding: 0; margin: 0 0 .7rem; display: flex; flex-direction: column; gap: .2rem; }
.acct-manage li { display: flex; justify-content: space-between; align-items: center;
  padding: .35rem .2rem; border-bottom: .5px solid #eee; }
.del-acct { background: none; border: none; color: #c0392b; cursor: pointer; font-size: 1rem; }
.acct-add { display: flex; gap: .5rem; }
.acct-add input { flex: 1; }
[data-theme="dark"] .acct-manage li { border-bottom-color: #2a2e35; }

/* month nav */
.month-nav { display: inline-flex; align-items: center; gap: .3rem; }
.month-nav button { background: #fff; border: 1px solid #ccc; border-radius: 6px;
  padding: .4rem .6rem; cursor: pointer; }
.month-nav button:hover { background: #f0f2f5; }
.month-nav #m-label { min-width: 120px; text-align: center; font-size: .92rem; }
.month-nav #m-all { color: #2d6cdf; }

/* inline edit */
.editable { cursor: pointer; }
#list tbody .editable:hover { background: #eef4ff; }
.cell-edit { width: 100%; max-width: 150px; padding: .25rem; border: 1px solid #2d6cdf;
  border-radius: 4px; font: inherit; }

/* per-account totals */
.acct-totals { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .5rem; }
.acct-chip { background: #f1f3f5; border-radius: 999px; padding: .2rem .65rem; font-size: .85rem; }
.acct-chip.grand { background: #e8f0fe; }

/* category drag + collapse */
.grip { cursor: grab; color: #bbb; margin-right: .4rem; user-select: none; }
.grip:active { cursor: grabbing; }
#cat-list tr.dragging { opacity: .5; background: #eef4ff; }
.caret { cursor: pointer; color: #888; margin-right: .35rem; user-select: none; display: inline-block; width: 1em; }
.caret-spacer { display: inline-block; width: calc(1em + .35rem); }

/* category icons */
.cat-icon { display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 50%; font-size: .9rem; cursor: pointer;
  vertical-align: middle; }
.cat-icon-sm { margin-right: .4rem; font-size: 1rem; vertical-align: middle; }
.popover.appearance { max-width: 250px; }
.pop-label { font-size: .72rem; color: #888; margin: .15rem 0 .3rem; text-transform: uppercase; letter-spacing: .03em; }
.icon-grid { display: inline-flex; flex-wrap: wrap; gap: .25rem; margin-bottom: .5rem; }
.icon-opt { width: 28px; height: 28px; display: inline-flex; align-items: center;
  justify-content: center; border-radius: 6px; cursor: pointer; font-size: 1.05rem; }
.icon-opt:hover { background: #eef0f3; }
.icon-opt.sel { outline: 2px solid #2d6cdf; }

/* search + export + move */
.filters-toggle { align-self: flex-start; background: #eef0f3; color: #555; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .4rem .7rem; font-size: .9rem; cursor: pointer; }
.filters-toggle:hover { background: #e2e5ea; }
.filters-toggle.active { background: #e6efff; color: #2d6cdf; border-color: #cdd9f2; }
.fcaret { font-size: .8em; }
#filters[hidden] { display: none; }
#f-q { padding: .5rem; border: 1px solid #ccc; border-radius: 6px; font-size: .95rem; }
#export { background: #eef0f3; color: #555; border: 1px solid #d8dbe0; border-radius: 6px; padding: .5rem .8rem; cursor: pointer; }
#export:hover { background: #e2e5ea; }
.cat-actions .move { color: #2d6cdf; }
.move-pop { display: flex; flex-direction: column; gap: .15rem; max-height: 240px; overflow-y: auto; min-width: 140px; }
.move-opt { text-align: left; background: #fff; border: none; border-radius: 4px; padding: .35rem .5rem; cursor: pointer; font: inherit; }
.move-opt:hover { background: #eef4ff; }

.cat-toolbar { display: flex; gap: .5rem; margin: .2rem 0 .6rem; }
.cat-toolbar button { background: #eef0f3; color: #555; border: 1px solid #d8dbe0;
  border-radius: 6px; padding: .35rem .7rem; font-size: .85rem; cursor: pointer; }
.cat-toolbar button:hover { background: #e2e5ea; }

/* theme toggle */
.theme-toggle { background: transparent; border: 1px solid #d8dbe0; border-radius: 6px;
  padding: .25rem .5rem; cursor: pointer; font-size: 1rem; line-height: 1; }
.theme-toggle:hover { background: #f0f2f5; }

/* toasts */
#toast-wrap { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column;
  gap: .5rem; z-index: 1000; }
.toast { background: #1c1e21; color: #fff; padding: .6rem .9rem; border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); font-size: .9rem; opacity: 1; transition: opacity .3s; max-width: 300px; }
.toast.err { background: #c0392b; }
.toast.out { opacity: 0; }

/* ---- dark mode ---- */
[data-theme="dark"] body { background: #16181c; color: #e6e8eb; }
[data-theme="dark"] .card, [data-theme="dark"] .topbar,
[data-theme="dark"] .login, [data-theme="dark"] .popover { background: #1f2227; border-color: #343a42; }
[data-theme="dark"] .topbar { border-bottom-color: #343a42; }
[data-theme="dark"] h1, [data-theme="dark"] .budget-name,
[data-theme="dark"] .cat-name-cell { color: #e6e8eb; }
[data-theme="dark"] #cat-list .sub-row .cat-name-cell { color: #b8bdc4; }
[data-theme="dark"] input, [data-theme="dark"] select, [data-theme="dark"] .cell-edit,
[data-theme="dark"] .budget-input, [data-theme="dark"] .add-name,
[data-theme="dark"] #f-q, [data-theme="dark"] #cat-parent {
  background: #1a1d22; color: #e6e8eb; border-color: #343a42; }
[data-theme="dark"] #list th, [data-theme="dark"] #list td,
[data-theme="dark"] #cat-list th, [data-theme="dark"] #cat-list td { border-bottom-color: #2a2e35; }
[data-theme="dark"] #list tbody .editable:hover,
[data-theme="dark"] #list tbody tr:hover,
[data-theme="dark"] #cat-list tbody tr:hover,
[data-theme="dark"] .move-opt:hover, [data-theme="dark"] .icon-opt:hover { background: #23272e; }
[data-theme="dark"] .page-hint, [data-theme="dark"] .budget-totals, [data-theme="dark"] .budget-nums,
[data-theme="dark"] .count-cell, [data-theme="dark"] .empty, [data-theme="dark"] .hint,
[data-theme="dark"] .color-label, [data-theme="dark"] .pop-label { color: #9aa0a8; }
[data-theme="dark"] .acct-chip { background: #2a2e35; color: #cdd2d8; }
[data-theme="dark"] .acct-chip.grand { background: #26344d; }
[data-theme="dark"] .bar { background: #2a2e35; }
[data-theme="dark"] #new-cat, [data-theme="dark"] #new-acct, [data-theme="dark"] .cat-actions button,
[data-theme="dark"] #export, [data-theme="dark"] .cat-toolbar button,
[data-theme="dark"] .month-nav button, [data-theme="dark"] .theme-toggle,
[data-theme="dark"] .filters-toggle {
  background: #2a2e35; color: #cdd2d8; border-color: #3a4049; }
[data-theme="dark"] .filters-toggle.active { background: #222a38; color: #7aa2ff; border-color: #3a4a7a; }
[data-theme="dark"] .add-cat-row .add-toggle { background: #1e2a1e; color: #7fc77f; border-color: #3a553a; }
[data-theme="dark"] .acct { background: #1f2227; color: #7aa2ff; border-color: #3a4a7a; }
[data-theme="dark"] .acct.active { background: #2d6cdf; color: #fff; }
[data-theme="dark"] .navlink, [data-theme="dark"] .logout { color: #7aa2ff; }
[data-theme="dark"] .navlink.active { color: #e6e8eb; }
[data-theme="dark"] .uname { background: #222a38; border-color: #3a4a7a; color: #7aa2ff; }

/* ---- mobile ---- */
@media (max-width: 600px) {
  /* topbar: title row, then nav, then account — each full width, tidy */
  .topbar { flex-wrap: wrap; gap: .4rem; padding: .55rem .8rem; }
  .topbar-left { width: 100%; gap: 1rem; flex-wrap: wrap; row-gap: .25rem; align-items: baseline; }
  .topbar-right { width: 100%; justify-content: flex-end; gap: .6rem; }
  h1 { font-size: 1.15rem; }
  .navlink { font-size: .92rem; }
  .theme-toggle { padding: .2rem .4rem; }
  main { padding: 0 .6rem; }
  .card { padding: .85rem; }
  #list th, #list td { padding: .45rem .4rem; font-size: .88rem; }
  .acct-totals { gap: .3rem; }
  .acct-chip { font-size: .8rem; }

  /* add form: big, full-width tap targets */
  #add-form input, #add-form select { font-size: 1rem; padding: .6rem; min-height: 44px; }
  .af-row { gap: .5rem; }
  .af-row > input, .af-row > select { flex: 1; min-width: 0; }
  #add-form #amount { width: auto; flex: 1; }
  #add-form #spent_on { width: 100%; }
  .account-toggle { width: 100%; }
  .account-toggle .acct { flex: 1; justify-content: center; padding: .6rem; }
  #add-form button[type=submit] { width: 100%; padding: .65rem; font-size: 1rem; }

  /* categories: drop Expenses col + grip, shrink controls so actions fit */
  #cat-list th:nth-child(4), #cat-list td:nth-child(4) { display: none; }
  .th-sub { display: none; }
  #cat-list .grip { display: none; }
  #cat-list th, #cat-list td { padding: .4rem .3rem; }
  .budget-cell .budget-input { width: 60px; }
  .cat-actions { white-space: nowrap; }
  .cat-actions button { padding: .2rem .35rem; font-size: .78rem; opacity: 1; }
  .cat-toolbar button { padding: .3rem .55rem; }
}

/* custom dropdown */
.dd-wrap { position: relative; display: inline-flex; }
.af-row > .dd-wrap, #filters .dd-wrap, .cat-cell .dd-wrap { flex: 1; min-width: 0; }
.dd-btn { width: 100%; text-align: left; display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; background: #fff; border: 1px solid #ccc; border-radius: 6px; padding: .5rem .6rem;
  font-size: 1rem; cursor: pointer; min-height: 42px; color: inherit; }
.dd-btn .dd-car { color: #888; font-size: .8em; }
.dd-pop { background: #fff; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.18);
  max-height: 60vh; overflow-y: auto; z-index: 1100; padding: .25rem; }
.dd-opt { padding: .6rem .7rem; border-radius: 6px; cursor: pointer; font-size: 1rem; white-space: nowrap; }
.dd-opt:hover { background: #eef4ff; }
.dd-opt.sel { background: #e6efff; font-weight: 500; }
.rename-input { width: 100%; max-width: 200px; padding: .3rem; border: 1px solid #2d6cdf; border-radius: 4px; font: inherit; }

/* confirm modal */
.modal-ov { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex;
  align-items: center; justify-content: center; z-index: 2000; padding: 1rem; }
.modal { background: #fff; border-radius: 12px; padding: 1.2rem 1.3rem; max-width: 340px; width: 100%;
  box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal p { margin: 0 0 1rem; }
.modal-btns { display: flex; justify-content: flex-end; gap: .6rem; }
.modal-btns button { border-radius: 6px; padding: .5rem .9rem; cursor: pointer; border: 1px solid #d8dbe0; background: #eef0f3; color: #333; }
.modal-btns .m-ok { background: #2d6cdf; color: #fff; border-color: #2d6cdf; }

[data-theme="dark"] .dd-btn { background: #1a1d22; color: #e6e8eb; border-color: #343a42; }
[data-theme="dark"] .dd-pop { background: #1f2227; border-color: #343a42; }
[data-theme="dark"] .dd-opt:hover { background: #23272e; }
[data-theme="dark"] .dd-opt.sel { background: #222a38; }
[data-theme="dark"] .modal { background: #1f2227; color: #e6e8eb; }
[data-theme="dark"] .modal-btns button { background: #2a2e35; color: #cdd2d8; border-color: #3a4049; }
[data-theme="dark"] .modal-btns .m-ok { background: #2d6cdf; color: #fff; }
