/* ==========================================================================
   PANDA GARAGE — Design system tokens, base styles & application shell
   All layout uses CSS logical properties so RTL/LTR flip automatically.
   ========================================================================== */

:root {
  /* Brand */
  --brand-amber: #F5B000;
  --brand-amber-strong: #DB9C00;
  --brand-ink: #15181D;

  /* Typography */
  --font-latin: "Inter", "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-arabic: "IBM Plex Sans Arabic", "Inter", system-ui, "Segoe UI", Tahoma, sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
  --font-sans: var(--font-latin);

  --fs-2xs: 0.6875rem;  /* 11 */
  --fs-xs: 0.75rem;     /* 12 */
  --fs-sm: 0.8125rem;   /* 13 */
  --fs-base: 0.9375rem; /* 15 */
  --fs-md: 1rem;        /* 16 */
  --fs-lg: 1.125rem;    /* 18 */
  --fs-xl: 1.375rem;    /* 22 */
  --fs-2xl: 1.75rem;    /* 28 */
  --fs-3xl: 2.25rem;    /* 36 */
  --fw-regular: 400;
  --fw-medium: 500;
  --fw-semibold: 600;
  --fw-bold: 700;
  --lh-tight: 1.25;
  --lh-base: 1.6;

  /* Spacing (4px grid) */
  --sp-0: 0;
  --sp-1: 0.25rem;
  --sp-2: 0.5rem;
  --sp-3: 0.75rem;
  --sp-4: 1rem;
  --sp-5: 1.25rem;
  --sp-6: 1.5rem;
  --sp-7: 2rem;
  --sp-8: 2.5rem;
  --sp-9: 3rem;
  --sp-10: 4rem;

  /* Radius */
  --radius-xs: 4px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-pill: 999px;

  /* Sizing */
  --tap: 44px;              /* minimum touch target */
  --control-h: 44px;
  --control-h-sm: 34px;
  --control-h-lg: 52px;
  --sidebar-w: 276px;
  --sidebar-rail: 76px;
  --topbar-h: 68px;
  --mobile-nav-h: 64px;
  --content-max: 1440px;

  /* Motion */
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --dur-fast: 120ms;
  --dur: 200ms;
  --dur-slow: 320ms;

  /* Z-index scale */
  --z-sticky: 20;
  --z-sidebar: 40;
  --z-dropdown: 60;
  --z-overlay: 80;
  --z-modal: 90;
  --z-toast: 100;

  /* Direction multiplier for transforms (1 = LTR, -1 = RTL) */
  --dir: 1;

  /* ---------- Light theme surfaces ---------- */
  --bg: #F2F3F5;
  --surface-1: #FFFFFF;
  --surface-2: #F7F8FA;
  --surface-3: #EEF0F3;
  --surface-inverse: #15181D;
  --border: #E2E5EA;
  --border-strong: #CBD0D8;
  --text: #14171C;
  --text-2: #4A5260;
  --text-3: #5F6674;   /* ≥ 4.8:1 on every light surface (WCAG AA) */
  --text-inverse: #FFFFFF;

  --primary-bg: #15181D;
  --primary-bg-hover: #2A2F37;
  --primary-text: #FFFFFF;
  --accent: var(--brand-amber);
  --accent-hover: var(--brand-amber-strong);
  --accent-text: #15181D;
  --accent-soft: #FFF5D6;
  --accent-ink: #8A5E00;

  /* Status */
  --success: #137333;  --success-soft: #E6F5EC;
  --warning: #A3480A;  --warning-soft: #FDF1DC;
  --danger: #C62828;   --danger-soft: #FCEBEB;
  --info: #1D4ED8;     --info-soft: #E8EEFD;
  --neutral: #4B5563;  --neutral-soft: #EEF0F3;
  --violet: #6D28D9;   --violet-soft: #F1EAFE;

  /* Elevation */
  --shadow-xs: 0 1px 2px rgba(16, 20, 26, 0.05);
  --shadow-sm: 0 1px 3px rgba(16, 20, 26, 0.07), 0 1px 2px rgba(16, 20, 26, 0.04);
  --shadow-md: 0 6px 16px -4px rgba(16, 20, 26, 0.10), 0 2px 6px -2px rgba(16, 20, 26, 0.06);
  --shadow-lg: 0 18px 40px -12px rgba(16, 20, 26, 0.22), 0 6px 14px -6px rgba(16, 20, 26, 0.10);
  --focus-ring: 0 0 0 3px rgba(245, 176, 0, 0.55);

  /* Sidebar is always dark — workshop identity */
  --sb-bg: #111317;
  --sb-surface: #1A1D23;
  --sb-border: #242830;
  --sb-text: #C5CBD4;
  --sb-text-strong: #F4F5F7;
  --sb-text-muted: #7B8390;
  --sb-hover: rgba(255, 255, 255, 0.05);
  --sb-active-bg: rgba(245, 176, 0, 0.13);
  --sb-active-text: #FFCF4D;

  color-scheme: light;
}

