/* ظِلّ Shade — dashboard.
   Palette validated with the dataviz palette validator:
   · sub-bar categorical  #C08A2E / #A93B12 / #2A78D6  — worst all-pairs CVD ΔE 19.9
   · priority / ndvi ramps — single hue, monotone L, adjacent ΔL >= 0.06
   · lst ramp — semantic-heat exception, always shown with a scale legend        */

:root {
  /* surfaces & ink */
  --paper: #FAF9F6;
  --card: #FFFFFF;
  --ink: #16211C;
  --ink-2: #4C5A50;
  --muted: #7B8880;
  --hairline: #E6E2D8;
  --hairline-strong: #D3CEC0;

  /* brand */
  --green: #17694A;
  --green-deep: #0F4C36;
  --green-wash: #EAF2ED;

  /* categorical series (validated trio) */
  --s-gap: #C08A2E;   /* الفجوة الخضراء — aridity */
  --s-heat: #A93B12;  /* الحرارة */
  --s-pop: #2A78D6;   /* السكان */
  --s-gap-track: #F3E7CF;
  --s-heat-track: #F5DCD2;
  --s-pop-track: #D9E6F8;

  /* map states */
  --state-green: #BFDCC9;        /* already >= 15% green cover -> no priority */
  --state-green-ink: #2E7D57;
  --state-nodata: #DCDAD2;

  --radius: 10px;
  --shadow-1: 0 1px 2px rgba(22,33,28,.05), 0 4px 14px rgba(22,33,28,.05);
  --shadow-2: 0 6px 28px rgba(22,33,28,.14);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%;
  font-family: 'IBM Plex Sans Arabic', system-ui, 'Segoe UI', Tahoma, sans-serif;
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; flex-direction: column; height: 100%; }

/* ───────── header ───────── */
header {
  display: flex; align-items: center; gap: 16px;
  padding: 0 18px; height: 58px; flex-shrink: 0;
  background: var(--card);
  border-bottom: 1px solid var(--hairline);
}
.brand { display: flex; align-items: baseline; gap: 8px; margin-inline-end: 4px; }
.brand .ar { font-size: 21px; font-weight: 700; color: var(--green); letter-spacing: -.2px; }
.brand .en {
  font-size: 11px; font-weight: 600; letter-spacing: .18em; color: var(--muted);
  direction: ltr; text-transform: uppercase;
}
.brand small { font-size: 12px; font-weight: 400; color: var(--muted); }

/* segmented control for the 3 layers */
.chips {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--paper); border: 1px solid var(--hairline); border-radius: 8px;
}
.chip {
  border: 0; background: transparent; border-radius: 6px;
  padding: 6px 14px; font-size: 13px; font-weight: 600;
  color: var(--ink-2); cursor: pointer; font-family: inherit;
  transition: background .12s, color .12s;
}
.chip:hover { color: var(--ink); }
.chip.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow-1); }

.controls { display: flex; align-items: center; gap: 8px; margin-inline-start: auto; }
.field { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
select {
  font-family: inherit; font-size: 13px; font-weight: 600; color: var(--ink);
  padding: 6px 10px; border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--card); cursor: pointer;
}
.switch { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ink-2); cursor: pointer; }
.switch input { accent-color: var(--green); width: 15px; height: 15px; cursor: pointer; }

.btn {
  font-family: inherit; font-size: 13px; font-weight: 600;
  padding: 7px 13px; border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--card); color: var(--ink); cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
  transition: border-color .12s, background .12s;
}
.btn:hover { border-color: var(--hairline-strong); background: var(--paper); }
.btn.on { background: var(--green-wash); border-color: #BBD5C6; color: var(--green-deep); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-deep); border-color: var(--green-deep); }
:is(.chip, .btn, select, input):focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }

/* ───────── layout ───────── */
main { display: flex; flex: 1; min-height: 0; }

#panel {
  width: 372px; flex-shrink: 0;
  background: var(--card);
  border-inline-end: 1px solid var(--hairline);
  display: flex; flex-direction: column; min-height: 0;
}

