:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #eef0f3; --text: #14171c; --muted: #5d6573;
  --border: #dfe2e7; --accent: #2f5bea; --accent-text: #ffffff; --danger: #d92d20; --warn: #b54708;
  --ok: #067647; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --radius: 14px; --tabbar-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1015; --surface: #161a21; --surface-2: #1e232c; --text: #e8ebf0; --muted: #9aa3b2;
    --border: #2a303b; --accent: #6d8cff; --accent-text: #0d1015; --danger: #f97066; --warn: #fdb022;
    --ok: #47cd89; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1015; --surface: #161a21; --surface-2: #1e232c; --text: #e8ebf0; --muted: #9aa3b2;
  --border: #2a303b; --accent: #6d8cff; --accent-text: #0d1015; --danger: #f97066; --warn: #fdb022;
  --ok: #47cd89; --shadow: none; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Arabic", "Geeza Pro", sans-serif;
  -webkit-text-size-adjust: 100%; padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--bg); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: -.01em; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.topbar-actions form { margin: 0; }
.sync { font-size: 12px; color: var(--muted); margin-inline-end: 4px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface);
  display: inline-grid; place-items: center; cursor: pointer; font-size: 16px;
}

/* panels */
.panel { padding: 14px 16px 24px; max-width: 720px; margin: 0 auto; }
#tab-map { padding: 0; max-width: none; }
#map-mount { min-height: calc(100vh - var(--tabbar-h) - 60px); }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 2px 8px; font-weight: 600; }
.empty { color: var(--muted); text-align: center; padding: 32px 8px; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card h3 { margin: 0 0 6px; font-size: 16px; line-height: 1.3; }
.meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); font-size: 12px; }
.question { margin: 0 0 10px; }
.context { margin: 0 0 10px; padding: 10px; border-radius: 10px; background: var(--surface-2); font-size: 13.5px; white-space: pre-wrap; color: var(--muted); }
.reco { margin: 0 0 12px; font-size: 14px; }
.reco b { color: var(--ok); }
.options { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.btn {
  min-height: 42px; padding: 0 16px; border-radius: 11px; border: 1px solid var(--border); background: var(--surface-2);
  cursor: pointer; font-weight: 550;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.free { display: flex; gap: 8px; align-items: flex-end; margin-top: 4px; }
.free textarea { flex: 1; min-height: 42px; max-height: 160px; resize: vertical; padding: 10px; border-radius: 11px; border: 1px solid var(--border); background: var(--bg); }
.card-actions { display: flex; justify-content: flex-end; margin-top: 10px; }
.answered { border-color: var(--ok); }
.answered .status-done { color: var(--ok); font-weight: 600; font-size: 13px; }

.latest .latest-body { white-space: pre-wrap; font-family: inherit; font-size: 14px; max-height: 240px; overflow: auto; margin: 0; }
.latest .latest-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.latest .latest-head h3 { margin: 0; font-size: 14px; color: var(--muted); font-weight: 600; }

/* agents */
.agent-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.agent-name { font-weight: 600; }
.state { font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); }
.state.running { color: var(--ok); }
.state.blocked, .state.failed { color: var(--danger); }
.stale-flag { font-size: 12px; font-weight: 600; color: var(--warn); margin-inline-start: 6px; }
.bar { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.bar-track { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); }
.agent-tokens { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); }

/* tokens */
.tok-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.tok { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.tok .k { font-size: 11.5px; color: var(--muted); }
.tok .v { font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.na { color: var(--muted); font-weight: 500; font-style: italic; }

/* tab bar */
.tabbar {
  position: fixed; inset-inline: 0; bottom: 0; z-index: 6; display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar button {
  position: relative; border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 11.5px; color: var(--muted); cursor: pointer;
}
.tabbar .ti { font-size: 18px; line-height: 1; }
.tabbar button[aria-selected="true"] { color: var(--accent); font-weight: 600; }
.badge {
  position: absolute; top: 6px; inset-inline-start: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: #e5251b; color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
}

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 9;
}

/* RTL content */
[dir="rtl"] { text-align: right; }

/* login */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 24px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 22px; margin: 16px 0 14px; }
.login-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.login-card input { width: 100%; height: 46px; padding: 0 12px; border-radius: 11px; border: 1px solid var(--border); background: var(--bg); font-size: 16px; }
.login-error { min-height: 20px; color: var(--danger); font-size: 13px; margin: 8px 0; }
