/*
 * ETC HQ — phone layout.
 *
 * Loaded only below 1024px (the <link> carries the media query), so the
 * desktop layout never reads a rule from this file. Its own cascade layer
 * (phone) comes after every layer of app.css, hq.css and
 * midnight-pearl.css, so a rule here wins without !important.
 *
 *  1. Page: no sideways drift, steady scrolling
 *  2. Tab bar: smaller, lower, five tabs plus "More"
 *  3. "More" sheet: scrolls on its own, the page underneath stays still
 *  4. Popovers and menus stay on the screen
 *  5. Pages (phones, below 640px)
 *  6. Motion: the light always behind the content; cards solid, their content arrives
 *  7. TESLA's chat on the whole screen while typing
 */

@layer phone {
  /* ================================================================ */
  /* 1. Page                                                           */
  /* ================================================================ */
  /* Nothing may widen the page: a hidden menu or an info panel hanging
     past the edge let the whole page slide sideways under a finger.
     `clip` (unlike `hidden`) makes no scroll container, so the sticky
     top bar keeps working; inner scrollers (tabs, karnet) still scroll. */
  html, body { overflow-x: clip; }
  body { overscroll-behavior-x: none; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  .hq-app, .hq-main, .hq-content { max-width: 100%; overflow-x: clip; }
  /* Horizontal scrollers keep their swipe to themselves. */
  :is(.hq-segmented, .hq-tc-tabs__list, .hq-fin-tabs, .hq-karnet__scroll, .hq-sys-scroll, .hq-table-wrap) { overscroll-behavior-x: contain; }

  /* ================================================================ */
  /* 2. Tab bar                                                        */
  /* ================================================================ */
  :root { --tabbar: 56px; --tabbar-bottom: max(6px, calc(env(safe-area-inset-bottom) - 14px)); }
  .hq-content { padding-bottom: calc(var(--tabbar) + var(--tabbar-bottom) + 28px); }

  .hq-tabbar {
    left: 8px;
    right: 8px;
    bottom: var(--tabbar-bottom);
    /* As many columns as there are tabs, plus "More". */
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    height: var(--tabbar);
    padding: 0 4px;
    border-radius: 20px;
  }
  .hq-tabbar__item {
    gap: 2px;
    min-width: 0;
    font-size: 10px;
    font-weight: 650;
    letter-spacing: 0;
  }
  .hq-tabbar__item > span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .hq-tabbar__item .icon { width: 20px; height: 20px; }
  .hq-tabbar__item .hq-portrait { width: 22px; height: 22px; }
  .hq-tabbar__item[aria-current="page"]::before { top: 2px; width: 16px; margin-left: -8px; }
  .hq-tabbar__item[aria-current="page"]::after { inset: 5px 2px; border-radius: 14px; }
  .hq-tabbar__more > summary { height: var(--tabbar); }

  /* ================================================================ */
  /* 3. "More" sheet                                                   */
  /* ================================================================ */
  /* While the sheet is open the page does not move: only the sheet
     scrolls, and reaching its end does not pass the scroll on. */
  html:has(.hq-tabbar__more[open]),
  html:has(.hq-tabbar__more[open]) body { overflow: hidden; }
  .hq-tabbar__more[open] > summary::before { touch-action: none; }
  .hq-sheet {
    left: 8px;
    right: 8px;
    bottom: calc(var(--tabbar) + var(--tabbar-bottom) + 10px);
    max-height: min(68dvh, 600px);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }

  /* ================================================================ */
  /* 4. Popovers and menus                                             */
  /* ================================================================ */
  /* Never wider than the screen; hq.js moves an open one back inside
     the edges when its button sits near one (data-phone-shift). */
  .hq-info__panel, .hq-info__panel--wide { max-width: calc(100vw - 24px); }
  .hq-menu { max-width: calc(100vw - 24px); }
  .hq-info__panel[data-phone-shift], .hq-menu[data-phone-shift] { translate: var(--phone-shift, 0) 0; }

  /* ================================================================ */
  /* 5. Pages (phones)                                                 */
  /* ================================================================ */
  @media (max-width: 639px) {
    /* Links in a card's meta line wrap instead of running off the card. */
    .hq-card__meta { white-space: normal; overflow-wrap: anywhere; }
    .hq-card__meta .hq-card__link { display: inline; white-space: normal; }

    /* Segmented filters stay on one swipeable line. */
    .hq-sites-filter .hq-segmented { flex-wrap: nowrap; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
    .hq-sites-filter .hq-segmented::-webkit-scrollbar { display: none; }
    .hq-sites-filter .hq-segmented__item { flex: none; white-space: nowrap; }

    /* ---- Charts: a ring's total fits inside the ring ---- */
    .hq-donut { width: 176px; }
    .hq-donut__value { font-size: clamp(13px, 4.2vw, 17px); letter-spacing: -0.02em; }

    /* ---- Projects (PROJEKTI): each card as tall as its content ---- */
    .hq-sitecards { grid-auto-rows: auto; }
    .hq-sitecard { gap: 12px; }
    .hq-sitecard__invoices, .hq-sitecard__note { min-height: 0; }
    .hq-sitecard__note:empty { display: none; }
    /* The whole name on a phone, on two lines when it needs them. */
    .hq-sitecard__name { white-space: normal; align-items: baseline; gap: 8px; }
    .hq-sitecard__link { overflow: visible; text-overflow: clip; overflow-wrap: anywhere; }

    /* ---- Home: construction projects ---- */
    .hq-ptable { --pcols: repeat(3, minmax(0, 1fr)); }
    .hq-ptable--no-revenue { --pcols: repeat(2, minmax(0, 1fr)); }
    .hq-ptable__head { display: none; }
    .hq-ptable__group { padding: 10px 2px; }
    .hq-ptable__project { gap: 6px 10px; }
    .hq-ptable__flags { width: 100%; }
    .hq-ptable__row { gap: 2px 10px; padding: 8px 0 6px; }
    .hq-ptable__type { grid-column: 1 / -1; margin-bottom: 2px; white-space: normal; }
    .hq-ptable__row .is-num { display: grid; gap: 1px; text-align: left; font-size: 14px; }
    .hq-ptable__row .is-num::before { content: attr(data-label); font-family: var(--font-sans); font-size: 10.5px; font-weight: 650; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-subtle); }

    /* ---- Employees: three facts per line ---- */
    .hq-emp-table tbody tr { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px 10px; padding: 14px 0; }
    .hq-emp-table tbody td { min-width: 0; overflow-wrap: anywhere; }

    /* ---- Reports: every report its own card ---- */
    .hq-card:has(> .hq-table-wrap > .hq-rep-table) { padding: 10px 10px 4px; }
    .hq-table-wrap:has(> .hq-rep-table) { margin: 0; }
    .hq-rep-table, .hq-rep-table tbody { display: block; }
    .hq-rep-table tbody { display: grid; gap: 10px; padding: 4px 0 10px; }
    .hq-rep-table tbody tr,
    .hq-rep-table tbody tr:first-child {
      grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
      gap: 10px 14px;
      padding: 14px 14px 14px 16px;
      border: 0;
      border-radius: 16px;
      background: rgb(var(--fg-rgb) / 0.045);
      box-shadow: inset 0 0 0 1px var(--line), inset 3px 0 0 rgb(var(--blue-rgb) / 0.55);
    }
    .hq-rep-table tbody tr:hover { background: rgb(var(--fg-rgb) / 0.06); }
    .hq-rep-table tbody :is(th, td),
    .hq-rep-table tbody :is(th, td):first-child,
    .hq-rep-table tbody :is(th, td):last-child { padding: 0; }
    .hq-rep-table tbody th { padding-bottom: 10px; border-bottom: 1px solid var(--line-soft); }
    .hq-rep-table tbody td { gap: 4px; align-content: start; min-width: 0; }
    .hq-rep-table .hq-table__name { font-size: 15px; line-height: 1.35; }
    .hq-rep-table .hq-table__sub { margin-top: 3px; }
    .hq-rep-table :is(.hq-rep-type, .hq-rep-state) { width: fit-content; }
    .hq-rep-table .hq-rep-open { display: none; }
    .hq-rep-table.is-selecting tbody tr { grid-template-columns: auto minmax(0, 1fr) minmax(0, 1fr); }
    .hq-rep-table.is-selecting .hq-rep-table__pick { grid-row: 1; grid-column: 1; align-self: start; width: auto; }
    .hq-rep-table.is-selecting tbody th { grid-column: 2 / -1; }
    .hq-rep-table.is-selecting tbody td:not(.hq-rep-table__pick) { grid-column: span 1; }
    .hq-rep-table.is-selecting tbody td:nth-of-type(2), .hq-rep-table.is-selecting tbody td:nth-of-type(4) { grid-column: 2; }
    .hq-rep-table.is-selecting tbody tr:has(input:checked) { box-shadow: inset 0 0 0 1px rgb(var(--orange-rgb) / 0.55), inset 3px 0 0 var(--orange); }

    /* ---- Market intelligence: opportunities and projects as cards ---- */
    /* The cards stand on the page: no second frame around them. */
    .hq-mi-table-card { padding: 0; background: none; box-shadow: none; -webkit-backdrop-filter: none; backdrop-filter: none; }
    .hq-mi-table-card::before, .hq-mi-table-card::after { content: none; }
    .hq-mi-table-card .pagination { padding: 6px 4px 4px; border-top: 0; }
    .hq-mi-table, .hq-mi-table tbody { display: block; min-width: 0; table-layout: auto; }
    .hq-mi-table tbody { display: grid; gap: 10px; padding: 2px 0 10px; }
    .hq-mi-table tbody tr,
    .hq-mi-table tbody tr:first-child {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 0 8px;
      padding: 14px 14px 12px;
      border: 0;
      border-radius: 16px;
      background: rgb(var(--fg-rgb) / 0.045);
      box-shadow: inset 0 0 0 1px var(--line);
    }
    .hq-mi-table tbody :is(th, td),
    .hq-mi-table tbody :is(th, td):first-child,
    .hq-mi-table tbody :is(th, td):last-child { display: block; padding: 0; border: 0; min-width: 0; }
    .hq-mi-table tbody td::before { content: none; }
    .hq-mi-table .hq-mi-sub { max-width: 100%; }
    /* Line breaks between the card's rows. */
    .hq-mi-table tbody tr::before,
    .hq-mi-table tbody tr::after { content: ""; flex: 0 0 100%; height: 0; }

    /* Opportunities: name and priority / company · place / chips. */
    .hq-mi-table--opps tbody th { order: 1; flex: 1 1 0; }
    .hq-mi-table--opps tbody td:nth-child(1) { order: 2; align-self: start; }
    .hq-mi-table--opps tbody tr::before { order: 3; margin-top: 8px; }
    .hq-mi-table--opps tbody td:nth-child(3) { order: 4; font-size: 13px; }
    .hq-mi-table--opps tbody td:nth-child(4) { order: 5; flex: 1 1 auto; font-size: 13px; color: var(--text-subtle); }
    .hq-mi-table--opps tbody td:nth-child(4)::before { content: "· "; }
    .hq-mi-table--opps tbody td:nth-child(4) :is(.hq-mi-place, .hq-mi-sub) { display: inline; margin: 0; white-space: normal; }
    .hq-mi-table--opps tbody td:nth-child(4) .hq-mi-sub::before { content: " · "; }
    .hq-mi-table--opps tbody tr::after { order: 6; margin-top: 10px; }
    .hq-mi-table--opps tbody td:nth-child(5) { order: 7; }
    .hq-mi-table--opps tbody td:nth-child(6) { order: 8; }
    .hq-mi-table--opps tbody td:nth-child(7) { order: 9; margin-left: auto; }
    /* The discovery date is on the opportunity's page; the card keeps one line of chips. */
    .hq-mi-table--opps tbody td:nth-child(7) .hq-mi-date { display: none; }
    .hq-mi-table--opps tbody td:nth-child(5) .hq-mi-contact { height: 26px; }
    .hq-mi-table--opps .hq-mi-name { font-size: 15px; }
    .hq-mi-table--opps .hq-mi-act { cursor: default; }

    /* Projects: name / place · type · phase / status, companies, claims. */
    .hq-mi-table--projects tbody th { order: 1; flex: 1 1 100%; }
    .hq-mi-table--projects tbody tr::before { order: 2; margin-top: 8px; }
    .hq-mi-table--projects tbody td:nth-child(2),
    .hq-mi-table--projects tbody td:nth-child(3),
    .hq-mi-table--projects tbody td:nth-child(4) { order: 3; font-size: 13px; color: var(--text-muted); }
    .hq-mi-table--projects tbody td:nth-child(3)::before,
    .hq-mi-table--projects tbody td:nth-child(4)::before { content: "· "; color: var(--text-subtle); }
    .hq-mi-table--projects tbody tr::after { order: 4; margin-top: 10px; }
    .hq-mi-table--projects tbody td:nth-child(5) { order: 5; }
    .hq-mi-table--projects tbody td.num { order: 6; font-size: 12.5px; font-weight: 700; color: var(--text); }
    .hq-mi-table--projects tbody td.num:nth-child(6) { margin-left: auto; }
    .hq-mi-table--projects tbody td.num::before { content: attr(data-label) " "; font-weight: 600; color: var(--text-subtle); }
  }

  /* ================================================================ */
  /* 6. Motion                                                         */
  /* ================================================================ */
  /* The content is a layer of its own above the light stream, so the
     stream can only ever be behind it. (Safari composites the WebGL canvas
     by itself; during page changes it was drawn over the tables.) */
  .hq-app { z-index: 1; isolation: isolate; }

  /* A card's surface never turns see-through: when a whole card faded in,
     the light behind it shone through for a second. Here the surfaces
     stand from the first frame and their content arrives, rising and
     fading in one part after another; parts reached by scrolling arrive
     the same way, a little quicker. The fill is "backwards" only, so
     nothing keeps a layer of its own once it is in place (menus inside a
     card still open over the parts below them). */
  @media (prefers-reduced-motion: no-preference) {
    .hq-motion [data-enter],
    .hq-motion .hq-content :is(.kpis, .panel) { opacity: 1; animation: none; }
    .hq-motion [data-enter]:not(.is-revealed) > *,
    .hq-motion .hq-content :is(.kpis, .panel):not(.is-revealed) > *,
    .hq-motion .hq-content .page-header:not(.is-revealed) { opacity: 0; }
    .hq-motion [data-enter].is-revealed > *,
    .hq-motion .hq-content :is(.kpis, .panel).is-revealed > *,
    .hq-motion .hq-content .page-header.is-revealed {
      animation: hq-phone-in 680ms var(--ease-out) backwards;
      animation-delay: calc(60ms + var(--i, 0) * 70ms);
    }
    /* The second and further parts of a card follow the first. */
    .hq-motion :is([data-enter], .hq-content .panel).is-revealed > :nth-child(2) { animation-delay: calc(120ms + var(--i, 0) * 70ms); }
    .hq-motion :is([data-enter], .hq-content .panel).is-revealed > :nth-child(n + 3) { animation-delay: calc(180ms + var(--i, 0) * 70ms); }
    /* Reached by scrolling: the same entrance, a little quicker. */
    .hq-motion [data-enter].is-revealed.is-scrolled-in > *,
    .hq-motion .hq-content :is(.kpis, .panel).is-revealed.is-scrolled-in > *,
    .hq-motion .hq-content .page-header.is-revealed.is-scrolled-in { animation-duration: 520ms; animation-delay: 0ms; }
    .hq-motion :is([data-enter], .hq-content .panel).is-revealed.is-scrolled-in > :nth-child(2) { animation-delay: 50ms; }
    .hq-motion :is([data-enter], .hq-content .panel).is-revealed.is-scrolled-in > :nth-child(n + 3) { animation-delay: 100ms; }
  }
  @keyframes hq-phone-in { from { opacity: 0; transform: translateY(14px); } }

  /* On a narrow screen the stream runs through the middle of every page,
     behind headings, fields and tables, not beside them as on a desktop:
     it stays a quieter light, and glass is nearly solid. */
  .hq-energy-live .hq-energy { opacity: 0.55; }
  html.hq-energy-on {
    --glass-1: rgb(var(--navy-hi-rgb) / 0.97);
    --glass-2: rgb(25 42 64 / 0.98);
    --glass-3: rgb(var(--navy-rgb) / 0.98);
  }
  /* Fields on the navy page are solid wells too (pearl panels keep theirs). */
  :is(input[type="text"], input[type="password"], input[type="email"], input[type="search"], input[type="number"], input[type="date"], select, textarea):not(:is(.hq-pearl, .hq-form-card, .hq-auth__card, .auth-card, .hq-ask__form) *) {
    background-color: rgb(var(--navy-rgb) / 0.96);
  }

  /* ================================================================ */
  /* 7. TESLA's chat on the whole screen                               */
  /* ================================================================ */
  /* Touching the question field opens the chat over the whole screen
     above the keyboard (hq.js sets .is-immersive, the visible height and
     where the page stood). The page underneath is pinned, so nothing
     scrolls behind the chat, and the tab bar steps aside. On iPhone hq.js
     stops Safari from zooming into the field, so the text can stay small. */
  .hq-ask__form textarea { font-size: 15px; }
  html.hq-chat-open, html.hq-chat-open body { overflow: hidden; overscroll-behavior: none; }
  html.hq-chat-open body { position: fixed; top: var(--chat-lock, 0px); left: 0; right: 0; }
  html.hq-chat-open .hq-tabbar { visibility: hidden; }
  .hq-ask.is-immersive {
    position: fixed;
    z-index: 95;
    left: 0;
    right: 0;
    top: var(--chat-top, 0px);
    height: var(--chat-h, 100dvh);
    display: flex;
    flex-direction: column;
    margin: 0;
    border-radius: 0;
    animation: none;
    transform: none;
    background:
      radial-gradient(120% 50% at 0% 0%, rgb(var(--orange-rgb) / 0.08), transparent 60%),
      var(--navy-900);
    box-shadow: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .hq-ask.is-immersive > * { animation: none; }
  .hq-ask.is-immersive::before, .hq-ask.is-immersive::after { content: none; }
  .hq-ask.is-immersive .hq-ask__head {
    flex: none;
    flex-wrap: nowrap;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) 10px 8px 6px;
    background: rgb(var(--navy-hi-rgb) / 0.6);
  }
  .hq-ask.is-immersive .hq-ask__who { gap: 9px; min-width: 0; }
  .hq-ask.is-immersive .hq-portrait--ask { width: 32px; height: 32px; }
  .hq-ask.is-immersive .hq-ask__dot { right: 0; bottom: 0; width: 9px; height: 9px; }
  .hq-ask.is-immersive .hq-ask__name { font-size: 16px; }
  .hq-ask.is-immersive .hq-ask__tool { height: 34px; padding: 0 10px; border-radius: 10px; }
  .hq-ask.is-immersive .hq-ask__head .hq-info__button { width: 32px; height: 32px; }
  .hq-ask__close {
    flex: none;
    display: inline-grid;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 11px;
    color: var(--text);
    background: none;
    cursor: pointer;
  }
  .hq-ask__close[hidden] { display: none; }
  .hq-ask__close .icon { width: 20px; height: 20px; }
  .hq-ask__close:active { background: rgb(var(--fg-rgb) / 0.08); }
  .hq-ask__close:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; }
  .hq-ask.is-immersive .hq-ask__stage {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 10px 10px 2px;
  }
  .hq-ask.is-immersive .hq-ask__log {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    padding-right: 0;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  /* Messages sit at the bottom, next to the field, as in any messenger. */
  .hq-ask.is-immersive .hq-ask__log:not([hidden]) { display: flex; flex-direction: column; gap: 10px; }
  .hq-ask.is-immersive .hq-ask__log > :first-child { margin-top: auto; }
  .hq-ask.is-immersive .hq-ask__blank { flex: 1 1 auto; }
  /* Messenger-sized text and bubbles. */
  .hq-ask.is-immersive .hq-chat__bubble { padding: 9px 12px; border-radius: 15px; font-size: 14px; line-height: 1.45; }
  .hq-ask.is-immersive .hq-ask__para { line-height: 1.45; }
  .hq-ask.is-immersive .hq-ask__text { gap: 6px; }
  .hq-ask.is-immersive .hq-ask__form {
    flex: none;
    gap: 8px;
    margin: 4px 8px max(8px, env(safe-area-inset-bottom));
    padding: 5px 5px 5px 14px;
    border-radius: 16px;
  }
  .hq-ask.is-immersive .hq-ask__form textarea { min-height: 34px; max-height: 120px; padding: 7px 0; font-size: 15px; line-height: 1.35; }
  .hq-ask.is-immersive .hq-ask__send { width: 34px; height: 34px; border-radius: 11px; }
  .hq-ask.is-immersive .hq-ask__send .icon { width: 16px; height: 16px; }
}

/* Page changes on a phone are plain navigations: Safari's cross-document
   transition put the light and the content together in the wrong order for
   a moment. The shell stays where it is and the new content builds up
   (section 6). Outside the layers, so it wins over hq.css. */
@view-transition { navigation: none; }
