/* =============================================================================
   Precision AI — shared design tokens (the single source of truth)
   -----------------------------------------------------------------------------
   ONE authoritative token layer consumed by all four front-ends:
     - Landing   (GET /)         zero-build HTML — <link>s this file
     - Explorer  (GET /ui)       zero-build HTML — <link>s this file
     - Portal    (GET /portal)   React/Vite      — <link>s the served URL
     - AgriViz   (GET /agriviz)  React/Vite      — <link>s the served URL

   Served by FastAPI at `/ui/tokens.css` (see api/routes_web.py). Because every
   surface is served off one origin, the two React apps reference the same
   absolute URL rather than each bundling a divergent copy — no more copy-pasted
   `:root` palettes drifting apart (the core problem in the UX audit §4).

   THREE TIERS (GitHub Primer's model):
     1. PRIMITIVE  --pai-*         raw brand values + neutral ramp. Constant.
     2. SEMANTIC   --color-*       role-based, theme-switchable (light + dark).
     3. LEGACY     --ink/--surface/--c-* aliases → semantic, so each app's
                   existing CSS keeps working AND themes for free.

   THEMING: light + dark are two sets of the SEMANTIC variables. Default follows
   `prefers-color-scheme`; an explicit toggle stamps `data-theme="dark|light"`
   on <html> (persisted to localStorage key `pai-theme`, shared across surfaces).
   Data colormaps (heatmaps, 3D graph, parallel-coords) are NOT themed here —
   only the chrome switches; charts keep constant colors across themes.
   ========================================================================== */

