/* lanaCSR — Wooden palette. Text contrast >= 4.5:1 in both themes; no background textures. */
:root {
  --espresso: #391d1c; --terracotta: #b84432; --terracotta-ink: #9a3424; --crema: #d6b893; --crema-soft: #efe2c9;
  --bg: #f6efe3; --paper: #fbf6ec; --surface: #ffffff;
  --ink: #1f1513; --ink-2: #4a3a35; --ink-3: #6b574d;
  --line: rgba(31, 21, 19, .12); --line-strong: rgba(31, 21, 19, .26);
  --good: #2c6e4f; --good-bg: #e3f1ea; --warn: #8a5a00; --warn-bg: #fbefd5; --bad: #9a3424; --bad-bg: #f8e3de; --info: #2f4f7f; --info-bg: #e4ebf5;
  --radius: 10px; --shadow: 0 1px 2px rgba(57, 29, 28, .06), 0 6px 18px rgba(57, 29, 28, .06);
  --font: 'IBM Plex Sans Arabic', 'Tajawal', system-ui, sans-serif;
  --font-display: 'Tajawal', 'IBM Plex Sans Arabic', system-ui, sans-serif;
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #161010; --paper: #1e1715; --surface: #261d1a;
  --ink: #f6efe3; --ink-2: #ddd0c1; --ink-3: #b9a898;
  --line: rgba(246, 239, 227, .12); --line-strong: rgba(246, 239, 227, .26);
  --terracotta: #e8806b; --terracotta-ink: #f09a87; --crema: #d9bd97; --crema-soft: #3a2d26;
  --good: #79d0a4; --good-bg: #173326; --warn: #f0bf5a; --warn-bg: #3a2c10; --bad: #f09a87; --bad-bg: #3d1d17; --info: #9dbcf0; --info-bg: #1b2840;
  --shadow: none;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--terracotta-ink); }
