/* Arabic-first dashboard CSS, modeled on the consulting-report template. */

@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&family=Cairo:wght@400;600;700&display=swap');

:root {
  --bg: #ffffff;
  --bg-soft: #f6f7f9;
  --bg-page: #eef0f3;
  --ink: #1b2030;
  --ink-soft: #4b5563;
  --ink-muted: #6b7280;
  --line: #e2e6ec;
  --line-soft: #eef0f3;
  --dark-header: #3a3d44;
  --dark-header-2: #4a4d54;

  --positive: #b4cce5;       /* light blue (defend) */
  --positive-strong: #4a7fb5;
  --negative: #b71c2c;        /* deep red (against) */
  --negative-soft: #e5b6bb;
  --neutral: #cccccc;          /* gray (mixed) */
  --accent: #f4c842;           /* yellow highlight */
  --accent-2: #1c2c4a;         /* deep navy */
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  font-family: 'Tajawal', 'Cairo', 'Segoe UI', 'Tahoma', 'Noto Sans Arabic', Arial, sans-serif;
  background: var(--bg-page);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-size: 14px;
}

html[dir="rtl"] { text-align: right; }

a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Topbar (sticky) */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: var(--dark-header);
  color: white;
  padding: 10px 22px;
  display: flex; align-items: center; gap: 14px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 700; }
.topbar .spacer { flex: 1; }
.topbar .badge {
  background: rgba(255,255,255,0.12);
  padding: 4px 10px; border-radius: 4px; font-size: 12px;
}
.topbar .platforms {
  display: flex; gap: 6px; font-size: 12px; opacity: 0.85;
}
.topbar .platforms span {
  background: rgba(255,255,255,0.1);
  padding: 3px 8px; border-radius: 12px;
}
.topbar button, .topbar .btn {
  background: var(--accent);
  color: var(--accent-2);
  border: 0;
  font-weight: 700;
  font-size: 13px;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  transition: filter .15s;
}
.topbar button:hover { filter: brightness(0.95); }
.topbar button:disabled { background: #94a3b8; color: white; cursor: not-allowed; }
.topbar .btn-ghost {
  background: rgba(255,255,255,0.15);
  color: white;
}

/* Section header strip (mirrors template) */
.section-strip {
  background: var(--dark-header);
  color: white;
  padding: 10px 18px;
  margin: 22px 0 14px;
  border-radius: 6px;
  font-size: 15px;
  font-weight: 700;
  display: flex; align-items: center; gap: 10px;
}
.section-strip .icons { margin-inline-start: auto; opacity: 0.7; font-size: 12px; }

.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 18px 22px 60px;
}

