/* SMURPS IoT, Tier 1 brand overlay.
 *
 * Loaded by a <link> injected before </head> in ThingsBoard's index.html.
 * Imports the vendored design system tokens, then maps them onto the small
 * number of theming hooks Angular Material actually exposes.
 *
 * SCOPE, deliberately narrow. Almost every rule below sets a documented
 * Angular Material CSS custom property, or a font-family on a plain element.
 * Nothing depends on Material's internal DOM.
 *
 * TWO rules use !important, and both are argued in place where they appear:
 * the active sidebar item, and the login screen's Sign in button. Both target
 * semantic classes only (.tb-side-menu, .tb-active, .tb-login-content,
 * .mat-accent), never generated .mat-mdc-* names. Both are needed because the
 * stock values come from Angular component-scoped rules, which are injected
 * into <head> at runtime and therefore always win source order. Both fail
 * visibly rather than silently if they stop applying, and both disappear in
 * Tier 2 where the theme sets these once.
 *
 * See docs/decisions.md D-012, D-016 and D-025.
 *
 * ThingsBoard exposes almost no CSS custom properties of its own (six, all
 * map-widget specific), so this overlay cannot retheme the application
 * wholesale. That is Tier 2's job, in the compiled theme. This file exists to
 * get the brand's identity, palette anchor, and typefaces in place early.
 */

@import url("/smurps/tokens.css");

/* --------------------------------------------------------------------------
 * Typefaces
 *
 * ThingsBoard ships Roboto. The design system specifies Quicksand for
 * headings and numerics, Open Sans for body and UI. The font files are
 * served from /smurps/fonts/ by the edge.
 *
 * Material Icons are deliberately untouched: the icon font is a functional
 * dependency, not a brand choice, and replacing it would break every glyph.
 * ------------------------------------------------------------------------ */

@font-face {
  font-family: "Open Sans";
  src: url("/smurps/fonts/open-sans-latin.woff2") format("woff2");
  font-weight: 300 800;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Quicksand";
  src: url("/smurps/fonts/quicksand-latin.woff2") format("woff2");
  font-weight: 300 700;
  font-style: normal;
  font-display: swap;
}

html,
body {
  font-family: var(--font-fs-body);
}

h1, h2, h3, h4 {
  font-family: var(--font-fs-head);
  letter-spacing: var(--ls-head);
}

/* --------------------------------------------------------------------------
 * Application chrome: light surface, ink text
 *
 * The SMURPS design system is a light system (--canvas #f4f7fa,
 * --surface #ffffff, --ink #333333), so the shell follows it: white chrome,
 * ink text, a light canvas behind the content, and the accent reserved for
 * the active item and calls to action.
 *
 * All three shell surfaces are Material toolbars carrying .mat-primary:
 *   mat-toolbar.tb-primary-toolbar      the top bar
 *   mat-toolbar.tb-nav-header-toolbar   the strip holding the wordmark
 *   mat-toolbar.tb-side-menu-toolbar    the left menu
 *
 * SELECTOR. Stock declares these at (0,3,0) via
 * `.tb-default .mat-toolbar.mat-primary`. This mirrors that selector exactly,
 * which ties on specificity, and ties go to whichever comes last in source
 * order. The stock rule is in the static styles-*.css at head position 2 and
 * this overlay is injected at position 3, so the overlay wins. An earlier
 * version used `mat-toolbar.mat-primary` at (0,1,1) and lost silently. T-010.
 * ------------------------------------------------------------------------ */

.tb-default .mat-toolbar.mat-primary,
.tb-dark .mat-toolbar.mat-primary {
  --mat-toolbar-container-background-color: var(--surface);
  --mat-toolbar-container-text-color: var(--text-strong);
}

/* The menu links need no rule of their own. Angular Material derives their
 * label color from the toolbar:
 *
 *   .mat-toolbar .mat-mdc-button-base.mat-mdc-button-base.mat-unthemed {
 *     --mat-button-text-label-text-color:
 *         var(--mat-toolbar-container-text-color, ...);
 *   }
 *
 * so setting the toolbar's text color above carries to every item and icon in
 * the sidebar. This is the theming system working as designed, which is why
 * the light shell needs far less CSS than it might appear. */

