/* Market Intelligence Engine — dashboard styles (RTL, 780 brand, shadcn/ui-style components).
   Colors are the 780 brand tokens (PLAN-007 § 2): every color below is a :root token; components never use a
   raw hex value. Layout: fixed sidebar on the right (RTL), header with the page title + period picker. */

@font-face {
  font-family: "Vazirmatn";
  src: url("/static/fonts/Vazirmatn-wght.woff2") format("woff2-variations"), url("/static/fonts/Vazirmatn-wght.woff2") format("woff2");
  font-weight: 100 900;
  font-display: swap;
}

:root {
  /* brand */
  --primary: #105bab;
  --primary-hover: #0d4a8c;
  --primary-foreground: #ffffff;
  --brand-50: #eaf2fb;
  --pink: #bd1a8d;
  --pink-50: #fbeaf5;
  --pink-hover: #a3157a;   /* pink, 15 % darker — button hover */
  /* neutral */
  --background: #ffffff;
  --foreground: #0a0a0a;
  --muted: #f4f5f7;
  --muted-foreground: #667085;
  --border: #e7e9ee;
  --input: #e1e4ea;
  --ring: #9db7d8;
  --sidebar: #f7f8fa;
  --sidebar-accent: #eceff3;
  --row-hover: #fafbfc;    /* between background and sidebar — table row hover */
  /* status */
  --good: #15803d;
  --good-50: #ecf7f0;
  --bad: #c2410c;
  --bad-50: #fdf1ea;
  --danger: #dc2626;
  --danger-50: #fdeeee;
  --mark: #fff1b8;
  /* chart series, in this order */
  --chart-1: #105bab;
  --chart-2: #bd1a8d;
  --chart-3: #5b8fd0;
  --chart-4: #9cbfe6;
  --chart-5: #e07ab9;

  --radius: 10px;
  --radius-sm: 6px;
  --gap: 16px;
  --sidebar-width: 248px;
  --header-height: 56px;
  --shadow-xs: 0 1px 2px rgba(16, 24, 40, .05);
}

* { box-sizing: border-box; }
html { font-size: 15px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: "Vazirmatn", Tahoma, sans-serif; color: var(--foreground); background: var(--background);
  line-height: 1.7; overflow-x: hidden;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 8px; line-height: 1.4; color: var(--foreground); font-weight: 600; }
h1 { font-size: 1.3rem; } h2 { font-size: 1rem; } h3 { font-size: .93rem; }
.muted { color: var(--muted-foreground); }
.small { font-size: .85rem; }
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }
.nowrap { white-space: nowrap; }
:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

