/* ============================================================
   tgdash design system
   Style: data-dense dashboard, dark, RTL (Persian)
   Palette: validated dataviz dark set (CVD-checked)
   Type: Vazirmatn, self-hosted (Google Fonts is unreliable from Iran)
   ============================================================ */

@font-face {
  font-family: 'Vazirmatn';
  src: url('/static/fonts/Vazirmatn-Regular.woff2') format('woff2');
  font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'Vazirmatn';
  src: url('/static/fonts/Vazirmatn-Medium.woff2') format('woff2');
  font-weight: 500; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'Vazirmatn';
  src: url('/static/fonts/Vazirmatn-Bold.woff2') format('woff2');
  font-weight: 700; font-style: normal; font-display: swap;
}

:root {
  /* surfaces & ink */
  --plane: #0d0d0d;
  --surface: #191918;
  --surface-2: #21211f;
  --ink: #ffffff;
  --ink2: #c3c2b7;
  --muted: #918f88;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.16);

  /* series (validated dark steps) */
  --s1: #3987e5; --s2: #d95926; --s3: #199e70;
  --s4: #c98500; --s5: #d55181; --s6: #008300;

  /* status */
  --good: #0ca30c;
  --bad: #d03b3b;
  --warn: #fab219;

  /* spacing — density 8/10 */
  --sp1: 4px; --sp2: 8px; --sp3: 12px; --sp4: 16px; --sp5: 20px; --sp6: 24px; --sp8: 32px;

  /* type */
  --fs-micro: 12px; --fs-sm: 13px; --fs-body: 15px; --fs-h: 15px;
  --fs-value: 26px; --lh: 1.7;

  --radius: 10px;
  --radius-lg: 14px;
  --ring: 0 0 0 2px var(--plane), 0 0 0 4px var(--s1);
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --dur: 180ms;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--plane);
  color: var(--ink2);
  font-family: 'Vazirmatn', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  font-size: var(--fs-body);
  line-height: var(--lh);
  -webkit-font-smoothing: antialiased;
}

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

:focus-visible { outline: none; box-shadow: var(--ring); border-radius: 6px; }

/* ---------- header ---------- */
header {
  display: flex; align-items: center; gap: var(--sp5);
  padding: var(--sp3) var(--sp6);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
  background: rgba(13, 13, 13, 0.88);
  backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: var(--sp2); color: var(--ink); font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--s1); box-shadow: 0 0 0 3px rgba(57, 135, 229, 0.18); }
.brand small { color: var(--muted); font-weight: 400; font-size: var(--fs-micro); }