:root {
  /* ---------------------------------------------------------------------------
     TIER 1 — PRIMITIVES (raw brand palette + scales). Theme-independent.
     Exact PAI brand hexes, so light mode is unchanged from today.
     --------------------------------------------------------------------------- */
  --pai-teal:      #006a7c;  /* primary brand teal */
  --pai-teal-2:    #008899;
  --pai-teal-deep: #00596a;  /* teal hover / pressed */
  --pai-green:     #29cca5;  /* interactive accent + focus ring */
  --pai-plant:     #02aa52;  /* success green */
  --pai-fresh:     #6df2a3;  /* light highlight / ramp high */
  --pai-earth:     #68462a;  /* warm neutral, sparing */
  --pai-field:     #e8e6b5;  /* soft "Field" khaki surface (light only) */
  --pai-navy:      #013755;  /* AgriViz sidebar / deep chrome */
  --pai-navy-mid:  #024a75;

  /* Radius scale */
  --radius:     14px;
  --radius-sm:  9px;
  --radius-lg:  18px;

  /* Spacing scale — one 4px rhythm shared across every surface. */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  /* Typography — Inter is now the one family across all four surfaces
     (AgriViz's Epilogue divergence is retired). */
  --font: 'Inter', -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, 'SFMono-Regular', 'JetBrains Mono', Menlo, Consolas, monospace;

  /* ---------------------------------------------------------------------------
     TIER 2 — SEMANTIC (LIGHT). These exactly reproduce today's light palette.
     --------------------------------------------------------------------------- */
  color-scheme: light;

  --color-bg:            #f5f8f8;  /* page background */
  --color-surface:       #ffffff;  /* cards, top bar, panels */
  --color-surface-2:     #eef3f4;  /* raised / secondary fill (chips, inputs) */
  --color-hover:         #eaf3f6;  /* subtle hover fill */

  --color-text:          #13313a;  /* body text (deep teal-tinted near-black) */
  --color-text-muted:    #4a5f66;  /* secondary text */
  --color-text-subtle:   #8a96a8;  /* tertiary text (dense data views) */

  --color-border:        #dde5e6;  /* hairline borders */
  --color-border-strong: #c0cdd8;  /* stronger dividers */

  --color-accent:        #006a7c;  /* links, accent text, active states, thin accent borders */
  --color-accent-hover:  #00596a;
  --color-accent-solid:        #006a7c;  /* filled brand-button background */
  --color-accent-solid-hover:  #00596a;
  --color-accent-subtle:       #e5f4f7;  /* pale accent tint fill */
  --color-accent-subtle-2:     #dcf0f4;  /* accent-tinted active row */
  --color-on-accent:     #ffffff;  /* text/icon on a solid accent */

  --color-danger:        #b3261e;
  --color-danger-bg:     #fdecea;
  --color-success:       #02aa52;
  --color-success-text:  #036b36;  /* readable success text on light */
  --color-success-bg:    rgba(2, 170, 82, 0.14);
  --color-warning:       #d97706;

  /* "Field" surfaces (brand khaki) used sparingly as soft panel/hover fills. */
  --color-field:         #e8e6b5;
  --color-field-soft:    #f6f5e6;

  /* Focus ring — a single shared, high-visibility ring (green) used by the
     global `:focus-visible` rule on every surface. Constant across themes. */
  --color-focus:      #29cca5;
  --focus-ring-width: 3px;
  --focus-ring:       0 0 0 3px rgba(41, 204, 165, 0.45);

  --shadow:    0 1px 2px rgba(19, 49, 58, 0.06), 0 8px 24px rgba(19, 49, 58, 0.07);
  --shadow-sm: 0 1px 2px rgba(19, 49, 58, 0.08);

  /* ---------------------------------------------------------------------------
     TIER 3 — LEGACY ALIASES. Map each app's historical variable names onto the
     semantic layer so their existing CSS is untouched yet themes automatically.
     (CSS custom properties resolve at use-time, so overriding a --color-* in a
     dark block re-resolves every alias below.)
     --------------------------------------------------------------------------- */
  /* Landing / Explorer / Portal names */
  --ink:        var(--color-text);
  --ink-soft:   var(--color-text-muted);
  --surface:    var(--color-surface);
  --surface-2:  var(--color-bg);
  --hairline:   var(--color-border);
  --danger-bg:  var(--color-danger-bg);
  --field-soft: var(--color-field-soft);
  /* Explorer/Portal used --danger for text; keep it mapping to the token. */
  --danger:     var(--color-danger);

  /* AgriViz `--c-*` names */
  --c-bg:           var(--color-bg);
  --c-surface:      var(--color-surface);
  --c-raised:       var(--color-surface-2);
  --c-hover:        var(--color-hover);
  --c-active-row:   var(--color-accent-subtle-2);
  --c-border:       var(--color-border);
  --c-border-2:     var(--color-border-strong);
  --c-t1:           var(--color-text);
  --c-t2:           var(--color-text-muted);
  --c-t3:           var(--color-text-subtle);
  --c-accent:       var(--color-accent);
  --c-accent-h:     var(--color-accent-hover);
  --c-accent-light: var(--color-accent-subtle);
  --c-success:      var(--color-success);
  --c-warn:         var(--color-warning);
  --c-danger:       var(--color-danger);
  /* AgriViz sidebar stays a deep navy in both themes (its own dark chrome). */
  --c-sidebar:      var(--pai-navy);
  --c-sidebar-h:    rgba(109, 242, 163, 0.10);
  --c-sidebar-a:    rgba(109, 242, 163, 0.17);
  --c-sidebar-t1:   rgba(255, 255, 255, 0.92);
  --c-sidebar-t2:   rgba(255, 255, 255, 0.55);
  --c-sidebar-b:    rgba(255, 255, 255, 0.07);
}

