/* Phone and small-tablet layout. Loaded after app.css.
   Everything that changes a layout sits inside a max-width query: from 981px up the desktop is untouched.
   980px = the drawer breakpoint used by app.css; 640px = phones. */

/* Controls that exist only for the narrow layout. */
.side .drawer-close, .side .drawer-tools, .side .drawer-profile, body.console .side .drawer-head { display: none; }

@media (max-width: 980px) {
  /* ── Shell ─────────────────────────────────────────────────────────── */
  /* A grid track written "1fr" grows to its widest child and pushes the page sideways. */
  .login-card, .grid-2, .grid-3, .metabox-grid, .admit-board, .profile-grid, .two { grid-template-columns: minmax(0, 1fr); }
  .metrics, .metrics.five { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .attn-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* Block, not grid: a sticky header cannot travel inside a grid row of its own height. */
  .shell, body.console .console-shell { display: block; }
  .main, .content { min-width: 0; }
  .top, body.console .adminbar { position: sticky; top: 0; z-index: 25; }
  body.console .adminbar { height: calc(56px + env(safe-area-inset-top)); }
  body.console .content { padding-bottom: calc(92px + env(safe-area-inset-bottom)); }
  .top .menu-btn, body.console .adminbar .menu-btn { display: inline-flex; align-items: center; justify-content: center; flex: none; width: 44px; height: 44px; padding: 0; }
  body.console .adminbar .icon-btn { width: 44px; height: 44px; flex: none; }
  body.console .adminbar .brand { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; }
  body.console .adminbar [data-cmd='logout'] { min-width: 44px; justify-content: center; }
  .search input { height: 44px; min-width: 0; }
  .search .btn { flex: none; width: 44px; padding: 0; }
  body.nav-open { overflow: hidden; }

  /* ── Drawer ────────────────────────────────────────────────────────── */
  .side, body.console .side { visibility: hidden; transition: transform 0.25s ease, visibility 0.25s; }
  body.nav-open .side { visibility: visible; }
  .side { gap: 0; overflow-y: auto; overscroll-behavior: contain; padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
  body:not(.console) .side { background: var(--glass-strong); }
  .side .drawer-head, body.console .side .drawer-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex: none; }
  body.console .side .drawer-head { padding: 0 12px; }
  body.console .side .drawer-head .brand { color: #f4f0fb; text-decoration: none; }
  .side .drawer-close { display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; padding: 0; flex: none; font-size: 1.5rem; line-height: 1; }
  body.console .side .drawer-close { background: transparent; color: #f4f0fb; border-color: rgb(255 255 255 / 0.24); }
  body.console .side .side-search { flex: none; }
  /* One scroll for the whole drawer: a short screen must not trap the links in a small inner box. */
  .side .nav { flex: none; overflow: visible; margin-top: 8px; gap: 2px; }
  .side .who, .side .side-foot { flex: none; margin-top: 12px; }
  .side .nav a, body.console .nav a, body.collapsed .nav a, body.console .nav-more summary { min-height: 44px; font-size: 0.95rem; }
  body.collapsed .nav-text { display: block; }
  body.collapsed .nav a, body.collapsed .nav-more summary { justify-content: flex-start; padding: 8px 12px; }
  .side .who-link { display: flex; align-items: center; min-height: 44px; }
  .side .drawer-profile { display: flex; align-items: center; gap: 10px; min-height: 44px; padding-inline: 12px; color: inherit; text-decoration: none; font-weight: 600; border-radius: 10px; }
  .side .drawer-profile[aria-current='page'] { background: var(--violet-600); }

  /* ── Tap targets ───────────────────────────────────────────────────── */
  /* Width comes from padding, not min-width: a flex item given a min-width may shrink under its own label. */
  .btn, .btn.small { min-height: 44px; }
  .btn.small { padding-inline: 14px; }
  a.btn { text-align: center; }
  /* Direct children only: a tab may carry its own small "i" button. */
  .tabs > a, .tabs > button { flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; min-width: 44px; }
  .tabbar .tab { min-height: 48px; min-width: 0; align-content: center; }
  .brand { min-height: 44px; }
  summary { min-height: 44px; cursor: pointer; }
  .content summary, .login summary { padding-block: 10px; }
  .metabox-toggle { min-width: 44px; min-height: 44px; flex: none; }
  .screen-options label, .seg-choice label, form label.row, .check { min-height: 44px; align-items: center; }
  .screen-options label { display: flex; gap: 8px; }
  .screen-options input, .seg-choice input, .check input, label.row input[type='checkbox'] { width: 20px; height: 20px; min-height: 0; flex: none; }
  input:not([type='checkbox']):not([type='radio']):not([type='hidden']), select { min-height: 44px; }
  input[type='file'] { padding-block: 9px; }
  /* Under 16px an iPhone zooms the whole page the moment a field is tapped, and leaves it zoomed. */
  body input:not([type='checkbox']):not([type='radio']):not([type='hidden']), body select, body textarea { font-size: 1rem; }
  input[type='datetime-local'], input[type='date'] { -webkit-appearance: none; appearance: none; display: block; }
  /* The small "i" keeps its size; the area a finger can hit grows around it. */
  .info::after { inset: -15px; }
  /* Inside a tab it was inheriting the tab's own pill padding. */
  .tabs .info { flex: none; width: 18px; height: 18px; padding: 0; border: 1.5px solid currentColor; border-radius: 50%; }

  /* Text links: padding on an inline link widens what a finger can hit without moving a line. */
  .content p > a:not(.btn), .content .small a, .content .kv a,
  .login p > a:not(.btn), .login .check a { padding-block: 13px; padding-inline: 12px; margin-inline: -8px; }
  /* A linked heading is already wide: it only needs the height, and must not push past its card. */
  .content h2 > a, .content h3 > a { padding: 13px 8px; margin-inline: -8px; }
  /* Links that stand alone in a list get a real row. */
  .glance { gap: 0; }
  .glance a, .metabox li > a, .admit-card > a, .timeline li > a { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; min-width: 44px; }
  .glance a { display: flex; }
  .admit-card div > a { display: inline-block; min-width: 44px; padding-block: 12px; }
  .who-link, .content .numbers a, .content .attn-link, .content .stat { min-height: 44px; }

  /* ── Shared components ─────────────────────────────────────────────── */
  .person { flex-wrap: wrap; min-height: 56px; row-gap: 4px; }
  .person > span:not(.avatar):not(.chip) { display: flex; flex-direction: column; flex: 1 1 11rem; min-width: 0; }
  .person > .chip:last-child { margin-inline-start: auto; }
  .content > .panel + .panel, .content > .panel + .stack, .content > .panel + .form,
  .content > .stack + .form, .content > .stack + .panel, .content > .banner + .banner,
  .content > .metabox-grid + * { margin-top: 12px; }
  .content > .banner { margin-block: 0 12px; }
  .panel, .card, .metric { min-width: 0; }
  .content { overflow-wrap: break-word; }
  .tabs { max-width: 100%; min-width: 0; scroll-padding-inline: 40px; overscroll-behavior-inline: contain; }
  .workflow { gap: 4px 8px; }

  /* Website requests: a long address, link or campaign name must wrap inside its card. */
  .lead { grid-template-columns: minmax(0, 1fr); overflow-wrap: anywhere; }
  .lead > *, .lead .lead-top > div { min-width: 0; }
  .lead .lead-top > div { flex: 1 1 12rem; }
  .lead .chip { overflow-wrap: normal; }
  /* An address or number moves to its own line before it is ever cut in the middle. */
  .lead-top bdi { display: inline-block; max-width: 100%; }
  .fact { max-width: 100%; }
  .lead-actions { gap: 8px; }
  .lead-actions .btn { flex: 1 1 auto; justify-content: center; }
  .lead-more > summary { display: flex; align-items: center; }

  /* Between phone and desktop (tablets, a phone held sideways) tables stay tables:
     words stay whole, long code may break, and the wrapper scrolls if it still does not fit. */
  .table-wrap, .tablewrap { max-width: 100%; overflow-x: auto; }
  .content th, .content td { overflow-wrap: break-word; }
  .content td code { overflow-wrap: anywhere; }
  .content td a:not(.btn) { display: inline-flex; align-items: center; min-height: 44px; min-width: 44px; margin-block: -10px; }
}

@media (max-width: 640px) {
  /* ── Header. Language and theme move into the drawer. ───────────────── */
  .top, body.console .adminbar { flex-wrap: nowrap; gap: 6px; padding-inline: 10px; }
  .top { padding-bottom: 8px; }
  .top .search { flex: 1 1 0; min-width: 0; gap: 6px; }
  .top > [data-cmd='lang'], .top > [data-cmd='theme'],
  body.console .adminbar > [data-cmd='lang'], body.console .adminbar > [data-cmd='theme'] { display: none; }
  .side .drawer-tools { display: flex; gap: 8px; margin: 12px 12px 0; flex: none; }
  body:not(.console) .side .drawer-tools { margin-inline: 0; }
  .side .drawer-tools .btn { flex: 1; }
  body.console .side .drawer-tools .btn.ghost { background: transparent; color: #f4f0fb; border-color: rgb(255 255 255 / 0.28); }

  /* ── Page rhythm ───────────────────────────────────────────────────── */
  .content, body.console .content { padding-inline: 12px; padding-top: 12px; }
  .content > .banner:first-child { font-size: 0.85rem; padding: 8px 12px; }
  .page-head { padding-block: 0 10px; margin-bottom: 12px; }
  .page-head h1 { font-size: 1.5rem; }
  .progress-card .metrics, .metrics.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .progress-card .metric, .metrics.three .metric { padding: 10px 8px; }
  .progress-card .metric .n, .metrics.three .metric .n { font-size: 1.45rem; }
  .metric .small { line-height: 1.35; display: block; }
  .grid-cards, .stack, ul.plain { grid-template-columns: minmax(0, 1fr); }
  /* Request card: name and status share the first line; date, phone and address get the full width. */
  .lead .lead-top { display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: 'name state' 'meta meta'; column-gap: 8px; align-items: start; }
  .lead .lead-top > div { display: contents; }
  .lead .lead-top h3 { grid-area: name; }
  .lead .lead-top p { grid-area: meta; }
  .lead .lead-top > .chip { grid-area: state; }

  /* Request counters: all of them on screen instead of a strip that hides half. */
  .stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); overflow: visible; }
  .stat { padding: 10px 6px; align-content: start; }

  /* ── Forms ─────────────────────────────────────────────────────────── */
  .content form.row { align-items: flex-end; }
  .content form.row > input:not([type='hidden']):not([type='checkbox']):not([type='radio']),
  .content form.row > label, .content form > .row > label { flex: 1 1 100%; }
  .content form.row > select { flex: 1 1 9rem; width: auto; }
  .content form.row > .btn, .content form > .row > .btn { flex: 1 0 auto; }
  .content form.row > strong { flex: 1 1 100%; }
  .content .row > form:not(.search) { max-width: 100%; }
  .content .row > form[data-action='outbox-sent'] { flex: 1 1 100%; }
  .content form[data-action='outbox-sent'] { display: flex; flex-wrap: wrap; gap: 8px; }

  /* ── Wide data: each table row becomes a card, each cell "label: value". ── */
  .table-wrap, .tablewrap { overflow: visible; }
  .content table { display: block; width: 100%; font-size: 0.9rem; }
  .content thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  /* A column's "i" must stay reachable once the header row is gone: those headings become a help line above the cards. */
  .content thead:has(.info) { position: static; width: auto; height: auto; overflow: visible; clip-path: none; display: block; white-space: normal; }
  .content thead:has(.info) tr { display: flex; flex-wrap: wrap; column-gap: 28px; border: 0; background: none; padding: 0; margin-bottom: 8px; }
  .content thead:has(.info) th { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); border: 0; padding: 0; }
  .content thead:has(.info) th:has(.info) { position: static; width: auto; height: auto; overflow: visible; clip-path: none; display: inline-flex; align-items: center; gap: 4px; min-height: 44px; }
  .content tbody { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .content tr { display: block; border: 1px solid var(--line); border-radius: 14px; background: var(--glass-strong); padding: 0 12px; }
  .content td, .content .table td {
    display: block; position: relative; min-height: 44px; padding: 10px 0;
    border-bottom: 1px solid var(--line); white-space: normal; overflow-wrap: anywhere;
  }
  .content td:last-child { border-bottom: 0; }
  .content td:empty { display: none; }
  .content td:first-child { font-weight: 700; }
  .content td[data-label] { padding-inline-start: 38%; }
  .content td[data-label]::before {
    content: attr(data-label); position: absolute; top: 11px; inset-inline-start: 0; width: 34%;
    font-size: 0.76rem; font-weight: 700; color: var(--ink-3); line-height: 1.5;
  }
  /* Latin figures keep their own direction, but their label stays on the same side as every other label. */
  [dir='rtl'] .content td[dir='ltr'] { text-align: right; }
  [dir='rtl'] .content td[dir='ltr'][data-label] { padding-left: 0; padding-right: 38%; }
  [dir='rtl'] .content td[dir='ltr'][data-label]::before { left: auto; right: 0; direction: rtl; }
  .content td code { direction: ltr; unicode-bidi: isolate; display: inline-block; max-width: 100%; font-size: 0.78rem; line-height: 1.5; }

  /* The daily chart is drawn 640 units wide; scaled to a phone its 11px labels turn into dust. */
  .chart { direction: ltr; overflow: visible; max-height: none; }
  .chart .axis { font-size: 20px; }

  /* ── Sign-in and sign-up: the form has to be on the first screen. ───── */
  .login { padding: 10px 10px 24px; align-content: start; }
  .login-card { gap: 4px; padding: 6px; border-radius: 22px; }
  .login-card > div:first-child { padding: 14px 16px; border-radius: 17px; }
  .login-card > div, .login-card > form { padding: 14px 12px; }
  .login-card > div:first-child .brand-lg { flex-direction: row; align-items: center; }
  .login-card > div:first-child .brand-lg .mark { width: 48px; height: 48px; }
  .login-card > div:first-child .eyebrow { margin-top: 8px; }
  .login-card .display { font-size: 1.4rem; margin-block: 2px; }
  .login-card > div:first-child .muted { font-size: 0.86rem; margin-block: 4px; }
  .login-card > div:first-child .demo-note { font-size: 0.78rem; padding: 8px 10px; margin-block: 8px 0; }
  .login-card > div:first-child .small { margin-block: 8px 0; }
}

@media (max-width: 520px) {
  .attn-grid { grid-template-columns: minmax(0, 1fr); }
  /* Speed figures carry two "i" buttons in their title: one per row keeps them apart and the number unbroken. */
  .metrics.five { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .metric.vital .small { display: flex; flex-wrap: wrap; align-items: center; column-gap: 6px; min-height: 32px; }
}