/* The drawer behind the left menu, so nothing shows through while the menu
 * scrolls or during the open and close transition. */
.tb-default .mat-sidenav.tb-site-sidenav,
.tb-dark .mat-sidenav.tb-site-sidenav {
  background-color: var(--surface);
  border-right: 1px solid var(--border);
}

/* Separate the top bar from the content beneath it. */
.tb-default .mat-toolbar.tb-primary-toolbar,
.tb-dark .mat-toolbar.tb-primary-toolbar {
  border-bottom: 1px solid var(--border);
}

/* The working area sits on the design system's canvas, one step down from the
 * white chrome, so cards read as raised. */
.tb-default .mat-sidenav-container,
.tb-dark .mat-sidenav-container {
  background-color: var(--canvas);
}

/* Accent and warn toolbars keep their semantic meaning, in brand colors. */
.tb-default .mat-toolbar.mat-accent,
.tb-dark .mat-toolbar.mat-accent {
  --mat-toolbar-container-background-color: var(--accent-warm);
  --mat-toolbar-container-text-color: var(--text-on-accent);
}

.tb-default .mat-toolbar.mat-warn,
.tb-dark .mat-toolbar.mat-warn {
  --mat-toolbar-container-background-color: var(--stop);
  --mat-toolbar-container-text-color: var(--text-on-accent);
}

/* --------------------------------------------------------------------------
 * Toolbar icon buttons
 *
 * REGRESSION FIX. Stock forces every toolbar icon to white, because the stock
 * toolbar is dark:
 *
 *   .tb-default .mat-toolbar.mat-primary button.mat-mdc-icon-button .mat-icon,
 *   .tb-dark   .mat-toolbar.mat-primary button.mat-mdc-icon-button .mat-icon
 *       { color: #fff }
 *
 * On a white toolbar that makes them invisible. It hid the fullscreen button,
 * the notification bell, and the three-dot account menu, which also made the
 * account menu look unclickable: the target was there, just unseeable.
 *
 * The selector is mirrored EXACTLY, at (0,5,1), which ties on specificity and
 * wins on source order because the stock rule is in the static stylesheet.
 * Mirroring is deliberate: this override now lives and dies with the rule it
 * corrects. If Angular Material renames these classes, both rules stop
 * applying together and the icons fall back to inheriting the toolbar's text
 * color, which is already ink. It fails safe.
 * ------------------------------------------------------------------------ */

.tb-default .mat-toolbar.mat-primary button.mat-mdc-icon-button .mat-icon,
.tb-dark .mat-toolbar.mat-primary button.mat-mdc-icon-button .mat-icon {
  color: var(--text-body);
}

.tb-default .mat-toolbar.mat-primary button.mat-mdc-icon-button:hover .mat-icon,
.tb-dark .mat-toolbar.mat-primary button.mat-mdc-icon-button:hover .mat-icon {
  color: var(--accent-strong);
}

/* --------------------------------------------------------------------------
 * Hint tooltip icons
 *
 * Not a regression from this overlay: stock ThingsBoard renders the small "i"
 * help icons at #e0e0e0, which is 1.32:1 on white against a 3:1 guideline for
 * non-text UI. They are tooltip triggers, so a help affordance nobody can see
 * is not doing its job. Fixed here because we are already responsible for how
 * this product looks.
 *
 * --text-muted (#8a95a1) clears 3:1 while staying clearly secondary to the
 * label beside it. Hover uses --accent-strong (#2c7bb0) rather than --accent:
 * plain --accent is 2.85:1 on white, which would make the icon LESS visible
 * on hover than at rest. A hover state that reduces contrast is a bug.
 *
 * SELECTOR, and the repetition is deliberate, not a typo.
 *
 * Stock declares this from an Angular component-scoped rule:
 *
 *   [_nghost-ng-cNNN] .tb-hint-tooltip-icon[_ngcontent-ng-cNNN]  { color: #e0e0e0 }
 *
 * which is (0,3,0): one attribute, one class, one attribute. Component styles
 * are injected into <head> at runtime, so they win any TIE on source order.
 * Beating them therefore needs (0,4,0) or higher.
 *
 * `.tb-default .mat-icon.tb-hint-tooltip-icon.tb-hint-tooltip-icon` is four
 * classes. A first attempt used `.tb-default .tb-hint-tooltip-icon` doubled,
 * which is only three and silently tied and lost.
 *
 * Why this rather than !important: it uses only semantic, stable class names,
 * no generated .mat-mdc-* internals, and unlike !important it leaves the
 * cascade intact so anything downstream can still override it normally. The
 * two !important rules elsewhere in this file exist because no amount of
 * specificity could beat those particular selectors; that is not the case
 * here.
 * ------------------------------------------------------------------------ */