h1, h2, h3 { font-family: var(--font-display); line-height: 1.25; margin: 0 0 .5rem; color: var(--ink); }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
small, .muted { color: var(--ink-3); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* Shell */
.topbar { display: flex; align-items: center; gap: 1.25rem; padding: .7rem 1.5rem; background: var(--espresso); color: #f6efe3; position: sticky; top: 0; z-index: 10; flex-wrap: wrap; }
.brand { font-family: var(--font-display); font-weight: 800; font-size: 1.2rem; color: #f6efe3; text-decoration: none; white-space: nowrap; }
.brand span { font-weight: 500; color: var(--crema); margin-inline-start: .35rem; }
.brand-lg { font-size: 1.6rem; color: var(--ink); } .brand-lg span { color: var(--terracotta-ink); }
.mainnav { display: flex; gap: .25rem; flex-wrap: nowrap; flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; scrollbar-width: thin; }
.mainnav a { flex: none; color: #efe2c9; text-decoration: none; padding: .35rem .7rem; border-radius: 6px; font-weight: 500; }
.mainnav a:hover { background: rgba(246, 239, 227, .1); }
.mainnav a.is-active { background: #f6efe3; color: var(--espresso); }
.topbar-tools { display: flex; align-items: center; gap: .6rem; }
@media screen and (max-width: 720px) {
  .topbar { padding: .6rem 1rem; gap: .6rem 1rem; }
  .mainnav { order: 3; flex: 1 0 100%; }
  .topbar-tools { margin-inline-start: auto; flex-wrap: wrap; }
  .page { padding: 1rem; }
}
.month-picker select { background: #f6efe3; color: var(--espresso); border: 0; border-radius: 6px; padding: .3rem .5rem; font: inherit; }
.icon-btn { background: transparent; border: 1px solid rgba(246, 239, 227, .35); color: #f6efe3; border-radius: 6px; padding: .2rem .55rem; cursor: pointer; font: inherit; }
.logout { display: flex; align-items: center; gap: .4rem; margin: 0; }
.logout .who { color: var(--crema); }
.logout .link-btn { color: #f6efe3; }
.page { max-width: 1360px; margin: 0 auto; padding: 1.5rem; display: grid; gap: 1.25rem; }
.footer { text-align: center; color: var(--ink-3); padding: 2rem 1rem; font-size: .85rem; }

/* Building blocks */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem 1.4rem; box-shadow: var(--shadow); min-width: 0; }
.card > h1 small, .card > h2 small { font-size: .8rem; font-weight: 500; margin-inline-start: .4rem; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.25rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1.25rem; }
.grid-main-side { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 1.25rem; align-items: start; }
@media screen and (max-width: 1000px) { .grid-2, .grid-3, .grid-main-side { grid-template-columns: 1fr; } }
.stack { display: grid; gap: .9rem; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .55rem 1.1rem; border-radius: 8px; border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn-primary { background: var(--terracotta); border-color: var(--terracotta); color: #fff; }
[data-theme="dark"] .btn-primary { color: #1b0f0c; }
.btn-danger { border-color: var(--bad); color: var(--bad); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.link-btn { background: none; border: 0; padding: 0; color: var(--terracotta-ink); font: inherit; cursor: pointer; text-decoration: underline; }
label { display: grid; gap: .3rem; font-weight: 500; color: var(--ink-2); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--line-strong); border-radius: 8px; padding: .5rem .65rem; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible { outline: 3px solid color-mix(in srgb, var(--terracotta) 45%, transparent); outline-offset: 1px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .9rem; align-items: end; }
.flash { padding: .7rem 1rem; border-radius: 8px; font-weight: 500; }
.flash.is-good { background: var(--good-bg); color: var(--good); }
.flash.is-bad { background: var(--bad-bg); color: var(--bad); }
.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--crema-soft); color: var(--ink-2); white-space: nowrap; }
.badge.is-good { background: var(--good-bg); color: var(--good); }
.badge.is-warn { background: var(--warn-bg); color: var(--warn); }
.badge.is-bad { background: var(--bad-bg); color: var(--bad); }
.badge.is-info { background: var(--info-bg); color: var(--info); }
.tabs { display: flex; gap: .3rem; flex-wrap: wrap; border-bottom: 1px solid var(--line-strong); }
.tabs a { padding: .5rem .9rem; text-decoration: none; color: var(--ink-2); border-bottom: 3px solid transparent; font-weight: 600; }
.tabs a.is-active { color: var(--ink); border-bottom-color: var(--terracotta); }

/* Tables */
.table-wrap { overflow-x: auto; position: relative; } /* relative: keeps absolutely-positioned .sr-only cells from escaping the scroll box */
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th.num { direction: rtl; unicode-bidi: normal; }
.table th.num, .table td.num { text-align: right; }
.table td.num { white-space: nowrap; }
.table th { text-align: start; font-weight: 600; color: var(--ink-3); font-size: .8rem; padding: .5rem .6rem; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
.table td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:hover td { background: color-mix(in srgb, var(--crema-soft) 40%, transparent); }
.table .clip { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; gap: .8rem; }
.row-actions form { margin: 0; }

@media screen and (max-width: 720px) {
  .table.wide { min-width: 760px; }
  .table.nowrap td { white-space: nowrap; } /* one-line rows (branch list) instead of 190px-tall wrapped ones */
}

/* Auth */
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 2rem; box-shadow: var(--shadow); display: grid; gap: 1rem; }
.empty-state { text-align: center; padding: 3rem 1.5rem; }

/* Import screens */
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.actions { display: flex; gap: .75rem; flex-wrap: wrap; align-items: center; }
.actions form { margin: 0; }
.upload-form { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1.25rem; margin: 0 0 1rem; }
.facts dt { color: var(--ink-3); }
.facts dd { margin: 0; font-weight: 600; }
.checks { list-style: none; padding: 0; margin: 0; display: grid; gap: .35rem; }
.checks .is-good { color: var(--good); }
.checks .is-bad { color: var(--bad); font-weight: 600; }
.hbar { display: grid; grid-template-columns: 3.5rem 1fr 4.5rem; gap: .6rem; align-items: center; margin: .25rem 0; }
.hbar-label { color: var(--ink-2); }
.hbar-track { height: 10px; background: var(--crema-soft); border-radius: 999px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; background: var(--crema); border-radius: 999px; }
.hbar-fill.is-bad { background: var(--terracotta); }

/* Page header, KPIs, small parts */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 1.6rem; }
.page-head p { margin: .25rem 0 0; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.kpi-label { color: var(--ink-3); font-size: .85rem; font-weight: 600; }
.kpi-value { font-family: var(--font-display); font-size: 2rem; font-weight: 800; line-height: 1.2; margin: .25rem 0; color: var(--ink); text-align: start; }
.kpi-delta { font-size: .85rem; font-weight: 600; }
.kpi-delta.is-bad { color: var(--bad); }
.kpi-delta.is-good { color: var(--good); }
.kpi-delta.is-neutral { color: var(--ink-3); }
.kpi-note { color: var(--ink-2); font-size: .85rem; margin-top: .2rem; }
.small { font-size: .82rem; }
.code { font: 600 .75rem/1.6 ui-monospace, monospace; background: var(--crema-soft); color: var(--ink-2); padding: 0 .35rem; border-radius: 4px; direction: ltr; unicode-bidi: isolate; }
.pos { color: var(--good); font-weight: 600; }
.neg { color: var(--bad); font-weight: 600; }
.plain { list-style: none; padding: 0; margin: 0; display: grid; gap: .35rem; }
.flash.is-warn { background: var(--warn-bg); color: var(--warn); }
.stars { color: var(--warn); letter-spacing: 1px; }

/* Findings */
.findings { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; }
.finding { display: grid; grid-template-columns: auto 1fr auto; gap: .3rem .75rem; align-items: start; padding: .7rem .85rem; border-radius: 8px; background: var(--paper); border-inline-start: 4px solid var(--line-strong); }
.finding p { margin: 0; color: var(--ink); }
.finding a { white-space: nowrap; font-size: .85rem; }
.finding.is-critical { border-inline-start-color: var(--bad); }
.finding.is-warning { border-inline-start-color: var(--warn); }
.finding.is-positive { border-inline-start-color: var(--good); }
.finding.is-info { border-inline-start-color: var(--info); }
@media screen and (max-width: 600px) { .finding { grid-template-columns: 1fr; } }

/* Charts and bars */
.chart-box { position: relative; width: 100%; }
.hbars .hbar { grid-template-columns: minmax(7rem, 15rem) 1fr 7.5rem; }
.hbar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: right; }
.hbar-fill.is-good { background: var(--good); }
.hbar-fill.is-info { background: var(--info); }
.stack-bar { width: 40%; white-space: nowrap; }
.stack-bar span { display: inline-block; height: 8px; vertical-align: middle; }
.seg-pos { background: var(--good); }
.seg-neg { background: var(--terracotta); }
.table.compact td, .table.compact th { padding: .35rem .45rem; }
th.sortable { cursor: pointer; }
th[aria-sort="ascending"]::after { content: ' ▲'; }
th[aria-sort="descending"]::after { content: ' ▼'; }

/* Region map */
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; margin-bottom: .75rem; }
.seg button { background: transparent; border: 0; padding: .4rem .8rem; font: inherit; color: var(--ink-2); cursor: pointer; }
.seg button.is-active { background: var(--espresso); color: #f6efe3; }
.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.25rem; align-items: start; }
@media screen and (max-width: 900px) { .map-layout { grid-template-columns: 1fr; } }
.sa-map { width: 100%; height: auto; display: block; }
.sa-map path { stroke: var(--surface); stroke-width: 1.5; cursor: pointer; transition: fill .2s; }
.sa-map .heat-0 { fill: #cfcac6; cursor: default; }
.sa-map .heat-1 { fill: #f0dcc4; }
.sa-map .heat-2 { fill: #e3b693; }
.sa-map .heat-3 { fill: #d08764; }
.sa-map .heat-4 { fill: #b84432; }
.sa-map .heat-5 { fill: #7a2318; }
[data-theme="dark"] .sa-map .heat-0 { fill: #57524f; }
[data-theme="dark"] .sa-map .heat-1 { fill: #4a3229; }
[data-theme="dark"] .sa-map .heat-2 { fill: #6e3a2a; }
[data-theme="dark"] .sa-map .heat-3 { fill: #9a4a33; }
[data-theme="dark"] .sa-map .heat-4 { fill: #c9603f; }
[data-theme="dark"] .sa-map .heat-5 { fill: #ef8a6c; }
.sa-map path.is-selected, .sa-map path:focus-visible { stroke: var(--ink); stroke-width: 3; outline: none; }
.map-detail { background: var(--paper); border-radius: 8px; padding: 1rem; min-height: 12rem; }
.map-detail h3 { margin-top: 0; }

/* Heatmap */
.heatmap { border-collapse: separate; border-spacing: 2px; font-size: .75rem; }
.heatmap th { color: var(--ink-3); font-weight: 600; padding: .15rem .3rem; white-space: nowrap; }
.heatmap td { min-width: 1.9rem; height: 1.7rem; text-align: center; border-radius: 4px; background: color-mix(in srgb, var(--crema) calc(var(--heat, 0) * 100%), var(--paper)); }
.heat-low { color: var(--ink); font-weight: 800; border-inline-start: 3px solid var(--terracotta); padding-inline-start: .2rem; }
[data-theme="dark"] .heatmap td { background: color-mix(in srgb, var(--terracotta) calc(var(--heat, 0) * 50%), var(--surface)); }

/* Filters, quotes, comments, paging */
.filters { display: flex; gap: .9rem; flex-wrap: wrap; align-items: end; margin-bottom: .75rem; }
.filters label { min-width: 10rem; max-width: 100%; }
.filters select, .filters input { max-width: 100%; min-width: 0; }
@media screen and (max-width: 720px) { .filters label { min-width: 0; flex: 1 1 100%; } }
.quote { margin: .5rem 0; padding: .6rem .85rem; border-radius: 8px; background: var(--paper); border-inline-start: 3px solid var(--line-strong); }
.quote.is-positive { border-inline-start-color: var(--good); }
.quote.is-negative { border-inline-start-color: var(--bad); }
.quote p { margin: 0 0 .25rem; }
.comments { list-style: none; padding: 0; margin: 0; display: grid; gap: .6rem; }
.comment { padding: .7rem .85rem; border: 1px solid var(--line); border-radius: 8px; }
.comment p { margin: 0 0 .35rem; }
.chips { display: inline-flex; gap: .3rem; flex-wrap: wrap; }
.pager { display: flex; gap: .75rem; align-items: center; justify-content: center; margin-top: 1rem; }

@media screen and (max-width: 720px) {
  /* Bars need room: label and value share the first row, the bar gets its own full-width row. */
  .hbars .hbar { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "label val" "track track"; row-gap: .15rem; margin: .5rem 0; }
  .hbars .hbar-label { grid-area: label; }
  .hbars .hbar-track { grid-area: track; }
  .hbars .hbar > .num { grid-area: val; white-space: nowrap; }
}

/* Monthly report and print */
.report .kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); }
@media screen and (max-width: 900px) { .report .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media print {
  /* Full light palette so a page printed from the dark theme is legible (every token the screen dark theme overrides). */
  :root, [data-theme="dark"] {
    --terracotta: #b84432; --terracotta-ink: #9a3424; --crema: #d6b893; --crema-soft: #f1e6d3;
    --bg: #fff; --paper: #faf6ee; --surface: #fff;
    --ink: #111; --ink-2: #333; --ink-3: #555; --line: #ddd; --line-strong: #bbb;
    --good: #2c6e4f; --good-bg: #e3f1ea; --warn: #8a5a00; --warn-bg: #fbefd5; --bad: #9a3424; --bad-bg: #f8e3de; --info: #2f4f7f; --info-bg: #e4ebf5;
    --shadow: none; color-scheme: light;
  }
  /* Bars, badges and heat cells are background colours, which browsers drop when printing unless asked. */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .topbar, .footer, .no-print, .flash { display: none !important; }
  body { background: #fff; font-size: 9.5pt; line-height: 1.4; }
  .page { max-width: none; padding: 0; gap: 6px; }
  .card, .kpi, .finding, .quote, .branch-pair, .trend-grid, .hbar, .table tr { break-inside: avoid; box-shadow: none; }
  /* Density: the monthly report must fit on two A4 pages. */
  .report { gap: 6px; }
  .report h1 { font-size: 14pt; margin-bottom: .15rem; }
  .report h2 { font-size: 11pt; margin-bottom: .25rem; }
  .report h3 { font-size: 9.5pt; margin: .3rem 0 .2rem; }
  .report .card { padding: .45rem .65rem; border-radius: 6px; }
  .report .kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .report .kpi { padding: .35rem .5rem; border-radius: 6px; }
  .report .kpi-label, .report .kpi-delta, .report .kpi-note { font-size: 8.5pt; margin-top: 0; }
  .report .kpi-value { font-size: 15pt; margin: .05rem 0; }
  .report .findings { gap: 3px; }
  .report .finding { grid-template-columns: auto 1fr; gap: .1rem .5rem; align-items: baseline; padding: .2rem .5rem; border-radius: 4px; }
  .report .finding p { line-height: 1.35; }
  .report .badge { font-size: 8.5pt; padding: .02rem .4rem; white-space: nowrap; }
  .report .grid-2 { gap: 6px; }
  .report > .grid-2:first-of-type { grid-template-columns: 1fr; }
  .report .branch-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 .9rem; }
  .report .table { font-size: 8.5pt; }
  .report .table.compact th, .report .table.compact td { padding: .15rem .3rem; }
  .report .trend-grid { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 0 .9rem; align-items: start; }
  .report .chart-box { height: 210px !important; }
  .report .trend-bars h3:first-child { margin-top: 0; }
  /* Half an A4 page is narrow: label and value share a row and the bar gets its own, as on a phone. */
  .hbars .hbar { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "label val" "track track"; row-gap: .05rem; margin: .2rem 0; }
  .hbars .hbar-label { grid-area: label; }
  .hbars .hbar-track { grid-area: track; height: 7px; }
  .hbars .hbar > .num { grid-area: val; white-space: nowrap; }
  .report .quote { margin: .2rem 0; padding: .25rem .5rem; border-radius: 4px; }
  .report .quote p { margin: 0 0 .1rem; line-height: 1.35; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3; overflow: hidden; }
  .report .quote footer { font-size: 8.5pt; }
  .report .muted.small { font-size: 8.5pt; line-height: 1.35; margin: 0; padding: 0 .65rem; }
  a { color: inherit; text-decoration: none; }
  @page { size: A4; margin: 12mm; }
}

/* While printing, app.js adds .is-printing so charts are redrawn at the printed width (the print layout itself runs after
   the beforeprint handler, so the canvases must already be sized for it). 186mm = A4 minus the 12mm page margins. */
html.is-printing body { width: 186mm; margin: 0; }
html.is-printing .page { max-width: none; padding: 0; gap: 8px; }
html.is-printing .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
html.is-printing .report > .grid-2:first-of-type { grid-template-columns: 1fr; }
html.is-printing .report .chart-box { height: 210px !important; }
html.is-printing .report .trend-grid { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 0 .9rem; }