/* Cover (consulting-report style) */
.cover {
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 56px 40px 48px;
  margin: 18px 0;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.cover::before {
  content: ""; position: absolute; top: 0; inset-inline-start: 0; right: 0;
  height: 6px; background: linear-gradient(90deg, var(--accent-2) 0%, var(--accent) 100%);
}
.cover-title {
  font-size: 40px;
  margin: 0 0 36px;
  color: var(--accent-2);
  font-weight: 800;
  line-height: 1.3;
}
.cover-title .cover-kw {
  color: var(--negative);
  border-bottom: 3px solid var(--accent);
  padding-bottom: 2px;
}
.cover-block {
  display: inline-block;
  text-align: center;
  line-height: 1.9;
}
.cover-appendix {
  font-size: 18px; color: var(--ink-soft); font-weight: 600;
  letter-spacing: 0.5px;
}
.cover-year {
  font-size: 56px; color: var(--accent-2); font-weight: 800;
  margin: 4px 0 14px; letter-spacing: 1px;
}
.cover-subtitle {
  font-size: 20px; color: var(--ink); font-weight: 700;
}
.cover-date {
  font-size: 16px; color: var(--ink-muted); font-weight: 500; margin-top: 4px;
}
.cover-foot {
  margin-top: 32px;
  font-size: 11px;
  color: var(--ink-muted);
  letter-spacing: 1.5px;
  border-top: 1px solid var(--line-soft);
  padding-top: 14px;
}

/* KPI grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}
.kpi {
  background: white; border: 1px solid var(--line); border-radius: 8px;
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 4px;
  position: relative; overflow: hidden;
}
.kpi::before {
  content: ""; position: absolute; inset-inline-start: 0; top: 0; bottom: 0;
  width: 4px; background: var(--accent-2);
}
.kpi.accent::before { background: var(--accent); }
.kpi.danger::before { background: var(--negative); }
.kpi.success::before { background: var(--positive-strong); }
.kpi .label { font-size: 11px; color: var(--ink-muted); font-weight: 500; }
.kpi .value { font-size: 22px; font-weight: 800; color: var(--ink); }
.kpi .sub { font-size: 11px; color: var(--ink-muted); }

/* Cards */
.card {
  background: white; border: 1px solid var(--line); border-radius: 8px;
  padding: 16px;
}
.card h3 {
  margin: 0 0 12px; font-size: 14px; font-weight: 700; color: var(--accent-2);
  border-bottom: 1px solid var(--line-soft);
  padding-bottom: 8px;
}
.card-row { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
.card-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.card-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.chart-wrap { position: relative; height: 320px; }
.chart-wrap.short { height: 240px; }
.chart-wrap.tall  { height: 420px; }

/* Two-column exec summary */
.exec-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 12px; }
.exec-card { padding: 18px 22px; border-radius: 8px; }
.exec-card.problem { background: #f7e1e3; border: 1px solid #e9c2c6; }
.exec-card.context { background: #dfe9f3; border: 1px solid #c3d4e3; }
.exec-card h4 { margin: 0 0 4px; font-size: 16px; }
.exec-card .when { font-size: 12px; opacity: 0.7; margin-bottom: 12px; }
.exec-card ul { margin: 0; padding-inline-start: 18px; }
.exec-card li { margin-bottom: 10px; line-height: 1.6; font-size: 13px; }

/* Tables */
table.tbl {
  width: 100%; border-collapse: collapse; font-size: 12px;
}
.tbl th, .tbl td {
  border-bottom: 1px solid var(--line-soft); padding: 8px 10px; text-align: start;
  vertical-align: top;
}
.tbl th { background: var(--bg-soft); font-weight: 700; color: var(--ink-soft); }
.tbl tr:hover td { background: var(--bg-soft); }

/* Posts */
.post {
  background: white; border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 14px; margin-bottom: 8px;
}
.post-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.post-text { white-space: pre-wrap; line-height: 1.7; margin: 6px 0; font-size: 13px; }
.metrics { display: flex; gap: 6px; flex-wrap: wrap; font-size: 11px; }
.chip {
  background: var(--bg-soft); border: 1px solid var(--line-soft); border-radius: 999px;
  padding: 2px 10px; color: var(--ink-soft);
}
.chip.positive { background: #e8f0f9; color: var(--positive-strong); border-color: #c9dbef; }
.chip.negative { background: #f8e0e2; color: var(--negative); border-color: #ecc1c5; }
.chip.neutral  { background: #f0f0f0; color: var(--ink-soft); }

/* Account cards */
.accounts-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.account {
  background: white; border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px;
}
.account .name { font-weight: 700; }
.account .meta-line { color: var(--ink-muted); font-size: 11px; margin-top: 4px; }
.verified { color: var(--positive-strong); font-weight: 700; font-size: 11px; }

/* Hashtag chips */
.hashtag-cloud { display: flex; flex-wrap: wrap; gap: 6px; }
.hashtag-cloud .tag {
  background: var(--bg-soft); padding: 4px 10px; border-radius: 999px; font-size: 12px;
  border: 1px solid var(--line-soft);
}
.hashtag-cloud .tag strong { color: var(--accent-2); }

/* Landing page form */
.landing {
  min-height: 100vh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1c2c4a 0%, #3a3d44 100%);
  padding: 20px;
  position: relative;
}
.manage-kw-btn {
  position: absolute;
  top: 18px; inset-inline-end: 22px;
  background: var(--accent);
  color: var(--accent-2) !important;
  font-weight: 700;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 8px;
  text-decoration: none !important;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
  transition: transform .15s, filter .15s;
  z-index: 10;
}
.manage-kw-btn:hover { filter: brightness(1.05); transform: translateY(-1px); }
.form-card {
  background: white; border-radius: 12px; box-shadow: 0 20px 50px rgba(0,0,0,0.25);
  padding: 32px 30px; width: 100%; max-width: 540px;
}
.form-card h1 { margin: 0 0 4px; font-size: 22px; color: var(--accent-2); font-weight: 800; }
.form-card .sub { color: var(--ink-muted); font-size: 13px; margin-bottom: 22px; }
.form-card label { display: block; font-size: 12px; font-weight: 700; color: var(--ink-soft); margin: 14px 0 6px; }
.form-card input[type="text"], .form-card input[type="date"], .form-card input[type="number"], .form-card select {
  width: 100%; padding: 10px 12px; font-size: 14px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--bg-soft); outline: none;
  font-family: inherit;
}
.form-card input:focus, .form-card select:focus {
  border-color: var(--accent-2); background: white;
  box-shadow: 0 0 0 3px rgba(28,44,74,0.15);
}
.form-card .row { display: flex; gap: 10px; }
.form-card .row > div { flex: 1; }
.form-card button {
  margin-top: 22px; width: 100%; padding: 12px; font-size: 15px; font-weight: 700;
  background: var(--accent-2); color: white; border: 0; border-radius: 8px; cursor: pointer;
  font-family: inherit; transition: filter .15s;
}
.form-card button:hover { filter: brightness(1.1); }
.form-card .hint { font-size: 11px; color: var(--ink-muted); margin-top: 4px; }
.form-card .kw-suggest {
  display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px;
}
.form-card .kw-suggest button {
  width: auto; margin: 0; padding: 6px 12px; font-size: 13px; font-weight: 500;
  background: var(--bg-soft); color: var(--accent-2); border: 1px solid var(--line);
  border-radius: 999px; cursor: pointer; transition: all .15s;
}
.form-card .kw-suggest button:hover { background: #e9eef5; border-color: var(--accent-2); }
.form-card .kw-suggest button.selected {
  background: var(--accent); border-color: var(--accent); color: var(--accent-2); font-weight: 700;
}
#status { margin-top: 12px; font-size: 12px; min-height: 16px; }
.status-err { color: var(--negative); }
.status-ok  { color: #15803d; }

/* Page-break helpers (used by Puppeteer in PDF mode) */
.page-break { break-after: page; page-break-after: always; }
.no-break   { break-inside: avoid; page-break-inside: avoid; }

/* ---- Shared PDF/Print rules ---- */
/* Apply via either @media print OR body.pdf-mode, so output is consistent
   whether the browser uses screen or print media. */
@media print {
  body { background: white; font-size: 11px; }
  .topbar { display: none !important; }
  .container { padding: 0; max-width: none; }
  .card, .post, .account, .kpi { border-color: #d0d4da; box-shadow: none; }
}

/* When ?pdf=1: same layout tweaks, but reliably applied via class. */
body.pdf-mode { background: white; font-size: 11px; }
body.pdf-mode .topbar { display: none !important; }
body.pdf-mode .container { max-width: none; padding: 14px 12px; }
body.pdf-mode .card, body.pdf-mode .post, body.pdf-mode .account, body.pdf-mode .kpi {
  border-color: #d0d4da; box-shadow: none;
}

/* ---- Page-break behavior: keep titles glued to their charts ---- */
@media print, (pdf-mode) {
  /* fallback, real rules below via .pdf-mode and @media print */
}

/* Cards stay together (chart + title on the same page) */
@media print {
  .card, .post, .account, .exec-card, .kpi {
    break-inside: avoid;
    page-break-inside: avoid;
  }
  /* Section-strip and headings must not break away from following content */
  .section-strip, .card h3, .exec-card h4 {
    break-after: avoid;
    page-break-after: avoid;
  }
  /* Allow multi-card rows to break BETWEEN cards, not within a card */
  .card-row, .card-row-2, .card-row-3, .kpi-grid, .accounts-grid {
    break-inside: auto;
    page-break-inside: auto;
  }
  /* Tighter chart heights so a title + chart fits on one PDF page */
  .chart-wrap        { height: 220px !important; }
  .chart-wrap.short  { height: 170px !important; }
  .chart-wrap.tall   { height: 300px !important; }
  /* Network graph is the tallest element — keep it bounded */
  #network           { height: 380px !important; }
  /* Tighter top spacing for section strips so they don't get orphaned */
  .section-strip { margin: 16px 0 10px; }
  /* Chart title spacing */
  .card h3 { margin-bottom: 10px; }
}

/* Same rules duplicated under body.pdf-mode so they fire even if Puppeteer
   doesn't emulate print media for some reason. */
body.pdf-mode .card,
body.pdf-mode .post,
body.pdf-mode .account,
body.pdf-mode .exec-card,
body.pdf-mode .kpi {
  break-inside: avoid;
  page-break-inside: avoid;
}
body.pdf-mode .section-strip,
body.pdf-mode .card h3,
body.pdf-mode .exec-card h4 {
  break-after: avoid;
  page-break-after: avoid;
}
body.pdf-mode .card-row,
body.pdf-mode .card-row-2,
body.pdf-mode .card-row-3,
body.pdf-mode .kpi-grid,
body.pdf-mode .accounts-grid {
  break-inside: auto;
  page-break-inside: auto;
}
body.pdf-mode .chart-wrap        { height: 220px !important; }
body.pdf-mode .chart-wrap.short  { height: 170px !important; }
body.pdf-mode .chart-wrap.tall   { height: 300px !important; }
body.pdf-mode #network           { height: 380px !important; }
body.pdf-mode .section-strip { margin: 16px 0 10px; }
body.pdf-mode .card h3 { margin-bottom: 10px; }
body.pdf-mode .cover { break-after: page; page-break-after: always; }

@media (max-width: 960px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .card-row, .card-row-2, .card-row-3, .exec-summary, .accounts-grid {
    grid-template-columns: 1fr;
  }
}
