:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e3e6ea;
  --text: #1c2128;
  --muted: #6b7380;
  --accent: #2f5bea;
  --accent-text: #ffffff;
  --pos: #15803d;
  --neg: #c2410c;
  --due: #d97706;               /* invoices still to be paid */
  --danger: #b91c1c;
  --in: #22a06b;
  --out: #e0703a;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1a1f25;
    --border: #2b323b;
    --text: #e6e9ed;
    --muted: #949ba6;
    --accent: #6d8dff;
    --accent-text: #0d1117;
    --pos: #4ade80;
    --neg: #fb923c;
    --due: #fbbf24;
    --danger: #f87171;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1rem; margin: 0; }
code, pre { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .85em; }
pre { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: .75rem; overflow-x: auto; }

/* --- Top bar --- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .75rem 1.5rem; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
.topbar nav { display: flex; gap: .25rem; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: var(--muted); padding: .35rem .7rem; border-radius: 6px; }
.topbar nav a:hover { background: var(--bg); text-decoration: none; color: var(--text); }
/* Radio buttons side by side (language, expense / income) */
.radio-switch { display: inline-flex; width: fit-content; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.radio-switch label { display: flex; align-items: center; gap: .3rem; padding: .25rem .6rem; cursor: pointer; font-size: .85rem; font-weight: 600; color: var(--muted); }
.radio-switch label + label { border-left: 1px solid var(--border); }
.radio-switch input { width: auto; margin: 0; accent-color: var(--accent); cursor: pointer; }
.radio-switch label:has(input:checked) { background: var(--bg); color: var(--text); }
.lang-switch { margin-left: auto; }
.topbar nav a.active { background: var(--bg); color: var(--text); font-weight: 600; }

main { max-width: 1100px; margin: 0 auto; padding: 1.5rem; }

/* --- Components --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; margin-bottom: 1.25rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; gap: 1rem; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.actions { display: flex; gap: .5rem; }
/* Title on the left, main action in the centre, secondary actions on the right */
.page-head-3 { display: grid; grid-template-columns: 1fr auto 1fr; }
.page-head-3 > .actions { justify-self: end; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1.25rem; }
.grid-2 .card { margin-bottom: 1.25rem; }

.btn {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .45rem .9rem; border-radius: 7px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .2rem .55rem; font-size: .85rem; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: .9rem; padding: .2rem .4rem; border-radius: 4px; }
.icon-btn:hover { color: var(--danger); background: var(--bg); }

.flashes { margin-bottom: 1rem; display: grid; gap: .4rem; }
.flash { padding: .6rem .9rem; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); }
.flash.ok { border-left: 4px solid var(--pos); }
.flash.error { border-left: 4px solid var(--danger); }

.muted { color: var(--muted); }
.small { font-size: .82rem; }
.nowrap { white-space: nowrap; }
.r { text-align: right; }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.empty { color: var(--muted); text-align: center; padding: 1.5rem 0; margin: 0; }
.dot { display: inline-block; width: .6rem; height: .6rem; border-radius: 50%; margin-right: .4rem; vertical-align: middle; }
.dot.in { background: var(--in); }
.dot.out { background: var(--out); }
.tag { display: inline-flex; align-items: center; font-size: .82rem; padding: .05rem .5rem; border-radius: 99px; background: var(--bg); border: 1px solid var(--border); }

/* --- Stats --- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
a.stat { color: inherit; text-decoration: none; }
a.stat:hover { border-color: var(--accent); }

.month-nav { display: flex; align-items: center; gap: .6rem; }
.month-nav span { min-width: 9rem; text-align: center; font-weight: 600; text-transform: capitalize; }

/* --- Accounts (dashboard) --- */
.account-list { list-style: none; margin: 0; padding: 0; }
.account-list a { display: flex; justify-content: space-between; padding: .55rem 0; border-bottom: 1px solid var(--border); color: var(--text); }
.account-list li:last-child a { border-bottom: 0; }
.account-list a:hover { text-decoration: none; color: var(--accent); }
.account-total { display: flex; justify-content: space-between; align-items: baseline; margin-top: .25rem; padding-top: .7rem; border-top: 2px solid var(--border); font-weight: 600; }
.account-total strong { font-size: 1.15rem; }

/* --- Category bars --- */
.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.bars li { display: grid; grid-template-columns: 9rem 1fr auto; gap: .75rem; align-items: center; font-size: .9rem; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: .55rem; background: var(--bg); border-radius: 99px; overflow: hidden; }
.bar { display: block; height: 100%; border-radius: 99px; }

