/* Mobile first. One column on a phone with a bottom tab bar; a centred column with
   top tabs on wider screens. Colours come from the tokens below, in light and dark. */
:root {
  --bg: #f6f5ef;
  --surface: #ffffff;
  --ink: #182d2b;
  --muted: #5b6c66;
  --line: #dde3dc;
  --brand: #12483f;
  --brand-ink: #ffffff;
  --accent: #f0dbaa;
  --accent-ink: #203b34;
  --soft: #eaf0ea;
  --warn: #9a422c;
  --gain: #1a7a4c;
  --loss: #b3432b;
  --radius: 14px;
  --tabs: 62px;
  font: 16px/1.6 system-ui, -apple-system, "SF Arabic", "Geeza Pro", "Noto Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1917;
    --surface: #172623;
    --ink: #e7efea;
    --muted: #9db1a9;
    --line: #2a403b;
    --brand: #123a33;
    --accent: #e6cd92;
    --accent-ink: #14261f;
    --soft: #1e312d;
    --warn: #f0a48d;
    --gain: #6fd3a0;
    --loss: #f09a85;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0 auto;
  max-width: 1040px;
  padding: 0 14px calc(var(--tabs) + 28px + env(safe-area-inset-bottom));
  overflow-wrap: anywhere;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.15rem; line-height: 1.3; }
h2 { font-size: 1.05rem; margin: 26px 2px 10px; }
h3 { font-size: .95rem; margin: 18px 2px 8px; }
small { font-size: .82rem; color: var(--muted); }
a { color: inherit; text-decoration-color: var(--muted); text-underline-offset: 3px; }
bdi.num, .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.none { color: var(--muted); }
.gain { color: var(--gain); }
.loss { color: var(--loss); }
.warn { color: var(--warn); }
.hint { color: var(--muted); font-size: .86rem; margin: 8px 2px; }
.empty { color: var(--muted); text-align: center; padding: 26px 12px; border: 1px dashed var(--line); border-radius: var(--radius); margin: 8px 0; }
.refs { font-size: .84rem; color: var(--muted); margin-top: 6px; }