:root[dir="rtl"] { --dir: -1; }
:root:lang(ar) { --font-sans: var(--font-arabic); }

:root[data-theme="dark"] {
  --bg: #0D0F12;
  --surface-1: #15181D;
  --surface-2: #1A1E24;
  --surface-3: #222730;
  --surface-inverse: #F2F3F5;
  --border: #252A32;
  --border-strong: #353B46;
  --text: #ECEEF1;
  --text-2: #A8AFBB;
  --text-3: #949BA6;   /* ≥ 5.2:1 on every dark surface */
  --text-inverse: #15181D;

  --primary-bg: #ECEEF1;
  --primary-bg-hover: #FFFFFF;
  --primary-text: #15181D;
  --accent-soft: rgba(245, 176, 0, 0.14);
  --accent-ink: #FFCF4D;

  --success: #4ADE80;  --success-soft: rgba(74, 222, 128, 0.12);
  --warning: #FBBF24;  --warning-soft: rgba(251, 191, 36, 0.12);
  --danger: #F87171;   --danger-soft: rgba(248, 113, 113, 0.13);
  --info: #7AA2FF;     --info-soft: rgba(122, 162, 255, 0.13);
  --neutral: #A8AFBB;  --neutral-soft: rgba(168, 175, 187, 0.12);
  --violet: #B794F6;   --violet-soft: rgba(183, 148, 246, 0.13);

  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 8px 20px -6px rgba(0, 0, 0, 0.55);
  --shadow-lg: 0 20px 44px -12px rgba(0, 0, 0, 0.7);

  --sb-bg: #0A0B0E;
  --sb-surface: #14161A;
  --sb-border: #1C1F25;

  color-scheme: dark;
}