/* --- History --- */
.legend { display: flex; gap: 1rem; font-size: .85rem; color: var(--muted); }
.history { display: grid; grid-template-columns: repeat(12, 1fr); gap: .4rem; height: 180px; }
.hist-col { display: flex; flex-direction: column; align-items: center; gap: .3rem; color: var(--muted); border-radius: 6px; padding: .3rem 0 0; }
.hist-col:hover { background: var(--bg); text-decoration: none; }
.hist-col.current { background: var(--bg); color: var(--text); font-weight: 600; }
.hist-bars { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 3px; }
.hbar { width: 32%; max-width: 14px; border-radius: 3px 3px 0 0; min-height: 1px; }
.hbar.in { background: var(--in); }
.hbar.out { background: var(--out); }
.hist-label { font-size: .75rem; }

/* --- Tables --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; padding: .5rem .6rem; border-bottom: 1px solid var(--border); }
th.r { text-align: right; }
td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--bg); }
tr.archived td { opacity: .55; }
tr.planned td { color: var(--muted); font-style: italic; }
tr.planned td .pos, tr.planned td .neg, tr.planned td.pos, tr.planned td.neg, tr.planned td a { color: var(--muted); }
tr.planned .tag { opacity: .6; }
.icon-btn.status-btn:hover { color: var(--pos); }
form.inline { display: inline; }
.icon-btn.edit-btn:hover { color: var(--accent); }
tr.editing td { padding-top: .3rem; padding-bottom: .3rem; }
tr.editing input, tr.editing select { padding: .3rem .45rem; font-size: .9rem; }
tr.editing input[name=amount] { text-align: right; max-width: 8rem; }
tr.editing input[name=date] { max-width: 10rem; }
tr.editing input[name=new_category] { margin-top: .3rem; }

/* --- Forms --- */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: .45rem .6rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=color] { padding: .1rem; height: 2.2rem; width: 3rem; }
input[type=checkbox], input[type=radio] { width: auto; }

