:root {
  --sky: #17384A;
  --facade: #2A566B;
  --win-off: #1B4153;
  --lit: #FFC857;
  --paper: #F1F4F2;
  --card: #FFFFFF;
  --ink: #13272F;
  --muted: #5A6F76;
  --line: #D8E0DE;
  --bad: #B3372F;
  --good: #24724F;
  --nav: 62px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { background: var(--paper); }
body {
  margin: 0 auto; max-width: 520px; min-height: 100dvh;
  background: var(--paper); color: var(--ink);
  font: 400 16px/1.55 'Cairo', Tahoma, sans-serif;
  padding-bottom: calc(var(--nav) + env(safe-area-inset-bottom) + 16px);
}
body.night { background: var(--sky); color: #fff; padding-bottom: 0; }
html:has(body.night) { background: var(--sky); }
h1, h2, h3, p { margin: 0; }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--lit); outline-offset: 2px; }
.paper :focus-visible, dialog :focus-visible { outline-color: var(--sky); }
bdi { font-variant-numeric: tabular-nums; }

/* الواجهة: العمارة في الغروب */
.dusk { background: var(--sky); color: #fff; padding: calc(18px + env(safe-area-inset-top)) 16px 0; }
.bname { font-size: 15px; font-weight: 500; opacity: .8; }
.bal-label { margin-top: 12px; font-size: 14px; opacity: .75; }
.bal { font-size: clamp(44px, 14vw, 58px); font-weight: 600; line-height: 1.15; letter-spacing: -.5px; }
.bal small { font-size: 18px; font-weight: 400; opacity: .75; letter-spacing: 0; }
.bal.neg { color: #FF9C8F; }
.funds { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 8px; font-size: 13px; }
.funds a { opacity: .85; text-decoration: none; }

.monthnav { display: flex; align-items: center; justify-content: space-between; margin: 16px 0 14px; }
.monthnav strong { font-weight: 500; }
.monthnav a {
  width: 44px; height: 44px; display: grid; place-items: center;
  text-decoration: none; font-size: 26px; line-height: 1; border-radius: 50%;
  background: rgba(255,255,255,.1);
}
.chev { width: 10px; height: 10px; border: solid currentColor; border-width: 2px 2px 0 0; }
.chev.to-right { transform: translateX(-2px) rotate(45deg); }
.chev.to-left { transform: translateX(2px) rotate(-135deg); }
.monthnav.dark { margin: 10px 0 0; }
.monthnav.dark a { background: var(--card); border: 1px solid var(--line); }

.tower {
  --gap: 12px;
  margin: 0 auto; width: min(100%, calc(var(--cols) * 62px + (var(--cols) - 1) * var(--gap) + 36px));
  background: var(--facade); padding: 18px 18px 0;
  border-radius: 6px 6px 0 0; border-top: 6px solid #356B83;
}
.floor {
  display: grid; grid-template-columns: repeat(var(--cols), 1fr);
  gap: var(--gap); margin-bottom: var(--gap);
}
.win {
  position: relative; aspect-ratio: 1; border: 0; padding: 0; overflow: hidden;
  border-radius: 4px 4px 2px 2px; background: var(--win-off); color: #9DB8C4;
  font-size: clamp(15px, 4.6vw, 19px); font-weight: 600;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.07);
}
.win::before {
  content: ''; position: absolute; inset: auto 0 0; height: var(--fill);
  background: var(--lit); transition: height .25s;
}
.win span { position: relative; }
.win.paid { color: #4A3200; box-shadow: 0 0 18px rgba(255,200,87,.45); }
.win.part { color: #fff; }
.win.part span { text-shadow: 0 1px 2px rgba(0,0,0,.6); }
.win:active { transform: scale(.96); }
.win.exempt {
  color: #D5E2E8;
  background: repeating-linear-gradient(to bottom, #4C7486 0 5px, #3A6172 5px 8px);
}
.win.exempt span { text-shadow: 0 1px 2px rgba(0,0,0,.7); }
.win.closed { outline: 2px dashed rgba(255,255,255,.65); outline-offset: -6px; }
.win.paid.closed { outline-color: rgba(74,50,0,.6); }
.ground { height: 46px; display: flex; justify-content: center; align-items: flex-end; margin-top: 4px; }
.ground i { width: 40px; height: 42px; background: var(--sky); border-radius: 20px 20px 0 0; }

/* تشغيل الأنوار مرة واحدة أول الدخول */
.first-run .win::before { animation: on .4s both; animation-delay: var(--d, 0ms); }
@keyframes on { from { height: 0; opacity: .3; } }

/* الورق */
.paper { padding: 16px; }
.sums { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 0 0 20px; }
.sums div { display: grid; }
.sums span, .sums em { font-size: 13px; font-style: normal; opacity: .75; }
.sums b { font-size: 22px; font-weight: 600; line-height: 1.3; }
.legend { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.dot { width: 10px; height: 12px; border-radius: 2px; display: inline-block; }
.dot.on { background: var(--lit); }
.dot.off { background: var(--win-off); margin-inline-start: 8px; }
.dot.shut { background: repeating-linear-gradient(to bottom, #4C7486 0 2px, #A9BEC7 2px 3px); margin-inline-start: 8px; }
.late { margin-top: 12px; padding: 10px 12px; background: #FBEBE9; color: var(--bad); font-size: 14px; border-radius: 6px; }

h2 { font-size: 17px; font-weight: 600; margin: 22px 0 8px; display: flex; gap: 8px; align-items: baseline; }
h2 small { font-size: 14px; font-weight: 400; color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.list li { border-bottom: 1px solid var(--line); }
.row {
  width: 100%; display: flex; align-items: center; gap: 12px; min-height: 58px;
  padding: 8px 0; background: none; border: 0; text-align: start;
}
.num {
  flex: none; min-width: 38px; height: 44px; padding: 0 4px; display: grid; place-items: center;
  border-radius: 4px 4px 2px 2px; background: var(--win-off); color: #C9DBE3; font-weight: 600;
}
.num.lit { background: var(--lit); color: #4A3200; }
.num.shut { background: repeating-linear-gradient(to bottom, #4C7486 0 5px, #3A6172 5px 8px); color: #fff; }
.tag { display: inline-block; margin-bottom: 10px; padding: 2px 10px; border-radius: 12px; background: #DDE6E8; font-size: 14px; }
.skips { list-style: none; margin: 8px 0 0; padding: 0; max-height: 240px; overflow-y: auto; }
.skips label { display: flex !important; align-items: center; gap: 10px; min-height: 44px; margin: 0 !important; color: var(--ink) !important; font-size: 15px; }
.skips input { width: 20px; min-height: 20px; height: 20px; flex: none; accent-color: var(--sky); }
.skips small { color: var(--muted); }
.who { flex: 1; min-width: 0; display: grid; overflow-wrap: anywhere; }
.who small { color: var(--muted); font-size: 13px; }
.amt { font-weight: 600; white-space: nowrap; }
.amt.due { color: var(--bad); }
.done { padding: 12px 0; color: var(--good); font-weight: 500; }
.fold summary { list-style: none; cursor: pointer; }
.fold summary::-webkit-details-marker { display: none; }
.fold summary h2::after { content: '+'; margin-inline-start: auto; font-weight: 400; color: var(--muted); }
.fold[open] summary h2::after { content: '−'; }
.empty { text-align: center; padding: 32px 0; color: var(--muted); display: grid; gap: 14px; justify-items: center; }

.plain { padding: calc(18px + env(safe-area-inset-top)) 16px 0; display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; }
.plain h1 { font-size: 26px; font-weight: 600; }
.plain .monthnav { width: 100%; }
.gear { font-size: 15px; color: var(--muted); padding: 10px 0; }

.total { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 16px 0 4px; }
.total span { color: var(--muted); font-size: 14px; }
.total b { font-size: 22px; font-weight: 600; white-space: nowrap; }
.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.bars li {
  display: flex; justify-content: space-between; padding: 8px 10px; border-radius: 4px;
  background: linear-gradient(to left, #FFE3A3 var(--w), transparent var(--w));
}
.bars b { font-weight: 600; }
.exp { display: flex; align-items: center; gap: 12px; padding: 10px 0; }
.exp > div { flex: 1; min-width: 0; display: grid; overflow-wrap: anywhere; }
.exp strong { font-weight: 500; }
.exp small { color: var(--muted); font-size: 13px; }

/* النماذج */
.form { display: grid; gap: 14px; }
.form label, .pay label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.form small { font-size: 13px; }
input, select {
  width: 100%; min-height: 48px; padding: 10px 12px; font-size: 16px; color: var(--ink);
  background: var(--card); border: 1px solid var(--line); border-radius: 6px;
}
input[dir='ltr'] { text-align: right; }
input[type='date'] { -webkit-appearance: none; appearance: none; }
.btn {
  display: inline-grid; place-items: center; min-height: 50px; padding: 0 22px; border: 0; border-radius: 6px;
  background: var(--sky); color: #fff; font-weight: 500; text-decoration: none;
}
.btn.wide { width: 100%; }
.btn.ghost { background: none; border: 1px solid var(--sky); color: var(--sky); }
.btn[disabled] { opacity: .6; }
.link { background: none; border: 0; padding: 8px 0; color: var(--muted); text-decoration: underline; font-size: 14px; }
.link.danger { color: var(--bad); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: -6px; }
.chip { min-height: 36px; padding: 0 12px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; font-size: 14px; }

/* نافذة الشقة */
.sheet {
  width: min(100%, 520px); max-width: 100%; max-height: 90dvh; margin: auto auto 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  border: 0; border-radius: 14px 14px 0 0; background: var(--paper); color: var(--ink); overflow-y: auto;
}
.sheet::backdrop { background: rgba(10,28,37,.55); }
.sheet[open] { animation: rise .2s ease-out; }
@keyframes rise { from { transform: translateY(30px); } }
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; }
.sheet-head h3 { font-size: 22px; font-weight: 600; }
.sheet-head p { color: var(--muted); }
.x { width: 44px; height: 44px; margin: -6px; background: none; border: 0; font-size: 28px; line-height: 1; color: var(--muted); }
.facts { margin: 0 0 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.facts dt { font-size: 13px; color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; font-size: 18px; }
.pay { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; }
.pay .chips { grid-column: 1 / -1; margin: 0; }
.sheet-head h3 small { font-size: 14px; font-weight: 400; color: var(--muted); }
.sheet-head a { color: var(--sky); }
.rented summary { cursor: pointer; font-size: 14px; color: var(--sky); font-weight: 500; padding: 4px 0; }
.rented label { margin-top: 10px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.seg input { position: absolute; opacity: 0; width: 0; height: 0; min-height: 0; padding: 0; }
.seg span { display: grid; place-items: center; min-height: 44px; border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--ink); font-size: 15px; }
.seg input:checked + span { background: var(--sky); border-color: var(--sky); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--sky); outline-offset: 2px; }
.expform .only-major { display: none; }
.expform:has([value=major]:checked) .only-major { display: grid; }
.expform:has([value=major]:checked) .chips.only-major { display: flex; }
.expform:has([value=major]:checked) .only-periodic { display: none; }
.fund { display: grid; gap: 2px; padding-bottom: 16px; border-bottom: 1px solid var(--line); }
.fund span { font-size: 14px; color: var(--muted); }
.fund > b { font-size: 26px; font-weight: 600; line-height: 1.3; }
.fund > small { color: var(--muted); font-size: 13px; }
.fund-acts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.fund summary { padding: 10px 0 0; color: var(--sky); font-size: 14px; }
.move { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 15px; }
.move span { color: var(--ink); font-size: 15px; }
.move small { color: var(--muted); }
.list.spaced { margin-top: 10px; }
a.row { color: inherit; text-decoration: none; }
.sums.light { margin-bottom: 10px; }
.sums.light span, .sums.light em { color: var(--muted); opacity: 1; }
.latebox, .pays { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); font-size: 15px; }
.latebox p, .pays p { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.latebox a { display: block; padding: 6px 0; color: var(--bad); }
.pays form { display: flex; justify-content: space-between; align-items: center; }
.pays small { color: var(--muted); }
.edit { display: block; margin-top: 12px; }

.tabs {
  position: fixed; inset: auto 0 0; z-index: 5; margin: auto; max-width: 520px;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a { display: grid; place-items: center; text-decoration: none; color: var(--muted); font-size: 14px; }
.tabs a[aria-current] { color: var(--sky); font-weight: 600; box-shadow: inset 0 3px 0 var(--lit); }

.toast {
  position: fixed; inset: auto 16px calc(var(--nav) + env(safe-area-inset-bottom) + 12px); z-index: 20;
  max-width: 488px; margin: auto; padding: 12px 14px; border-radius: 6px;
  background: var(--good); color: #fff; animation: rise .2s ease-out;
}
.toast.bad { background: var(--bad); }

.login { min-height: 100dvh; padding: 32px 16px; display: grid; align-content: center; gap: 6px; }
.login h1 { font-size: 30px; font-weight: 600; }
.login p { opacity: .75; margin-bottom: 20px; }
.login label { color: #D6E3E8; }
.login .btn { background: var(--lit); color: #4A3200; }

@media (prefers-reduced-motion: reduce) {
  *, *::before { animation: none !important; transition: none !important; }
}
