:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #1b1b1a;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3eefb;
  --income: #2a78d6;
  --expense: #eb6834;
  --pos: #1f7a3a;
  --neg: #c03a39;
  --good: #1f8a4c;
  --warning: #c98500;
  --critical: #d03b3b;
  --good-soft: #e1f3e8;
  --warning-soft: #fbf0d6;
  --critical-soft: #fbe3e3;
  --sel: #eaf2fc;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 2px 8px rgb(0 0 0 / .04);
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
    --text: #f2f2ef; --text-2: #c3c2b7; --muted: #8f8e86;
    --accent: #3987e5; --accent-soft: #1d2d42; --income: #3987e5; --expense: #d95926;
    --pos: #5cc77d; --neg: #f07b7a; --good: #4cbb72; --warning: #e0a630; --critical: #e66767;
    --good-soft: #17301f; --warning-soft: #352a12; --critical-soft: #3a1c1c; --sel: #1d2a3b;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
  --text: #f2f2ef; --text-2: #c3c2b7; --muted: #8f8e86;
  --accent: #3987e5; --accent-soft: #1d2d42; --income: #3987e5; --expense: #d95926;
  --pos: #5cc77d; --neg: #f07b7a; --good: #4cbb72; --warning: #e0a630; --critical: #e66767;
  --good-soft: #17301f; --warning-soft: #352a12; --critical-soft: #3a1c1c; --sel: #1d2a3b;
  --shadow: 0 1px 2px rgb(0 0 0 / .4);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); display: flex; min-height: 100vh; }
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0; font-weight: 600; display: flex; align-items: center; gap: 8px; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.r { text-align: right; }
.c { text-align: center; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* ---------- Barre latérale ---------- */
#sidebar { width: 270px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; display: flex; flex-direction: column; gap: 6px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { font-size: 20px; padding: 2px 8px 10px; letter-spacing: -.3px; }
.brand b { color: var(--accent); }
nav { display: flex; flex-direction: column; margin: 8px 0; }
nav a, .acc-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none; }
nav a:hover, .acc-item:hover { background: var(--surface-2); }
nav a.active, .acc-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.ico { width: 18px; text-align: center; opacity: .8; }
.sb-section { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 600; }
.acc-item { gap: 8px; }
.acc-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc-name small { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.acc-bal { font-variant-numeric: tabular-nums; font-size: 13px; white-space: nowrap; }
.acc-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; margin-right: 6px; vertical-align: middle; }
.acc-dot.big { width: 14px; height: 14px; }
.acc-item .acc-dot { margin-right: 0; }
.acc-total { display: flex; justify-content: space-between; padding: 8px 10px; border-top: 1px solid var(--border); margin-top: 4px; }
.closed-accs summary { cursor: pointer; padding: 6px 10px; color: var(--muted); font-size: 12px; }
.sb-foot { margin-top: auto; padding: 10px; font-size: 12px; }
#save-status[data-state="saved"] { color: var(--good); }
#save-status[data-state="saved"]::before { content: "● "; }
#save-status[data-state="error"], #save-status[data-state="conflict"] { color: var(--critical); font-weight: 600; }
#save-status[data-state="pending"], #save-status[data-state="saving"] { color: var(--muted); }
.pad { padding: 4px 10px; }

/* ---------- Zone principale ---------- */
main { flex: 1; min-width: 0; padding: 24px 28px 60px; display: flex; flex-direction: column; gap: 16px; }
main > * { min-width: 0; max-width: 100%; } /* un tableau large défile dans sa carte au lieu d'élargir la page */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-head .muted { margin-top: 4px; max-width: 760px; }
.balances { display: flex; gap: 24px; flex-wrap: wrap; }
.balances > div { display: flex; flex-direction: column; align-items: flex-end; }
.balances span { font-size: 12px; color: var(--muted); }
.balances b { font-size: 18px; font-variant-numeric: tabular-nums; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; box-shadow: var(--shadow); }
.tile-label { font-size: 12px; color: var(--muted); }
.tile-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile.hero .tile-value { font-size: 28px; }
.tile-sub { font-size: 12px; color: var(--text-2); }
.empty-state { max-width: 560px; margin: 10vh auto; text-align: center; }
.empty-state h1 { justify-content: center; margin-bottom: 12px; }
.empty-state p { color: var(--text-2); line-height: 1.6; }