.tb-default .mat-icon.tb-hint-tooltip-icon.tb-hint-tooltip-icon,
.tb-dark .mat-icon.tb-hint-tooltip-icon.tb-hint-tooltip-icon {
  color: var(--text-muted);
}

.tb-default .mat-icon.tb-hint-tooltip-icon.tb-hint-tooltip-icon:hover,
.tb-dark .mat-icon.tb-hint-tooltip-icon.tb-hint-tooltip-icon:hover {
  color: var(--accent-strong);
  cursor: help;
}

/* --------------------------------------------------------------------------
 * The active menu item
 *
 * THIS IS THE ONE RULE IN THIS FILE THAT USES !important, and the reasoning
 * is worth reading before removing it.
 *
 * Stock highlights the active item with a white overlay:
 *
 *   [_nghost-ng-cNNNNNN] a.mat-mdc-button.mat-mdc-button-base.tb-active
 *       { background-color: #ffffff26 }
 *
 * White at 15% over a white sidebar is invisible, so on a light shell the
 * active item loses its highlight entirely. It has to be overridden.
 *
 * That rule is Angular component-scoped at specificity (0,4,1), and Angular
 * injects component styles into <head> at runtime so they always come last in
 * source order. Beating it leaves two options:
 *
 *   a) chain the generated Material class names, reaching (0,5,1):
 *        .tb-side-menu a.mat-mdc-button.mat-mdc-button-base.mat-unthemed.tb-active
 *   b) one !important on a semantic selector:
 *        .tb-side-menu a.tb-active
 *
 * (b) is chosen, and it is the MORE durable of the two. `.tb-side-menu` and
 * `.tb-active` are ThingsBoard's own classes, and `tb-active` is applied by
 * Angular's routerLinkActive, so both are stable and meaningful. The
 * `.mat-mdc-*` names in (a) are Angular Material internals that are renamed
 * between major versions, which is exactly the fragility this project set out
 * to avoid.
 *
 * If this ever stops applying, it fails visibly: the active item simply looks
 * like the others. It cannot fail silently or corrupt anything.
 *
 * The real fix is Tier 2, where the active state is set in the compiled
 * theme and needs no override at all. See docs/decisions.md D-025.
 * ------------------------------------------------------------------------ */

.tb-side-menu a.tb-active {
  background-color: var(--blue-tint) !important;
  --mat-button-text-label-text-color: var(--accent-strong) !important;
  color: var(--accent-strong) !important;
  font-weight: var(--fw-semibold);
}

/* --------------------------------------------------------------------------
 * Primary buttons
 *
 * Stock paints these ThingsBoard navy via:
 *
 *   .tb-default .mat-mdc-button.mat-primary,
 *   .tb-default .mat-mdc-unelevated-button.mat-primary, ...
 *       { --mat-button-filled-container-color: #305680 }
 *
 * That is (0,3,0) and lives in the STATIC stylesheet, not in an Angular
 * component-scoped rule. Mirroring the selector ties on specificity and wins
 * on source order, so no !important is needed. That is the opposite of the
 * accent buttons below, and the difference is worth understanding before
 * touching either. Read off the live cascade with CDP, not guessed.
 * ------------------------------------------------------------------------ */

