:root {
  --bg: #f7f5ef;
  --surface: #fffdf9;
  --surface-2: #eef4f1;
  --ink: #20302d;
  --muted: #687773;
  --primary: #2f6f68;
  --primary-dark: #245a54;
  --accent: #d97735;
  --danger: #b64b45;
  --line: #dce3df;
  --shadow: 0 12px 36px rgba(37, 71, 65, .10);
  --radius: 22px;
  font-family: Inter, ui-rounded, "Segoe UI", Arial, sans-serif;
  font-size: 19px;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; min-height: 100vh; background: var(--bg); }
button, input, select { font: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: .55rem; font-size: clamp(2rem, 4vw, 3.25rem); line-height: 1.02; letter-spacing: -.04em; }
h2 { margin-bottom: .35rem; font-size: 1.4rem; letter-spacing: -.02em; }
small { color: var(--muted); }
.hidden { display: none !important; }
.eyebrow { margin-bottom: .45rem; color: var(--primary); font-size: .72rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.lead, .page-subtitle { color: var(--muted); line-height: 1.55; }

.center-screen, .login-screen { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.center-screen { color: var(--muted); align-content: center; gap: 1rem; }
.loader { width: 46px; height: 46px; border: 5px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.login-screen { background: radial-gradient(circle at 20% 5%, #e2efea 0, transparent 35%), var(--bg); }
.login-card { width: min(520px, 100%); padding: 2.4rem; text-align: center; background: var(--surface); border: 1px solid var(--line); border-radius: 32px; box-shadow: var(--shadow); }
.brand-mark { display: inline-grid; place-items: center; width: 70px; height: 70px; margin-bottom: 1rem; color: white; background: var(--primary); border-radius: 22px; font-size: 2rem; font-weight: 800; }
.brand-mark.small { width: 48px; height: 48px; margin: 0; border-radius: 15px; font-size: 1.35rem; }
.pin-form { display: grid; gap: .75rem; margin-top: 1.4rem; }
.pin-form label { font-weight: 700; }
#login-pin { width: 190px; margin: 0 auto; padding: .7rem 1rem; text-align: center; font-size: 1.8rem; letter-spacing: .45em; }
.pin-dots { display: none; }
.visitor-code { margin: 0; padding: .7rem; border-radius: 12px; color: var(--primary-dark); background: var(--surface-2); font-size: .9rem; }
#login-pin:disabled, #login-submit:disabled { cursor: not-allowed; opacity: .6; }
.form-error { min-height: 1.3rem; margin: 0; color: var(--danger); font-weight: 700; }

.app-shell { min-height: 100vh; }
.sidebar { position: fixed; inset: 0 auto 0 0; z-index: 5; display: flex; flex-direction: column; width: 270px; padding: 1.25rem; background: var(--surface); border-right: 1px solid var(--line); }
.brand-lockup { display: flex; align-items: center; gap: .75rem; }
.brand-lockup strong, .brand-lockup small { display: block; }
.brand-lockup strong { font-size: 1.15rem; }
.main-nav { display: grid; gap: .35rem; margin-top: 2.5rem; }
.nav-item { display: flex; align-items: center; gap: .75rem; width: 100%; padding: .8rem .9rem; border: 0; border-radius: 14px; color: var(--muted); background: transparent; text-align: left; font-weight: 700; }
.nav-item span { width: 1.5rem; font-size: 1.3rem; text-align: center; }
.nav-item:hover, .nav-item.active { color: var(--primary-dark); background: var(--surface-2); }
.sidebar-foot { display: grid; gap: .5rem; margin-top: auto; }
.user-chip { display: flex; align-items: center; gap: .65rem; width: 100%; padding: .75rem; border: 1px solid var(--line); border-radius: 16px; color: var(--ink); background: white; text-align: left; }
.user-chip > span:first-child, .avatar-button span { display: grid; place-items: center; width: 42px; height: 42px; flex: 0 0 auto; border-radius: 50%; color: white; background: var(--primary); font-weight: 800; }
.user-chip strong, .user-chip small { display: block; }
.content { margin-left: 270px; padding: 2.25rem clamp(1.25rem, 4vw, 4rem) 4rem; }
.mobile-header, .mobile-nav { display: none; }
.view { display: none; max-width: 1220px; margin: 0 auto; animation: fade .18s ease; }
.view.active-view { display: block; }
@keyframes fade { from { opacity: .3; transform: translateY(3px); } }
.page-heading { display: flex; justify-content: space-between; align-items: end; gap: 1.5rem; margin-bottom: 1.5rem; }
.home-heading { align-items: center; }
.heading-actions { display: flex; flex-wrap: wrap; gap: .55rem; }

.button { display: inline-flex; justify-content: center; align-items: center; gap: .45rem; min-height: 50px; padding: .65rem 1rem; border: 2px solid transparent; border-radius: 14px; font-weight: 800; text-decoration: none; }
.button.primary { color: white; background: var(--primary); }
.button.primary:hover { background: var(--primary-dark); }
.button.secondary { color: var(--primary-dark); border-color: #b9d1ca; background: var(--surface-2); }
.button.ghost { color: var(--ink); border-color: var(--line); background: transparent; }
.button.danger { color: white; background: var(--danger); }
.button.large { min-height: 62px; padding-inline: 1.5rem; }
.button.wide { width: 100%; }
.button.compact { min-height: 42px; padding: .45rem .7rem; font-size: .88rem; }
.text-button { padding: .2rem; border: 0; color: var(--primary); background: transparent; font-weight: 800; }

.period-tabs { display: flex; width: fit-content; max-width: 100%; margin-bottom: 1.25rem; padding: .25rem; overflow-x: auto; border: 1px solid var(--line); border-radius: 15px; background: var(--surface); }
.period-tabs button { min-width: 76px; padding: .55rem .8rem; border: 0; border-radius: 11px; color: var(--muted); background: transparent; font-weight: 700; }
.period-tabs button.active { color: white; background: var(--primary); }
.summary-grid { display: grid; grid-template-columns: 1.5fr 1fr; gap: 1rem; }
.summary-card { display: flex; flex-direction: column; justify-content: center; min-height: 170px; padding: 1.5rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 5px 20px rgba(37, 71, 65, .05); }
.summary-card span { color: var(--muted); font-weight: 700; }
.summary-card strong { margin: .45rem 0; font-size: clamp(2rem, 4vw, 3rem); letter-spacing: -.04em; }
.main-total { color: white; border: 0; background: var(--primary); }
.main-total span, .main-total small { color: #e6f2ef; }
.maison-total { border-left: 6px solid var(--accent); }
.maison-hero { max-width: 600px; margin-bottom: 1.2rem; border-left: 6px solid var(--accent); }
.section-block { margin-top: 2rem; }
.section-heading { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: .9rem; }
.section-heading h2, .section-heading p { margin-bottom: 0; }
.person-total-grid, .payer-total-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: .8rem; }
.person-total-card { padding: 1.1rem; border: 1px solid var(--line); border-radius: 18px; background: var(--surface); }
.person-total-card .person-line { display: flex; align-items: center; gap: .55rem; color: var(--muted); font-weight: 700; }
.color-dot { width: 13px; height: 13px; border-radius: 50%; background: var(--dot, var(--primary)); }
.person-total-card strong { display: block; margin-top: .55rem; font-size: 1.55rem; }
.payer-total-card { padding: 1rem 1.1rem; border: 1px solid var(--line); border-left: 6px solid var(--dot); border-radius: 18px; background: var(--surface); }
.payer-total-card span { display: block; color: var(--muted); font-weight: 700; }
.payer-total-card strong { display: block; margin-top: .35rem; font-size: 1.45rem; }
.dashboard-grid, .settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; margin-top: 2rem; }
.panel { padding: 1.25rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.category-list { display: grid; gap: .85rem; }
.category-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .6rem; align-items: center; }
.category-name { display: flex; align-items: center; gap: .5rem; font-weight: 700; }
.category-image { display: block; width: 58px; height: 58px; flex: 0 0 auto; border-radius: 16px; object-fit: cover; }
.category-image.small { width: 38px; height: 38px; border-radius: 11px; }
.category-track { grid-column: 1 / -1; height: 9px; overflow: hidden; border-radius: 9px; background: var(--line); }
.category-track i { display: block; height: 100%; width: var(--width); border-radius: 9px; background: var(--dot); }
.bar-chart { display: flex; align-items: end; gap: 5px; height: 230px; padding-top: 1rem; overflow-x: auto; }
.bar { min-width: 16px; flex: 1; height: var(--height); min-height: 3px; border-radius: 8px 8px 3px 3px; background: var(--primary); position: relative; }
.bar:hover::after { content: attr(data-label); position: absolute; bottom: calc(100% + 6px); left: 50%; z-index: 2; padding: .25rem .4rem; border-radius: 6px; color: white; background: var(--ink); font-size: .68rem; white-space: nowrap; transform: translateX(-50%); }
.budget-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: .8rem; }
.budget-card { padding: 1rem; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); }
.budget-card.exceeded { border-color: #e3aaa6; background: #fff5f3; }
.budget-top { display: flex; justify-content: space-between; gap: .5rem; }
.progress { height: 11px; margin: .7rem 0 .4rem; overflow: hidden; border-radius: 10px; background: var(--line); }
.progress i { display: block; height: 100%; width: min(var(--progress), 100%); background: var(--primary); }
.exceeded .progress i { background: var(--danger); }

.expense-form { max-width: 900px; padding: clamp(1rem, 3vw, 2rem); }
fieldset { padding: 0; border: 0; }
legend { margin-bottom: .7rem; font-size: 1.15rem; font-weight: 800; }
legend span, label > span { color: var(--danger); }
.field-help { margin: -.35rem 0 .8rem; color: var(--muted); font-size: .86rem; }
.choice-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .65rem; margin-bottom: 1.5rem; }
.choice-button { min-height: 66px; padding: .7rem; border: 2px solid var(--line); border-radius: 16px; color: var(--ink); background: white; font-weight: 800; }
.choice-button.selected { color: var(--primary-dark); border-color: var(--primary); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--primary); }
.payer-choice-grid { max-width: 440px; grid-template-columns: repeat(2, minmax(130px, 1fr)); }
.person-choice { display: flex; align-items: center; justify-content: center; gap: .5rem; }
.person-initial { display: grid; place-items: center; width: 34px; height: 34px; flex: 0 0 auto; border-radius: 50%; color: white; background: var(--dot, var(--primary)); font-size: .85rem; }
.person-initial.adel { background: #4E79A7; }
.category-field { margin-bottom: 1.5rem; }
.category-choice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: .65rem; }
.category-choice { display: grid; justify-items: center; align-content: center; gap: .45rem; min-height: 112px; padding: .65rem .45rem; border: 2px solid var(--line); border-radius: 17px; color: var(--ink); background: white; font-size: .82rem; font-weight: 800; }
.category-choice:hover, .category-choice:focus-visible { border-color: #9ebfb6; background: var(--surface-2); }
.category-choice.selected { color: var(--primary-dark); border-color: var(--primary); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--primary); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
label { display: grid; align-content: start; gap: .4rem; font-weight: 750; }
label small, label em { color: var(--muted); font-size: .78rem; font-style: normal; font-weight: 500; }
input, select { width: 100%; min-height: 54px; padding: .65rem .75rem; border: 2px solid var(--line); border-radius: 13px; color: var(--ink); background: white; }
input:focus, select:focus { outline: 3px solid rgba(47, 111, 104, .17); border-color: var(--primary); }
.amount-input { display: flex; align-items: center; border: 2px solid var(--line); border-radius: 13px; background: white; }
.amount-input:focus-within { outline: 3px solid rgba(47, 111, 104, .17); border-color: var(--primary); }
.amount-input input { border: 0; outline: 0; font-size: 1.4rem; font-weight: 800; }
.amount-input strong { padding-right: .8rem; color: var(--muted); }
.form-actions { display: flex; justify-content: flex-end; gap: .7rem; margin-top: 1.5rem; }

.filter-bar { display: grid; grid-template-columns: repeat(5, minmax(125px, 1fr)); gap: .7rem; align-items: end; padding: 1rem; }
.filter-bar label { font-size: .84rem; }
.filter-bar input, .filter-bar select { min-height: 48px; }
.filter-bar .search-label { grid-column: span 2; }
.result-count { margin: 1.2rem 0 .7rem; color: var(--muted); font-weight: 700; }
.expense-list { display: grid; gap: .7rem; }
.expense-card { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; gap: .85rem; align-items: center; padding: 1rem; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); }
.expense-icon { display: block; width: 52px; height: 52px; border-radius: 15px; object-fit: cover; }
.expense-main strong, .expense-main small { display: block; }
.expense-main small { margin-top: .2rem; }
.payer-badge { display: inline-block; padding: .12rem .38rem; border-radius: 999px; color: var(--primary-dark); background: var(--surface-2); font-size: .72rem; font-weight: 800; }
.locked-badge { color: var(--danger); font-weight: 800; }
.expense-amount { font-size: 1.18rem; font-weight: 900; white-space: nowrap; }
.card-actions { display: flex; gap: .35rem; }
.icon-text { min-height: 42px; padding: .4rem .6rem; border: 1px solid var(--line); border-radius: 11px; color: var(--ink); background: white; font-weight: 700; }
.icon-text.delete { color: var(--danger); }
.empty-state { padding: 2rem; border: 1px dashed #b9c7c3; border-radius: 18px; color: var(--muted); text-align: center; background: rgba(255,255,255,.5); }

.manage-list { display: grid; gap: .5rem; }
.manage-item { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .7rem 0; border-bottom: 1px solid var(--line); }
.manage-item:last-child { border: 0; }
.manage-item-name { display: flex; align-items: center; gap: .55rem; min-width: 0; }
.manage-item-name div { min-width: 0; }
.manage-item-name strong, .manage-item-name small { display: block; overflow: hidden; text-overflow: ellipsis; }
.manage-actions { display: flex; gap: .3rem; }
.notice { margin-bottom: 1rem; padding: 1rem; border-radius: 14px; color: var(--primary-dark); background: var(--surface-2); font-weight: 700; }
.read-only-banner { max-width: 1220px; margin: 0 auto 1.25rem; border-left: 5px solid var(--primary); }
.toggle-row, .check-row { display: flex; grid-template-columns: none; justify-content: space-between; align-items: center; gap: 1rem; padding: 1rem 0; border-bottom: 1px solid var(--line); }
.toggle-row span strong, .toggle-row span small { display: block; }
.toggle-row input, .check-row input { width: 24px; min-height: 24px; }

dialog { color: var(--ink); }
.modal { width: min(520px, calc(100% - 1.5rem)); padding: 0; border: 0; border-radius: 24px; background: var(--surface); box-shadow: 0 24px 80px rgba(20, 45, 40, .25); }
.modal::backdrop { background: rgba(23, 39, 36, .55); backdrop-filter: blur(3px); }
.modal form, .confirm-modal { padding: 1.35rem; }
.modal form { display: grid; gap: .9rem; }
.modal-head { display: flex; justify-content: space-between; align-items: start; gap: 1rem; }
.modal-head h2, .modal-head p { margin-bottom: 0; }
.close-button { width: 44px; height: 44px; border: 0; border-radius: 50%; color: var(--muted); background: var(--surface-2); font-size: 1.5rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .65rem; margin-top: .4rem; }
.confirm-modal { text-align: center; }
.confirm-icon { display: grid; place-items: center; width: 60px; height: 60px; margin: 0 auto 1rem; border-radius: 50%; color: white; background: var(--danger); font-size: 1.6rem; font-weight: 900; }
.toast { position: fixed; right: 1rem; bottom: 1rem; z-index: 20; max-width: min(420px, calc(100% - 2rem)); padding: .9rem 1.1rem; border-radius: 14px; color: white; background: var(--primary-dark); box-shadow: var(--shadow); font-weight: 750; opacity: 0; transform: translateY(20px); pointer-events: none; transition: .2s ease; }
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--danger); }

body.grandparent { font-size: 1.15em; }
body.grandparent .sidebar { width: 300px; }
body.grandparent .content { margin-left: 300px; }
body.grandparent .button, body.grandparent input, body.grandparent select, body.grandparent .category-choice { min-height: 60px; }

@media (max-width: 1050px) {
  .filter-bar { grid-template-columns: repeat(3, 1fr); }
  .filter-bar .search-label { grid-column: span 1; }
}

@media (max-width: 780px) {
  :root { font-size: 18px; --radius: 18px; }
  .sidebar { display: none; }
  .content, body.grandparent .content { margin-left: 0; padding: 5.5rem .85rem 6.5rem; }
  .mobile-header { position: fixed; inset: 0 0 auto 0; z-index: 4; display: flex; justify-content: space-between; align-items: center; height: 72px; padding: .65rem .9rem; border-bottom: 1px solid var(--line); background: rgba(255,253,249,.96); }
  .avatar-button { border: 0; background: transparent; }
  .mobile-nav { position: fixed; inset: auto 0 0 0; z-index: 5; display: grid; grid-template-columns: repeat(4, 1fr); padding: .3rem max(.25rem, env(safe-area-inset-right)) calc(.3rem + env(safe-area-inset-bottom)) max(.25rem, env(safe-area-inset-left)); border-top: 1px solid var(--line); background: var(--surface); }
  body.read-only .mobile-nav { grid-template-columns: repeat(3, 1fr); }
  .mobile-nav .nav-item { display: grid; justify-items: center; gap: .1rem; padding: .35rem .1rem; font-size: .68rem; text-align: center; }
  .mobile-nav .nav-item span { font-size: 1.35rem; }
  .page-heading { align-items: stretch; flex-direction: column; }
  .page-heading > .button { width: 100%; }
  .summary-grid, .dashboard-grid, .settings-grid, .form-grid { grid-template-columns: 1fr; }
  .summary-card { min-height: 145px; }
  .filter-bar { grid-template-columns: repeat(2, 1fr); }
  .filter-bar .search-label { grid-column: 1 / -1; }
  .expense-card { grid-template-columns: auto 1fr auto; }
  .expense-card .card-actions { grid-column: 2 / -1; }
  .login-card { padding: 1.5rem 1rem; }
}

@media (max-width: 480px) {
  .filter-bar { grid-template-columns: 1fr; }
  .filter-bar .search-label { grid-column: auto; }
  .choice-grid { grid-template-columns: repeat(2, 1fr); }
  .category-choice-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .heading-actions { display: grid; grid-template-columns: 1fr; }
  .expense-card { grid-template-columns: auto 1fr; }
  .expense-amount { grid-column: 2; }
  .expense-card .card-actions { grid-column: 1 / -1; }
  .card-actions .icon-text { flex: 1; }
  .form-actions, .modal-actions { flex-direction: column-reverse; }
}

@media print {
  .sidebar, .mobile-header, .mobile-nav, .page-heading .heading-actions, .filter-bar, .card-actions, .toast { display: none !important; }
  .content { margin: 0; padding: 0; }
  .view { display: none !important; }
  #view-history { display: block !important; }
  body { background: white; }
  .expense-card { break-inside: avoid; box-shadow: none; }
}