/* ---------- Boutons & champs ---------- */
a.btn { text-decoration: none; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.danger { background: var(--critical); border-color: var(--critical); color: #fff; }
.btn.danger-ghost { color: var(--critical); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn.big { padding: 12px 26px; font-size: 16px; }
.btn.block { width: 100%; justify-content: center; }
.btn.icon { padding: 5px 10px; }
.btn.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.seg { display: inline-flex; }
.seg .btn { border-radius: 0; }
.seg .btn:first-child { border-radius: 7px 0 0 7px; }
.seg .btn:last-child { border-radius: 0 7px 7px 0; margin-left: -1px; }
.icon-btn { background: none; border: 0; color: var(--text-2); cursor: pointer; padding: 3px 6px; border-radius: 5px; font-size: 14px; }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .3; }
.icon-btn.tiny { padding: 0 4px; font-size: 12px; width: 20px; }
.tiny-spacer { display: inline-block; width: 20px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 7px; padding: 7px 9px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
input[type=checkbox], input[type=radio] { accent-color: var(--accent); width: 16px; height: 16px; }
input[type=color] { padding: 2px; height: 36px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--text-2); }
label.check, label.inline { flex-direction: row; align-items: center; gap: 8px; }
label.check { color: var(--text); font-size: 13px; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar input[type=search] { width: 280px; }
.month-title { min-width: 140px; text-align: center; font-size: 16px; text-transform: capitalize; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid4 { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; align-items: end; }
.span2 { grid-column: span 2; }
.inline-fields { display: flex; gap: 6px; }
.inline-fields input { width: 70px; }
fieldset { border: 1px solid var(--border); border-radius: 8px; margin: 14px 0 0; padding: 12px; }
legend { font-weight: 600; padding: 0 6px; font-size: 13px; }
.small-select { padding: 3px 6px; font-size: 12.5px; max-width: 240px; }

/* ---------- Tables ---------- */
.table-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card .table-wrap { box-shadow: none; }
main > .table-wrap { max-height: calc(100vh - 230px); }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th { text-align: left; font-weight: 600; font-size: 12px; color: var(--text-2); padding: 8px 10px; background: var(--surface-2); position: sticky; top: 0; z-index: 1; white-space: nowrap; }
td { padding: 6px 10px; border-top: 1px solid var(--border); vertical-align: middle; }
table.simple th { background: none; position: static; border-bottom: 1px solid var(--border); padding: 6px 8px; }
table.simple td { padding: 7px 8px; }
table.simple tr:first-child td { border-top: 0; }
tr.link { cursor: pointer; }
tr.link:hover td { background: var(--surface-2); }
.register tbody tr:hover td { background: var(--surface-2); }
.register tr.sel td { background: var(--sel); }
.register tr.future td { color: var(--muted); font-style: italic; }
.register tr.due td:first-child { color: var(--warning); font-weight: 600; }
.register td.lbl { max-width: 380px; }
.register tfoot td { background: var(--surface-2); font-weight: 600; position: sticky; bottom: 0; }
.register .actions { opacity: .35; }
.register tr:hover .actions { opacity: 1; }
th.sortable { cursor: pointer; user-select: none; }
td.empty { text-align: center; color: var(--muted); padding: 30px; font-style: normal !important; }
.status-btn { width: 24px; height: 24px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 13px; color: var(--accent-ink); padding: 0; font-weight: 700; }
.status-btn.on { background: var(--good); border-color: var(--good); }
.status-btn.rec { background: var(--text-2); border-color: var(--text-2); color: var(--surface); }
.tag { display: inline-block; font-size: 10.5px; padding: 0 5px; border-radius: 4px; background: var(--surface-2); color: var(--muted); font-style: normal; vertical-align: 1px; border: 1px solid var(--border); }
.transfer { color: var(--text-2); }
.badge { display: inline-block; font-size: 11.5px; padding: 1px 7px; border-radius: 10px; font-weight: 600; white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.critical { background: var(--critical-soft); color: var(--critical); }
.bulkbar { display: flex; gap: 8px; align-items: center; padding: 8px 12px; background: var(--accent-soft); border-radius: 8px; flex-wrap: wrap; }
.grid-table td, .grid-table th { white-space: nowrap; }
.grid-table td:first-child { position: sticky; left: 0; background: var(--surface); }
.cell-ok { background: var(--good-soft); }
.cell-bad { background: var(--critical-soft); color: var(--critical); font-weight: 600; }

/* ---------- Menu Paramètres (roue crantée) ---------- */
.brand-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.brand-row .brand { padding-bottom: 6px; }
.gear-btn { font-size: 20px; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; transition: transform .25s ease; }
.gear-btn.on { background: var(--accent-soft); color: var(--accent); transform: rotate(60deg); }
.settings-nav { margin: 0 0 6px; padding: 6px; background: var(--surface-2); border-radius: 10px; }
.settings-title { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 600; padding: 4px 10px 6px; }
.settings-nav a.active { background: var(--surface); }

/* ---------- Utilisateur connecté (bas du menu) ---------- */
.sb-foot { display: flex; flex-direction: column; gap: 8px; }
.sb-foot-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sb-user { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 8px; color: var(--text); text-decoration: none; }
.sb-user:hover, .sb-user.active { background: var(--surface-2); }
.sb-avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; flex-shrink: 0; }
.sb-user-txt { display: flex; flex-direction: column; min-width: 0; }
.sb-user-txt b, .sb-user-txt small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-user-txt small { color: var(--muted); font-size: 11px; }
.users-table td { vertical-align: middle; }

/* ---------- Groupes de comptes ---------- */
.acc-group { border-radius: 8px; }
.acc-group-head { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border: 0; background: none; border-radius: 8px;
  font: inherit; color: var(--text); cursor: pointer; text-align: left; font-weight: 600; }
.acc-group-head:hover { background: var(--surface-2); }
.acc-group-head.active { background: var(--accent-soft); color: var(--accent); }
.acc-group-head .chev { width: 12px; color: var(--muted); font-size: 12px; }
.acc-group-head .acc-name small { font-weight: 400; }
.acc-group-items { padding-left: 14px; border-left: 2px solid var(--border); margin-left: 15px; }
.group-row td { background: var(--surface-2); }
tr.in-group td:first-child { padding-left: 22px; }

/* ---------- Ordre des comptes ---------- */
.order-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.order-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.order-name { flex: 1; min-width: 0; }
.order-row .icon-btn { font-size: 16px; width: 36px; height: 32px; border: 1px solid var(--border); }
.sb-section > span { display: flex; gap: 2px; }

/* ---------- Cagnottes ---------- */
.env-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.env-card { background: var(--surface); border: 1px solid var(--border); border-left: 5px solid var(--env); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.env-card header { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.env-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.env-balance { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.env-meta { color: var(--text-2); font-size: 13px; }
.env-card .budget-track { margin-top: 4px; height: 8px; }
.env-card .btn { align-self: flex-start; margin-top: 6px; }
.tile-ok { border-color: var(--good); }
.tile-warn { border-color: var(--warning); }
.alloc-list { display: flex; flex-direction: column; }
.alloc-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; padding: 10px 0; border-top: 1px solid var(--border); }
.alloc-row:first-child { border-top: 0; }
.alloc-row.future { opacity: .65; }
.alloc-main { flex: 1 1 260px; display: flex; flex-direction: column; min-width: 0; }
.alloc-amount { display: flex; flex-direction: column; align-items: flex-end; min-width: 120px; font-variant-numeric: tabular-nums; }
.alloc-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.split-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 6px 16px; }
.split-row { flex-direction: row; align-items: center; justify-content: space-between; gap: 10px; color: var(--text); font-size: 13.5px; }
.split-row input { width: 120px; text-align: right; }
.total-row td { border-top: 2px solid var(--border); }
.tag-warn { background: var(--warning-soft); color: var(--warning); border-color: transparent; text-decoration: none; }
@media (max-width: 700px) {
  .alloc-amount { align-items: flex-start; }
  .alloc-actions { width: 100%; }
  .alloc-actions > * { flex: 1 1 auto; }
  .env-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .env-balance { font-size: 18px; }
  .env-name { white-space: normal; }
  .accounts-total .c-start, .accounts-total .acc-type { display: none; }
  .accounts-total td:first-child { white-space: nowrap; }
  .tile-ok, .tile-warn { grid-column: 1 / -1; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------- Sélection multiple ---------- */
details.multi { position: relative; }
details.multi > summary { list-style: none; }
details.multi > summary::-webkit-details-marker { display: none; }
.multi-pop { position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgb(0 0 0 / .15); padding: 8px; min-width: 240px; display: flex; flex-direction: column; gap: 2px; }
.multi-pop label.check { padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.multi-pop label.check:hover { background: var(--surface-2); }
.multi-pop .acc-dot { margin-right: 0; }
.multi-actions { display: flex; gap: 6px; border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }

/* ---------- Import ---------- */
.dropzone { margin-top: 12px; border: 2px dashed var(--border); border-radius: 10px; padding: 22px; text-align: center; color: var(--muted); }
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
details summary { cursor: pointer; color: var(--text-2); margin-top: 10px; }
pre { white-space: pre-wrap; background: var(--surface-2); padding: 10px; border-radius: 6px; }

/* ---------- Catégories ---------- */
.cat-tree, .cat-tree ul { list-style: none; margin: 0; padding: 0; }
.cat-tree ul { padding-left: 22px; }
.cat-row { display: flex; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 6px; }
.cat-row:hover { background: var(--surface-2); }
.cat-row .icon-btn { opacity: .4; }
.cat-row:hover .icon-btn { opacity: 1; }

/* ---------- Budgets ---------- */
.budget-table td { padding: 6px 8px; }
.budget-table tr.sub td:first-child { padding-left: 34px; color: var(--text-2); }
.budget-input { width: 110px; text-align: right; padding: 4px 8px; }
.bar-cell { width: 42%; min-width: 220px; }
.budget-line { margin-bottom: 12px; }
.bar-cell .budget-line { margin: 0; }
.budget-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.budget-track { position: relative; height: 10px; background: var(--surface-2); border-radius: 5px; }
.budget-fill { height: 100%; border-radius: 5px; min-width: 2px; }
.budget-fill.good { background: var(--good); }
.budget-fill.warning { background: var(--warning); }
.budget-fill.critical { background: var(--critical); }
.budget-fill.neutral { background: var(--accent); }
.pace { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); opacity: .55; border-radius: 1px; }
.budget-status { font-size: 11.5px; margin-top: 3px; color: var(--text-2); }
.budget-status.critical { color: var(--critical); font-weight: 600; }
.budget-status.warning { color: var(--warning); }

/* ---------- Graphiques ---------- */
svg text.axis { fill: var(--muted); font-size: 11px; }
svg line.grid { stroke: var(--border); stroke-width: 1; }
svg line.baseline { stroke: var(--text-2); stroke-width: 1; }
svg .bar-income { fill: var(--income); }
svg .bar-expense { fill: var(--expense); }
svg .hit { fill: transparent; }
svg .hit-group:hover .hit { fill: var(--surface-2); opacity: .6; }
svg path.line { fill: none; stroke: var(--income); stroke-width: 2; stroke-linejoin: round; }
svg .cross { stroke: var(--muted); stroke-dasharray: 3 3; }
svg .dot { fill: var(--income); stroke: var(--surface); stroke-width: 2; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.income { background: var(--income); }
.sw.expense { background: var(--expense); }
.chart-tip { position: fixed; z-index: 100; pointer-events: none; background: var(--surface); border: 1px solid var(--border); box-shadow: 0 4px 16px rgb(0 0 0 / .15); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; line-height: 1.5; display: none; color: var(--text); }
.hbars { display: flex; flex-direction: column; gap: 4px; }
.hbar-row { display: grid; grid-template-columns: minmax(110px, 1.2fr) 2fr auto 52px; gap: 10px; align-items: center; padding: 4px 6px; border-radius: 6px; font-size: 13px; }
.hbar-row.clickable { cursor: pointer; }
.hbar-row.clickable:hover { background: var(--surface-2); }
.hbar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { color: var(--muted); }
.hbar-track { height: 12px; }
.hbar-fill { display: block; height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.hbar-fill.expense { background: var(--expense); }
.hbar-fill.income { background: var(--income); }
.hbar-value { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.hbar-pct { color: var(--muted); font-size: 12px; text-align: right; }

/* ---------- Fenêtres modales ---------- */
dialog.modal { border: 0; border-radius: 12px; padding: 0; width: min(640px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
dialog.modal.wide { width: min(760px, calc(100vw - 32px)); }
dialog.modal.small { width: min(440px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
dialog header h2 { margin: 0; }
.modal-body { padding: 16px 18px; max-height: calc(100vh - 180px); overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-body p { margin: 0; line-height: 1.5; }
dialog footer { display: flex; gap: 8px; align-items: center; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; flex-wrap: wrap; }
.kind-switch { display: flex; background: var(--surface-2); border-radius: 8px; padding: 3px; gap: 3px; }
.kind-switch label { flex: 1; flex-direction: row; justify-content: center; }
.kind-switch input { display: none; }
.kind-switch span { flex: 1; text-align: center; padding: 7px; border-radius: 6px; cursor: pointer; color: var(--text-2); font-size: 13px; }
.kind-switch input:checked + span { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.recon-info { display: grid; grid-template-columns: auto auto; gap: 4px 12px; align-content: center; font-size: 13px; }

/* ---------- Notifications ---------- */
#toasts { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / .2); max-width: 380px; transition: opacity .4s, transform .4s; }
.toast-success { background: var(--good); color: #fff; }
.toast-error { background: var(--critical); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- Connexion ---------- */
.login-page { align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 28px 24px; width: min(360px, 100%); display: flex; flex-direction: column; gap: 16px; }
.login-card .brand { font-size: 26px; text-align: center; padding: 0; }
.login-card input { font-size: 16px; padding: 10px 12px; }
.login-card .btn { padding: 10px; font-size: 15px; }
.login-error { color: var(--critical); margin: 0; min-height: 1.2em; text-align: center; }

/* ---------- Barre du haut (tablette / smartphone) ---------- */
#topbar, #backdrop { display: none; }

/* ---------- Tablette : menu latéral escamotable ---------- */
@media (max-width: 1000px) {
  body { flex-direction: column; }
  #topbar { display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 40;
    padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px calc(8px + env(safe-area-inset-left));
    background: var(--surface); border-bottom: 1px solid var(--border); }
  #topbar .brand { font-size: 18px; padding: 0; }
  .menu-btn { font-size: 22px; width: 44px; height: 40px; }
  #sidebar { position: fixed; top: 0; left: 0; bottom: 0; height: 100dvh; width: min(300px, 86vw); z-index: 60;
    transform: translateX(-105%); transition: transform .22s ease; box-shadow: 0 0 30px rgb(0 0 0 / .25);
    padding-top: calc(16px + env(safe-area-inset-top)); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  body.nav-open #sidebar { transform: none; }
  body.nav-open #backdrop { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 50; }
  body.nav-open { overflow: hidden; }
  main { padding: 16px calc(16px + env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left)); }
  .cols { grid-template-columns: 1fr; }
  main > .table-wrap { max-height: none; }
  .bar-cell { width: 36%; min-width: 160px; }
}

/* ---------- Écrans tactiles : pas de survol ---------- */
@media (hover: none) {
  .register .actions, .cat-row .icon-btn { opacity: 1; }
  .icon-btn { min-width: 36px; min-height: 36px; }
  .register tbody tr:hover td { background: none; }
  .status-btn { width: 32px; height: 32px; }
}

/* Tablette (821-1000 px) : colonnes Mode et N° du registre masquées pour gagner de la place */
@media (min-width: 821px) and (max-width: 1000px) {
  table.register.cards th:nth-child(3), table.register.cards td:nth-child(3),
  table.register.cards th:nth-child(4), table.register.cards td:nth-child(4) { display: none; }
}

/* ---------- Tablette en portrait et smartphone : opérations en cartes ---------- */
@media (max-width: 820px) {
  /* Tableaux d'opérations affichés en cartes */
  .table-wrap:has(table.cards) { background: none; border: 0; box-shadow: none; overflow: visible; }
  table.cards, table.cards tbody { display: block; }
  table.cards thead { display: none; }
  table.cards tfoot { display: block; }
  table.cards tfoot tr { display: flex; justify-content: flex-end; gap: 12px; padding: 10px 4px; }
  table.cards tfoot td { background: none; border: 0; padding: 0; position: static; }
  table.cards tfoot td:first-child { flex: 1; text-align: left; }
  table.cards tbody tr { display: grid; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
    margin-bottom: 8px; padding: 8px 10px; column-gap: 8px; align-items: center; }
  table.cards tbody td { border: 0; padding: 1px 0; background: none !important; min-width: 0; }
  table.cards td:empty { display: none; }
  table.cards td.empty { display: block; grid-column: 1 / -1; }
  table.register.cards tbody tr { grid-template-columns: 26px auto 1fr auto;
    grid-template-areas: "sel date mode amount" "sel lbl lbl amount" "sel payee payee bal" "sel cat cat status" "sel act act act"; }
  table.register.cards tr.sel { outline: 2px solid var(--accent); }
  .register.cards .c-sel { grid-area: sel; align-self: start; padding-top: 3px; }
  .register.cards .c-date { grid-area: date; font-size: 12.5px; color: var(--text-2); }
  .register.cards .c-mode { grid-area: mode; }
  .register.cards .c-num { display: none; }
  .register.cards .c-payee { grid-area: payee; font-size: 12.5px; color: var(--text-2); }
  .register.cards .c-lbl { grid-area: lbl; max-width: none; font-weight: 600; overflow-wrap: anywhere; }
  .register.cards .c-cat { grid-area: cat; color: var(--muted); }
  .register.cards .c-debit, .register.cards .c-credit { grid-area: amount; font-size: 16px; font-weight: 600; white-space: nowrap; }
  .register.cards .c-debit::before { content: "−"; }
  .register.cards .c-credit::before { content: "+"; }
  .register.cards .c-bal { grid-area: bal; font-size: 12px; color: var(--muted); }
  .register.cards .c-bal::before { content: "solde "; }
  .register.cards .c-status { grid-area: status; justify-self: end; }
  .register.cards .c-actions { grid-area: act; display: flex; gap: 4px; justify-self: start; opacity: 1; margin-top: 2px; }
  .register.cards tr.future { opacity: .7; }
  /* Aperçu d'import */
  table.import-cards tbody tr { grid-template-columns: 26px 1fr auto;
    grid-template-areas: "sel date amount" "sel lbl lbl" "sel cat state"; }
  .import-cards .c-sel { grid-area: sel; align-self: start; }
  .import-cards .c-date { grid-area: date; font-size: 12.5px; color: var(--text-2); }
  .import-cards .c-lbl { grid-area: lbl; max-width: none; }
  .import-cards .c-mode { display: none; }
  .import-cards .c-cat { grid-area: cat; }
  .import-cards .c-cat select { max-width: 100%; width: 100%; }
  .import-cards .c-amount { grid-area: amount; font-weight: 600; }
  .import-cards .c-state { grid-area: state; justify-self: end; }

}

/* ---------- Smartphone ---------- */
@media (max-width: 700px) {
  :root { font-size: 15px; }
  input, select, textarea { font-size: 16px; } /* évite le zoom automatique sur iPhone */
  h1 { font-size: 20px; }
  .grid2, .grid4 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .page-head { align-items: flex-start; }
  .balances { width: 100%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .balances > div { align-items: flex-start; }
  .balances b { font-size: 15px; }
  .toolbar > * { flex: 1 1 auto; }
  .toolbar input[type=search] { width: 100%; flex-basis: 100%; }
  .toolbar .spacer { display: none; }
  .toolbar select { min-width: 0; flex-basis: 140px; }
  .month-title { min-width: 0; flex: 1 1 auto; }
  .toolbar .btn.icon, .toolbar .seg { flex: 0 0 auto; }
  .tiles { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .tile { padding: 10px 12px; }
  .tile-value { font-size: 18px; }
  .tile.hero { grid-column: 1 / -1; }
  .tile.hero .tile-value { font-size: 24px; }
  .card { padding: 14px; }
  .multi-pop { max-width: calc(100vw - 32px); }

  /* Fenêtres modales en plein écran */
  dialog.modal, dialog.modal.wide { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  dialog.modal.small { height: auto; margin: auto; width: calc(100vw - 32px); border-radius: 12px; }
  dialog.modal > form { display: flex; flex-direction: column; height: 100%; }
  dialog.modal:not(.small) .modal-body { flex: 1; max-height: none; }
  dialog header { padding-top: calc(14px + env(safe-area-inset-top)); }
  dialog footer { border-radius: 0; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  dialog footer .btn { flex: 1 1 auto; justify-content: center; }
  dialog footer .spacer { display: none; }
  dialog footer label.check.inline { flex-basis: 100%; }

  /* Budgets : la barre de suivi passe sous la ligne */
  .budget-table thead { display: none; }
  .budget-table, .budget-table tbody { display: block; }
  .budget-table tr { display: grid; grid-template-columns: 1fr auto auto; gap: 4px 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--border); }
  .budget-table td { border: 0; padding: 0; }
  .budget-table td.bar-cell { grid-column: 1 / -1; width: auto; min-width: 0; }
  .budget-table td.bar-cell:empty { display: none; }
  .budget-table tr.sub td:first-child { padding-left: 20px; }
  .budget-input { width: 92px; }

  /* Répartition par catégorie : la barre passe sous le libellé */
  .hbar-row { grid-template-columns: 1fr auto 46px; row-gap: 3px; }
  .hbar-track { grid-column: 1 / -1; grid-row: 2; height: 8px; }

  #toasts { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
}
