:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --border: #e3e1db;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #8f7433;
  --on-accent: #ffffff;
  --accent-2: #6f5a26;
  --accent-soft: #f4ecd8;
  --src: #0f9d58;
  --dst: #d93a3a;
  --danger: #c0392b;
  --warn: #b7791f;
  --heat-zero: #f0efec;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --font: 'Inter', 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
html[dir="rtl"] { --font: 'IBM Plex Sans Arabic', 'Inter', system-ui, 'Segoe UI', sans-serif; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ------------------------------------------------------------ layout 30 / 70 */
.app { display: flex; height: 100vh; width: 100vw; }
.sidebar {
  flex: 0 0 25%; max-width: 25%; min-width: 300px;
  transition: flex-basis .22s ease, max-width .22s ease, min-width .22s ease;
  display: flex; flex-direction: column;
  background: var(--surface); border-inline-end: 1px solid var(--border);
  z-index: 2;
}
.map-wrap { flex: 1 1 auto; position: relative; min-width: 0; --dock-h: 0px; }
#map { position: absolute; inset: 0 0 var(--dock-h) 0; direction: ltr; background: #e8e6df; }
#map.no-basemap {
  background-color: #f6f5f1;
  background-image: linear-gradient(#e9e7e1 1px, transparent 1px), linear-gradient(90deg, #e9e7e1 1px, transparent 1px);
  background-size: 40px 40px;
}
#map.placing { cursor: crosshair; }
/* while placing a portal, clicks pass through imported vectors so portals can be dropped onto corridors */
#map.placing .leaflet-overlay-pane .leaflet-interactive { pointer-events: none !important; }

@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar { flex: 0 0 46%; max-width: none; min-width: 0; border-inline-end: 0; border-top: 1px solid var(--border); order: 2; }
  .map-wrap { flex: 1 1 auto; max-width: none; order: 1; }
}

