:root {
  --bg: #f5f4f1;
  --surface: #ffffff;
  --surface-2: #f9f8f6;
  --line: #e4e1db;
  --text: #1c1b19;
  --muted: #6b6760;
  --brand: #8a1538;          /* Qatar maroon */
  --brand-ink: #ffffff;
  --crisis: #c62828;
  --crisis-bg: #fdecec;
  --major: #d9480f;
  --major-bg: #fff1e6;
  --minor: #a07a00;
  --minor-bg: #fdf6dc;
  --mention: #8c8a85;
  --mention-bg: #f0efec;
  --ok: #2b8a3e;
  --focus: #1971c2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141312;
    --surface: #1d1c1a;
    --surface-2: #232220;
    --line: #34322f;
    --text: #ecebe8;
    --muted: #a19d96;
    --brand: #b0284f;
    --crisis: #ff6b6b;
    --crisis-bg: #3a1d1d;
    --major: #ff922b;
    --major-bg: #3a2616;
    --minor: #e0b631;
    --minor-bg: #332b12;
    --mention: #9a9790;
    --mention-bg: #2a2927;
    --ok: #51cf66;
    --focus: #4dabf7;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, "Noto Sans Arabic", Tahoma, sans-serif;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; font-size: 16px; }
.brand svg { width: 28px; height: 28px; }
.brand svg rect { fill: var(--brand); }
.brand svg path { stroke: #fff; stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.brand small { font-weight: 500; color: var(--muted); margin-left: 4px; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { text-decoration: none; padding: 6px 12px; border-radius: 6px; color: var(--muted); font-weight: 500; }
.topbar nav a:hover { background: var(--surface-2); color: var(--text); }
.topbar nav a.active { color: var(--text); background: var(--surface-2); }
.who { margin-left: auto; display: flex; gap: 12px; align-items: center; color: var(--muted); }
.who a { color: var(--muted); }

.wrap { max-width: 1180px; margin: 0 auto; padding: 20px 24px 64px; }

/* ---------- Status + alert ---------- */
.status-line { display: flex; align-items: center; gap: 8px; color: var(--muted); margin-bottom: 14px; font-size: 13px; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 0 var(--ok); animation: pulse 2s infinite; }
.live-dot.stale { background: var(--major); animation: none; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(43,138,62,.5); } 70% { box-shadow: 0 0 0 7px rgba(43,138,62,0); } 100% { box-shadow: 0 0 0 0 rgba(43,138,62,0); } }
@media (prefers-reduced-motion: reduce) { .live-dot { animation: none; } }
.status-line .warn { color: var(--major); }

.alert-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--crisis); color: #fff; border-radius: var(--radius);
  padding: 10px 16px; margin-bottom: 14px; font-weight: 600;
}
.alert-banner[hidden] { display: none; }
.alert-banner button { background: rgba(255,255,255,.18); border: 0; color: #fff; border-radius: 6px; padding: 4px 10px; cursor: pointer; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-bottom: 16px; }
.kpi {
  display: flex; flex-direction: column; align-items: flex-start; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); min-width: 0;
}
button.kpi { cursor: pointer; }
button.kpi:hover { border-color: var(--muted); }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kpi-value { font-size: 30px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.kpi-crisis .kpi-value { color: var(--crisis); }
.kpi-crisis.hot { background: var(--crisis-bg); border-color: var(--crisis); }
.kpi-major .kpi-value { color: var(--major); }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card h2 { font-size: 14px; margin: 0; }
.chart-card { padding: 14px 16px 10px; margin-bottom: 16px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.sw-crisis { background: var(--crisis); } .sw-major { background: var(--major); }
.sw-minor { background: var(--minor); } .sw-mention { background: var(--mention); }
.chart { height: 120px; position: relative; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .empty { fill: var(--muted); font-size: 12px; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg button { border: 0; background: none; padding: 5px 11px; border-radius: 6px; cursor: pointer; color: var(--muted); font-weight: 500; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--text); color: var(--bg); }
.seg-sm button { padding: 2px 9px; font-size: 12px; }
.sel { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.sel select, .search input {
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; color: var(--text);
}
.search { margin-left: auto; }
.search input { width: 220px; }

/* ---------- Feed ---------- */
.feed { display: flex; flex-direction: column; gap: 10px; }
.mention {
  position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px 12px 20px; box-shadow: var(--shadow);
}
.mention::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; border-radius: var(--radius) 0 0 var(--radius); background: var(--mention); }
.mention.sev-crisis::before { background: var(--crisis); }
.mention.sev-major::before { background: var(--major); }
.mention.sev-minor::before { background: var(--minor); }
.mention.is-done { opacity: .62; }
.mention.fresh { animation: fresh 2.4s ease-out; }
@keyframes fresh { from { background: var(--crisis-bg); } }

.m-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 13px; color: var(--muted); }
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 2px 8px; border-radius: 999px; }
.badge .score { font-variant-numeric: tabular-nums; opacity: .85; font-weight: 600; }
.b-crisis { background: var(--crisis-bg); color: var(--crisis); }
.b-major { background: var(--major-bg); color: var(--major); }
.b-minor { background: var(--minor-bg); color: var(--minor); }
.b-mention, .b-pending { background: var(--mention-bg); color: var(--mention); }
.pill { font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.pill.st-in_progress { border-color: var(--focus); color: var(--focus); }
.pill.st-handled { border-color: var(--ok); color: var(--ok); }
.author { color: var(--text); font-weight: 600; text-decoration: none; }
.author:hover { text-decoration: underline; }
.verified { color: var(--focus); }
.m-time { margin-left: auto; white-space: nowrap; }

.m-summary { font-size: 15px; font-weight: 600; margin: 8px 0 4px; }
.m-text {
  color: var(--text); opacity: .85; margin: 0; white-space: pre-line; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; cursor: pointer;
}
.m-text.open { -webkit-line-clamp: unset; display: block; }
.m-metrics { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 6px; font-variant-numeric: tabular-nums; }

.m-ai { margin-top: 10px; padding: 8px 12px; background: var(--surface-2); border-radius: 8px; font-size: 13px; display: grid; gap: 3px; }
.m-ai b { font-weight: 600; }
.m-ai .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 4px; background: var(--surface); border: 1px solid var(--line); color: var(--muted); }
.tag.neg { color: var(--crisis); border-color: currentColor; }
.tag.pos { color: var(--ok); border-color: currentColor; }

.m-summary a { text-decoration: none; }
.m-summary a:hover { text-decoration: underline; }
.m-text mark { background: var(--crisis-bg); color: var(--crisis); padding: 0 2px; border-radius: 3px; }
.m-block-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin-bottom: 6px; }
.tr { color: var(--muted); font-size: 13px; margin: 2px 0 0; }