nav { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
nav::-webkit-scrollbar { display: none; }
nav a {
  display: flex; align-items: center; gap: 7px;
  color: var(--ink2); padding: 8px 13px; border-radius: 9px;
  font-size: 14px; white-space: nowrap; min-height: 40px;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
nav a svg { width: 16px; height: 16px; opacity: 0.75; flex: none; }
nav a:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
nav a.active { background: var(--surface-2); color: var(--ink); font-weight: 500; }
nav a.active svg { opacity: 1; color: var(--s1); }

.head-right { margin-inline-start: auto; display: flex; align-items: center; gap: var(--sp3); }
.stamp { color: var(--muted); font-size: var(--fs-micro); white-space: nowrap; }
.live { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: var(--fs-micro); }
.live i { width: 6px; height: 6px; border-radius: 50%; background: var(--good); animation: pulse 2.4s var(--ease) infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

/* ---------- layout ---------- */
main { max-width: 1320px; margin: 0 auto; padding: var(--sp6) var(--sp6) 64px; }
/* min-width:0 — grid/flex children default to min-width:auto, which lets a
   canvas or wide table blow out the track (or collapse the canvas to 0). */
/* One vertical rhythm for every top-level block. The old `.card + .card` rule
   missed the common case of a card followed by a .grid2 row, which left them
   touching with 0px between. */
main > * + * { margin-top: var(--sp5); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp5); align-items: start; }
.grid2 > * { min-width: 0; }
/* grid/flex children default to min-width:auto, which lets wide tables push the
   page instead of scrolling inside their own container — this is the fix */
.grid2 > *, .stack > *, .card, .tablewrap { min-width: 0; }
.stack > * + * { margin-top: var(--sp5); }

/* ---------- KPI tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); gap: var(--sp3); }
.tile {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--sp3) var(--sp4) var(--sp4);
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
  /* tiles are buttons: they select which metric the chart plots */
  font: inherit; color: inherit; text-align: inherit; width: 100%;
  display: block; cursor: pointer;
}
.tile:hover { border-color: var(--border-strong); background: var(--surface-2); }
.tile[aria-pressed="true"] {
  border-color: rgba(57, 135, 229, 0.55);
  background: linear-gradient(180deg, rgba(57,135,229,0.10), transparent 60%), var(--surface);
}
.tile[aria-pressed="true"] .label { color: var(--ink2); }
.tile .label { color: var(--muted); font-size: var(--fs-micro); margin-bottom: var(--sp1); line-height: 1.4; }
.tile .value {
  color: var(--ink); font-size: var(--fs-value); font-weight: 700;
  direction: ltr; text-align: right; line-height: 1.25; letter-spacing: -0.01em;
}
/* RTL line: first item sits rightmost, so the number is read first.
   Numeric runs are isolated so bidi can't fling their digits to the far side. */
.tile .sub { font-size: var(--fs-micro); margin-top: 2px; direction: rtl; text-align: right; line-height: 1.5; }
.tile .sub .up, .tile .sub .down, .tile .sub .flat { direction: ltr; unicode-bidi: isolate; }
.tile .sub .muted { unicode-bidi: isolate; }
.tile.accent { border-color: rgba(57, 135, 229, 0.35); background: linear-gradient(180deg, rgba(57,135,229,0.07), transparent 60%), var(--surface); }

.up { color: var(--good); }
.down { color: var(--bad); }
.flat { color: var(--muted); }

/* ---------- cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: var(--sp6);
}
/* NOTE: no `.card + .card` margin. Inside .grid2 the second card is a sibling
   that sits BESIDE the first, so a top margin there pushed one column down and
   broke the row alignment. Vertical spacing comes from `main > * + *` and
   `.stack > * + *`, which only apply where cards actually stack. */
.card-head { display: flex; align-items: center; gap: var(--sp3); margin-bottom: var(--sp4); flex-wrap: wrap; }
.card h2 { margin: 0; font-size: var(--fs-h); color: var(--ink); font-weight: 600; line-height: 1.5; }
.card h2 small { color: var(--muted); font-weight: 400; font-size: var(--fs-micro); margin-inline-start: 6px; }
.card .note { color: var(--muted); font-size: var(--fs-micro); margin-top: var(--sp3); line-height: 1.6; }
.chartbox { position: relative; height: 300px; width: 100%; min-width: 0; }
.chartbox.tall { height: 340px; }
.chartbox canvas { max-width: 100%; }
.card { min-width: 0; }

/* segmented range control */
.seg { margin-inline-start: auto; display: flex; gap: 2px; background: var(--surface-2); border-radius: 9px; padding: 3px; }
.seg button {
  font: inherit; font-size: var(--fs-micro); color: var(--ink2);
  background: none; border: 0; border-radius: 7px; padding: 6px 11px;
  cursor: pointer; min-height: 32px;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.seg button:hover { color: var(--ink); background: rgba(255, 255, 255, 0.05); }
.seg button[aria-pressed="true"] { background: var(--s1); color: #fff; font-weight: 500; }

/* custom date range */
.daterange { display: none; align-items: center; gap: var(--sp3); flex-wrap: wrap; margin-bottom: var(--sp4); }
.daterange.on { display: flex; }
.daterange label { color: var(--muted); font-size: var(--fs-micro); display: flex; align-items: center; gap: 6px; }
.daterange input[type="date"] {
  font: inherit; font-size: var(--fs-micro); color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 9px; min-height: 32px; color-scheme: dark;
}
.daterange input[type="date"]:hover { border-color: var(--border-strong); }
.daterange button {
  font: inherit; font-size: var(--fs-micro); font-weight: 500; color: #fff;
  background: var(--s1); border: 0; border-radius: 8px; padding: 7px 14px;
  min-height: 32px; cursor: pointer;
  transition: filter var(--dur) var(--ease);
}
.daterange button:hover { filter: brightness(1.12); }

/* channel picker */
.sel {
  margin-inline-start: auto;
  font: inherit; font-size: var(--fs-micro); color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 9px; padding: 7px 11px; min-height: 34px; max-width: 200px;
  cursor: pointer; appearance: none;
  transition: border-color var(--dur) var(--ease);
}
.sel:hover { border-color: var(--border-strong); }
.sel option { background: var(--surface-2); color: var(--ink); }

/* ---------- tables ---------- */
/* bleed to the card edge so a scrolling table doesn't look inset */
.tablewrap { overflow-x: auto; max-width: 100%; margin: 0 calc(var(--sp6) * -1); padding: 0 var(--sp6); -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
th, td { padding: 9px 10px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th {
  color: var(--muted); font-weight: 500; font-size: var(--fs-micro);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
th.sortable { cursor: pointer; user-select: none; transition: color var(--dur) var(--ease); }
th.sortable:hover { color: var(--ink); }
th.sortable::after { content: '↕'; opacity: 0.3; margin-inline-start: 5px; font-size: 10px; }
th.sortable[data-dir="asc"]::after { content: '↑'; opacity: 1; color: var(--s1); }
th.sortable[data-dir="desc"]::after { content: '↓'; opacity: 1; color: var(--s1); }
td { color: var(--ink2); }
tbody tr { transition: background var(--dur) var(--ease); }
tbody tr:hover td { background: var(--surface-2); }
tr:last-child td { border-bottom: none; }
td.num, th.num { font-variant-numeric: tabular-nums; direction: ltr; text-align: end; }
td.wrap { white-space: normal; min-width: 260px; line-height: 1.6; }
tr.own td { color: var(--ink); font-weight: 500; background: rgba(57, 135, 229, 0.06); }
tr.own td:first-child { box-shadow: inset -3px 0 0 var(--s1); }
.rank { color: var(--muted); font-variant-numeric: tabular-nums; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border-strong); color: var(--ink2); font-size: 11px; }
.pill.s1 { border-color: rgba(57,135,229,0.5); color: #8bbcf3; }
.bar { display: inline-block; height: 5px; border-radius: 3px; background: var(--s1); vertical-align: middle; margin-inline-end: 7px; min-width: 2px; }

.muted { color: var(--muted); }
.empty { color: var(--muted); padding: var(--sp6) 0; text-align: center; font-size: var(--fs-sm); }
.err { color: var(--bad); padding: var(--sp5) 0; text-align: center; font-size: var(--fs-sm); }

/* ---------- skeletons ---------- */
.sk { background: linear-gradient(90deg, var(--surface-2) 25%, #2b2b28 50%, var(--surface-2) 75%);
      background-size: 200% 100%; animation: shimmer 1.4s linear infinite; border-radius: 6px; }
@keyframes shimmer { to { background-position: -200% 0; } }
.sk-line { height: 11px; margin: 7px 0; }
.sk-chart { height: 100%; border-radius: var(--radius); }

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .grid2 { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  :root { --fs-body: 16px; --fs-sm: 14px; --fs-value: 24px; }
  header { padding: var(--sp3) var(--sp4); gap: var(--sp3); flex-wrap: wrap; }
  .head-right { width: 100%; margin: 0; order: 3; justify-content: space-between; }
  main { padding: var(--sp4) var(--sp4) 48px; }
  .card { padding: var(--sp4); }
  .tablewrap { margin: 0 calc(var(--sp4) * -1); padding: 0 var(--sp4); }
  .chartbox { height: 240px; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}

/* ---------- login page ---------- */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: var(--sp6); }
.login-wrap { width: 100%; max-width: 380px; }
.login-card { display: flex; flex-direction: column; gap: var(--sp4); }
.login-brand { justify-content: center; font-size: 20px; margin-top: var(--sp2); }
.login-sub { text-align: center; font-size: var(--fs-sm); margin: 0 0 var(--sp2); }
.login-label { display: flex; flex-direction: column; gap: 6px; color: var(--ink2); font-size: var(--fs-sm); }
.login-input {
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px;
  color: var(--ink); padding: 10px var(--sp3); font: inherit; font-size: var(--fs-body);
}
.login-input:focus-visible { border-color: var(--s1); }
.login-btn {
  margin-top: var(--sp2); background: var(--s1); color: #fff; border: 0; border-radius: 8px;
  padding: 11px; font: inherit; font-size: var(--fs-body); font-weight: 500; cursor: pointer;
}
.login-btn:hover { filter: brightness(1.12); }
.login-err {
  background: rgba(208, 59, 59, 0.12); border: 1px solid rgba(208, 59, 59, 0.4);
  color: #f0908f; border-radius: 8px; padding: var(--sp2) var(--sp3); font-size: var(--fs-sm);
  text-align: center; line-height: 1.6;
}
.logout-btn {
  background: none; border: 1px solid var(--border); color: var(--muted); border-radius: 7px;
  padding: 4px 10px; font: inherit; font-size: var(--fs-micro); cursor: pointer;
}
.logout-btn:hover { color: var(--ink); border-color: var(--border-strong); }