/* ---------- Reset & base ---------- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scroll-padding-top: calc(var(--topbar-h) + var(--sp-4)); }
body {
  min-height: 100dvh;
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-base);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
img, svg, video { display: block; max-inline-size: 100%; }
svg.icon { flex-shrink: 0; }
input, button, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: not-allowed; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, h4 { line-height: var(--lh-tight); font-weight: var(--fw-bold); text-wrap: balance; }
p { text-wrap: pretty; }
ul[role="list"], ol[role="list"] { list-style: none; padding: 0; }
code, kbd { font-family: var(--font-mono); font-size: 0.875em; }
code { padding: 0.1em 0.4em; border-radius: var(--radius-xs); background: var(--surface-3); color: var(--text); }
kbd {
  display: inline-flex; align-items: center; justify-content: center; min-inline-size: 1.6em;
  padding: 0.1em 0.45em; border: 1px solid var(--border-strong); border-block-end-width: 2px;
  border-radius: var(--radius-xs); background: var(--surface-1); color: var(--text-2); font-size: var(--fs-2xs); line-height: 1.5;
}
hr { border: 0; border-block-start: 1px solid var(--border); }
::selection { background: rgba(245, 176, 0, 0.35); }

:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--radius-sm); }
[hidden] { display: none !important; }

.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-3); font-size: var(--fs-sm); }
.subhead { font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text-2); margin-block: var(--sp-5) var(--sp-3); }
.subhead:first-child { margin-block-start: 0; }
.cluster { display: flex; flex-wrap: wrap; gap: var(--sp-3); align-items: center; }
.cluster + .cluster { margin-block-start: var(--sp-4); }
.cluster--center { justify-content: center; }
.stack-sm > * + * { margin-block-start: var(--sp-2); }
.stack-sm { margin-block-start: var(--sp-6); }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr)); gap: var(--sp-6); margin-block-start: var(--sp-5); }
.grid-2 > * { min-inline-size: 0; }
.visually-hidden { position: absolute !important; inline-size: 1px; block-size: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
html.modal-open { overflow: hidden; }

/* ---------- Boot splash & noscript ---------- */
.boot-splash {
  position: fixed; inset: 0; z-index: var(--z-modal);
  display: grid; place-content: center; justify-items: center; gap: var(--sp-5);
  background: var(--bg);
}
.boot-splash img { animation: pulse 1.6s var(--ease) infinite; }
.boot-splash__text { min-block-size: 1.5em; font-size: var(--fs-sm); color: var(--text-3); }
@keyframes pulse { 50% { transform: scale(0.94); opacity: 0.75; } }
.noscript { padding: var(--sp-6); text-align: center; font-weight: var(--fw-semibold); }

/* ---------- Skip link ---------- */
.skip-link {
  position: fixed; inset-block-start: var(--sp-3); inset-inline-start: var(--sp-3); z-index: var(--z-toast);
  padding: var(--sp-2) var(--sp-4); border-radius: var(--radius-md);
  background: var(--accent); color: var(--accent-text); font-weight: var(--fw-semibold);
  transform: translateY(-200%); transition: transform var(--dur) var(--ease);
}
.skip-link:focus { transform: none; }