/* --- app shell: sidebar (right) + inset ------------------------------------------------------------- */
.sidebar {
  position: fixed; inset-block: 0; inset-inline-start: 0; width: var(--sidebar-width); z-index: 30;
  display: flex; flex-direction: column; gap: 4px; padding: 12px;
  background: var(--sidebar); border-inline-end: 1px solid var(--border);
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 14px; color: var(--foreground); }
.sidebar-brand:hover { text-decoration: none; }
.sidebar-brand img { width: 40px; height: auto; flex: none; }
.sidebar-brand b { display: block; font-size: .95rem; line-height: 1.3; }
.sidebar-brand span { display: block; font-size: .78rem; color: var(--muted-foreground); line-height: 1.3; }
.sidebar-label { display: block; padding: 10px 8px 4px; margin: 0; font-size: .75rem; font-weight: 400; color: var(--muted-foreground); }
.sidebar-project { padding: 0 0 4px; }
.sidebar-project select { width: 100%; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav a {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: var(--radius-sm);
  color: var(--foreground); font-size: .92rem;
}
.sidebar-nav a:hover { background: var(--sidebar-accent); text-decoration: none; }
.sidebar-nav a.on { background: var(--brand-50); color: var(--primary); font-weight: 600; }
.sidebar-nav svg { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8;
                   stroke-linecap: round; stroke-linejoin: round; }
.sidebar-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--border); }
.sidebar-user { display: flex; align-items: center; gap: 10px; padding: 6px 8px; }
.avatar {
  width: 32px; height: 32px; border-radius: 999px; flex: none; display: grid; place-items: center;
  background: var(--pink-50); color: var(--pink); font-weight: 700; font-size: .85rem;
}
.sidebar-user .who { flex: 1; min-width: 0; font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.inset { margin-inline-start: var(--sidebar-width); min-height: 100vh; display: flex; flex-direction: column; min-width: 0; }
.site-header {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  min-height: var(--header-height); padding: 8px 24px; background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(6px); border-bottom: 1px solid var(--border);
}
.site-header .title { font-weight: 600; font-size: 1rem; margin: 0; flex: 1; min-width: 0; }
.site-header .menu-toggle { display: none; }
.page { width: 100%; max-width: 1400px; margin: 0 auto; padding: 20px 24px 48px; }
.scrim { display: none; }

/* --- period picker (header) ------------------------------------------------------------------------- */
.period-picker { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toggle-group { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--background); }
.toggle-group a, .toggle-group button {
  font: inherit; font-size: .85rem; padding: 4px 12px; border: 0; background: transparent; color: var(--foreground);
  cursor: pointer; white-space: nowrap;
}
.toggle-group > * + * { border-inline-start: 1px solid var(--border); }
.toggle-group a:hover, .toggle-group button:hover { background: var(--muted); text-decoration: none; }
.toggle-group .on { background: var(--brand-50); color: var(--primary); font-weight: 600; }
.range-popover { position: relative; }
.range-popover > summary { list-style: none; }
.range-popover > summary::-webkit-details-marker { display: none; }
.range-popover .popover {
  position: absolute; inset-inline-end: 0; top: calc(100% + 6px); z-index: 40; width: 280px;
  background: var(--background); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(16, 24, 40, .12); padding: 14px;
}
.range-popover .field { margin-bottom: 10px; }
.range-popover input { width: 100%; }
.range-label { font-size: .8rem; color: var(--muted-foreground); white-space: nowrap; }

/* --- layout ------------------------------------------------------------------------------------------ */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--gap); margin-bottom: var(--gap); }
.grid { display: grid; gap: var(--gap); margin-bottom: var(--gap); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3-1 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
.grid > .card { margin-bottom: 0; }

/* --- card -------------------------------------------------------------------------------------------- */
.card {
  background: var(--background); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-xs); padding: 20px; margin-bottom: var(--gap); min-width: 0;
}
.card > h2, .card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.card-desc { color: var(--muted-foreground); font-size: .85rem; margin: 2px 0 14px; }
.card-link { font-size: .85rem; font-weight: 400; }

/* --- KPI stat cards ---------------------------------------------------------------------------------- */
.kpi { display: flex; flex-direction: column; gap: 2px; padding: 18px 20px; }
.kpi-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.kpi-label { color: var(--muted-foreground); font-size: .88rem; }
.kpi-value { font-size: 1.85rem; font-weight: 700; line-height: 1.35; font-variant-numeric: tabular-nums; }
.kpi-note { color: var(--muted-foreground); font-size: .8rem; }
.kpi.off .kpi-value { color: var(--muted-foreground); }
.delta {
  display: inline-flex; align-items: center; gap: 2px; padding: 0 8px; border-radius: 999px; font-size: .78rem;
  border: 1px solid var(--border); white-space: nowrap; direction: ltr; unicode-bidi: isolate;
}
.delta.up { color: var(--good); background: var(--good-50); border-color: transparent; }
.delta.down { color: var(--bad); background: var(--bad-50); border-color: transparent; }
.delta.flat, .delta.new { color: var(--muted-foreground); background: var(--muted); border-color: transparent; }

/* --- forms (shadcn input / select / button) ---------------------------------------------------------- */
form { margin: 0; }
label { display: block; font-size: .85rem; color: var(--foreground); font-weight: 500; margin-bottom: 4px; }
input[type=text], input[type=search], input[type=password], input[type=number], input[type=url], input[type=datetime-local], select, textarea {
  font: inherit; font-size: .9rem; color: var(--foreground); background: var(--background);
  border: 1px solid var(--input); border-radius: var(--radius-sm); padding: 6px 10px; min-height: 36px;
  min-width: 0; max-width: 100%; box-shadow: var(--shadow-xs);
}
textarea { width: 100%; font-family: Consolas, "Courier New", monospace; font-size: .85rem; direction: ltr; text-align: left; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--ring); box-shadow: 0 0 0 3px rgba(157, 183, 216, .45); }
select[multiple] { min-height: 92px; }
input[type=checkbox] { accent-color: var(--primary); width: 16px; height: 16px; vertical-align: middle; }
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.filters .field { display: flex; flex-direction: column; min-width: 0; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.checks label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; font-size: .9rem; margin: 0; }
.inline-form { display: inline-flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.form-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 10px; }
.form-row .field { display: flex; flex-direction: column; min-width: 0; }
.form-row > label { font-weight: 400; }

