/* Phone layout. Loaded after every module stylesheet so these rules win.
   Breakpoints used across web/*.css:
     ≥900px   desktop rail; nothing in this file applies except .phone-only
     ≤960px   (app.css) rail becomes a top bar, kept only for 900–960px
     ≤899px   phone shell, below: compact top bar, bottom tab bar, menu sheet
     ≤700px   single-column work areas (module stylesheets)
     ≤560px   phone content: 360–430px is the design target
   Phone contract: no horizontal page scroll, 16px side gutters (wider on
   notched landscape phones), tap targets at least 44px, sub-tabs in one
   scrolling row, and wide content (tables, code, diffs, raw captures)
   scrolling inside its own container. Colours come from theme.css tokens, so
   Dark (default), Cyberpunk and Light all apply. Amber marks the current place,
   as the section tabs do on desktop. */
.phone-only { display:none !important; }

@media (max-width:899px) {
  :root { --gutter-l:max(16px,env(safe-area-inset-left)); --gutter-r:max(16px,env(safe-area-inset-right));
    --bar-h:56px; --tabs-h:58px; }
  .phone-only { display:revert !important; }
  html { scroll-padding-top:calc(var(--bar-h) + env(safe-area-inset-top) + 8px); scroll-padding-bottom:calc(var(--tabs-h) + env(safe-area-inset-bottom) + 8px); }
  html,body { overflow-x:clip; }
  body { min-height:100dvh; }
  .workspace { min-height:100dvh; }
  .workspace-content { padding:0 var(--gutter-r) calc(var(--tabs-h) + env(safe-area-inset-bottom) + 28px) var(--gutter-l); }

  /* Top bar: the toolbar sticks; project name opens the menu, search folds away. */
  .workspace-toolbar { position:sticky; top:0; z-index:40; flex-wrap:wrap; gap:0 6px; min-height:0;
    margin:0 calc(-1 * var(--gutter-r)) 18px calc(-1 * var(--gutter-l));
    padding:calc(env(safe-area-inset-top) + 6px) var(--gutter-r) 6px var(--gutter-l);
    background:var(--sunk); border-bottom:1px solid var(--line); }
  .workspace-toolbar .breadcrumbs,#refresh { display:none; }
  .phone-project { display:flex !important; align-items:center; gap:9px; flex:1 1 0; min-width:0; min-height:44px; margin-left:-6px; padding:0 6px;
    border:0; border-radius:var(--radius-control); background:none; color:var(--ink); text-align:left; }
  .phone-project .brand-mark { width:22px; height:22px; flex-shrink:0; }
  #phone-project-name { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
    font-family:var(--font-display); font-size:17px; font-weight:640; letter-spacing:-.01em; }
  .phone-chevron { width:14px; height:14px; flex-shrink:0; fill:none; stroke:var(--muted); stroke-width:1.8; stroke-linecap:round; stroke-linejoin:round; }
  .phone-project:hover,.phone-icon-button:hover { background:var(--surface-hover); }
  .sync { order:2; min-width:0; font-size:12px; }
  #sync-status { max-width:7.5em; font-size:11.5px; line-height:1.25; text-align:right; overflow-wrap:anywhere; }
  .phone-icon-button { display:grid !important; place-items:center; order:3; width:44px; height:44px; flex-shrink:0; margin-right:-8px;
    border:0; border-radius:var(--radius-control); background:none; color:var(--ink); }
  .phone-icon-button svg { width:20px; height:20px; fill:none; stroke:currentColor; stroke-width:1.7; stroke-linecap:round; }
  .phone-icon-button[aria-expanded="true"] { background:var(--surface-hover); }
  .project-search { display:none; order:4; flex-basis:100%; max-width:none; margin:4px 0 2px; }
  .workspace-toolbar:has(#phone-search-toggle[aria-expanded="true"]) .project-search { display:block; }
  .search-panel { max-height:min(420px,60dvh); }

  /* Sub-tabs: one horizontally scrolling row of pills, edge to edge. */
  .section-nav { flex-wrap:nowrap; gap:6px; overflow-x:auto; scrollbar-width:none; overscroll-behavior-x:contain;
    margin:0 calc(-1 * var(--gutter-r)) 20px calc(-1 * var(--gutter-l)); padding:0 var(--gutter-r) 0 var(--gutter-l); border-bottom:0; }
  .section-nav::-webkit-scrollbar { display:none; }
  .section-nav button { flex:0 0 auto; min-height:44px; margin:0; padding:0 14px; white-space:nowrap;
    border:1px solid var(--line); border-radius:999px; font-size:13.5px; }
  .section-nav button[aria-current="page"] { background:var(--surface); color:var(--ink); border-color:var(--human); }
  .scope-note { margin:-8px 0 16px; }

  /* Bottom tab bar: four sections and More. */
  .phone-tabs { display:grid !important; grid-template-columns:repeat(5,minmax(0,1fr)); position:fixed; z-index:45; left:0; right:0; bottom:0;
    padding:0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background:var(--sunk); border-top:1px solid var(--line); }
  .phone-tab { position:relative; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:3px; min-width:0; min-height:var(--tabs-h);
    padding:6px 2px 5px; border:0; background:none; color:var(--muted); font-size:11.5px; font-weight:540; line-height:1.2; }
  .phone-tab .nav-icon { width:22px; height:22px; }
  .phone-tab[data-tab="work"] .nav-icon circle { fill:none; stroke:currentColor; }
  .phone-tab[aria-current="page"] { color:var(--ink); }
  .phone-tab[aria-current="page"]::before { content:""; position:absolute; top:-1px; left:28%; right:28%; height:2px; border-radius:0 0 2px 2px; background:var(--human); }
  .phone-tab[data-tab="home"][aria-current="page"] .nav-icon circle { fill:var(--human); }
  .phone-tab:focus-visible { outline-offset:-3px; }
  .phone-tab .nav-count { position:absolute; top:4px; left:calc(50% + 7px); margin:0; min-width:18px; padding:0 5px; font-size:11px; line-height:18px; }
  /* While typing, the keyboard needs the room. */
  body:has(:is(input:not([type="checkbox"]):not([type="radio"]),textarea,[contenteditable]):focus) .phone-tabs { display:none !important; }

  /* Menu sheet: the desktop rail, rising from the bottom. */
  .workspace-sidebar { position:fixed; z-index:60; top:auto; left:0; right:0; bottom:0; height:auto; max-height:min(90dvh,680px); overflow-y:auto; overscroll-behavior:contain;
    display:grid; grid-template-columns:minmax(0,1fr) auto; align-content:start; align-items:center; gap:6px 10px;
    padding:16px var(--gutter-r) calc(16px + env(safe-area-inset-bottom)) var(--gutter-l);
    border:0; border-top:1px solid var(--line-strong); border-radius:18px 18px 0 0; background:var(--sidebar);
    transform:translateY(102%); visibility:hidden; transition:transform .22s cubic-bezier(.3,.7,.2,1),visibility 0s linear .22s; }
  .menu-open .workspace-sidebar { transform:none; visibility:visible; transition:transform .22s cubic-bezier(.3,.7,.2,1); box-shadow:0 0 0 100vmax rgb(0 0 0 / .55); }
  .workspace-sidebar>* { order:0; grid-column:1 / -1; margin:0; }
  .workspace-sidebar>.brand { display:none; }
  .workspace-sidebar:focus { outline:none; }
  .workspace-sidebar>.header-project+.workspace-nav { margin-top:8px; }
  .workspace-sidebar>.header-project { grid-column:1; grid-row:1; flex:none; }
  #phone-menu-close { grid-column:2; grid-row:1; min-height:44px; }
  .workspace-sidebar>.nav-label { display:block; margin:12px 4px 2px; }
  .workspace-nav { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:4px; }
  .workspace-nav .nav-item { width:100%; min-height:48px; padding:8px 12px; font-size:14.5px; background:var(--surface-subtle); }
  .workspace-nav .nav-item[aria-current="page"] { background:var(--surface); box-shadow:inset 0 0 0 1px var(--human); }
  #nav-home,#nav-thoughts,#nav-work,#nav-runs { display:none; }
  .sidebar-worker { display:block; margin-top:10px; padding:14px 4px 4px; border-top:1px solid var(--line); }
  .sidebar-worker>strong { margin-top:6px; font-size:14px; }
  .sidebar-worker>p { margin-top:2px; }
  .project-context-detail { margin-top:8px; }
  .project-context-detail summary { padding-block:12px; }
  .sidebar-footer { display:flex; flex-wrap:wrap; align-items:center; justify-content:space-between; gap:10px 16px; padding:12px 4px 0; border-top:1px solid var(--line); }
  .sidebar-footer .account { flex:1 1 auto; }
  .account .quiet { min-height:44px; }
  .theme-control select { min-height:44px; padding:6px 10px; }

  /* Lists stack above their detail; selected detail scrolls into view (app.js). */
  /* With a run open: history, then that run, then the composer, so the response is not buried under the form. */
  .run-sidebar { display:contents; }
  .workbench:has(#detail:not([hidden])) .history { order:-1; }
  .workbench:has(#detail:not([hidden])) #compose { order:1; }
  .phone-reveal { min-height:calc(100dvh - var(--bar-h) - var(--tabs-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 24px); }
  .native-terminal-canvas { height:max(280px,min(520px,calc(100dvh - var(--bar-h) - var(--tabs-h) - 150px))); }
  .native-terminal-expanded { inset:max(8px,env(safe-area-inset-top)) max(8px,env(safe-area-inset-right)) max(8px,env(safe-area-inset-bottom),calc(var(--kb,0px) + 8px)) max(8px,env(safe-area-inset-left)); }
  /* 16px form text stops iOS Safari zooming on focus, landscape phones included;
     xterm's hidden input is a textarea too. */
  input:not([type="checkbox"]):not([type="radio"]),select,textarea,.xterm .xterm-helper-textarea { font-size:16px; }
  /* iOS only sends taps on clickable elements, so the dimmed backdrop says it is one. */
  body.menu-open { cursor:pointer; }
  body.menu-open .workspace-sidebar { cursor:auto; }

  .login-view { min-height:100dvh; padding:max(24px,env(safe-area-inset-top)) var(--gutter-r) max(24px,env(safe-area-inset-bottom)) var(--gutter-l); }
}

@media (max-width:560px) {
  /* Safety net: any overflow missed below scrolls inside its own box. */
  .workspace,.workspace-content,.panel,.workbench>*,.work-layout>*,.reviews-layout>*,
  .activity-layout>*,.roster-layout>*,.skills-layout>*,.home-grid>* { min-width:0; max-width:100%; }

  .login-view { align-content:start; }
  /* Sign-in fits one phone screen: drop the decorative threads. */
  .login-story { display:none; }
  .login-card .brand { margin-bottom:20px; }
  .login-card h1 { font-size:26px; }
  .login-card .intro { margin:10px 0 20px; }

  /* Tap targets */
  .button,.tab,.nav-item,select,.run-item,.work-goal,.work-task,.command-row,
  #runs-archived,#session-turn-list .button,.search-form .button,.thread-button { min-height:44px; }
  .thread-labels { grid-auto-rows:44px; }
  .thread-lines { height:calc(var(--lanes) * 44px); }
  summary { padding-top:12px; padding-bottom:12px; }
  .tab { padding:12px 0; }
  input[type="checkbox"],input[type="radio"] { width:20px; height:20px; flex-shrink:0; }
  input:not([type="checkbox"]):not([type="radio"]),select,textarea { max-width:100%; }

  /* Headings and action rows wrap instead of overflowing. */
  .page-heading,.detail-header,.work-detail-header,.work-task-heading,.reviews-section-heading,
  .section-heading { flex-wrap:wrap; align-items:flex-start; }
  .detail-actions,.work-detail-header>.work-actions { width:100%; justify-content:flex-start; }
  .page-heading h1 { font-size:24px; }

  /* Home */
  .home-brief h1 { font-size:30px; }
  .home-brief .home-actions { display:grid; grid-template-columns:1fr 1fr; }
  .home-status button { font-size:15px; min-height:40px; }
  .home-actions { width:100%; }
  .home-actions .button { flex:1; }
  .goal-row { padding:14px; }
  .goal-threads { grid-template-columns:minmax(0,1fr) 28px auto; }
  .decision-node { width:56px; height:56px; }
  .decision-node strong { font-size:16px; }
  .home-start { flex-direction:column; align-items:flex-start; padding:20px; }

  /* Run response */
  .run-overview { grid-template-columns:repeat(3,minmax(0,1fr)); }
  .run-overview .metric { padding:12px 10px; }
  .detail-header,.prompt-block,.session-turns,.follow-up,.response-panel,.output-panel,
  .run-metadata>summary { padding-left:16px; padding-right:16px; }
  .detail-header~.message,.state-note,.run-diagnosis { margin-left:16px; margin-right:16px; }
  .output-heading { padding:0 16px; overflow-x:auto; }
  .tabs { gap:16px; }
  .run-meta { grid-template-columns:1fr; padding:16px; }
  .run-list { max-height:260px; }

  /* Work task view, question answers and review decisions */
  .work-detail-header,.work-criteria,.work-task-heading,.work-task,.work-audit,
  .work-evidence,.work-decision,.work-decisions>summary,.work-policy,.work-editor { padding-left:16px; padding-right:16px; }
  .work-task-detail>.work-meta,.work-task-detail>.work-prose,.work-instruction-heading { margin-left:16px; margin-right:16px; }
  .work-actions,.work-editor-actions,.reviews-actions,.project-actions { flex-wrap:wrap; }
  .work-actions>.button,.reviews-actions>.button,.project-actions>.button { flex:1 1 auto; }
  .task-activity-entry textarea,.follow-up textarea,#reviews-view textarea { width:100%; }
  .reviews-detail,.reviews-editor,.reviews-queue,.reviews-library,.reviews-decision { padding:16px; }
  .conversation-message { padding:16px; }

  /* Wide content scrolls inside its own container, never the page. */
  pre,.output-panel,.agent-message .message-table,.agent-message .message-code,
  .reviews-markdown pre,.roster-saved pre { max-width:100%; overflow-x:auto; }
  .reviews-markdown table,.conversation-message table,.activity-message table {
    display:block; max-width:100%; overflow-x:auto; }
  .mono,code,.project-path,#detail-location { overflow-wrap:anywhere; }
  .checkout-browser { width:calc(100vw - 32px); padding:16px; }

  /* Reading panels run edge to edge so responses and diffs get the width. */
  :is(.workbench,.run-sidebar,.work-layout,.reviews-layout,.activity-layout)>.panel:not(.work-goals) {
    max-width:none; margin-inline:-16px; border-radius:0; border-inline:0; }

  /* Decision history: each record becomes a stacked entry instead of a wide table row. */
  .audit-scroll { overflow:visible; }
  .audit-table { min-width:0; }
  .audit-table thead { position:absolute; width:1px; height:1px; overflow:hidden; clip-path:inset(50%); }
  .audit-table,.audit-table tbody { display:block; }
  .audit-table tr { display:grid; grid-template-columns:auto minmax(0,1fr); gap:3px 12px; padding:12px 0; border-bottom:1px solid var(--line); }
  .audit-table td { grid-column:2; padding:0; border:0; }
  .audit-table td:nth-child(1) { grid-column:1; grid-row:1 / span 6; order:-2; }
  .audit-table td:nth-child(3) { order:-1; font-weight:550; }
  .audit-table td:nth-child(6)::before { content:"Revision "; color:var(--faint); }
  .audit-table td:nth-child(7) { color:var(--muted); }
}

@media (max-width: 899px) {
  /* Quiet refresh sits beside the title instead of taking a row of its own. */
  .page-heading { position:relative; }
  .page-heading:has(> .view-refresh) > div:first-child { padding-right:84px; }
  .page-heading > .view-refresh { position:absolute; top:0; right:0; min-height:36px; padding:6px 10px; }
  /* Long tab rows scroll sideways; fade the edge so it reads as more. */
  .section-nav { -webkit-mask-image:linear-gradient(to right, #000 calc(100% - 28px), transparent); mask-image:linear-gradient(to right, #000 calc(100% - 28px), transparent); padding-right:28px; }
}

@media (max-width: 899px) {
  /* The top bar already names the project and shows freshness; Home keeps only what to act on. */
  .home-project, #inbox-view .inbox-status { display:none; }
  #inbox-view .inbox-actions > button:last-child { display:none; }
}