/* stat strip */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--hairline); border-bottom: 1px solid var(--hairline); }
.stat { background: var(--card); padding: 12px 14px; }
.stat .l { font-size: 11.5px; color: var(--muted); margin-bottom: 3px; }
.stat .v { font-size: 20px; font-weight: 700; line-height: 1.15; letter-spacing: -.3px; }
.stat .v small { font-size: 12px; font-weight: 600; color: var(--muted); margin-inline-start: 3px; }
.stat.heat .v { color: var(--s-heat); }
.stat.greenish .v { color: var(--state-green-ink); }

.panel-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 8px; }
.panel-head h2 { font-size: 15px; margin: 0; flex: 1; font-weight: 700; }
.iconbtn {
  border: 1px solid var(--hairline); background: var(--card); border-radius: 7px;
  padding: 5px 9px; font-size: 12px; font-weight: 600; color: var(--ink-2);
  cursor: pointer; font-family: inherit;
}
.iconbtn.on { background: var(--green-wash); border-color: #BBD5C6; color: var(--green-deep); }

#rank-list { flex: 1; overflow-y: auto; padding: 0 10px 14px; }

.rank-row {
  display: grid; grid-template-columns: 26px 1fr auto;
  gap: 6px 10px; align-items: center;
  padding: 9px 8px; border-radius: 9px; cursor: pointer;
  transition: background .1s;
}
.rank-row:hover, .rank-row.active { background: var(--paper); }
.rank-row.active { box-shadow: inset 0 0 0 1px var(--hairline-strong); }
.rank-row .pos {
  width: 26px; height: 24px; border-radius: 6px;
  display: grid; place-items: center;
  font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums;
  background: var(--paper); color: var(--ink-2); border: 1px solid var(--hairline);
}
.rank-row.top .pos { background: var(--ink); color: #fff; border-color: var(--ink); }
.rank-row .name { font-size: 14px; font-weight: 600; }
.rank-row .score { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.rank-row.zero .score { color: var(--state-green-ink); font-size: 11.5px; font-weight: 600; }
.rank-row .subbars { grid-column: 2 / 4; display: grid; gap: 4px; }

/* meter: track is a lighter step of the fill's own hue */
.subbar { display: grid; grid-template-columns: 34px 1fr; align-items: center; gap: 7px; }
.subbar .lbl { font-size: 10.5px; color: var(--muted); }
.subbar .track { height: 5px; border-radius: 99px; overflow: hidden; }
.subbar .fill { display: block; height: 100%; border-radius: 99px; }
.subbar.gap  .track { background: var(--s-gap-track); }
.subbar.gap  .fill { background: var(--s-gap); }
.subbar.heat .track { background: var(--s-heat-track); }
.subbar.heat .fill { background: var(--s-heat); }
.subbar.pop  .track { background: var(--s-pop-track); }
.subbar.pop  .fill { background: var(--s-pop); }

/* table view twin — every value reachable without colour */
#rank-table { display: none; flex: 1; overflow-y: auto; padding: 0 12px 16px; }
body.table-view #rank-table { display: block; }
body.table-view #rank-list { display: none; }
#rank-table table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
#rank-table th, #rank-table td { padding: 7px 6px; text-align: right; border-bottom: 1px solid var(--hairline); }
#rank-table th { font-size: 11px; color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--card); }
#rank-table td { font-variant-numeric: tabular-nums; }
#rank-table td.nm { font-weight: 600; font-variant-numeric: normal; }
#rank-table tbody tr:hover td { background: var(--paper); cursor: pointer; }
#rank-table .z { color: var(--state-green-ink); font-weight: 600; }

/* ───────── maps ───────── */
#maps { flex: 1; display: flex; min-width: 0; }
.map-box { flex: 1; position: relative; min-width: 0; }
.map { position: absolute; inset: 0; background: #EFEDE6; }
#compare-box { border-inline-start: 1px solid var(--hairline-strong); display: none; }
body.compare #compare-box { display: block; }

.map-label {
  position: absolute; top: 12px; inset-inline-start: 12px; z-index: 700;
  background: rgba(255,255,255,.95); border: 1px solid var(--hairline);
  border-radius: 8px; padding: 5px 12px; font-size: 12.5px; font-weight: 700;
  box-shadow: var(--shadow-1);
}

/* legend: ramp + explicit state chips (colour is never the only channel) */
.legend {
  position: absolute; bottom: 16px; inset-inline-start: 12px; z-index: 700;
  background: rgba(255,255,255,.96); border: 1px solid var(--hairline);
  border-radius: var(--radius); padding: 10px 12px; min-width: 194px;
  box-shadow: var(--shadow-1); font-size: 11.5px; color: var(--muted);
}
.legend .title { font-weight: 700; color: var(--ink); font-size: 12px; display: block; margin-bottom: 7px; }
.legend .ramp-wrap { position: relative; }
.legend .ramp { height: 8px; border-radius: 99px; }
.legend .tgt { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink); border-radius: 2px; }
.legend .tgt::after { content: ""; position: absolute; top: -3px; left: -2px; border: 3px solid transparent; border-top-color: var(--ink); }
.legend .lchip i.tgt-key { width: 2px; height: 12px; border-radius: 1px; background: var(--ink); border: none; }
.legend .ends { display: flex; justify-content: space-between; margin-top: 4px; font-variant-numeric: tabular-nums; }
.legend .chips2 { margin-top: 9px; padding-top: 8px; border-top: 1px solid var(--hairline); display: grid; gap: 5px; }
.legend .lchip { display: flex; align-items: center; gap: 7px; color: var(--ink-2); }
.legend .lchip i { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; border: 1px solid rgba(22,33,28,.18); }

/* hover tooltip on polygons */
.map-tip {
  position: absolute; z-index: 800; pointer-events: none;
  background: var(--ink); color: #fff; border-radius: 7px;
  padding: 6px 10px; font-size: 12px; font-weight: 600;
  transform: translate(-50%, -140%); white-space: nowrap;
  box-shadow: var(--shadow-2); opacity: 0; transition: opacity .1s;
}
.map-tip .v { font-weight: 400; color: #C9D6CE; margin-inline-start: 6px; font-variant-numeric: tabular-nums; }
.map-tip.show { opacity: 1; }

/* popup */
.leaflet-popup-content-wrapper { border-radius: var(--radius); box-shadow: var(--shadow-2); font-family: inherit; }
.leaflet-popup-content { margin: 14px 16px; direction: rtl; text-align: right; }
.leaflet-popup-tip { box-shadow: none; }
.pop-name { font-size: 15px; font-weight: 700; margin-bottom: 2px; }
.pop-sub { font-size: 11.5px; color: var(--muted); margin-bottom: 10px; }
.pop-stats { display: grid; grid-template-columns: auto auto; gap: 4px 16px; font-size: 12.5px; margin: 0; }
.pop-stats dt { color: var(--muted); }
.pop-stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.pop-zero {
  margin-top: 9px; font-size: 11.5px; font-weight: 600; color: var(--state-green-ink);
  background: var(--green-wash); border-radius: 6px; padding: 6px 9px; line-height: 1.6;
}
.pop-spark { margin-top: 10px; }
.spark-cap { font-size: 11px; color: var(--muted); margin-bottom: 3px; }

.spin {
  position: absolute; inset: 0; z-index: 750; display: grid; place-items: center;
  background: rgba(250,249,246,.72); font-size: 13px; color: var(--muted);
}

@media (max-width: 900px) {
  main { flex-direction: column; }
  #panel { width: 100%; max-height: 320px; border-inline-end: none; border-bottom: 1px solid var(--hairline); }
  header { height: auto; flex-wrap: wrap; padding: 10px 14px; gap: 10px; }
  .controls { margin-inline-start: 0; flex-wrap: wrap; }
}