/* ------------------------------------------------------------ sidebar header & tabs */
.sb-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px 12px; border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { color: var(--accent); display: grid; place-items: center; flex: none; }
.brand h1 { font-size: 15.5px; margin: 0; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p { margin: 1px 0 0; font-size: 12px; color: var(--text-2); }


.tabs { display: grid; border-bottom: 1px solid var(--border); background: var(--surface); }
.tab {
  border: 0; background: none; padding: 10px 4px 9px; cursor: pointer; color: var(--text-2);
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; font-weight: 500;
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.tab-body { flex: 1; overflow-y: auto; padding: 14px; background: var(--bg); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.card h3, .section-title { margin: 0 0 10px; font-size: 13px; font-weight: 700; letter-spacing: .01em; display: flex; align-items: center; gap: 6px; }
.section-title { margin: 16px 2px 8px; }
.section-title .count { background: var(--border); color: var(--text-2); border-radius: 999px; padding: 0 7px; font-size: 11px; font-weight: 600; }
.hint { color: var(--text-2); font-size: 12.5px; margin: 8px 2px 4px; }
.empty { color: var(--muted); font-size: 12.5px; margin: 6px 2px; }

/* ------------------------------------------------------------ form controls */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 9px; min-width: 0; flex: 1; }
.field.narrow { flex: 0 0 64px; }
.field label { font-size: 11.5px; color: var(--text-2); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.field-row { display: flex; gap: 8px; }
.field-row.three > .field { flex: 1 1 0; }
input[type="number"], input[type="time"], input[type="text"], select, .name-input {
  width: 100%; min-width: 0; border: 1px solid var(--border); background: var(--surface); border-radius: 7px;
  padding: 6px 8px; font-size: 13px; outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; user-select: none; margin: 2px 0; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.color-in { width: 26px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; cursor: pointer; flex: none; }
.color-in.big { width: 100%; height: 32px; }
.color-in::-webkit-color-swatch-wrapper { padding: 2px; }
.color-in::-webkit-color-swatch { border: 0; border-radius: 4px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 12px;
  cursor: pointer; font-size: 13px; font-weight: 500; transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { border-color: #c9c7bf; background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); background: #fdf0ee; }
.btn.block { width: 100%; margin-top: 8px; }
.btn.big { padding: 10px 14px; font-size: 14px; font-weight: 600; flex: 1; }
.btn.icon { width: 34px; height: 34px; padding: 0; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); width: 26px; height: 26px; border-radius: 6px; flex: none; font-size: 14px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.mini-btn { border: 1px solid var(--border); background: var(--surface-2); border-radius: 6px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.mini-btn:hover { border-color: var(--accent); color: var(--accent); }
.mini-btn.add { width: 100%; padding: 6px; margin-top: 4px; border-style: dashed; background: none; }

.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.seg button { flex: 1; border: 0; background: none; padding: 6px 4px; cursor: pointer; font-size: 12.5px; font-weight: 500; color: var(--text-2); }
.seg button + button { border-inline-start: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: var(--on-accent); }

/* ------------------------------------------------------------ basemaps */
.basemap-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.bm { border: 2px solid transparent; background: none; padding: 0; border-radius: 9px; cursor: pointer; text-align: center; }
.bm-thumb {
  display: block; height: 54px; border-radius: 7px; background: #ddd center/cover no-repeat; border: 1px solid var(--border);
}
.bm-thumb.none {
  background-color: #f6f5f1;
  background-image: linear-gradient(#e2e0da 1px, transparent 1px), linear-gradient(90deg, #e2e0da 1px, transparent 1px);
  background-size: 10px 10px;
}
.bm-label { display: block; font-size: 11.5px; margin-top: 3px; color: var(--text-2); }
.bm:hover .bm-thumb { border-color: #c9c7bf; }
.bm.active { border-color: var(--accent); }
.bm.active .bm-label { color: var(--accent); font-weight: 600; }

/* ------------------------------------------------------------ import */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center; cursor: pointer;
  border: 1.5px dashed #c9c7bf; border-radius: 9px; padding: 14px 10px; background: var(--surface-2);
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dz-ico { font-size: 20px; color: var(--accent); }
.dropzone small { color: var(--muted); font-size: 11.5px; }
.net-stats { font-size: 12px; color: var(--accent-2); background: var(--accent-soft); border-radius: 7px; padding: 7px 9px; margin-top: 6px; }
.net-stats.empty { color: var(--warn); background: #fdf6e7; }

/* ------------------------------------------------------------ list items */
.item-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.item { background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 9px; }
.item-head { display: flex; align-items: center; gap: 6px; }
.item-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-title small { color: var(--muted); font-size: 11.5px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; font-size: 13px; }
.item-opts { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; margin-top: 7px; }
.name-input { font-weight: 600; border-color: transparent; background: transparent; padding: 4px 6px; }
.name-input:hover { border-color: var(--border); }
.portal-item.flash { animation: flash 1.2s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent-soft); border-color: var(--accent); } }
.sub-title { font-size: 11.5px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; margin: 8px 2px 6px; }
html[dir="rtl"] .sub-title { text-transform: none; letter-spacing: 0; }

.place-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn.place { padding: 10px; font-weight: 600; }
.btn.place.src.active { background: #e7f6ee; border-color: var(--src); color: #0b7a44; }
.btn.place.dst.active { background: #fcebeb; border-color: var(--dst); color: #b42727; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; display: inline-block; }
.dot.src { background: var(--src); box-shadow: 0 0 0 3px #d5efe1; }
.dot.dst { background: var(--dst); box-shadow: 0 0 0 3px #f7d9d9; }

.rule { border: 1px solid var(--border); border-inline-start: 3px solid var(--pc, #999); border-radius: 8px; padding: 8px 8px 6px; margin-bottom: 7px; background: var(--surface-2); }
.rule .field { margin-bottom: 6px; }
.rule input, .rule select { padding: 5px 6px; font-size: 12.5px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--border); border-radius: 999px;
  padding: 3px 9px; font-size: 12px; cursor: pointer; background: var(--surface); user-select: none;
}
.chip input { display: none; }
.chip.on { background: #fcebeb; border-color: #eba5a5; color: #9b1c1c; font-weight: 500; }
.chip.on::before { content: '✓'; font-size: 11px; }
.rule-foot { display: flex; align-items: center; gap: 8px; }
.progress { flex: 1; height: 5px; background: var(--border); border-radius: 99px; overflow: hidden; }
.progress .bar { height: 100%; background: var(--pc, var(--accent)); border-radius: 99px; transition: width .3s; }

.profile-item { border-inline-start: 4px solid var(--pc); }
.agent-preview { display: inline-flex; gap: 3px; align-items: center; width: 36px; flex: none; }
.agent-preview i { display: block; border-radius: 50%; background: var(--pc); box-shadow: 0 0 0 1px #fff; }
.profile-item .field-row { margin-top: 8px; }

/* ------------------------------------------------------------ sim tab */
.sim-controls { display: flex; gap: 8px; margin-bottom: 12px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stat { background: var(--surface-2); border-radius: 7px; padding: 7px 9px; display: flex; flex-direction: column; }
.stat span { font-size: 11.5px; color: var(--text-2); }
.stat b { font-size: 16px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ map overlays */
.hud, .map-toolbar, .legend, .place-hint {
  position: absolute; z-index: 1000; background: rgba(255,255,255,.94); backdrop-filter: blur(6px);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.hud { top: 12px; right: 12px; padding: 12px 14px; min-width: 220px; }
.hud-label { font-size: 11.5px; color: var(--text-2); font-weight: 500; }
.hud-value { font-size: 32px; font-weight: 700; line-height: 1.1; letter-spacing: -.02em; font-variant-numeric: tabular-nums; color: var(--accent); }
.hud-sub { font-size: 12px; color: var(--text-2); }
.hud-cols { display: grid; grid-template-columns: repeat(3, auto); gap: 12px; margin-top: 9px; padding-top: 9px; border-top: 1px solid var(--border); }
.hud-cols div { display: flex; flex-direction: column; font-size: 11px; color: var(--text-2); }
.hud-cols b { font-size: 14px; color: var(--text); font-variant-numeric: tabular-nums; direction: ltr; text-align: start; }

.map-toolbar { bottom: calc(var(--dock-h) + 22px); left: 50%; transform: translateX(-50%); display: flex; gap: 4px; padding: 5px; border-radius: 999px; }
.tb-btn { border: 0; background: none; border-radius: 999px; padding: 7px 13px; cursor: pointer; font-size: 13px; font-weight: 600; white-space: nowrap; }
.tb-btn:hover { background: var(--surface-2); }
.tb-btn.on { background: var(--accent-soft); color: var(--accent-2); }
.tb-btn.accent { background: var(--accent); color: var(--on-accent); }
.tb-btn.accent:hover { background: var(--accent-2); }
#tbPlay { min-width: 42px; }

.legend { bottom: calc(var(--dock-h) + 22px); inset-inline-start: 12px; padding: 8px 11px; font-size: 12px; max-height: 40%; overflow: auto; }
.lg-row { display: flex; align-items: center; gap: 7px; padding: 2px 0; }
.lg-dot { width: 10px; height: 10px; border-radius: 50%; box-shadow: 0 0 0 1.5px #fff, 0 0 0 2.5px rgba(0,0,0,.08); }
.lg-portal { width: 14px; height: 14px; border-radius: 50%; display: grid; place-items: center; font-size: 7px; color: #fff; }
.lg-portal.src { background: var(--src); }
.lg-portal.dst { background: var(--dst); }

.place-hint { top: 12px; left: 50%; transform: translateX(-50%); padding: 8px 14px; font-size: 13px; font-weight: 600; color: var(--accent-2); background: var(--accent-soft); border-color: #b5ddd8; }
.drop-overlay {
  position: absolute; inset: 0; z-index: 1500; display: grid; place-items: center;
  background: rgba(143,116,51,.18); border: 3px dashed var(--accent); font-size: 18px; font-weight: 700; color: var(--accent-2);
  pointer-events: none;
}
.drop-overlay div { background: #fff; padding: 14px 22px; border-radius: 12px; box-shadow: var(--shadow); }

.toast {
  position: absolute; z-index: 1600; bottom: calc(var(--dock-h) + 80px); left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #1f2937; color: #fff; padding: 9px 16px; border-radius: 10px; font-size: 13px; max-width: 80%;
  transition: opacity .2s, transform .2s; box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { background: #8a5a00; }
.toast.error { background: #a32020; }

/* ------------------------------------------------------------ portals on map */
.portal-icon { background: none; border: 0; }
.portal {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-size: 11px;
  border: 2.5px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.3); position: relative; cursor: grab;
}
.portal.src { background: var(--src); }
.portal.dst { background: var(--dst); }
.portal::after {
  content: ''; position: absolute; inset: -8px; border-radius: 50%; border: 2px solid currentColor; opacity: 0;
  animation: pulse 2.2s ease-out infinite;
}
.portal.src::after { color: var(--src); }
.portal.dst::after { color: var(--dst); animation-delay: 1.1s; }
@keyframes pulse { 0% { transform: scale(.6); opacity: .8; } 100% { transform: scale(1.5); opacity: 0; } }
.leaflet-tooltip.portal-label {
  font: 600 12px var(--font); padding: 3px 8px; border-radius: 6px; border: 0; color: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.leaflet-tooltip.portal-label.src { background: #0b7a44; }
.leaflet-tooltip.portal-label.dst { background: #b42727; }
.leaflet-tooltip.portal-label.src::before { border-top-color: #0b7a44; }
.leaflet-tooltip.portal-label.dst::before { border-top-color: #b42727; }
.agent-canvas { position: absolute; top: 0; left: 0; pointer-events: none; }
table.props { border-collapse: collapse; font-size: 12px; }
table.props th { text-align: start; color: var(--text-2); font-weight: 500; padding: 2px 8px 2px 0; vertical-align: top; }
table.props td { padding: 2px 0; }
.leaflet-container { font-family: var(--font); }

/* ------------------------------------------------------------ dashboard modal */
.modal { position: fixed; inset: 0; z-index: 3000; background: rgba(15, 23, 22, .45); display: grid; place-items: center; padding: 20px; }
.modal-box { background: var(--bg); width: min(1280px, 100%); height: min(92vh, 100%); border-radius: 16px; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-head p { margin: 2px 0 0; font-size: 12.5px; }
.modal-actions { display: flex; gap: 8px; }
.modal-body { flex: 1; overflow-y: auto; padding: 16px 20px 24px; }
.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 11px 13px; display: flex; flex-direction: column; min-width: 0; }
.kpi-label { font-size: 11.5px; color: var(--text-2); font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; color: var(--text); margin: 2px 0; }
.kpi-sub { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.chart-card.wide { grid-column: 1 / -1; }
.chart-card h4 { margin: 0 0 8px; font-size: 13px; font-weight: 600; }
.chart-box { position: relative; height: 230px; }
.chart-card.wide .chart-box { height: 260px; }
.dash-map { height: 360px; border-radius: 8px; overflow: hidden; direction: ltr; }
@media (max-width: 1100px) { .kpis { grid-template-columns: repeat(3, 1fr); } .chart-grid { grid-template-columns: 1fr 1fr; } }
@media (max-width: 700px) { .kpis { grid-template-columns: 1fr 1fr; } .chart-grid { grid-template-columns: 1fr; } .modal { padding: 0; } .modal-box { height: 100%; border-radius: 0; } }

.time-heat { overflow-x: auto; }
.th-grid { display: grid; gap: 2px; align-items: center; min-width: 480px; }
.th-row-label { font-size: 12px; color: var(--text); padding-inline-end: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.th-cell { height: 26px; border-radius: 3px; transition: transform .1s; }
.th-cell:hover { outline: 2px solid var(--text); outline-offset: 1px; z-index: 1; }
.th-col-label { font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: visible; direction: ltr; }
.th-legend { display: flex; align-items: center; gap: 3px; margin-top: 10px; font-size: 11.5px; color: var(--text-2); }
.th-legend i { width: 18px; height: 10px; border-radius: 2px; display: inline-block; }
.th-legend span { margin: 0 4px; }

/* messages ("Saved", "Opened project …", "Deleted …") at the top middle of the map, clear of tables and the dock */
.toast { top: 14px; bottom: auto; transform: translate(-50%, -14px); transition: opacity .25s ease, transform .3s cubic-bezier(.22, .9, .3, 1); }
.toast.show { transform: translate(-50%, 0); }
/* the message can be closed with its × */
.toast { display: flex; align-items: center; gap: 10px; padding: 7px 8px 7px 16px; }
html[dir="rtl"] .toast { padding: 7px 16px 7px 8px; }
.toast.show { pointer-events: auto; }
.toast-text { flex: 1; min-width: 0; }
.toast-x { flex: none; width: 24px; height: 24px; display: grid; place-items: center; border: 0; border-radius: 6px; background: transparent; color: inherit; opacity: .7; cursor: pointer; transition: opacity .15s, background .15s; }
.toast-x:hover, .toast-x:focus-visible { opacity: 1; background: rgba(255, 255, 255, .16); outline: none; }
.toast-x .ic { width: 14px; height: 14px; }

/* tables: double-click a column header to sort (table-sort.js) */
th.ds-th, .etl-pick th[data-sortk] { cursor: pointer; user-select: none; }
th[data-ds]::after { content: ' ▲'; font-size: .8em; opacity: .75; }
th[data-ds="desc"]::after { content: ' ▼'; }