/* ==========================================================================
   Application shell
   ========================================================================== */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-block-size: 100dvh;
  transition: grid-template-columns var(--dur-slow) var(--ease);
}
html.sidebar-collapsed .app { grid-template-columns: var(--sidebar-rail) minmax(0, 1fr); }
.app__main { display: flex; flex-direction: column; min-inline-size: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  position: sticky; inset-block-start: 0; block-size: 100dvh;
  display: flex; flex-direction: column;
  background: var(--sb-bg); color: var(--sb-text);
  border-inline-end: 1px solid var(--sb-border);
  z-index: var(--z-sidebar);
  overflow: hidden;
}
.sidebar::before { /* subtle amber hairline — brand signature */
  content: ""; position: absolute; inset-block-start: 0; inset-inline: 0; block-size: 3px;
  background: linear-gradient(90deg, transparent, var(--brand-amber) 30%, var(--brand-amber) 70%, transparent);
  opacity: 0.85;
}
.sidebar__brand {
  display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2);
  padding: var(--sp-5) var(--sp-4) var(--sp-4);
  min-block-size: var(--topbar-h);
}
.brand { display: flex; align-items: center; gap: var(--sp-3); min-inline-size: 0; border-radius: var(--radius-md); }
.brand-mark { flex-shrink: 0; object-fit: contain; filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.18)); }
/* Full logo (black lettering): always on a light plate so it stays readable in the dark theme */
.brand-logo { display: inline-flex; align-items: center; justify-content: center; background: #fff; border-radius: var(--radius-md); padding: 6px 10px; line-height: 0; }
.brand-logo img { display: block; max-inline-size: 100%; block-size: auto; }
.brand__text { display: flex; flex-direction: column; min-inline-size: 0; }
.brand__name { color: var(--sb-text-strong); font-weight: var(--fw-bold); font-size: var(--fs-md); letter-spacing: 0.01em; white-space: nowrap; }
:root:lang(en) .brand__name { letter-spacing: 0.08em; font-weight: 800; }
.brand__sub { color: var(--sb-text-muted); font-size: var(--fs-2xs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn.sidebar__close { display: none; color: var(--sb-text); }

.sidebar__nav {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: var(--sp-2) var(--sp-3) var(--sp-4);
  scrollbar-width: thin; scrollbar-color: #2A2E36 transparent;
}
.nav-group + .nav-group { margin-block-start: var(--sp-2); }
.nav-group__toggle {
  display: flex; align-items: center; justify-content: space-between; inline-size: 100%;
  padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-sm);
  color: var(--sb-text-muted); font-size: var(--fs-2xs); font-weight: var(--fw-semibold);
  text-transform: uppercase; letter-spacing: 0.08em;
}
:root:lang(ar) .nav-group__toggle { text-transform: none; letter-spacing: 0; font-size: var(--fs-xs); }
.nav-group__toggle:hover { color: var(--sb-text); }
.nav-group__chevron { transition: transform var(--dur) var(--ease); opacity: 0.7; }
.nav-group.is-collapsed .nav-group__chevron { transform: rotate(calc(90deg * var(--dir))); }
.nav-group.is-collapsed .nav-group__list { display: none; }
.nav-group__list { display: grid; gap: 2px; }

.nav-item {
  position: relative;
  display: flex; align-items: center; gap: var(--sp-3);
  min-block-size: 42px; padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius-md);
  color: var(--sb-text); font-size: var(--fs-sm); font-weight: var(--fw-medium);
  transition: background var(--dur-fast), color var(--dur-fast);
}
.nav-item:hover { background: var(--sb-hover); color: var(--sb-text-strong); }
.nav-item__icon { display: grid; place-items: center; color: var(--sb-text-muted); transition: color var(--dur-fast); }
.nav-item:hover .nav-item__icon { color: var(--sb-text-strong); }
.nav-item__label { flex: 1; min-inline-size: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-item__tag {
  font-size: 10px; font-weight: var(--fw-semibold); line-height: 1; padding: 4px 6px;
  border-radius: var(--radius-pill); color: var(--sb-text-muted); background: rgba(255, 255, 255, 0.06);
  white-space: nowrap;
}
.nav-item.is-later .nav-item__label { color: color-mix(in srgb, var(--sb-text) 78%, transparent); }
.nav-item.is-active { background: var(--sb-active-bg); color: var(--sb-active-text); }
.nav-item.is-active .nav-item__icon { color: var(--sb-active-text); }
.nav-item.is-active::before {
  content: ""; position: absolute; inset-block: 9px; inset-inline-start: calc(-1 * var(--sp-3));
  inline-size: 3px; border-start-end-radius: 3px; border-end-end-radius: 3px; background: var(--brand-amber);
}

.sidebar__footer {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4); border-block-start: 1px solid var(--sb-border);
}
.phase-chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px;
  border-radius: var(--radius-pill); background: rgba(245, 176, 0, 0.1); color: var(--sb-active-text);
  font-size: var(--fs-2xs); font-weight: var(--fw-semibold); white-space: nowrap; direction: ltr;
}
.sidebar__version { color: var(--sb-text-muted); font-size: var(--fs-2xs); direction: ltr; }
.sidebar__collapse { margin-inline-start: auto; color: var(--sb-text-muted); }
.sidebar__collapse:hover { color: var(--sb-text-strong); background: var(--sb-hover); }