.m-evidence { margin-top: 10px; }
.m-evidence blockquote { margin: 0 0 6px; padding: 6px 12px; border-left: 3px solid var(--crisis); background: var(--crisis-bg); border-radius: 0 6px 6px 0; }
.m-evidence blockquote p { margin: 0; }
.m-evidence blockquote p[dir] { font-weight: 600; }

.m-reactions { margin-top: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; }
.r-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--mention-bg); }
.r-bar .neg { background: var(--crisis); } .r-bar .neu { background: var(--mention); } .r-bar .pos { background: var(--ok); }
.r-legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 4px; font-variant-numeric: tabular-nums; }
.r-legend .neg { color: var(--crisis); font-weight: 600; } .r-legend .pos { color: var(--ok); }
.r-summary { margin: 8px 0 4px; font-size: 13px; }
.r-examples { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 8px; }
.r-examples li { padding: 8px 10px; background: var(--surface-2); border-radius: 6px; font-size: 13px; }
.r-examples p { margin: 2px 0 0; }
.r-head { display: flex; flex-wrap: wrap; gap: 4px; align-items: baseline; color: var(--muted); font-size: 12px; }
.r-head b { color: var(--text); }
.r-head a { margin-left: auto; color: var(--focus); text-decoration: none; font-weight: 600; }
.r-head a:hover { text-decoration: underline; }

.m-notes { margin-top: 8px; font-size: 13px; padding: 6px 10px; border-left: 3px solid var(--focus); background: var(--surface-2); white-space: pre-line; }
.m-meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