.tb-default .mat-mdc-button.mat-primary,
.tb-default .mat-mdc-unelevated-button.mat-primary,
.tb-default .mat-mdc-raised-button.mat-primary,
.tb-dark .mat-mdc-button.mat-primary,
.tb-dark .mat-mdc-unelevated-button.mat-primary,
.tb-dark .mat-mdc-raised-button.mat-primary {
  --mat-button-filled-container-color: var(--accent);
  --mat-button-protected-container-color: var(--accent);
  --mat-button-filled-label-text-color: var(--text-on-accent);
  --mat-button-protected-label-text-color: var(--text-on-accent);
}

/* --------------------------------------------------------------------------
 * Accent buttons: NOT DONE IN TIER 1, deliberately.
 *
 * Stock's accent is #ff5722. Unlike the primary buttons above, the accent is
 * pinned by an Angular component-scoped rule at specificity (0,5,0):
 *
 *   .tb-dark[_ngcontent-X] .mat-mdc-unelevated-button.mat-accent[_ngcontent-X]
 *       { --mat-button-filled-container-color: #ff5722 }
 *
 * and component styles are injected at runtime so they always win source
 * order. Overriding needs either !important or chaining generated .mat-mdc-*
 * names.
 *
 * ONE EXCEPTION, the login screen's Sign in button. It is the primary call to
 * action on the first screen anyone sees, and stock paints it #ff5722, which
 * is not even SMURPS saffron (#eb800c). Leaving it reads as a branding miss
 * rather than a choice.
 *
 * This is the SECOND and last !important in this file. Both use semantic
 * selectors only: `.tb-login-content` and `.mat-accent` are ThingsBoard's and
 * Angular Material's own public classes, not generated internals, so they are
 * far more durable than chaining `.mat-mdc-button-base`. Both fail visibly if
 * they ever stop applying, never silently.
 *
 * Elsewhere in the application the accent is left alone. Tier 2 sets it once
 * in the palette and both of these rules disappear. D-018, D-025.
 * ------------------------------------------------------------------------ */

div.tb-login-content.tb-dark button.mat-accent {
  --mat-button-filled-container-color: var(--accent) !important;
  --mat-button-protected-container-color: var(--accent) !important;
}

/* --------------------------------------------------------------------------
 * Login card
 *
 * Light, matching the application shell. The wordmark has no hue collision on
 * white, which is the background the asset was designed for.
 *
 * The card lives inside a .tb-dark context, so without the token overrides
 * below its text and form controls stay light-on-light and become invisible.
 * Every one of these is a documented Material theming token.
 *
 * SELECTOR. Stock declares the card color on `.tb-dark[_ngcontent-X]`,
 * which is (0,2,0). The container carries both .tb-login-content and
 * .tb-dark, and adding the element name reaches (0,2,1), which wins outright
 * rather than relying on source order. Needed because Angular injects
 * component styles at runtime, after this sheet. See T-010.
 * ------------------------------------------------------------------------ */