.form { max-width: 640px; display: grid; gap: 1rem; padding: 1.5rem; }
.form label { display: grid; gap: .3rem; font-weight: 500; font-size: .9rem; }
.form label.check { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; }
.form-actions { display: flex; gap: .5rem; flex-wrap: wrap; padding-top: .25rem; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; width: fit-content; }
.segmented label { display: block; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: .45rem 1.2rem; cursor: pointer; font-weight: 500; }
.segmented input:checked + span { background: var(--accent); color: var(--accent-text); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

.tabs { display: flex; gap: .25rem; flex-wrap: wrap; margin-bottom: .75rem; border-bottom: 1px solid var(--border); }
.tabs a { color: var(--muted); padding: .45rem .8rem; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

.repeat { border: 1px solid var(--border); border-radius: 7px; padding: .6rem .9rem; }
.repeat summary { cursor: pointer; font-weight: 500; font-size: .9rem; }
.repeat[open] summary { margin-bottom: .75rem; }
.repeat p { margin: .6rem 0 0; }

.dir-switch label { display: flex; align-items: center; gap: .35rem; padding: .45rem .9rem; font-size: .95rem; font-weight: 600; }
.dir-switch label.out:has(input:checked) { color: var(--neg); }
.dir-switch label.out input { accent-color: var(--neg); }
.dir-switch label.in:has(input:checked) { color: var(--pos); }
.dir-switch label.in input { accent-color: var(--pos); }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.filters input[type=search] { flex: 2 1 200px; width: auto; }
.filters select { flex: 1 1 150px; width: auto; }
.filters .field { flex: 1 1 180px; display: flex; align-items: center; gap: .4rem; color: var(--muted); font-size: .9rem; }
.filters .field input { flex: 1; width: auto; min-width: 0; }
.filters .check-field { flex: 0 0 auto; }
.filters .check-field input { flex: 0 0 auto; }
.filters .amount-range { flex: 1 1 260px; white-space: nowrap; }
.filters .amount-range input { max-width: 7rem; }

.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.inline-form input[name=name] { flex: 1 1 200px; width: auto; }
.inline-form select { width: auto; }
.inline-form.plain { margin: 0; }
.cat-table .col-kind { width: 9rem; }
.cat-table .col-color { width: 5rem; }
.acc-table td:first-child input { min-width: 10rem; }
.acc-table .col-kind { width: 7rem; }
.acc-table select[name=kind] { min-width: 6.5rem; }
.acc-table .col-ops { width: 4.5rem; }
.acc-table .col-balance { width: 8rem; }
.acc-table .start-field { display: flex; gap: .3rem; align-items: center; }
.acc-table .start-field input[name=initial_amount] { width: 8rem; flex: 0 0 auto; }
.acc-table .start-field .date-fr input { min-width: 6.5rem; }
.acc-table .col-archived { width: 5rem; text-align: center; }
.acc-table td.c { text-align: center; }
.acc-table input[type=checkbox] { width: auto; }
.acc-table input.r { text-align: right; }
.acc-table .url-field { display: flex; align-items: center; gap: .3rem; }
.acc-table .url-field input { min-width: 10rem; }
.acc-table .url-field a { font-size: 1.05rem; text-decoration: none; }
.acc-table .url-field a:hover { color: var(--accent); }
.acc-table .new-body td { border-bottom: 2px solid var(--border); }

.summary { display: flex; align-items: center; gap: .75rem 2.5rem; flex-wrap: wrap; margin: 0 0 .75rem; font-variant-numeric: tabular-nums; color: var(--muted); }
.summary .sum-group { display: flex; align-items: center; gap: .5rem 1.25rem; flex-wrap: wrap; }
.summary .sum-group + .sum-group { padding-left: 2.5rem; border-left: 1px solid var(--border); }
.summary .sum-balance .balance { font-size: 1.05rem; color: var(--text); }
.summary .sel-total { color: var(--text); }
.summary .sum-tools { margin-left: auto; }
.summary .sum-group[hidden] { display: none; }
.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; }
.help ul { padding-left: 1.2rem; }

@media (max-width: 640px) {
  .topbar { padding: .6rem 1rem; gap: .6rem; }
  main { padding: 1rem; }
  .bars li { grid-template-columns: 6rem 1fr auto; }
  .history { height: 140px; gap: .15rem; }
}

/* --- Currency converter --- */
.convert-pair { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; align-items: end; }
.convert-result { border-top: 1px solid var(--border); padding-top: 1rem; display: grid; gap: .3rem; }
.convert-value { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
@media (max-width: 560px) { .convert-pair { grid-template-columns: 1fr; } }

/* Repeat end date + "forever" button */
.until-field { display: flex; gap: .4rem; align-items: center; }
.until-field .btn { white-space: nowrap; }

/* French date fields (DD/MM/YYYY text + calendar button) */
.date-fr { position: relative; display: inline-flex; align-items: center; gap: .2rem; width: 100%; }
.date-fr input[type=text] { flex: 1; min-width: 7.5rem; }
.date-fr .date-btn { flex: 0 0 auto; color: var(--muted); }
.date-fr .date-btn:hover { color: var(--accent); }
.date-fr .date-picker { position: absolute; right: 0; bottom: 0; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }

/* Row selection (bulk edit) */
table.tx th.sel, table.tx td.sel { width: 2rem; padding-right: 0; text-align: center; }
table.tx .sel input { width: auto; margin: 0; cursor: pointer; }
.bulk-edit .bulk-delete { margin-left: auto; }

/* Equivalent in another currency */
.summary .conv-form input[name=cur] { width: 17rem; max-width: 100%; padding: .25rem .5rem; font-size: .85rem; }
.conv-info { margin: -.5rem 0 1rem; }
table.tx td.conv { font-size: .9rem; }

/* Invoices: list on the left, details on the right */
.inv-layout { display: grid; grid-template-columns: minmax(18rem, 30rem) 1fr; gap: 1.25rem; align-items: start; }
@media (max-width: 760px) { .inv-layout { grid-template-columns: 1fr; } }
.inv-list { padding: .75rem; }
.inv-list-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap;
  margin-bottom: .5rem; padding-bottom: .5rem; border-bottom: 1px solid var(--border); }
.inv-filters { display: flex; gap: .4rem; flex-wrap: wrap; }
.inv-filters label { white-space: nowrap; }
.inv-list ul { list-style: none; margin: 0; padding: 0; max-height: 70vh; overflow-y: auto; }
.inv-cols, .inv-list li a { display: grid; grid-template-columns: .6rem auto 1fr 5.2rem; gap: .6rem; align-items: center; }
.inv-cols { padding: 0 .6rem .3rem; }
.inv-list li a {
  padding: .5rem .6rem; border-radius: 6px; color: var(--text); }
.inv-list li a:hover { background: var(--bg); text-decoration: none; }
.inv-list li a.selected { background: var(--bg); outline: 2px solid var(--accent); outline-offset: -2px; }
.inv-list .inv-num { font-weight: 600; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; }
.inv-list .inv-client { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-list .inv-date { font-size: .85rem; font-variant-numeric: tabular-nums; }
.dot-status { width: .6rem; height: .6rem; border-radius: 50%; background: var(--due); }
.dot-status.status-paid { background: var(--pos); }
.inv-detail h2 { margin: 0; }
.inv-detail-head { display: flex; align-items: center; gap: .75rem; margin-bottom: 1rem; }
.inv-form { max-width: none; padding: 0; }
.inv-form fieldset { border: 1px solid var(--border); border-radius: 8px; padding: .6rem 1rem 1rem; display: grid; gap: .6rem; }
.inv-form legend { padding: 0 .3rem; font-weight: 600; font-size: .9rem; }
.inv-form .inv-row3 { grid-template-columns: 1fr 1fr 1fr; }
.inv-form .inv-row2 { grid-template-columns: 3fr 2fr; }
@media (max-width: 560px) { .inv-form .inv-row3, .inv-form .inv-row2 { grid-template-columns: 1fr; } }
.pay-switch label { display: flex; align-items: center; gap: .35rem; padding: .45rem .9rem; font-size: .95rem; font-weight: 600; }
.pay-switch label.open:has(input:checked) { color: var(--due); }
.pay-switch label.open input { accent-color: var(--due); }
.due { color: var(--due); }
.pay-switch label.paid:has(input:checked) { color: var(--pos); }
.pay-switch label.paid input { accent-color: var(--pos); }
.inv-form fieldset p { margin: 0; }
.inv-form .inv-delete-btn { margin-left: auto; }
.inv-form .inv-tx { display: flex; align-items: center; gap: .3rem .6rem; flex-wrap: wrap; font-size: .9rem; }
.inv-account { display: flex; align-items: center; gap: .5rem; }
.inv-account label { display: flex; align-items: center; gap: .5rem; font-size: .9rem; color: var(--muted); white-space: nowrap; }
.inv-account select { width: auto; padding: .3rem .5rem; }
.tag.status-open { color: var(--due); }
.tag.status-paid { color: var(--pos); }
.tag.urssaf-todo { color: var(--neg); }
.tag.urssaf-done { color: var(--pos); }
.inv-form .pay-row { display: flex; align-items: center; gap: .6rem 1rem; flex-wrap: wrap; }
/* The date field may shrink (down to a date's width) so that "Declared on"
   stays on the switch's line; the row only wraps on narrow screens. */
.inv-form .pay-row .paid-date { display: flex; align-items: center; gap: .5rem; white-space: nowrap; flex: 1 1 auto; }
.inv-form .pay-row .paid-date > input, .inv-form .pay-row .paid-date .date-fr { width: 6.5rem; flex: 1 1 auto; max-width: 10rem; }
.inv-form .pay-row .paid-date .date-fr input[type=text] { min-width: 6.5rem; width: 0; }
.inv-form .pay-row .paid-date > input[type=date] { min-width: 8.75rem; }  /* native picker (EN): never truncated */
.inv-form .pay-switch label { padding-inline: .75rem; }
.urssaf-switch label.open:has(input:checked) { color: var(--neg); }
.urssaf-switch label.open input { accent-color: var(--neg); }
.radio-switch label:has(input:disabled) { opacity: .45; cursor: not-allowed; }
.radio-switch input:disabled { cursor: not-allowed; }

/* Search field coupled with a list (data-combo, see app.js) */
.combo { position: relative; display: block; }
.combo-list {
  position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 2px);
  margin: 0; padding: .25rem 0; list-style: none; max-height: 16rem; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .12);
}
.combo-list li { padding: .3rem .7rem; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.combo-list li:hover { background: var(--bg); }
.combo-list li.active { background: var(--accent); color: var(--accent-text); }
.conv-form .combo { display: inline-block; max-width: 100%; }
.conv-form .combo-list { font-size: .85rem; min-width: 100%; right: auto; }

/* Login page */
.login-form { max-width: 360px; margin: 3rem auto; }
.login-form h1 { margin: 0; }