button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); padding: 9px 14px; font-weight: 600; min-height: 42px; }
button:disabled { opacity: .55; cursor: progress; }
button.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
button.small { min-height: 34px; padding: 4px 12px; font-size: .88rem; }
button.danger { color: var(--warn); }
button.link { border: 0; background: none; padding: 0; min-height: 0; font-weight: 500; text-decoration: underline; text-underline-offset: 3px; color: var(--muted); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

input, select, textarea { width: 100%; min-height: 44px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
input[type=checkbox] { width: 20px; min-height: 20px; height: 20px; flex: none; accent-color: var(--brand); }
textarea { resize: vertical; }
label { display: grid; gap: 4px; font-weight: 600; font-size: .9rem; min-width: 0; }
label.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 500; }

/* ── header */
.bar { position: sticky; top: 0; z-index: 5; margin: 0 -14px; padding: calc(10px + env(safe-area-inset-top)) 14px 10px; background: var(--brand); color: var(--brand-ink); }
.brand { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.chip { min-height: 30px; padding: 2px 12px; border-radius: 999px; border: 1px solid #ffffff40; background: #ffffff1c; color: inherit; font-size: .8rem; font-weight: 500; white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 8px; flex: none; }
.plus { width: 38px; min-height: 38px; padding: 0; border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-size: 1.6rem; line-height: 1; font-weight: 400; }
.chip[data-state=stale] { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.scope { display: grid; grid-template-columns: 1.4fr 1fr; gap: 8px; margin-top: 10px; }
.scope label { font-weight: 500; }
.scope span { font-size: .74rem; opacity: .8; }
.scope select, .scope input { min-height: 38px; padding: 4px 8px; border-color: #ffffff40; background: #ffffff14; color: inherit; color-scheme: dark; }
.scope option { color: #182d2b; }
body.setup .scope, body.setup #tabs, body.setup .tools { display: none; }

/* ── tabs */
#tabs { position: fixed; inset: auto 0 0 0; z-index: 6; display: grid; grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
#tabs button { display: grid; justify-items: center; gap: 1px; height: var(--tabs); min-height: 0; padding: 7px 2px 5px; border: 0; border-radius: 0; background: none; color: var(--muted); font-size: .72rem; font-weight: 500; }
#tabs button[aria-current=page] { color: var(--brand); font-weight: 700; }
@media (prefers-color-scheme: dark) { #tabs button[aria-current=page] { color: var(--accent); } }

/* ── building blocks */
.hero { margin-top: 14px; padding: 18px; border-radius: var(--radius); background: var(--brand); color: var(--brand-ink); }
.hero small { color: inherit; opacity: .8; }
.hero .big { display: block; font-size: clamp(1.6rem, 7vw, 2.3rem); line-height: 1.35; margin: 2px 0; }
.hero .big small { font-size: .5em; font-weight: 500; }
.hero p { font-size: .9rem; opacity: .9; }
.hero .warn { color: var(--accent); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; margin-top: 10px; }
.tile { display: grid; gap: 2px; align-content: start; text-align: start; min-height: 0; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); font-weight: 400; }
.tile b { font-size: 1.12rem; }
.tile span { font-size: .84rem; color: var(--muted); }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 4px; }
.actions button { flex: 1 1 auto; }

.list, .cards, .years, .timeline, .bullets, .steps { margin: 0; padding: 0; list-style: none; }
.list { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.item { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; min-height: 58px; padding: 10px 14px; border: 0; border-radius: 0; background: none; text-align: start; font-weight: 400; }
button.item:hover { background: var(--soft); }
.item .main, .item .end { display: grid; gap: 1px; min-width: 0; }
.item .end { justify-items: end; text-align: end; flex: none; max-width: 55%; }
.item b { font-weight: 650; }
.group { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-top: 20px; color: var(--muted); }
.group span:first-child { color: var(--ink); }

.cards { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); }
.card { display: grid; gap: 6px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.calc { font-size: 1.02rem; }
.tag { display: inline-block; padding: 1px 10px; border-radius: 999px; background: var(--soft); color: var(--muted); font-size: .78rem; font-weight: 600; white-space: nowrap; }
.tag.ok { color: var(--gain); }
.tag.warn { color: var(--warn); }

.segments { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; margin-top: 14px; padding: 4px; border-radius: 12px; background: var(--soft); }
.segments button { min-height: 38px; padding: 4px 6px; border: 0; background: none; font-size: .88rem; font-weight: 500; color: var(--muted); }
.segments button[aria-selected=true] { background: var(--surface); color: var(--ink); font-weight: 700; box-shadow: 0 1px 3px #00000022; }

.notes details, .fold { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 0 14px; }
.notes details + details { margin-top: 8px; }
summary { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 12px 0; cursor: pointer; font-weight: 650; list-style: none; }
summary::-webkit-details-marker { display: none; }
summary::after { content: "⌄"; color: var(--muted); flex: none; }
details[open] > summary::after { content: "⌃"; }
summary small { flex: none; margin-inline-start: auto; font-weight: 400; }
details > p, details > ul { padding-bottom: 12px; }

.bullets { display: grid; gap: 8px; }
.bullets li { padding: 10px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); font-size: .93rem; }
.fold .bullets li { border: 0; padding: 4px 0; background: none; }
.steps { display: grid; gap: 8px; counter-reset: step; font-size: .93rem; }
.steps li { counter-increment: step; padding-inline-start: 34px; position: relative; }
.steps li::before { content: counter(step); position: absolute; inset-inline-start: 0; top: 1px; width: 24px; height: 24px; border-radius: 50%; background: var(--soft); text-align: center; font-size: .8rem; font-weight: 700; line-height: 24px; }

.years { display: grid; gap: 8px; }
.years li { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.years .hint { margin: 0; padding: 0 14px 10px; }
.bar > i, .years .bar i { display: block; height: 6px; border-radius: 0 3px 3px 0; background: var(--accent); inline-size: 0; }
.years .bar { background: var(--soft); margin-bottom: 8px; }
.years .bar:last-child { margin-bottom: 0; }

.scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; text-align: start; font-size: .9rem; }
th, td { padding: 9px 12px; text-align: start; white-space: nowrap; }
thead th { color: var(--muted); font-size: .8rem; font-weight: 600; }
tbody tr { border-top: 1px solid var(--line); }
.cells td { white-space: normal; }
.cell { display: inline-block; margin: 0 0 4px 12px; }
.cell .ref { color: var(--muted); font-size: .78rem; }
code { font: .82rem ui-monospace, Menlo, monospace; background: var(--soft); border-radius: 5px; padding: 1px 5px; }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin: 12px 0 0; }
.facts div { padding: 10px 12px; border-radius: 12px; background: var(--soft); }
.facts dt { font-size: .78rem; color: var(--muted); }
.facts dd { margin: 0; font-weight: 650; }
.timeline { display: grid; gap: 10px; }
.timeline li { display: grid; gap: 4px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; }
.chat { display: grid; gap: 8px; }
.chat article { padding: 10px 12px; border-radius: 12px; background: var(--soft); }
.chat article.mine { background: none; border: 1px solid var(--line); }
.chat p, .excerpt { white-space: pre-wrap; font-size: .93rem; }

form.card, form[data-form] { display: grid; gap: 12px; }
form.card { margin-top: 12px; }
.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-items: end; }
.fields .wide { grid-column: 1 / -1; }
.wide button { width: 100%; }
form.inline { grid-template-columns: 1fr 1fr auto; align-items: end; margin: 14px 0 0; }
[data-error]:empty { display: none; }

/* ── sheet and toast */
dialog { width: 100%; max-width: 640px; max-height: 92dvh; margin: auto auto 0; padding: 0; border: 0; border-radius: 20px 20px 0 0; background: var(--bg); color: var(--ink); overflow: hidden; }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: #0b1f1b99; }
#sheetBody { overflow-y: auto; padding: 18px 16px calc(22px + env(safe-area-inset-bottom)); }
#sheetBody > h2:first-child, #sheetBody form > h2:first-child { margin: 0 0 4px; padding-inline-end: 44px; font-size: 1.15rem; }
dialog .close { position: absolute; top: 10px; inset-inline-end: 10px; z-index: 1; width: 38px; min-height: 38px; padding: 0; border-radius: 50%; font-size: 1.3rem; line-height: 1; }

#toast { position: fixed; z-index: 20; inset-inline: 14px; bottom: calc(var(--tabs) + 20px + env(safe-area-inset-bottom)); max-width: 460px; margin: 0 auto; padding: 12px 16px; border-radius: 12px; background: #182d2b; color: #fff; font-size: .92rem; text-align: center; opacity: 0; translate: 0 12px; pointer-events: none; transition: opacity .2s, translate .2s; }
#toast.show { opacity: 1; translate: 0 0; }
#toast.error { background: #7d2f1c; }

@media (max-width: 339px) {
  .fields { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
  form.inline { grid-template-columns: 1fr 1fr; }
  form.inline button { grid-column: 1 / -1; }
}

@media (min-width: 820px) {
  body { padding: 0 24px 60px; }
  .bar { margin: 18px 0 0; padding: 18px 22px; border-radius: 18px; position: static; display: flex; align-items: center; justify-content: space-between; gap: 24px; }
  .brand { gap: 16px; }
  h1 { font-size: 1.5rem; }
  .scope { margin: 0; min-width: 420px; }
  #tabs { position: sticky; top: 0; inset: auto; display: flex; gap: 4px; margin: 12px 0 0; padding: 6px; border: 1px solid var(--line); border-radius: 14px; }
  #tabs button { display: flex; align-items: center; gap: 8px; flex: 1; justify-content: center; height: 44px; border-radius: 10px; font-size: .95rem; }
  #tabs button[aria-current=page] { background: var(--soft); }
  #toast { bottom: 32px; }
  dialog { margin: auto; border-radius: 20px; max-height: 86dvh; }
}

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