.btn {
  font: inherit; font-size: .88rem; font-weight: 500; cursor: pointer; border-radius: var(--radius-sm);
  padding: 6px 14px; min-height: 36px; white-space: nowrap; display: inline-flex; align-items: center;
  justify-content: center; gap: 6px; border: 1px solid var(--primary); background: var(--primary);
  color: var(--primary-foreground); box-shadow: var(--shadow-xs);
}
.btn:hover { background: var(--primary-hover); border-color: var(--primary-hover); text-decoration: none; }
.btn-accent { background: var(--pink); border-color: var(--pink); color: var(--primary-foreground); }
.btn-accent:hover { background: var(--pink-hover); border-color: var(--pink-hover); }
.btn-light, .btn-outline { background: var(--background); color: var(--foreground); border-color: var(--input); }
.btn-light:hover, .btn-outline:hover { background: var(--muted); border-color: var(--input); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--foreground); box-shadow: none; }
.btn-ghost:hover { background: var(--sidebar-accent); border-color: transparent; }
.btn-icon { padding: 6px; min-width: 36px; }
.btn-icon svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn-small { padding: 2px 10px; min-height: 30px; font-size: .8rem; }
a.btn { display: inline-flex; }

/* --- tables ------------------------------------------------------------------------------------------ */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.card .table-wrap { margin-top: 4px; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: right; vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
th { color: var(--muted-foreground); font-weight: 500; background: var(--muted); white-space: nowrap; font-size: .82rem; }
th a { color: inherit; }
tbody tr:hover td { background: var(--row-hover); }
td.num, th.num { text-align: left; direction: ltr; white-space: nowrap; font-variant-numeric: tabular-nums; }
td.context { min-width: 320px; max-width: 560px; }
.empty { color: var(--muted-foreground); text-align: center; padding: 24px; }

mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 3px; }

/* --- badges ------------------------------------------------------------------------------------------ */
.badge {
  display: inline-block; padding: 0 8px; border-radius: 999px; font-size: .76rem; font-weight: 500;
  white-space: nowrap; border: 1px solid transparent; line-height: 1.8;
}
.badge-healthy, .badge-succeeded, .badge-accepted, .badge-active { background: var(--good-50); color: var(--good); }
.badge-warning, .badge-queued, .badge-suggested, .badge-paused { background: var(--bad-50); color: var(--bad); }
.badge-failed, .badge-rejected { background: var(--danger-50); color: var(--danger); }
.badge-unknown, .badge-archived, .badge-off { background: var(--muted); color: var(--muted-foreground); }
.badge-running { background: var(--brand-50); color: var(--primary); }
.badge-type { background: var(--background); color: var(--muted-foreground); border-color: var(--border); }
.badge-match { background: var(--pink-50); color: var(--pink); }

/* --- alerts ------------------------------------------------------------------------------------------ */
.alert { border-radius: var(--radius); padding: 10px 14px; margin-bottom: var(--gap); border: 1px solid; font-size: .9rem; }
.alert-error { background: var(--danger-50); border-color: var(--danger); color: var(--danger); }
.alert-warning { background: var(--bad-50); border-color: var(--bad); color: var(--bad); }
.alert-info { background: var(--brand-50); border-color: var(--ring); color: var(--primary); }
.alert-ok { background: var(--good-50); border-color: var(--good); color: var(--good); }