/* Collapsed rail (desktop) */
html.sidebar-collapsed .brand__text,
html.sidebar-collapsed .nav-item__label,
html.sidebar-collapsed .nav-item__tag,
html.sidebar-collapsed .sidebar__version,
html.sidebar-collapsed .phase-chip span { display: none; }
html.sidebar-collapsed .sidebar__brand { justify-content: center; padding-inline: var(--sp-2); }
html.sidebar-collapsed .nav-group__toggle { justify-content: center; padding-block: var(--sp-1); pointer-events: none; }
html.sidebar-collapsed .nav-group__toggle span { display: none; }
html.sidebar-collapsed .nav-group__toggle::before { content: ""; inline-size: 20px; block-size: 1px; background: var(--sb-border); }
html.sidebar-collapsed .nav-group__chevron { display: none; }
html.sidebar-collapsed .nav-group.is-collapsed .nav-group__list { display: grid; }
html.sidebar-collapsed .nav-item { justify-content: center; padding-inline: 0; }
html.sidebar-collapsed .sidebar__footer { flex-direction: column; padding-inline: var(--sp-2); }
html.sidebar-collapsed .sidebar__collapse { margin-inline-start: 0; }
html.sidebar-collapsed .sidebar__nav { overflow: visible auto; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; inset-block-start: 0; z-index: var(--z-sticky);
  display: flex; align-items: center; gap: var(--sp-3);
  block-size: var(--topbar-h); padding-inline: var(--sp-6);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-block-end: 1px solid var(--border);
}
.icon-btn.topbar__menu { display: none; }
.topbar__actions { display: flex; align-items: center; gap: var(--sp-2); margin-inline-start: auto; }

.search-trigger {
  display: flex; align-items: center; gap: var(--sp-3);
  inline-size: min(420px, 100%); block-size: 42px; padding-inline: var(--sp-4);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--surface-1); color: var(--text-3); box-shadow: var(--shadow-xs);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.search-trigger:hover { border-color: var(--border-strong); }
.search-trigger__text { flex: 1; text-align: start; font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-trigger__kbd { direction: ltr; }

/* ---------- Preview bar ---------- */
.preview-bar {
  display: flex; align-items: center; flex-wrap: wrap; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-6);
  background: repeating-linear-gradient(-45deg, var(--accent-soft), var(--accent-soft) 12px, color-mix(in srgb, var(--accent-soft) 70%, var(--surface-1)) 12px, color-mix(in srgb, var(--accent-soft) 70%, var(--surface-1)) 24px);
  border-block-end: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  color: var(--accent-ink); font-size: var(--fs-xs); font-weight: var(--fw-medium);
}
.preview-bar__icon { display: grid; place-items: center; }
.preview-bar__text { flex: 1 1 320px; }
.preview-bar__role { display: flex; align-items: center; gap: var(--sp-2); white-space: nowrap; }

/* ---------- Content ---------- */
.content {
  flex: 1; inline-size: 100%; max-inline-size: var(--content-max); margin-inline: auto;
  padding: var(--sp-6) var(--sp-6) var(--sp-9);
}
.content:focus { box-shadow: none; }
.page-header { margin-block-end: var(--sp-6); }
.page-header__row { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--sp-4); flex-wrap: wrap; }
.page-title { font-size: var(--fs-2xl); letter-spacing: -0.015em; }
:root:lang(ar) .page-title { letter-spacing: 0; }
.page-subtitle { margin-block-start: var(--sp-1); color: var(--text-2); }
.page-header__actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.page-header__actions:empty { display: none; }

.breadcrumb { margin-block-end: var(--sp-3); }
.breadcrumb ol { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1); list-style: none; padding: 0; font-size: var(--fs-xs); color: var(--text-3); }
.breadcrumb li { display: inline-flex; align-items: center; gap: var(--sp-1); }
.breadcrumb a { display: inline-flex; align-items: center; gap: 6px; padding: 2px 4px; border-radius: var(--radius-xs); color: var(--text-2); }
.breadcrumb a:hover { color: var(--text); background: var(--surface-3); }
.breadcrumb [aria-current="page"] { color: var(--text); font-weight: var(--fw-medium); }
.breadcrumb__sep { opacity: 0.5; }

/* ---------- Mobile bottom nav & drawer scrim (shown only on small screens) ---------- */
.mobile-nav { display: none; }
.sidebar-scrim { display: none; }

/* ---------- Sections ---------- */
.section + .section, .section + .dash-grid { margin-block-start: var(--sp-7); }
.section__head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--sp-4); margin-block-end: var(--sp-4); }
.section__title { font-size: var(--fs-lg); }
.section__hint { color: var(--text-3); font-size: var(--fs-sm); margin-block-start: 2px; max-inline-size: 70ch; }

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