div.tb-login-content.tb-dark {
  --mat-card-elevated-container-color: var(--surface);
  --mat-sys-on-surface: var(--text-strong);
  --mdc-outlined-text-field-input-text-color: var(--text-strong);
  --mdc-outlined-text-field-label-text-color: var(--text-body);
  /* The outline border reads --mat-form-field-outlined-*, NOT the
   * --mdc-outlined-text-field-* family. Both exist, and setting the wrong one
   * leaves the token correctly set and the border unchanged, which is exactly
   * how this was missed the first time. Confirmed against the live rule:
   *
   *   .mdc-text-field--outlined:not(.mdc-text-field--disabled) .mat-mdc-notch-piece
   *     { border-color: var(--mat-form-field-outlined-outline-color, ...) }
   *
   * --border (#e4eaf0) is a divider tone and is nearly invisible as a form
   * outline on white, so --ink-4 is used: subtle but actually visible. */
  /* The ENTIRE outlined form field family. Angular Material exposes two
   * parallel sets of names, --mdc-outlined-text-field-* and
   * --mat-form-field-outlined-*, and for these controls it is consistently
   * the --mat-form-field-outlined-* set that is wired to the rendered
   * property. Setting only the --mdc-* names leaves the tokens holding
   * exactly the right values while the field renders unchanged, which is how
   * both the invisible outline and the invisible typed text got shipped.
   * See T-014 and T-016.
   *
   * Confirmed against the live rules:
   *   .mdc-text-field--outlined:not(.mdc-text-field--disabled) .mat-mdc-notch-piece
   *       { border-color: var(--mat-form-field-outlined-outline-color, ...) }
   *   .mdc-text-field--outlined:not(.mdc-text-field--disabled) .mdc-text-field__input
   *       { color: var(--mat-form-field-outlined-input-text-color, ...) } */

  /* what the user types */
  --mat-form-field-outlined-input-text-color: var(--text-strong);
  --mat-form-field-outlined-caret-color: var(--accent);
  --mat-form-field-outlined-input-text-placeholder-color: var(--text-muted);

  /* the floating label, in each state */
  --mat-form-field-outlined-label-text-color: var(--text-body);
  --mat-form-field-outlined-hover-label-text-color: var(--text-body);
  --mat-form-field-outlined-focus-label-text-color: var(--accent);
  --mat-form-field-outlined-disabled-label-text-color: var(--text-faint);

  /* the box */
  --mat-form-field-outlined-outline-color: var(--ink-4);
  --mat-form-field-outlined-hover-outline-color: var(--accent);
  --mat-form-field-outlined-focus-outline-color: var(--accent);
  --mat-form-field-outlined-disabled-outline-color: var(--border);
  --mat-form-field-outlined-error-outline-color: var(--stop);
  --mat-form-field-outlined-error-label-text-color: var(--stop);

  /* the --mdc-* names are set too: harmless, and correct if a future
   * Material version wires these instead */
  --mdc-outlined-text-field-input-text-color: var(--text-strong);
  --mdc-outlined-text-field-caret-color: var(--accent);
  --mdc-outlined-text-field-outline-color: var(--ink-4);
  --mdc-outlined-text-field-hover-outline-color: var(--accent);
  --mdc-outlined-text-field-focus-outline-color: var(--accent);
  --mdc-outlined-text-field-caret-color: var(--accent);
  --mat-form-field-leading-icon-color: var(--text-muted);
  --mat-form-field-trailing-icon-color: var(--text-muted);
  /* Plain text buttons in the card, notably "Forgot your password?". The dark
   * context sets their label to white, which on the light card rendered them
   * at 1:1 against the background: present, focusable, and completely
   * invisible. Caught by the contrast sweep, not by looking at the page. */
  --mat-button-text-label-text-color: var(--accent-strong);
  color: var(--text-strong);
}

/* Card text. Buttons are excluded deliberately: they carry their own label
 * color from the Material theme, and catching their spans here turned the
 * Sign in button's white label dark on an orange fill. */
div.tb-login-content.tb-dark mat-card,
div.tb-login-content.tb-dark mat-card h1,
div.tb-login-content.tb-dark mat-card h2,
div.tb-login-content.tb-dark mat-card > span,
div.tb-login-content.tb-dark mat-card label:not(.mdc-button__label) {
  color: var(--text-strong);
}

/* --------------------------------------------------------------------------
 * Logo sizing
 *
 * The SMURPS wordmark is 4.07:1 where ThingsBoard's own is 4.82:1, so at a
 * fixed height it renders slightly narrower. Constraining by height and
 * letting width follow keeps it from being stretched.
 * ------------------------------------------------------------------------ */

img[src*="logo_title_white.svg"] {
  height: 36px;
  width: auto;
  object-fit: contain;
}

/* --------------------------------------------------------------------------
 * Accent anchor
 *
 * A minimal set: the focus ring and text selection, both of which read as
 * "this product's color" on nearly every screen, and both of which are plain
 * CSS rather than Material internals.
 * ------------------------------------------------------------------------ */

:root {
  accent-color: var(--accent);
  caret-color: var(--accent);
}

::selection {
  background: var(--blue-tint);
  color: var(--text-strong);
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}
