/* ==========================================================================
   Iran 4-Platform Labor Market Intelligence Dashboard (JobVision + e-Estekhdam + Divar + Sheypoor)
   Modern Web Guidance Compliant CSS:
   - dark-mode & dark-mode-toggle: color-scheme on :root + light-dark() with @supports fallback
   - tabs: @property --fade-start / --fade-end scroll-driven mask + [aria-selected="true"]::after
   - responsive-table: @container (width < 600px) stacked mobile layout
   - sortable-table: th[aria-sort] CSS-driven sort icons + view-transition-name: match-element
   ========================================================================== */

@property --fade-start {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

@property --fade-end {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

:root {
  color-scheme: light dark;

  /* Fallback variables for older browsers without light-dark() */
  --bg-canvas: #f1f5f9;
  --bg-surface: #ffffff;
  --bg-elevated: #f8fafc;
  --text-primary: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;
  --border-subtle: #cbd5e1;
  --border-strong: #94a3b8;

  --accent: #2563eb;
  --accent-soft: rgb(37 99 235 / 0.12);
  --color-jv: #0284c7;
  --color-ee: #7c3aed;
  --color-dv: #dc2626;
  --color-sh: #ea580c;
  --color-emerald: #059669;
  --color-amber: #d97706;
  --color-rose: #e11d48;

  --scrollbar-thumb: #94a3b8;
  --scrollbar-track: #e2e8f0;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-canvas: #090d16;
    --bg-surface: #111827;
    --bg-elevated: #1e293b;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border-subtle: #1e293b;
    --border-strong: #334155;

    --accent: #60a5fa;
    --accent-soft: rgb(96 165 250 / 0.16);
    --color-jv: #38bdf8;
    --color-ee: #a78bfa;
    --color-dv: #f87171;
    --color-sh: #fb923c;
    --color-emerald: #34d399;
    --color-amber: #fbbf24;
    --color-rose: #fb7185;

    --scrollbar-thumb: #475569;
    --scrollbar-track: #0f172a;
  }
}

:root[data-theme="light"] {
  color-scheme: light;
}

:root[data-theme="dark"] {
  color-scheme: dark;
}

@supports (color: light-dark(white, black)) {
  :root {
    --bg-canvas: light-dark(#f1f5f9, #090d16);
    --bg-surface: light-dark(#ffffff, #111827);
    --bg-elevated: light-dark(#f8fafc, #1e293b);
    --text-primary: light-dark(#0f172a, #f8fafc);
    --text-secondary: light-dark(#334155, #cbd5e1);
    --text-muted: light-dark(#64748b, #94a3b8);
    --border-subtle: light-dark(#cbd5e1, #1e293b);
    --border-strong: light-dark(#94a3b8, #334155);

    --accent: light-dark(#2563eb, #60a5fa);
    --accent-soft: light-dark(rgb(37 99 235 / 0.12), rgb(96 165 250 / 0.16));
    --color-jv: light-dark(#0284c7, #38bdf8);
    --color-ee: light-dark(#7c3aed, #a78bfa);
    --color-dv: light-dark(#dc2626, #f87171);
    --color-sh: light-dark(#ea580c, #fb923c);
    --color-emerald: light-dark(#059669, #34d399);
    --color-amber: light-dark(#d97706, #fbbf24);
    --color-rose: light-dark(#e11d48, #fb7185);

    --scrollbar-thumb: light-dark(#94a3b8, #475569);
    --scrollbar-track: light-dark(#e2e8f0, #0f172a);
  }
}

/* Custom Scrollbar with WebKit fallback */
@supports (scrollbar-color: auto) {
  * {
    scrollbar-width: auto;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
  }
}

@supports selector(::-webkit-scrollbar) {
  ::-webkit-scrollbar {
    width: 11px;
    height: 11px;
  }
  ::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 6px;
  }
  ::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Vazirmatn", "Tahoma", system-ui, -apple-system, sans-serif;
  background-color: var(--bg-canvas);
  color: var(--text-primary);
  line-height: 1.6;
  direction: rtl;
}

/* Visually hidden status element for screen reader announcements (sortable-table) */
.sr-only {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ==========================================================================
   App Header & Platform Pills
   ========================================================================== */
.app-header {
  background: var(--bg-surface);
  border-block-end: 1px solid var(--border-subtle);
  padding: 0.9rem 1.4rem;
  position: sticky;
  inset-block-start: 0;
  z-index: 40;
}

.header-inner {
  max-inline-size: 1640px;
  margin-inline: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-badge {
  inline-size: 42px;
  block-size: 42px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--color-jv), var(--color-ee));
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 800;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.brand-title {
  margin: 0;
  font-size: clamp(1rem, 1.6vw, 1.28rem);
  font-weight: 800;
}

.brand-subtitle {
  margin: 0;
  font-size: 0.79rem;
  color: var(--text-secondary);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.source-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.68rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  text-decoration: none;
  color: var(--text-primary);
}

.source-pill.jv {
  border-color: var(--color-jv);
  color: var(--color-jv);
}

.source-pill.ee {
  border-color: var(--color-ee);
  color: var(--color-ee);
}

.source-pill.dv {
  border-color: var(--color-dv);
  color: var(--color-dv);
}

.source-pill.sh {
  border-color: var(--color-sh);
  color: var(--color-sh);
}

.theme-toggle-btn,
.live-refresh-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.42rem 0.82rem;
  border-radius: 0.6rem;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  min-block-size: 36px;
}

.live-refresh-btn {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.live-refresh-btn:hover,
.theme-toggle-btn:hover {
  opacity: 0.92;
  border-color: var(--accent);
}

/* Live Update Status Drawer */
.live-status-drawer {
  max-inline-size: 1640px;
  margin: 0.6rem auto 0;
  padding: 0.75rem 1rem;
  background: var(--bg-elevated);
  border: 1px solid var(--accent);
  border-radius: 0.7rem;
  font-size: 0.82rem;
}

.live-status-drawer[hidden] {
  display: none;
}

.live-progress-track {
  inline-size: 100%;
  block-size: 8px;
  background: var(--border-subtle);
  border-radius: 999px;
  overflow: hidden;
  margin-block: 0.45rem;
}

.live-progress-bar {
  block-size: 100%;
  background: linear-gradient(90deg, var(--color-jv), var(--color-emerald));
  transition: width 0.3s ease;
}

/* ==========================================================================
   Main Container & Accessible Tabs (modern-web-guidance: tabs)
   ========================================================================== */
.app-main {
  max-inline-size: 1640px;
  margin-inline: auto;
  padding: 1.15rem 1.4rem 3rem;
}

.tabs-container {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 0.85rem;
  padding-inline: 0.65rem;
  margin-block-end: 1.25rem;
  position: sticky;
  inset-block-start: 68px;
  z-index: 35;
}

[role="tablist"] {
  display: flex;
  gap: 0.15rem;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
  border-block-end: 1px solid var(--border-subtle);

  --fade-size: 2.5rem;
  --fade-dir: right;
  --fade-start-pos: 0%;
  --fade-end-pos: 100%;

  mask-image:
    linear-gradient(to var(--fade-dir), transparent, black var(--fade-start)),
    linear-gradient(to var(--fade-dir), black calc(100% - var(--fade-end)), transparent);
  mask-composite: intersect;

  animation:
    reveal-fade-start linear both,
    hide-fade-end linear both;
  animation-timeline: scroll(self inline);
  animation-range:
    0 var(--fade-size),
    calc(100% - var(--fade-size)) 100%;

  &:dir(rtl) {
    --fade-dir: left;
    --fade-start-pos: 100%;
    --fade-end-pos: 0;
  }
}

@keyframes reveal-fade-start {
  to {
    --fade-start: var(--fade-size);
  }
}

@keyframes hide-fade-end {
  from {
    --fade-end: var(--fade-size);
  }
}

[role="tab"] {
  flex: none;
  scroll-snap-align: start;
  position: relative;
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0.85rem 0.95rem;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

[role="tab"]:hover {
  color: var(--text-primary);
}

[role="tab"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -3px;
  border-radius: 0.4rem;
}

/* MANDATORY: Style selected tab from aria-selected with a 3px border underline on ::after */
[role="tab"][aria-selected="true"] {
  color: var(--accent);

  &::after {
    content: "";
    position: absolute;
    inset: auto 0 0;
    border-block-end: 3px solid var(--accent);
  }
}

/* MANDATORY: Do NOT put padding/borders/margins/backgrounds on [role="tabpanel"] itself.
   Also ensure [role="tabpanel"][hidden] never intercepts pointer/scroll events! */
.tabpanels-grid {
  position: relative;
}

[role="tabpanel"][hidden] {
  pointer-events: none !important;
  visibility: hidden !important;
  position: absolute !important;
  inset-block-start: 0 !important;
  inset-inline-start: 0 !important;
  inline-size: 0 !important;
  block-size: 0 !important;
  overflow: hidden !important;
  z-index: -1 !important;
}

.panel-inner {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ==========================================================================
   KPI Cards, Analytical Insights & Grid Layouts
   ========================================================================== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.85rem;
}

.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 0.85rem;
  padding: 1rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: "";
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  inline-size: 4px;
  block-size: 100%;
  background: var(--accent);
}

.kpi-card.jv::before { background: var(--color-jv); }
.kpi-card.ee::before { background: var(--color-ee); }
.kpi-card.dv::before { background: var(--color-dv); }
.kpi-card.sh::before { background: var(--color-sh); }
.kpi-card.emerald::before { background: var(--color-emerald); }
.kpi-card.amber::before { background: var(--color-amber); }
.kpi-card.rose::before { background: var(--color-rose); }

.kpi-label {
  font-size: 0.78rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.kpi-value {
  font-size: 1.42rem;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1.25;
}

.kpi-sub {
  font-size: 0.74rem;
  color: var(--text-muted);
}

/* Analytical Commentary / Decision Cards */
.insights-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 1rem;
}

.insight-box {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-inline-start: 4px solid var(--accent);
  border-radius: 0.85rem;
  padding: 1.1rem 1.25rem;
}

.insight-box h3 {
  margin: 0 0 0.45rem;
  font-size: 0.96rem;
  font-weight: 800;
  color: var(--text-primary);
}

.insight-box p {
  margin: 0 0 0.55rem;
  font-size: 0.84rem;
  color: var(--text-secondary);
}

.insight-box ul {
  margin: 0;
  padding-inline-start: 1.2rem;
  font-size: 0.82rem;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.charts-grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 540px), 1fr));
  gap: 1.15rem;
}

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 0.9rem;
  padding: 1.2rem 1.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.95rem;
}

.card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.card-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 800;
  color: var(--text-primary);
}

.card-subtitle {
  margin: 0.2rem 0 0;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.chart-box {
  position: relative;
  inline-size: 100%;
  block-size: 360px;
}

.chart-box.tall {
  block-size: 460px;
}

.chart-box canvas {
  inline-size: 100%;
  block-size: 100%;
  display: block;
}

/* ==========================================================================
   Filter Controls & Select Dropdowns (Guaranteed Interactive)
   ========================================================================== */
.controls-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.95rem;
  padding: 0.95rem 1.05rem;
  background: var(--bg-elevated);
  border-radius: 0.75rem;
  border: 1px solid var(--border-subtle);
  position: relative;
  z-index: 10;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1 1 210px;
}

.control-group label {
  font-size: 0.79rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.control-group select,
.control-group input[type="search"],
.control-group input[type="number"] {
  inline-size: 100%;
  min-block-size: 40px;
  padding: 0.45rem 0.75rem;
  border-radius: 0.55rem;
  border: 1px solid var(--border-strong);
  background-color: var(--bg-surface);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  position: relative;
  z-index: 12;
}

.control-group input[type="search"] {
  cursor: text;
}

.control-group select:focus-visible,
.control-group input:focus-visible {
  outline: 2px solid var(--accent);
  border-color: var(--accent);
}

.btn-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 0.95rem;
  border-radius: 0.55rem;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  min-block-size: 38px;
}

.btn-action.secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.btn-action:hover {
  opacity: 0.92;
}

/* ==========================================================================
   Responsive & Sortable Data Tables (Fixed Vertical & Horizontal Scrolling!)
   ========================================================================== */
.table-container-outer {
  container-type: inline-size;
  inline-size: 100%;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-block-end: 0.45rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.table-wrapper {
  overflow-y: auto;
  overflow-x: auto;
  max-inline-size: 100%;
  max-block-size: 540px;
  overscroll-behavior: contain;
  border: 1px solid var(--border-strong);
  border-radius: 0.75rem;
  background: var(--bg-surface);
  position: relative;
  z-index: 5;
}

.table-wrapper.expanded-height {
  max-block-size: none;
  overflow-y: visible;
}

.table-wrapper:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

table.data-table {
  inline-size: 100%;
  min-inline-size: 780px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.83rem;
}

table.data-table caption {
  text-align: start;
  padding: 0.7rem 1rem;
  font-weight: 700;
  font-size: 0.84rem;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  border-block-end: 1px solid var(--border-subtle);
}

table.data-table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 4;
  background: var(--bg-elevated);
}

table.data-table th,
table.data-table td {
  padding: 0.68rem 0.82rem;
  text-align: start;
  border-block-end: 1px solid var(--border-subtle);
  white-space: nowrap;
}

table.data-table thead th {
  font-weight: 700;
  color: var(--text-primary);
  background: var(--bg-elevated);
}

/* Drop cell padding on th:has(> button) since the button supplies it (sortable-table) */
table.data-table thead th:has(> button) {
  padding: 0;
}

table.data-table thead th > button {
  appearance: none;
  background: transparent;
  border: none;
  color: inherit;
  font: inherit;
  font-weight: 700;
  inline-size: 100%;
  min-block-size: 38px;
  padding: 0.68rem 0.82rem;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  cursor: pointer;
  text-align: start;
}

table.data-table thead th > button:hover {
  background: rgb(100 116 139 / 0.12);
}

table.data-table thead th > button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.sort-icon {
  display: inline-block;
  inline-size: 0.85rem;
  text-align: center;
  opacity: 0.35;
  font-size: 0.75rem;
}

th[aria-sort="ascending"] .sort-icon,
th[aria-sort="descending"] .sort-icon {
  opacity: 1;
  color: var(--accent);
}

th[aria-sort="ascending"] .sort-icon::before {
  content: "▲";
}

th[aria-sort="descending"] .sort-icon::before {
  content: "▼";
}

th:not([aria-sort]) .sort-icon::before {
  content: "⇅";
}

table.data-table tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 2;
  background: var(--bg-surface);
  font-weight: 700;
  border-inline-end: 1px solid var(--border-subtle);
}

table.data-table tbody tr {
  view-transition-name: match-element;
}

table.data-table tbody tr:hover td,
table.data-table tbody tr:hover th {
  background: var(--bg-elevated);
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none;
  }
}

/* Stacked Mobile Table via @container query (responsive-table) */
@container (width < 600px) {
  table.data-table {
    min-inline-size: 100%;
  }

  table.data-table,
  table.data-table thead,
  table.data-table tbody,
  table.data-table tr,
  table.data-table th,
  table.data-table td {
    display: block;
    white-space: normal;
  }

  table.data-table thead {
    display: none;
  }

  table.data-table tbody tr {
    margin-block-end: 1rem;
    border: 1px solid var(--border-subtle);
    border-radius: 0.5rem;
  }

  table.data-table tbody th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--bg-elevated);
    border-inline-end: none;
  }

  table.data-table td:nth-child(2)::before { content: var(--label-2, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(3)::before { content: var(--label-3, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(4)::before { content: var(--label-4, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(5)::before { content: var(--label-5, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(6)::before { content: var(--label-6, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(7)::before { content: var(--label-7, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(8)::before { content: var(--label-8, "") ": "; font-weight: 700; color: var(--text-secondary); }
  table.data-table td:nth-child(9)::before { content: var(--label-9, "") ": "; font-weight: 700; color: var(--text-secondary); }
}

/* Badges & Status Chips */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
}

.badge.golden_opportunity,
.badge.golden_optimal {
  background: rgb(16 185 129 / 0.16);
  color: var(--color-emerald);
}

.badge.hot_competitive,
.badge.high_roi {
  background: rgb(245 158 11 / 0.16);
  color: var(--color-amber);
}

.badge.saturated,
.badge.over_saturated {
  background: rgb(244 63 94 / 0.16);
  color: var(--color-rose);
}

.badge.niche,
.badge.balanced {
  background: rgb(59 130 246 / 0.16);
  color: var(--accent);
}

/* Level Explorer Result Cards */
.sim-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.85rem;
}

.sim-box {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 0.75rem;
  padding: 0.9rem 1.05rem;
}

.sim-box-title {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.sim-box-val {
  font-size: 1.28rem;
  font-weight: 800;
  margin-block-start: 0.25rem;
  color: var(--text-primary);
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.tag-chip {
  padding: 0.3rem 0.65rem;
  border-radius: 0.5rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  font-size: 0.78rem;
  font-weight: 600;
}