/* --- dashboard pieces -------------------------------------------------------------------------------- */
.chart { width: 100%; height: 280px; }
.chart-sm { height: 220px; }
.bars { list-style: none; margin: 0; padding: 0; }
.bars li { display: grid; grid-template-columns: minmax(0, 120px) minmax(0, 1fr) auto; gap: 10px; align-items: center; padding: 6px 0; }
.bars li > a, .bars li > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { height: 8px; border-radius: 999px; background: var(--muted); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--chart-1); border-radius: 999px; }
.bars li:nth-child(5n+2) .bar > span { background: var(--chart-2); }
.bars li:nth-child(5n+3) .bar > span { background: var(--chart-3); }
.bars li:nth-child(5n+4) .bar > span { background: var(--chart-4); }
.bars li:nth-child(5n+5) .bar > span { background: var(--chart-5); }
.health-strip { display: flex; flex-wrap: wrap; gap: 8px; }
.health-strip .badge { font-size: .85rem; padding: 2px 12px; }
.lag { font-size: .92rem; }
.tabs-list { display: inline-flex; gap: 2px; padding: 3px; border-radius: var(--radius-sm); background: var(--muted); }
.tabs-list a, .tabs-list button {
  font: inherit; font-size: .82rem; padding: 3px 12px; border-radius: 5px; border: 0; background: transparent;
  color: var(--muted-foreground); cursor: pointer; white-space: nowrap;
}
.tabs-list a:hover { text-decoration: none; color: var(--foreground); }
.tabs-list .on, .tabs-list [aria-selected=true] { background: var(--background); color: var(--foreground); box-shadow: var(--shadow-xs); }

/* --- result detail ----------------------------------------------------------------------------------- */
.evidence dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 16px; margin: 0 0 12px; }
.evidence dt { color: var(--muted-foreground); }
.evidence dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.fulltext { white-space: pre-wrap; line-height: 2; background: var(--sidebar); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px; max-height: 520px; overflow: auto; }
.tabs { display: inline-flex; gap: 2px; padding: 3px; margin-bottom: 8px; border-radius: var(--radius-sm); background: var(--muted); }
.tabs .btn { background: transparent; border-color: transparent; color: var(--muted-foreground); box-shadow: none; min-height: 28px; }
.tabs .btn[aria-pressed=true] { background: var(--background); color: var(--foreground); box-shadow: var(--shadow-xs); }
.related ul { padding-inline-start: 18px; }
.related li { margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0; margin: 0; list-style: none; }
.chips li { background: var(--brand-50); color: var(--primary); border-radius: 999px; padding: 0 10px; font-size: .85rem; }

/* --- login ------------------------------------------------------------------------------------------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: var(--sidebar); }
.login-box { width: 100%; max-width: 380px; margin: 0; padding: 28px 24px; }
.login-box .logo { display: block; width: 92px; height: auto; margin: 0 auto 12px; }
.login-box h1 { text-align: center; font-size: 1.15rem; margin-bottom: 2px; }
.login-box .card-desc { text-align: center; }
.login-box .field { margin-bottom: 14px; }
.login-box input { width: 100%; }
.login-box .btn { width: 100%; min-height: 40px; font-size: .95rem; }

.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }
details > summary { cursor: pointer; color: var(--primary); }
.htmx-request .spin { opacity: .5; }

/* --- responsive -------------------------------------------------------------------------------------- */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2, .grid-3-1 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 900px) {
  .sidebar { transform: translateX(100%); transition: transform .2s ease; }
  /* the shadow only while open — a closed drawer's shadow would leak onto the screen edge */
  .nav-toggle:checked ~ .sidebar { transform: none; box-shadow: -8px 0 24px rgba(16, 24, 40, .12); }
  .nav-toggle:checked ~ .scrim { display: block; position: fixed; inset: 0; z-index: 25; background: rgba(10, 10, 10, .35); }
  .inset { margin-inline-start: 0; }
  .site-header .menu-toggle { display: inline-flex; }
  .site-header { padding: 8px 16px; }
  .page { padding: 16px 16px 40px; }
}
@media (max-width: 560px) {
  .grid-4 { grid-template-columns: minmax(0, 1fr); }
  .site-header .period-picker { width: 100%; }
  .range-label { display: none; }
  .card { padding: 16px; }
  .kpi-value { font-size: 1.6rem; }
  td.context { min-width: 240px; }
  .bars li { grid-template-columns: minmax(0, 96px) minmax(0, 1fr) auto; }
  .range-popover .popover { inset-inline-end: auto; inset-inline-start: 0; width: min(280px, calc(100vw - 32px)); }
}