/* =============================================================================
   DARK THEME — override ONLY the semantic layer; the legacy aliases above
   re-resolve automatically. Two triggers:
     (a) system preference, unless the user explicitly chose light;
     (b) explicit `data-theme="dark"`.
   ========================================================================== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;

    --color-bg:            #0f151b;
    --color-surface:       #182028;
    --color-surface-2:     #212b34;
    --color-hover:         #24303a;

    --color-text:          #e6edf1;
    --color-text-muted:    #a5b5c0;
    --color-text-subtle:   #7f8f9c;

    --color-border:        #2b3742;
    --color-border-strong: #3a4854;

    --color-accent:        #5cc7d8;  /* lightened teal — reads on dark surfaces */
    --color-accent-hover:  #7ad6e5;
    --color-accent-solid:        #0a7d91;  /* solid button stays teal, a touch brighter */
    --color-accent-solid-hover:  #0b91a8;
    --color-accent-subtle:       #14323a;
    --color-accent-subtle-2:     #16323a;
    --color-on-accent:     #ffffff;

    --color-danger:        #ff8f87;
    --color-danger-bg:     #3a1c1a;
    --color-success:       #34d9a8;
    --color-success-text:  #57e0b0;
    --color-success-bg:    rgba(41, 204, 165, 0.16);
    --color-warning:       #f0a83c;

    --color-field:         #26302a;
    --color-field-soft:    #202a33;

    --shadow:    0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.5);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.5);
  }
}

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

  --color-bg:            #0f151b;
  --color-surface:       #182028;
  --color-surface-2:     #212b34;
  --color-hover:         #24303a;

  --color-text:          #e6edf1;
  --color-text-muted:    #a5b5c0;
  --color-text-subtle:   #7f8f9c;

  --color-border:        #2b3742;
  --color-border-strong: #3a4854;

  --color-accent:        #5cc7d8;
  --color-accent-hover:  #7ad6e5;
  --color-accent-solid:        #0a7d91;
  --color-accent-solid-hover:  #0b91a8;
  --color-accent-subtle:       #14323a;
  --color-accent-subtle-2:     #16323a;
  --color-on-accent:     #ffffff;

  --color-danger:        #ff8f87;
  --color-danger-bg:     #3a1c1a;
  --color-success:       #34d9a8;
  --color-success-text:  #57e0b0;
  --color-success-bg:    rgba(41, 204, 165, 0.16);
  --color-warning:       #f0a83c;

  --color-field:         #26302a;
  --color-field-soft:    #202a33;

  --shadow:    0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* =============================================================================
   SHARED GLOBAL FOCUS RING — audit Critical: no `:focus-visible` in Portal or
   AgriViz. One rule, every surface, for keyboard users only (mouse clicks don't
   trigger `:focus-visible`). Loaded by all four front-ends via this file.
   ========================================================================== */
:where(a, button, input, select, textarea, summary, [tabindex], [role='button'],
       [role='tab'], [role='link'], [role='menuitem']):focus-visible {
  outline: var(--focus-ring-width) solid var(--color-focus);
  outline-offset: 2px;
  border-radius: 6px;
}

/* =============================================================================
   SHARED APP SHELL — the one unified top bar across all four surfaces.
   -----------------------------------------------------------------------------
   The product-switcher header that makes the four front-ends feel like ONE
   product instead of four islands (UX audit §4/§7). Every surface renders the
   SAME markup + these SAME classes, so the bar is pixel-identical whether it's
   emitted by the zero-build Landing/Explorer HTML or the Portal/AgriViz React
   `<AppHeader>` components. Because the styling lives here (not per app), it can
   never drift apart — the exact fix pattern of the shared token layer.

   Markup contract (keep in lockstep across all four surfaces):
     <header class="pai-shell">
       <a class="pai-shell-brand" href="/" aria-label="Precision AI home">
         <img class="brand-logo" src="/ui/logo.png" alt="Precision AI" …>
       </a>
       <nav class="pai-shell-nav" aria-label="Products">
         <a href="/ui"      [aria-current="page"]>Explorer</a>
         <a href="/portal"  [aria-current="page"]>Portal</a>
         <a href="/agriviz" [aria-current="page"]>AgriViz</a>
         <a href="/docs">API docs</a>
       </nav>
       <div class="pai-shell-right"> … user affordance … + theme toggle </div>
     </header>
   ========================================================================== */