.m-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 7px; padding: 5px 11px;
  cursor: pointer; font-size: 13px; font-weight: 500; text-decoration: none; display: inline-flex; align-items: center; gap: 4px;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; color: var(--muted); }
.btn[disabled] { opacity: .5; cursor: default; }
.note-box { width: 100%; margin-top: 8px; display: grid; gap: 6px; }
.note-box textarea { width: 100%; min-height: 64px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; background: var(--surface); resize: vertical; }

.empty-state { text-align: center; padding: 48px 16px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty-state strong { display: block; color: var(--text); font-size: 15px; margin-bottom: 4px; }
.more { text-align: center; margin-top: 14px; }

/* ---------- Forms / settings ---------- */
.login { display: grid; place-items: center; min-height: calc(100vh - 56px); padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 28px; display: grid; gap: 8px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0 0 8px; }
label { font-weight: 600; font-size: 13px; }
input[type=text], input[type=password], input[type=email], input[type=number], input:not([type]), textarea, select.full {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--surface); width: 100%;
}
.login-card .btn { justify-content: center; padding: 9px; margin-top: 8px; }
.form-error { background: var(--crisis-bg); color: var(--crisis); padding: 8px 12px; border-radius: 8px; font-size: 13px; }
.flash { background: #e6f4ea; color: #1e6b30; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
@media (prefers-color-scheme: dark) { .flash { background: #183020; color: #8ce99a; } }

.settings { display: grid; gap: 16px; }
.settings .card { padding: 18px 20px; }
.settings h2 { font-size: 16px; margin: 0 0 4px; }
.settings .help { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.settings textarea { min-height: 120px; }
.settings table { width: 100%; border-collapse: collapse; font-size: 13px; }
.settings th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.settings td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.settings td input[type=number] { width: 80px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > input:not([type=checkbox]) { flex: 1; min-width: 200px; }
.checks { display: inline-flex; gap: 10px; flex-wrap: wrap; font-weight: 400; }
.checks label { font-weight: 400; display: inline-flex; gap: 4px; align-items: center; }
.cost { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.err { color: var(--crisis); font-size: 12px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi-noise { grid-column: span 2; }
  .search { margin-left: 0; width: 100%; }
  .search input { width: 100%; }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
  .topbar { padding: 0 16px; gap: 12px; }
  .brand small, .who span { display: none; }
  .wrap { padding: 16px 16px 48px; }
  .m-time { margin-left: 0; width: 100%; }
  .toolbar { gap: 8px; }
  .seg { overflow-x: auto; max-width: 100%; }
  .settings td, .settings th { padding: 6px 4px; }
}

/* Settings: suggestions + bulk add */
.origin { font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 4px; background: var(--mention-bg); color: var(--muted); }
.origin.trends { background: #e7f0fd; color: #1864ab; }
@media (prefers-color-scheme: dark) { .origin.trends { background: #1b2b40; color: #74c0fc; } }
.bulk { margin-top: 14px; }
.bulk summary { cursor: pointer; font-weight: 600; color: var(--focus); }
.settings .card-head { align-items: flex-start; }

/* Undo toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 16px; padding: 10px 12px 10px 16px;
  background: var(--text); color: var(--bg); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25); font-weight: 500;
  max-width: calc(100vw - 32px);
}
.toast button { border: 0; background: none; color: #74c0fc; font-weight: 700; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.toast button:hover { background: rgba(255,255,255,.12); }
.fetch-btn { margin-left: auto; padding: 3px 10px; font-size: 12px; }

/* Draft reply */
.m-draft { margin-top: 10px; padding: 10px 12px; border: 1px dashed var(--focus); border-radius: 8px; font-size: 13px; display: grid; gap: 6px; }
.d-advice.yes b { color: var(--ok); } .d-advice.no b { color: var(--major); }
.d-reply { display: flex; gap: 8px; align-items: flex-start; background: var(--surface-2); border-radius: 6px; padding: 8px 10px; }
.d-reply p { margin: 0; flex: 1; font-size: 15px; line-height: 1.7; }
.d-copy { flex: none; }
.d-facts ul { margin: 2px 0 0; padding-left: 18px; }
.d-note { font-size: 11px; color: var(--muted); }
.spike-banner { background: var(--major); }
