/* Phone fit: iPhone safe areas, one scroller, Ask controls, skeletons.
   @layer screens, linked after layout-review.css and before mobile-os.css.
   A short unlayered block at the bottom beats the phone-OS !important that
   clips the Delegator control. Do not add rules to mobile-os.css. */
@layer screens {

  /* Page color fills the status-bar and home-indicator bands. Real devices
     read env(); a harness may set --gl-safe-top / --gl-safe-bottom. */
  html, body {
    background: var(--bg-app);
    background-color: var(--bg-app);
  }

  @media (max-width: 820px) {
    :root {
      --accent: #2196F3;
      --accent-hover: #1E88E5;
    }

    html, body {
      max-width: 100%;
      overflow-x: clip;
      min-height: 100dvh;
      background: var(--bg-app);
      background-color: var(--bg-app);
    }

    /* App bar background runs behind the clock. No blur, veil, or fade. */
    nav.mobile-topnav {
      background: var(--bg-app);
      background-color: var(--bg-app);
      background-image: none;
      box-shadow: none;
      backdrop-filter: none;
      -webkit-backdrop-filter: none;
      height: calc(var(--gl-safe-top, env(safe-area-inset-top, 0px)) + 56px);
      min-height: calc(var(--gl-safe-top, env(safe-area-inset-top, 0px)) + 56px);
      max-height: none;
      padding-top: var(--gl-safe-top, env(safe-area-inset-top, 0px));
      transition: none;
      transform: none;
    }
    #mobile-topbar-veil,
    #perplexity-fade,
    #perplexity-fade-bottom,
    body.route-ask .ai-view::before,
    body.route-ask::before {
      content: none;
      display: none;
      background: none;
      backdrop-filter: none;
      -webkit-backdrop-filter: none;
      opacity: 0;
      pointer-events: none;
    }

    /* One document scroller on ordinary pages. Ask, Messages, and the map
       keep the frame they already own. */
    body:not(.route-ask):not(.route-messages):not(.route-hood) #main,
    body:not(.route-ask):not(.route-messages):not(.route-hood) #main-content {
      overflow: visible;
      height: auto;
      max-height: none;
      max-width: 100%;
      min-width: 0;
    }
    body:not(.route-ask):not(.route-messages):not(.route-hood):not(.is-auth-page) #main-content {
      padding-top: calc(56px + var(--gl-safe-top, env(safe-area-inset-top, 0px)) + var(--page-top, 12px));
      padding-bottom: calc(16px + var(--gl-safe-bottom, env(safe-area-inset-bottom, 0px)));
    }

    /* Composer and the fallback tab bar stay put: no fade, no slide. */
    .gl-tabbar,
    body.route-ask .ai-composer-wrap {
      transition: none;
      animation: none;
      transform: none;
    }
    html body.route-ask .ai-view .ai-composer-wrap,
    html body.route-ask .ai-composer-wrap {
      position: fixed;
      left: 0;
      right: 0;
      width: auto;
      bottom: var(--kb-offset, 0px) !important;
      padding-bottom: calc(8px + var(--gl-safe-bottom, env(safe-area-inset-bottom, 0px)));
      background: var(--bg-app);
      background-color: var(--bg-app);
      background-image: none;
      box-shadow: none;
      backdrop-filter: none;
      -webkit-backdrop-filter: none;
      opacity: 1;
      visibility: visible;
      z-index: 80;
    }
    body.kb-open.route-ask .ai-composer-wrap {
      opacity: 1;
      visibility: visible;
    }

    /* Grids shrink inside the viewport instead of growing a sideways scroller. */
    .gl-divisions-view,
    .gl-divisions-grid,
    .gl-chip-grid,
    .predictions-view,
    .pred-sections,
    .pred-grid,
    .pred-card,
    .cust-view,
    .cust-list,
    .cust-filter-row,
    .ask-examples,
    .chat-suggest-row,
    .ai-view {
      min-width: 0;
      max-width: 100%;
    }
    html body .gl-divisions-grid,
    html body .gl-divisions-grid.gl-divisions-grid--row5 {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .pred-grid,
    .predictions-view .pred-grid {
      grid-template-columns: minmax(0, 1fr);
    }
    .gl-chip-grid,
    .gl-chip-grid.gl-chip-grid--domain {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 8px;
    }

    /* Customers: the table fits the pane. No second horizontal scroller. */
    html body.route-customers .cust-list {
      overflow-x: clip;
    }
    html body.route-customers .cust-table {
      width: 100%;
      min-width: 0;
      table-layout: fixed;
    }
    html body.route-customers .cust-table :is(th, td) {
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .cust-filter-row,
    .cust-filter-chips {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      min-width: 0;
      max-width: 100%;
    }
    .cust-chip,
    .cust-sort,
    .pred-tool {
      min-height: 44px;
    }

    /* Suggestion chips: equal columns, 44px targets, 8px gap, neutral.
       The fixed composer is about two rows tall, so the empty state keeps
       that much clear or the second chip row sits under the bar. */
    body.route-ask:not(.ai-has-messages) .ai-view {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      box-sizing: border-box;
      padding-bottom: calc(176px + var(--gl-safe-bottom, env(safe-area-inset-bottom, 0px)));
    }
    body.route-ask .ask-examples {
      width: 100%;
      box-sizing: border-box;
      margin-bottom: 12px;
      padding-left: max(12px, env(safe-area-inset-left, 0px));
      padding-right: max(12px, env(safe-area-inset-right, 0px));
    }
    body.route-ask .ask-examples .ask-examples-row,
    body.route-ask .chat-suggest-row,
    body.route-ask .nx-followups {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 8px;
      width: 100%;
      max-width: 100%;
      justify-content: stretch;
      justify-items: stretch;
    }
    body.route-ask .nx-followups .nx-followups-label {
      grid-column: 1 / -1;
    }
    body.route-ask .chat-suggest-chip,
    body.route-ask .ask-example-chip,
    body.route-ask .nx-followup-chip {
      width: 100%;
      min-height: 44px;
      margin: 0;
      justify-content: center;
      text-align: center;
      white-space: normal;
      background: var(--bg-card);
      border: 1px solid var(--border);
      color: var(--text-primary);
      box-shadow: none;
    }

    /* Ask composer: six equal tracks. Model and Delegator each take half of
       the row above the input, with the 8px column gap between them. */
    body.route-ask .ai-view #ask-composer.rich-composer {
      grid-template-columns: repeat(6, minmax(0, 1fr));
      row-gap: 8px;
      column-gap: 8px;
    }
    body.route-ask .ai-view #ask-composer .rc-chip--model {
      display: inline-flex;
      align-items: center;
      justify-content: flex-start;
      grid-row: 4;
      grid-column: 1 / 4;
      position: static;
      width: auto;
      min-width: 0;
      height: 44px;
      min-height: 44px;
      max-height: 44px;
      padding: 0 12px;
      overflow: hidden;
      background: var(--bg-elev);
      color: var(--text-primary);
      border: 1px solid var(--border);
      border-radius: 8px;
      box-shadow: none;
    }
    body.route-ask .ai-view #ask-composer .rc-chip--model .rc-chip-label {
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    body.route-ask .ai-view #ask-composer .rc-mode-select {
      width: 100%;
      min-width: 0;
      height: 44px;
      min-height: 44px;
      padding: 0 10px;
      border: 1px solid var(--border);
      border-radius: 8px;
      background: var(--bg-elev);
      color: var(--text-primary);
      font-size: 14px;
      box-shadow: none;
    }
    body.route-ask .ai-view #ask-composer .rc-plus-wrap {
      grid-row: 5;
      grid-column: 1;
      justify-self: start;
    }
    body.route-ask .ai-view #ask-composer .rc-input {
      grid-row: 5;
      grid-column: 2 / 5;
      margin: 0;
    }
    body.route-ask .ai-view #ask-composer .rc-chip[data-chip="web"].rc-chip--active {
      grid-row: 5;
      grid-column: 4;
    }
    body.route-ask .ai-view #ask-composer .rc-mic {
      grid-row: 5;
      grid-column: 5;
      justify-self: center;
    }
    body.route-ask .ai-view #ask-composer :is(.rc-send, .gl-stop-btn) {
      grid-row: 5;
      grid-column: 6;
      justify-self: end;
    }
    body.route-ask .ai-view #ask-composer.rich-composer::before {
      grid-row: 5;
      grid-column: 2 / -1;
      margin-left: 0;
    }
    body.route-ask .ai-view #ask-composer :where(.rc-chip[data-chip="web"].rc-chip--active, .rc-mic, .rc-send, .gl-stop-btn) {
      width: 44px;
      height: 44px;
      min-width: 44px;
      min-height: 44px;
      max-width: 44px;
      max-height: 44px;
      margin: 0;
    }
    body.route-ask .ai-view #ask-composer :where(.rc-chip[data-chip="web"].rc-chip--active, .rc-mic, .rc-send, .gl-stop-btn)::after {
      content: none;
    }
  }

  /* ---- Skeletons shaped like the finished page (flat fill, no shimmer) ---- */
  .gl-division-card--skel {
    min-height: 120px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: none;
    pointer-events: none;
  }
  .gl-division-card--skel::before,
  .gl-division-card--skel::after {
    content: "";
    display: block;
    background: var(--bg-elev);
    border-radius: 6px;
  }
  .gl-division-card--skel::before { width: 36px; height: 36px; border-radius: 8px; }
  .gl-division-card--skel::after { width: 56%; height: 12px; }

  .cust-row--skel .pred-skel-cell {
    height: 12px;
    max-width: 100%;
    background: var(--bg-elev);
    border-radius: 6px;
    animation: none;
  }
  .cust-row--skel td:first-child .pred-skel-cell {
    width: 72%;
    height: 14px;
  }

  .pred-card[data-loading="true"] {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: none;
  }
  .pred-card[data-loading="true"] .pred-badge--loading { animation: none; }
  .pred-card[data-loading="true"] .pred-card-body {
    display: grid;
    grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
    gap: 10px 12px;
    align-items: center;
  }
  .pred-card[data-loading="true"] .pred-skel-cell {
    width: auto;
    margin: 0;
    background: var(--bg-elev);
    border-radius: 6px;
    animation: none;
  }
  .pred-card[data-loading="true"] .pred-skel-cell:nth-child(1) {
    height: 28px;
    max-width: 96px;
  }
  .pred-card[data-loading="true"] .pred-skel-cell:nth-child(2) {
    height: 16px;
    justify-self: end;
    width: 70%;
  }
  .pred-card[data-loading="true"] .pred-skel-cell:nth-child(n+3) {
    grid-column: 1 / -1;
    height: 44px;
  }

  /* ---- Account card (settings). Flat, sidebar palette, one scroller. ---- */
  .acct-card {
    margin: 16px 0 8px;
    background: var(--bg-sidebar);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: none;
  }
  .acct-card > summary {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 48px;
    padding: 0 16px;
    cursor: pointer;
    list-style: none;
    font-size: 15px;
    font-weight: 600;
  }
  .acct-card > summary::-webkit-details-marker { display: none; }
  .acct-card > summary .glm-chev { margin-left: auto; color: var(--text-faint); }
  .acct-card[open] > summary .glm-chev { transform: rotate(90deg); }
  .acct-card-list {
    list-style: none;
    margin: 0;
    padding: 0;
    border-top: 1px solid var(--border);
  }
  .acct-card-list a,
  .acct-card-list button {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 48px;
    padding: 0 16px;
    border: 0;
    border-top: 1px solid var(--border);
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    font-size: 15px;
    font-weight: 500;
    text-align: left;
    text-decoration: none;
    cursor: pointer;
    box-shadow: none;
  }
  .acct-card-list li:first-child :is(a, button) { border-top: 0; }
  .acct-card-list a:hover,
  .acct-card-list button:hover { background: var(--bg-hover); }
  .acct-card-danger,
  .acct-card-danger .ti { color: var(--st-danger); }
  .acct-card-list > li > .acct-card-logout {
    box-sizing: border-box;
    width: calc(100% - 24px);
    max-width: calc(100% - 24px);
    min-height: 44px;
    margin: 8px 12px 12px;
    padding: 0 16px;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-elev);
  }

  /* Phone menu: Account starts closed. The menu page is the only scroller. */
  .gl-menu .glm-account {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: none;
  }
  .gl-menu .glm-account > summary {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 48px;
    padding: 0 16px;
    cursor: pointer;
    list-style: none;
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    background: var(--bg-card);
  }
  .gl-menu .glm-account > summary::-webkit-details-marker { display: none; }
  .gl-menu .glm-account > summary .glm-chev { margin-left: auto; }
  .gl-menu .glm-account[open] > summary .glm-chev { transform: rotate(90deg); }
  .gl-menu .glm-account .glm-card {
    border: 0;
    border-radius: 0;
    border-top: 1px solid var(--border);
    background: var(--bg-card);
  }
}

