/* Phone layer (S5). Loaded after css/responsive.css. Desktop (>= 1181 px) and tablets (821-1180 px, which keep the top
   bar with its menu button) are untouched: every rule that changes layout sits inside the phone query below.

   Phone mode = up to 820 px wide (portrait phones, small tablets in portrait) or up to 500 px tall (phones in landscape).
     - the top bar keeps the title and the burner / next-reading pills; the tabs, the menu button and the alarm pill go away,
     - a fixed bottom navigation bar (js/app/bottom-nav.js) takes their place: Planta 3D and Alarmas (with the badge), 58 px + safe area,
     - #view ends above the bar, so the HUD sheet, camera strip, pages and their last content are always above it,
     - the bar steps aside for the on-screen keyboard (body.kb-open, js/app/keyboard.js).
   Tables as cards and sub-tab rows are in the "page contents" part below. Tokens only. */

.bnav { display: none; }

@media (max-width: 820px), (max-height: 500px) {
  :root { --bnav-h: 58px; --bnav-total: calc(var(--bnav-h) + env(safe-area-inset-bottom, 0px)); }

  /* ---------- top bar ---------- */
  .topbar .menu-btn, .topbar #tabs, .topbar #pill-alarms { display: none; }          /* the bar replaces the menu; its badge replaces the alarm pill */
  .topbar .sub { display: block; flex: 1 1 auto; font-size: 16px; }                  /* app name; ellipsis by design when the pills need the room */
  .topbar .status { margin-left: 0; }

  /* ---------- the bar ---------- */
  #view { bottom: var(--bnav-total); }
  .bnav { display: flex; position: fixed; z-index: 42; left: 0; right: 0; bottom: 0; height: var(--bnav-total);
    padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    background: var(--brand); border-top: 1px solid var(--glass-line); box-shadow: 0 -6px 18px rgb(0 0 0 / .35); }
  .bnav__item { position: relative; flex: 1 1 0; min-width: 0; height: var(--bnav-h); min-height: 44px; padding: 6px 2px; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 3px; border: 0; background: none; color: rgba(255, 255, 255, .74);
    font: 700 12px/1.1 var(--font); text-decoration: none; -webkit-tap-highlight-color: transparent; }
  .bnav__item:active { background: rgba(255, 255, 255, .06); }
  .bnav__ico { flex: none; box-sizing: content-box; padding: 3px 16px; border-radius: var(--pill); transition: background-color .15s; }
  .bnav__label { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .bnav__item[aria-current="page"] { color: #fff; }
  .bnav__item[aria-current="page"] .bnav__ico { background: var(--accent); box-shadow: var(--extrude); }
  .bnav__badge { position: absolute; top: 4px; left: calc(50% + 8px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: var(--pill);
    background: var(--alarm); color: #fff; font: 800 11px/18px var(--font); text-align: center; box-shadow: 0 0 0 2px var(--brand); }
  .bnav__badge[hidden] { display: none; }

  /* the bar steps aside for the on-screen keyboard */
  body.kb-open .bnav { display: none; }
  body.kb-open #view { bottom: 0; }

  /* ---------- page: content ends above the bar ---------- */
  .page { padding-bottom: 76px; }
}

@media (max-height: 500px) {
  :root { --topbar-h: 48px; --bnav-h: 54px; }
  .topbar { padding-top: 0; padding-bottom: 0; }
  .bnav__item { flex-direction: row; gap: 8px; padding: 0 6px; }
  .bnav__ico { padding: 2px 10px; }
  .bnav__badge { position: static; margin-left: -2px; align-self: center; box-shadow: none; }
  /* landscape: every pixel of height goes to the content; the title is in the top bar and the bar below names the section */
  .page > h1, .page .orn, .page .lead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .page { padding-top: 10px; padding-bottom: 68px; }
}

/* ---------- page contents on phones: one column, key information first ---------- */
@media (max-width: 820px), (max-height: 500px) {
  .page { font-size: 14px; }
  .page h1 { font-size: 22px; }
  .lead { margin: 6px 0 12px; font-size: 13px; }
  .lead__text { display: none; }                          /* the title and the "Datos simulados" chip say it; the long lead is for wide screens */
  .lead .chip { margin-left: 0; }
  body .page .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kc { padding: 10px 12px; }
  .kc span { font-size: 13px; }
  .kc b { white-space: nowrap; }
  .kc small { font-size: 12px; }
  .page .panel h2, .page .pcard h2 { font-size: 16px; flex-wrap: wrap; }
  .page .panel h2 small { font-size: 12px; }
  .page .panel th, .page th { font-size: 12px; }

  /* sub-tab rows: one row that scrolls sideways; the edge that hides more tabs fades out (js/ui/tab-rows.js sets can-left / can-right) */
  body .page .subtabs, body .page .tabrow { display: flex; flex-wrap: nowrap; gap: 6px; width: 100%; max-width: 100%; overflow-x: auto; overscroll-behavior-x: contain;
    scrollbar-width: none; position: relative; border-radius: var(--pill); --fl: 0px; --fr: 0px;
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 var(--fl), #000 calc(100% - var(--fr)), transparent 100%);
            mask-image: linear-gradient(90deg, transparent 0, #000 var(--fl), #000 calc(100% - var(--fr)), transparent 100%); }
  /* camera strip of the 3D plant: same edge hint */
  .pah-hud-cams { --fl: 0px; --fr: 0px; -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 var(--fl), #000 calc(100% - var(--fr)), transparent 100%);
    mask-image: linear-gradient(90deg, transparent 0, #000 var(--fl), #000 calc(100% - var(--fr)), transparent 100%); }
  .pah-hud-cams.can-left { --fl: 32px; } .pah-hud-cams.can-right { --fr: 32px; }
  .subtabs::-webkit-scrollbar, .tabrow::-webkit-scrollbar { display: none; }
  .subtabs.can-left, .tabrow.can-left { --fl: 44px; }
  .subtabs.can-right, .tabrow.can-right { --fr: 44px; }
  .subtabs button { flex: none; min-height: 44px; padding: 0 16px; font-size: 14px; }

  /* wide tables -> stacked cards: title on top, then label / value pairs (labels from js/ui/table-cards.js) */
  body .page table.tbl-cards { display: block; width: 100%; border: 0; }
  body .page table.tbl-cards thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }   /* headers stay for screen readers */
  body .page table.tbl-cards tbody, body .page table.tbl-cards tfoot { display: grid; gap: 8px; }
  body .page .panel:has(> .scroll > table.tbl-cards) { padding: 8px; }                     /* the rows are the cards */
  body .page .panel:has(> .scroll > table.tbl-cards) > h2 { margin: 4px 6px 8px; }
  body .page .panel:has(> .scroll > table.tbl-cards) > .filters, body .page .panel:has(> .scroll > table.tbl-cards) > .hint { margin-left: 6px; margin-right: 6px; }
  body .page table.tbl-cards tfoot { margin-top: 10px; }
  body .page table.tbl-cards tr { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(88px, 100%), 1fr)); gap: 8px 10px; padding: 10px 12px; background: #fff;
    border: 1px solid var(--line); border-radius: var(--radius); }
  body .page .en-report table.tbl-cards tr { background: var(--card); }
  body .page table.tbl-cards tr.tbl-empty { display: block; }
  body .page table.tbl-cards td, body .page table.tbl-cards tbody th { display: block; min-width: 0; padding: 0; border: 0; text-align: left; white-space: normal; overflow-wrap: anywhere; font-size: 14px; }
  body .page table.tbl-cards td::before { content: attr(data-label); display: block; margin-bottom: 2px; font-size: 12px; font-weight: 700; color: var(--mute); overflow-wrap: normal; }
  body .page table.tbl-cards td[data-label=""]::before, body .page table.tbl-cards td[data-role="title"]::before { display: none; }
  body .page table.tbl-cards td[data-role="title"] { grid-column: 1 / -1; font-size: 15px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
  body .page table.tbl-cards td[data-span="2"] { grid-column: span 2; }
  body .page table.tbl-cards td[data-wide] { grid-column: 1 / -1; }
  body .page table.tbl-cards td.num, body .page table.tbl-cards th.num { text-align: left; white-space: normal; }
  body .page table.tbl-cards td small, body .page table.tbl-cards td .ack-reason { font-size: 12px; }
  body .page table.tbl-cards .empty { padding: 14px 4px; }
  body .page table.tbl-cards .btn, body .page table.tbl-cards .ack { min-height: 44px; }

  /* tables with 2-3 columns (maintenance plan, nameplate, tariffs) stay tables: text wraps inside the cell instead of scrolling the table sideways */
  body .page table:not(.tbl-cards) th, body .page table:not(.tbl-cards) td { white-space: normal; overflow-wrap: anywhere; }
  body .page table:not(.tbl-cards) thead th { white-space: nowrap; overflow-wrap: normal; }
}

@media (max-width: 820px), (max-height: 500px) {
  .page small, .page code, .page dt, .page .orn { font-size: 12px; }
}

/* 3D labels and the unit card keep a readable size on phones (12 px minimum) */
@media (max-width: 820px), (max-height: 500px) {
  .pah-label__id { font-size: 12px; }
  .pah-hud-unit, .pah-hud-card__model, .pah-hud-grid, .pah-hud-alarms li, .pah-hud-tag { font-size: 12px; }
}
