/*
 * Team Forge MFE styling: makes the real MFEs the shell loads look like the designer's stand-ins and like the shell
 * itself, from the host side only (the MFE code is never changed).
 *
 * How it works
 *   - Every MFE mounts in the light DOM inside a <dynamic-component data-remote="...">, so the rules below are
 *     scoped to that element. The names are matched by prefix, so the local container name (`..._localhost`) and the
 *     deployed one both match.
 *   - Colors, borders and surfaces come only from the public theme tokens (--app-*, see packages/platform-ui/theme),
 *     each with a literal fallback that is the light theme's value, so the Appearance picker and the theme editor
 *     restyle the MFEs too (dark and high contrast included).
 *   - This file is unlayered, so it never defines a --app-* token (it would beat the theme layer). It uses native CSS
 *     nesting, and only unversioned Neon names (neon-* attributes, `[class*="-button-primary"]`), because the MFEs
 *     ship different Neon versions (4.4, 4.6, 4.7 and 3.1 side by side).
 *   - Sections 1 to 8 style Neon components every MFE shares. Section 9 holds the legacy overrides: rules that target
 *     one MFE's own class names. Each says why it exists and what Neon or the MFE lacked, so they can be dropped as
 *     the MFEs move to UI Core.
 *
 * Not reachable from here: anything a MFE renders into a Shadow DOM or portals outside its <dynamic-component>
 * (host CSS only gets in through inherited properties and custom properties), and Neon's fixed colors in rules that
 * do not read a variable. Those are listed in README.md, "MFE styling".
 */