/* legacy-n-base is the first cascade layer, so !important here beats the
   phone-OS !important (legacy-i) that pulls Delegator out of its grid cell. */
@layer legacy-n-base {
  @media (max-width: 820px) {
    html body.route-ask .ai-view .rich-composer.rc-controls-native#ask-composer .rc-mode {
      position: static !important;
      display: flex !important;
      overflow: visible !important;
      width: auto !important;
      height: auto !important;
      min-height: 44px !important;
      max-height: none !important;
      margin: 0 !important;
      clip: auto !important;
      clip-path: none !important;
    }
  }
}

/* Unlayered grid placement. position is owned by legacy-n-base above. */
@media (max-width: 820px) {
  body.route-ask .ai-view #ask-composer.rich-composer {
    display: grid !important;
    grid-template-columns: repeat(6, minmax(0, 1fr)) !important;
    column-gap: 8px !important;
    row-gap: 8px !important;
  }
  body.route-ask .ai-view #ask-composer :is(.rc-toolbar, .rc-left, .rc-right, .rc-send-actions) {
    display: contents !important;
  }
  body.route-ask .ai-view #ask-composer .rc-chip--model {
    display: inline-flex !important;
    position: static !important;
    grid-row: 4 !important;
    grid-column: 1 / 4 !important;
    width: auto !important;
    min-width: 0 !important;
    height: 44px !important;
    min-height: 44px !important;
    max-height: 44px !important;
  }
  body.route-ask .ai-view #ask-composer .rc-mode {
    position: static !important;
    display: flex !important;
    grid-row: 4 !important;
    grid-column: 4 / 7 !important;
    align-items: stretch;
    width: auto !important;
    height: auto !important;
    min-width: 0 !important;
    min-height: 44px !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    clip: auto !important;
    clip-path: none !important;
  }
  body.route-ask .ai-view #ask-composer .rc-input {
    grid-row: 5 !important;
    grid-column: 2 / 5 !important;
  }
  body.route-ask .ai-view #ask-composer .rc-plus-wrap { grid-row: 5 !important; grid-column: 1 !important; }
  body.route-ask .ai-view #ask-composer .rc-mic { grid-row: 5 !important; grid-column: 5 !important; }
  body.route-ask .ai-view #ask-composer :is(.rc-send, .gl-stop-btn) { grid-row: 5 !important; grid-column: 6 !important; }
}