.pai-shell {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-wrap: wrap;
  min-height: 56px;
  padding: 9px clamp(16px, 4vw, 40px);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  /* Sit above app chrome (e.g. the Explorer hero) and never scroll away with a
     tall page's content region; individual apps keep their own layout below. */
  position: relative;
  z-index: 20;
}
.pai-shell-brand {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  text-decoration: none;
}
.pai-shell-brand .brand-logo { height: 28px; width: auto; display: block; }

/* The product switcher — the heart of the unification. Marked-active link uses
   the accent role color + a pale accent fill so "you are here" reads at a glance
   in both themes. */
.pai-shell-nav {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-wrap: wrap;
  /* A hairline divider separating the cross-app switcher from the brand mark. */
  padding-left: var(--space-4);
  border-left: 1px solid var(--color-border);
}
.pai-shell-nav a {
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text-muted);
  text-decoration: none;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  transition: color 0.15s ease, background 0.15s ease;
  white-space: nowrap;
}
.pai-shell-nav a:hover { color: var(--color-accent); background: var(--color-hover); }
.pai-shell-nav a[aria-current='page'] {
  color: var(--color-accent);
  background: var(--color-accent-subtle);
}

.pai-shell-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}
/* Wrapper for the signed-in-user affordance (email + Sign out), used where the
   two sit together as a toggled unit (the Explorer's static header). */
.pai-shell-userbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
}
/* Signed-in user label (folded in from the per-app top bars). */
.pai-shell-user {
  font-size: 13px;
  color: var(--color-text-muted);
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pai-shell-user strong { color: var(--color-text); font-weight: 600; }
.pai-shell-signout {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  color: var(--color-accent);
  background: transparent;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}
.pai-shell-signout:hover { border-color: var(--color-accent); background: var(--color-hover); }

/* The one theme toggle, now living in the shell's right side on every surface —
   square ghost button; sun/moon swap via the shared [data-theme] icon rules
   below. Scoped to .pai-shell so it wins over any per-app `.theme-toggle`. */
.pai-shell .theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  border: 1px solid var(--color-border-strong);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.pai-shell .theme-toggle:hover { color: var(--color-accent); border-color: var(--color-accent); }

/* Narrow widths: the switcher wraps under the brand and the right-side controls
   drop to their own line — a clean collapse, no horizontal overflow, still
   fully keyboard-reachable. */
@media (max-width: 640px) {
  .pai-shell { gap: var(--space-2) var(--space-3); }
  .pai-shell-nav {
    order: 3;
    width: 100%;
    padding-left: 0;
    border-left: 0;
  }
  .pai-shell-right { gap: var(--space-2); }
}

/* =============================================================================
   SHARED THEME-TOGGLE ICON SWAP — each surface's toggle button marks its two
   glyphs `.pai-theme-icon--sun` (shown in light) and `.pai-theme-icon--moon`
   (shown in dark). This resolves the active theme exactly like the token blocks
   above (explicit data-theme wins; otherwise the OS preference), so the glyph
   always reflects the theme currently in effect.
   ========================================================================== */
.pai-theme-icon--moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .pai-theme-icon--sun { display: none; }
  :root:not([data-theme='light']) .pai-theme-icon--moon { display: inline-flex; }
}
:root[data-theme='dark'] .pai-theme-icon--sun { display: none; }
:root[data-theme='dark'] .pai-theme-icon--moon { display: inline-flex; }
:root[data-theme='light'] .pai-theme-icon--sun { display: inline-flex; }
:root[data-theme='light'] .pai-theme-icon--moon { display: none; }

/* =============================================================================
   BRAND LOGO IN DARK MODE — the shared lockup is a dark-teal wordmark on a
   transparent ground, which would vanish on a dark top bar. Render it as a crisp
   white lockup in dark mode so it stays legible on every surface. (Every surface
   marks its logo `<img>` with `.brand-logo`.)
   ========================================================================== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .brand-logo { filter: brightness(0) invert(1); }
}
:root[data-theme='dark'] .brand-logo { filter: brightness(0) invert(1); }
:root[data-theme='light'] .brand-logo { filter: none; }