/* 1. Base: font and text color for everything inside a MFE. */
:is(
  dynamic-component[data-remote^="feature_manager"],
  dynamic-component[data-remote^="application_menu"],
  dynamic-component[data-remote^="school_year_rollover"],
  dynamic-component[data-remote^="directory"],
  dynamic-component[data-remote^="messenger_inbox"],
  dynamic-component[data-remote^="calendar"],
  dynamic-component[data-remote^="post_newsfeed"],
  dynamic-component[data-remote^="psnp_mfe_all"],
  dynamic-component[data-remote^="class_hub"]
) {
  color: var(--app-text, #292832);
  font-family: var(--app-font-family, "Inter", Roboto, Arial, sans-serif);

  /* Neon sets its own font on most elements; :where() keeps this at the root's specificity. */
  & :where(*) {
    font-family: inherit;
  }

  /* 2. Page header (title and subtitle). */
  & :is([neon-page-header] h1, [class*="page-header-heading"]) {
    color: var(--app-text, #292832);
    font-weight: 600;
    letter-spacing: -0.02em;
  }

  & [class*="page-header-subheading"] {
    color: var(--app-text-muted, #5b5968);
  }

  /* Neon text components, form labels and the sortable table headers set their own dark ink. */
  & :is(label, :is(h1, h2, h3, h4, h5, h6, p, span, div)[class*="-text"]:not([class*="button"], [class*="error"], [class*="success"], [class*="warning"])),
  & :is(div, span)[class*="-text"]:not([class*="error"]) > p {
    color: var(--app-text, #292832);
  }

  & :is(.neon-input-field-label, [class*="input-field-label"]) {
    color: var(--app-text, #292832);
  }

  & :is([class*="-help-text"], [class*="-helper-text"]) {
    color: var(--app-text-muted, #5b5968);
  }

  & :is(.neon-table-header-text, .neon-table-header-main-container) {
    color: var(--app-text-muted, #5b5968);
    font-weight: 600;
  }

  /* 3. Buttons: 8px radius, weight 500, token colors. Disabled buttons mix the surface with the text color. */
  & button.neon-button {
    border-radius: 8px;
    font-weight: 500;
  }

  & button.neon-button[class*="-button-primary"] {
    background-color: var(--app-primary-bg, #00427c);
    border-color: var(--app-primary-border, #00427c);
    color: var(--app-primary-text, #ffffff);
  }

  & button.neon-button[class*="-button-primary"]:hover:not(:disabled):not([aria-disabled="true"]) {
    background-color: color-mix(in srgb, var(--app-primary-bg, #00427c) 80%, #000000);
    border-color: color-mix(in srgb, var(--app-primary-border, #00427c) 80%, #000000);
  }

  & button.neon-button[class*="-button-secondary"] {
    background-color: var(--app-surface-raised, #ffffff);
    border-color: var(--app-accent, #00427c);
    color: var(--app-accent, #00427c);
  }

  & button.neon-button[class*="-button-secondary"] svg {
    color: inherit;
    fill: currentcolor;
  }

  & button.neon-button[class*="-button-secondary"]:hover:not(:disabled):not([aria-disabled="true"]) {
    background-color: var(--app-hover, #e7edf3);
  }

  & button.neon-button[class*="-button-borderless"],
  & button.neon-button[class*="-button-utility"] {
    color: var(--app-accent, #00427c);
  }

  & button.neon-button[class*="-button-borderless"]:hover:not(:disabled):not([aria-disabled="true"]),
  & button.neon-button[class*="-button-utility"]:hover:not(:disabled):not([aria-disabled="true"]) {
    background-color: var(--app-hover, #e7edf3);
  }

  & button.neon-button:is(:disabled, [aria-disabled="true"]) {
    background-color: color-mix(in srgb, var(--app-surface-raised, #ffffff) 92%, var(--app-text, #292832));
    border-color: color-mix(in srgb, var(--app-surface-raised, #ffffff) 92%, var(--app-text, #292832));
    color: color-mix(in srgb, var(--app-text-muted, #5b5968) 90%, var(--app-surface-raised, #ffffff));
  }

  /* 4. Links. */
  & a:not([class*="button"]) {
    color: var(--app-accent, #00427c);
  }

  /* 4b. Popper menus and item pickers (the dropdowns). Neon paints the panel, the list and every row white. */
  & :is(article[class*="popper-content"], ul[class*="item-picker-list"], ul[class*="menu-list"]) {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
  }

  & button.neon-list-item {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
  }

  & button.neon-list-item:hover:not(:disabled) {
    background-color: var(--app-hover, #e7edf3);
  }

  & button.neon-list-item:is([aria-selected="true"], [aria-checked="true"], [class*="list-item-selected"], [class*="is-selected"]) {
    background-color: var(--app-selected, #dde8fa);
  }

  & :is(.neon-list-item-contents, [class*="list-item-text"]) {
    color: var(--app-text, #292832);
  }

  /* 5. Tabs: muted until hovered or selected, the selected tab and its underline in the accent. */
  & .neon-tabs-tab {
    color: var(--app-text-muted, #5b5968);
    font-size: 0.9375rem;
  }

  & .neon-tabs-tab:hover:not(:disabled) {
    color: var(--app-text, #292832);
  }

  & .neon-tabs-tab.neon-tabs-selected {
    color: var(--app-accent, #00427c);
    font-weight: 600;
  }

  & .neon-tabs-tab.neon-tabs-selected::after {
    background-color: var(--app-accent, #00427c);
  }

  /* 6. Cards: a 1px border and no shadow, on the raised surface. */
  & [neon-card-standard] {
    background-color: var(--app-surface-raised, #ffffff);
    border: 1px solid var(--app-border, #e2e8f0);
    border-radius: 10px;
    box-shadow: none;
    color: var(--app-text, #292832);
  }

  & [class*="card-standard-heading"] {
    color: var(--app-text, #292832);
    font-weight: 600;
  }

  /* 7. Tables: a bordered raised card, muted bold header cells on the canvas color, token borders. */
  & [class*="table-responsive-container-wrapper"] {
    background-color: var(--app-surface-raised, #ffffff);
    border: 1px solid var(--app-border, #e2e8f0);
    border-radius: 10px;
  }

  & table {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
  }

  & tr {
    background-color: var(--app-surface-raised, #ffffff);
  }

  & table th {
    background-color: var(--app-surface, #f8fafc);
    border-bottom: 1px solid var(--app-border, #e2e8f0);
    color: var(--app-text-muted, #5b5968);
    font-weight: 600;
  }

  & thead tr:first-child th:first-child {
    border-start-start-radius: 9px;
  }

  & thead tr:first-child th:last-child {
    border-start-end-radius: 9px;
  }

  & :is(th, td) {
    border-color: var(--app-border, #e2e8f0);
  }

  & tbody tr:hover td {
    background-color: var(--app-hover, #e7edf3);
  }

  /* 8. Inputs: 8px radius, token surface and borders. */
  & :is(input:not([type="checkbox"], [type="radio"]), select, textarea) {
    background-color: var(--app-input-bg, #ffffff);
    color: var(--app-text, #292832);
  }

  & :is(.neon-search-box, .neon-select-field-button) {
    background-color: var(--app-input-bg, #ffffff);
    border-color: var(--app-control-border, #cbd5e1);
    border-radius: 8px;
    color: var(--app-text, #292832);
  }

  & :is(.neon-search-box, .neon-select-field-button):hover:not(:disabled) {
    border-color: var(--app-accent, #00427c);
  }

  & .neon-search-box label {
    background-color: transparent;
  }

  /* Neon icons are inline SVG with their own fixed fill: dark by default, brand blue for selected controls. */
  & [neon-icon]:not([data-color]) svg {
    fill: var(--app-text, #292832);
  }

  & [neon-icon][data-color="navy"] svg {
    fill: var(--app-accent, #00427c);
  }

  & button [neon-icon] svg {
    fill: currentcolor;
  }
}

/*
 * 9. Legacy overrides, one section per MFE. They target the MFE's own class names (or a third-party library's custom
 * properties), which are not a public contract, so each can break on an MFE update. The plan is to delete a section
 * when its MFE moves to UI Core, which reads the theme tokens itself.
 */

/*
 * Legacy override: messaging inbox and settings (messenger_inbox).
 * Why: the inbox is Sendbird UI. Its colors are Sendbird's --sendbird-light-* custom properties, which the MFE's Neon
 * bridge fills with Neon's fixed blue, grey and white, so the bubbles, selection and surfaces ignore the theme.
 * Option missing: a Neon or MFE setting that takes the theme tokens. Sendbird's properties are its documented
 * theming hook, so they are remapped here instead of overriding its class names. The badge and the conversation
 * panes also paint fixed colors that the remap does not reach.
 */
dynamic-component[data-remote^="messenger_inbox"] {
  --sendbird-light-primary-500: color-mix(in srgb, var(--app-accent, #00427c) 70%, var(--app-text, #292832));
  --sendbird-light-primary-400: color-mix(in srgb, var(--app-accent, #00427c) 85%, var(--app-text, #292832));
  --sendbird-light-primary-300: var(--app-accent, #00427c);
  --sendbird-light-primary-200: color-mix(in srgb, var(--app-accent, #00427c) 40%, var(--app-surface-raised, #ffffff));
  --sendbird-light-primary-100: var(--app-selected, #dde8fa);
  --sendbird-light-background-50: var(--app-surface-raised, #ffffff);
  --sendbird-light-background-100: var(--app-hover, #e7edf3);
  --sendbird-light-background-200: var(--app-border, #e2e8f0);
  --sendbird-light-onlight-01: var(--app-text, #292832);
  --sendbird-light-onlight-02: var(--app-text-muted, #5b5968);
  --sendbird-light-onlight-03: var(--app-text-muted, #5b5968);
  --sendbird-light-onlight-04: var(--app-border, #e2e8f0);
  --sendbird-light-ondark-01: var(--app-primary-text, #ffffff);

  & .messenger-inbox :is(.sendbird-channel-list, .sendbird-conversation) {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
  }

  & .messenger-inbox .sendbird-channel-list {
    border-inline-end-color: var(--app-border, #e2e8f0);
  }

  & .messenger-channel-preview__badge {
    background-color: var(--app-primary-bg, #00427c);
    color: var(--app-primary-text, #ffffff);
  }

  & .messenger-channel-preview--active {
    background-color: var(--app-selected, #dde8fa);
  }

  & :is([neon-accordion], .messenger-inbox__action-menu, .messenger-inbox__custom-message-input__textarea) {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
  }

  & :is([class*="accordion-heading"], [class*="accordion-subheading"], .messenger-inbox__messenger-channel-preview__button) {
    color: var(--app-text, #292832);
  }

  & :is(
    .messenger-channel-preview__description,
    .messenger-channel-preview__last-message-at,
    .messenger-channel-preview__last-message
  ) {
    color: var(--app-text-muted, #5b5968);
  }

  & .messenger-channel-preview__badge__text {
    color: var(--app-primary-text, #ffffff);
  }

  & .messenger-inbox__message-content__middle__body--incoming {
    background-color: var(--app-hover, #e7edf3);
    color: var(--app-text, #292832);
  }
}

/*
 * Legacy override: calendar and manage calendars (calendar_mfe).
 * Why: the calendar paints its own greys (#333, #677274) and its own link blue on its month grid, upcoming events and
 * the manage table, so they stay dark or bright blue on the dark and high contrast themes.
 * Option missing: the MFE reads no theme token; Neon 4.6 has none for text or link colors.
 */
dynamic-component[data-remote^="calendar"] {
  & :is(
    [class*="calendar-month-view-month-and-year-label-container"],
    [class*="calendar-upcoming-events-month-text"],
    [class*="calendar-upcoming-event-title"]
  ) {
    color: var(--app-text, #292832);
  }

  & :is(
    [class*="timezone-header-section"],
    [class*="calendar-scheduler__header__date"],
    [class*="footer-tertiary-main-section"],
    [class*="calendar-upcoming-event-date-and-time"]
  ) {
    color: var(--app-text-muted, #5b5968);
  }

  /* The event chips and the "+ n" overflow buttons on the month grid are plain buttons with a white fill. */
  & button:is([class*="schedule-block"], [class*="schedule-card"], [class*="count-btn"]) {
    background-color: var(--app-surface-raised, #ffffff);
    border-color: var(--app-border, #e2e8f0);
    color: var(--app-text, #292832);
  }

  /* The day view's event blocks are buttons with a white fill and their own grey and near-black text. */
  & button:is(.mfe-calendar-mfe-schedule, [class*="mfe-calendar-mfe-schedule_"]) {
    background-color: var(--app-surface-raised, #ffffff);
    color: var(--app-text, #292832);
    outline: 1px solid var(--app-border, #e2e8f0);
    outline-offset: -1px;
  }

  & [class*="pt-calendar-view-card-content-title"] {
    color: var(--app-text, #292832);
  }

  & :is([class*="schedule__content"], [class*="card-content-details"], [class*="day-view-card-content-section"]) {
    color: var(--app-text-muted, #5b5968);
  }

  & [class*="calendar-day-body-month-header"] {
    color: var(--app-text, #292832);
  }

  & [class*="calendar-month-schedule-title"] {
    color: var(--app-text, #292832);
  }

  & :is([class*="user-details-popper-trigger"], neon-4_6_0-ellipsis) {
    color: var(--app-accent, #00427c);
  }
}

/*
 * Legacy override: term extensions (school_year_rollover).
 * Why: the empty-extension dash and the divider under the header use Neon's fixed grey.
 * Option missing: the MFE exposes no class modifier or token for them.
 */
dynamic-component[data-remote^="school_year_rollover"] {
  & [class*="term-extensions-divider"] {
    border-color: var(--app-border, #e2e8f0);
  }

  & [class*="extension-empty"] {
    color: var(--app-text-muted, #5b5968);
  }
}

/*
 * Legacy override: attendance and identity (psnp_mfe_all).
 * Why: the table header cells take Neon's fixed dark grey from a two-class selector in the MFE's own CSS, which
 * the shared table rule above cannot out-rank.
 * Option missing: the MFE and Neon 3.1 expose no token for header text.
 */
dynamic-component[data-remote^="psnp_mfe_all"] {
  & [class*="data-table"] [class*="table-header"] th {
    color: var(--app-text-muted, #5b5968);
  }

  /* The select fields' labels ("Status", "School") are <p>s that Neon 3.1 colors #333 with a selector the shared
     label rule cannot out-rank. */
  & p.neon-input-field-label.neon-input-field-label {
    color: var(--app-text, #292832);
  }
}

/*
 * Legacy override: application menu drawer (application_menu).
 * Why: the app groups are plain white boxes with a Neon shadow, and the group headings use Neon's fixed dark ink.
 * Option missing: the MFE builds these from its own divs, not from a Neon card, so the card rule above misses them.
 */
dynamic-component[data-remote^="application_menu"] {
  & [class*="app-menu-app-container"] {
    background-color: var(--app-surface-raised, #ffffff);
    border: 1px solid var(--app-border, #e2e8f0);
    border-radius: 10px;
    box-shadow: none;
  }

  & [class*="app-menu-app-header"] {
    color: var(--app-text, #292832);
    font-weight: 600;
  }
}

/*
 * Legacy override: directory (directory_local).
 * Why: the filter label, the divider in the filter card and the error text use fixed dark and grey ink.
 * Option missing: Neon 4.4 has no token for them.
 */
dynamic-component[data-remote^="directory"] {
  & [class*="home-filter-label"] {
    color: var(--app-text, #292832);
    font-size: 0.8125rem;
    font-weight: 600;
  }

  & [class*="error-card-content-text"] {
    color: var(--app-text, #292832);
  }

  & hr {
    border-color: var(--app-border, #e2e8f0);
  }
}

/*
 * 10. Home dashboard. The dashboard MFE lays its tiles out edge to edge and leaves the page padding to the host, so the
 * shell supplies it here (the same padding the Neon page layout uses around other pages).
 */
dynamic-component[data-remote^="appshell_dashboard"] {
  display: block;
  box-sizing: border-box;
  padding: 1.5rem 2rem 3rem;
}

@media (max-width: 768px) {
  dynamic-component[data-remote^="appshell_dashboard"] {
    padding: 1rem 1rem 5rem;
  }
}

/*
 * Equal-height tiles. The dashboard's grid cell stretches to the tallest tile in its row, but each tile is a card inside
 * a few plain wrappers (div and dynamic-component) that stay at their content height, so the shorter card stops short
 * of its neighbour. Neon's card has data-full-height for this, but it is set by the MFE inside the tile. From the host,
 * the wrappers between the cell and the card become a column that fills the cell and the card takes the leftover height.
 * Wrappers inside a card are left alone.
 */
dynamic-component[data-remote^="appshell_dashboard"] [class*="grid-container"] > div {
  display: flex;
  flex-direction: column;

  & :is(div, dynamic-component):has(.neon-card-standard-header):not(:has(> .neon-card-standard-header) *) {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-block-size: 0;
  }

  & :has(> .neon-card-standard-header):not(:has(> .neon-card-standard-header) *) {
    flex: 1 1 auto;
  }
}

/*
 * Legacy override: newsfeed (post_newsfeed), including its Home tile.
 * Why: the newsfeed (Neon 4.4) paints its cards white and its text in fixed greys and link blue, so on the dark and high
 * contrast themes the cards stay white and the text unreadable.
 * Option missing: the MFE reads no theme token and Neon 4.4's card has none for its fill.
 */
dynamic-component[data-remote^="post_newsfeed"] {
  & :is([class*="power-tile"], [class*="powertile"], [class*="post-container"], [class*="Pt-container"]):not(button, a, span, b) {
    background-color: var(--app-surface-raised, #ffffff);
    border-color: var(--app-border, #e2e8f0);
  }

  & :is([class*="card-standard-heading"], .post-title, .powertile_card_text, .powertile_card_text *) {
    color: var(--app-text, #292832);
  }

  & :is(.post-newsfeed-group-information, .post-newsfeed-subheading, .post-newsfeed-author-name, .rightside__date, .post-newsfeed-count-number, span:not([class])) {
    color: var(--app-text-muted, #5b5968);
  }

  & :is(.neon-accordion-heading, .neon-accordion-subheading, p.neon-input-field-label) {
    color: var(--app-text, #292832);
  }

  & :is(a.borderless-link.borderless-link, [class*="button-text"]) {
    color: var(--app-accent, #00427c);
  }

  /* Neon's borderless link sets its blue with a rule this file cannot out-rank by specificity, so it needs !important. */
  & a.borderless-link {
    color: var(--app-accent, #00427c) !important;
  }

  /* The avatar's initials sit in a bare span on a colored circle; they keep the avatar's own white. */
  & [class*="avatar-content"] span:not([class]) {
    color: inherit;
  }
}

/*
 * Select focus ring: feature management and the calendar.
 * Why: the designer's version of these two MFEs outlines a focused or hovered select in the accent color with a soft
 * 2px ring.
 * Option missing: Neon's select draws its border and focus ring in fixed grey and blue.
 */
dynamic-component:is([data-remote^="feature_manager"], [data-remote^="calendar"]) {
  & .neon-select-field-button:hover:not(:disabled):not([aria-disabled="true"]) {
    border-color: var(--app-accent, #00427c);
  }

  & .neon-select-field-button:focus-visible {
    border-color: var(--app-accent, #00427c);
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--app-accent, #00427c) 20%, transparent);
  }
}

/*
 * Legacy override: the calendar's Home tile (calendar_mfe, ./pt-calendar-mfe).
 * Why: the tile paints its month label, day highlight and hour labels in fixed colors and the day grid on a white fill.
 * Option missing: the MFE reads no theme token.
 */
dynamic-component[data-remote^="calendar"] {
  & [class*="powertile-calendar-month-and-year-label-container"] {
    color: var(--app-text, #292832);
  }

  & [class*="powertile-day-highlight-blue"] {
    color: var(--app-accent, #00427c);
  }

  & [class*="calendar-hour-row-time-slot"] {
    color: var(--app-text-muted, #5b5968);
  }

  & [class*="calendar-hour-row-event-container"] {
    background-color: var(--app-surface-raised, #ffffff);
  }

  & [class*="calendar-hour-row"] {
    border-color: var(--app-border, #e2e8f0);
  }
}
