/* Mobile controls are created once; desktop keeps its existing focus order. */
.mobile-sheet-head, .mobile-touch-tools { display: none; }
/* Mobile gameplay layer. Load after styles.css and robots.css. The Godot
   viewport remains full-screen; HTML chrome stays compact and dismissible. */

@media (max-width: 900px),
       (pointer: coarse) and (max-height: 520px) {
  :root {
    --mobile-safe-top: env(safe-area-inset-top, 0px);
    --mobile-safe-right: env(safe-area-inset-right, 0px);
    --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
    --mobile-safe-left: env(safe-area-inset-left, 0px);
    --mobile-header: 76px;
    --mobile-build: 92px;
  }

  html, body {
    width: 100%;
    min-height: 100dvh;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: none;
  }

  #stage, #game { width: 100%; height: 100%; }

  /* Visible controls stay thumb-sized, including text-only disclosure links. */
  button, .link-btn { min-width: 44px; min-height: 44px; }

  #hud-top {
    height: calc(var(--mobile-header) + var(--mobile-safe-top));
    min-height: calc(var(--mobile-header) + var(--mobile-safe-top));
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 36px 32px;
    align-content: start;
    gap: 0 8px;
    padding: var(--mobile-safe-top) max(10px, var(--mobile-safe-right)) 0 max(10px, var(--mobile-safe-left));
  }
  .hud-brand { min-width: 0; }
  .hud-brand span { display: inline; }
  .hud-sep, #quota-chip, .strip-milestone { display: none; }
  #controls { grid-column: 2; grid-row: 1; gap: 4px; }
  #resources {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    min-width: 0;
    gap: 12px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  #resources::-webkit-scrollbar { display: none; }
  .strip-item { min-height: 32px; }
  .strip-label { font-size: 11px; }
  .strip-value { font-size: 14px; }

  /* Every native action remains reachable while a player is typing. */
  #controls .btn, #btn-locale, #btn-menu, .menu-item,
  #btn-locale-intro, .mobile-sheet button, .mobile-touch-tools button {
    min-width: 44px;
    min-height: 44px;
  }
  #controls .btn-slim { height: 44px; padding: 0 10px; }
  .menu {
    top: calc(var(--mobile-header) + var(--mobile-safe-top) + 4px);
    right: max(8px, var(--mobile-safe-right));
    width: min(240px, calc(100vw - 16px));
    max-height: calc(100dvh - var(--mobile-header) - var(--mobile-safe-top) - 16px);
    overflow-y: auto;
  }
  .menu-item { padding: 8px 12px; }

  /* A single bottom sheet replaces the desktop right stack. It starts folded,
     so the world is the default view. */
  #side.mobile-sheet {
    position: fixed;
    top: auto;
    right: max(8px, var(--mobile-safe-right));
    bottom: calc(var(--mobile-build) + var(--mobile-safe-bottom) + 8px);
    left: max(8px, var(--mobile-safe-left));
    width: auto;
    max-height: min(45vh, 320px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 8px;
    border-radius: 14px;
    z-index: 27;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-up);
  }
  #side.mobile-sheet:not(.mobile-sheet-collapsed) {
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  #side.mobile-sheet.mobile-sheet-collapsed {
    max-height: 52px;
    overflow: hidden;
  }
  .mobile-sheet-head {
    position: sticky;
    top: 0;
    z-index: 2;
    min-height: 52px;
    display: flex;
    align-items: stretch;
    gap: 6px;
    padding: 4px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .mobile-sheet-toggle {
    flex: 0 0 auto;
    height: 44px;
    padding: 0 12px;
    background: var(--surface-2);
  }
  .mobile-sheet-summary {
    min-width: 0;
    flex: 1 1 auto;
    height: 44px;
    overflow: hidden;
    padding: 0 10px;
    border: 1px solid transparent;
    border-radius: var(--r-btn);
    background: transparent;
    color: var(--ink-2);
    font-size: 13px;
    font-weight: 700;
    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .mobile-sheet-summary.has-question { color: var(--ink); background: var(--surface-2); border-color: var(--border); }
  #side.mobile-sheet > :not(.mobile-sheet-head) { flex: 0 0 auto; }
  #side.mobile-sheet .card { box-shadow: none; border-radius: 10px; }
  #side.mobile-sheet .robot-panel, #side.mobile-sheet .guide,
  #side.mobile-sheet .bubble, #side.mobile-sheet .routines { margin-inline: 4px; }
  #side.mobile-sheet .bubble-choices .btn,
  #side.mobile-sheet .guide-action,
  #side.mobile-sheet .robot-control,
  #side.mobile-sheet .robot-name-row .btn { min-height: 44px; }
  #side.mobile-sheet input, #side.mobile-sheet textarea,
  #side.mobile-sheet select { min-height: 44px; font-size: 16px; }

  /* Construction remains legible, horizontally scrollable and out of the
     world viewport's center. */
  #buildbar {
    left: max(8px, var(--mobile-safe-left));
    right: max(8px, var(--mobile-safe-right));
    bottom: var(--mobile-safe-bottom);
    width: auto;
    max-width: none;
    transform: none;
    align-items: stretch;
    gap: 4px;
    z-index: 28;
  }
  #buildbar-hint { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .buildbar-row {
    min-height: var(--mobile-build);
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    padding: 4px;
    gap: 2px;
    scrollbar-width: thin;
  }
  .build-btn {
    width: 88px;
    min-width: 88px;
    min-height: 80px;
    height: 80px;
    padding: 5px 4px;
  }
  .build-ic { width: 28px; height: 28px; }
  .build-name { font-size: 12px; }
  .build-cost { min-height: 16px; font-size: 11px; }
  .build-key { display: none; }

  .events { display: none; }
  .kbdhelp { display: none; }
  .mobile-touch-tools {
    position: fixed;
    right: max(8px, var(--mobile-safe-right));
    bottom: calc(var(--mobile-build) + var(--mobile-safe-bottom) + 10px);
    z-index: 26;
    display: flex;
    align-items: flex-end;
    gap: 4px;
    pointer-events: auto;
  }
  /* This is the only closed-sheet entry point. The real sheet removes its
     hidden attribute in mobile.js, so the global [hidden] rule cannot leave
     a focusable but invisible control behind. */
  .mobile-robots-toggle {
    min-width: 72px;
    height: 44px;
    padding: 0 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--soft-shadow);
    color: var(--ink);
    font-weight: 700;
  }
  .mobile-touch-tools.mobile-robots-open { display: none; }
  #left-dock.mobile-robots-open {
    visibility: hidden;
    pointer-events: none;
  }
  .mobile-tool-btn {
    width: 44px;
    height: 44px;
    padding: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--soft-shadow);
    font-size: 20px;
    line-height: 1;
  }
  .mobile-touch-help { max-width: min(240px, 62vw); }
  .mobile-touch-help summary {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0 9px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--soft-shadow);
    color: var(--ink-2);
    cursor: pointer;
    font-size: 12px;
    font-weight: 700;
  }
  .mobile-touch-help p {
    position: absolute;
    right: 0;
    bottom: 50px;
    width: min(260px, calc(100vw - 20px));
    margin: 0;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    font-size: 13px;
  }

  .panel, .modal {
    max-width: calc(100vw - var(--mobile-safe-left) - var(--mobile-safe-right) - 16px);
    max-height: calc(100dvh - var(--mobile-header) - var(--mobile-safe-top) - var(--mobile-safe-bottom) - 16px);
    overflow-y: auto;
  }
  .panel { top: calc(var(--mobile-header) + var(--mobile-safe-top) + 8px); right: max(8px, var(--mobile-safe-right)); width: min(360px, calc(100vw - 16px)); }
  .modal { padding: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .mobile-sheet, .mobile-touch-tools, .mobile-touch-help p { transition: none; animation: none; }
}

@media (max-width: 900px), (max-height: 520px) and (pointer: coarse) {
  #hud-top { grid-template-rows: 44px 28px; --mobile-header: 80px; }
  #resources .strip-bots { display: inline-flex; }
  #resources { gap: 10px; }
  #controls { gap: 3px; }
  #btn-company { padding-inline: 8px; font-size: 12px; }
  .hud-brand { grid-column: 1; grid-row: 1; font-size: 15px; }
  .mobile-touch-tools { right: 8px; bottom: auto; top: calc(90px + env(safe-area-inset-top)); flex-direction: column; }
  .mobile-touch-help { display: none; }
  .mobile-tool-btn { width: 44px; min-height: 44px; }
  .mobile-robots-toggle { min-width: 44px; padding-inline: 8px; }
  #side.mobile-sheet { background: var(--paper, #fffbf3); }
  .mobile-sheet-summary { display: block; }
}

/* The assignment sheet is a compact, deliberate affordance until opened by
   notebook-sheet.js. The guide remains useful without competing for the world. */
@media (max-width: 900px), (max-height: 520px) and (pointer: coarse) {
  #left-dock:not(.notebook-sheet-expanded) {
    bottom: calc(var(--mobile-build) + var(--mobile-safe-bottom) + 8px);
    max-height: min(208px, calc(100dvh - var(--mobile-header) - var(--mobile-build) - 24px));
    gap: 8px;
  }
  #left-dock:not(.notebook-sheet-expanded) #ada-dock { gap: 0; }
  #left-dock:not(.notebook-sheet-expanded) #guide {
    max-height: none;
    padding: 8px 10px;
    border-radius: 12px;
  }
  #left-dock:not(.notebook-sheet-expanded) #guide::after { display: none; }
  #left-dock:not(.notebook-sheet-expanded) #guide .guide-body {
    display: -webkit-box;
    margin: 2px 0 6px;
    font-size: 13px;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  #left-dock:not(.notebook-sheet-expanded) #guide .guide-instruction,
  #left-dock:not(.notebook-sheet-expanded) #guide .guide-progress,
  #left-dock:not(.notebook-sheet-expanded) #guide .guide-choices { display: none; }
  #left-dock:not(.notebook-sheet-expanded) #guide-action { min-height: 44px; }
  #left-dock:not(.notebook-sheet-expanded) #notebook-host {
    flex: 0 0 auto;
    min-height: 0;
    overflow: visible;
  }
  #notebook-host .notebook:not([data-sheet]),
  #notebook-host .notebook[data-sheet="compact"] {
    max-height: 76px;
    min-height: 76px;
    padding: 8px 10px;
    overflow: hidden;
  }
  #notebook-host .notebook:not([data-sheet]) .book-head,
  #notebook-host .notebook[data-sheet="compact"] .book-head {
    min-height: 58px;
    flex-wrap: nowrap;
    gap: 8px;
    padding: 0;
    border: 0;
  }
  #notebook-host .notebook:not([data-sheet]) .book-kicker,
  #notebook-host .notebook:not([data-sheet]) .book-body,
  #notebook-host .notebook:not([data-sheet]) .book-actions,
  #notebook-host .notebook:not([data-sheet]) .alert-marks,
  #notebook-host .notebook[data-sheet="compact"] .book-kicker,
  #notebook-host .notebook[data-sheet="compact"] .book-body,
  #notebook-host .notebook[data-sheet="compact"] .book-actions,
  #notebook-host .notebook[data-sheet="compact"] .alert-marks { display: none; }
  #notebook-host .notebook:not([data-sheet]) .book-title,
  #notebook-host .notebook[data-sheet="compact"] .book-title {
    font-size: 16px;
    line-height: 1.2;
  }
  #notebook-host .notebook:not([data-sheet]) .notebook-sheet-toggle,
  #notebook-host .notebook[data-sheet="compact"] .notebook-sheet-toggle {
    min-width: 44px;
    min-height: 44px;
    padding-inline: 10px;
    white-space: nowrap;
  }
  #left-dock.notebook-sheet-expanded #notebook-host,
  #left-dock.notebook-sheet-expanded #notebook-host .notebook[data-sheet="expanded"] { min-height: 0; }
  #left-dock.notebook-sheet-expanded .notebook[data-sheet="expanded"] {
    max-height: 100%;
    padding: 12px;
  }
  #left-dock.notebook-sheet-expanded .notebook[data-sheet="expanded"] .book-body { overflow-y: auto; overscroll-behavior: contain; }
  #left-dock .link-btn,
  #left-dock .btn,
  #left-dock .notebook button { min-width: 44px; min-height: 44px; }
}

/* A short landscape viewport keeps the colony visible until a player explicitly
   opens the one sheet. The build strip remains scrollable rather than clipped. */
@media (max-height: 520px) and (orientation: landscape) {
  #side.mobile-sheet.mobile-sheet-collapsed { max-height: 52px; }
  #buildbar { --mobile-build: 80px; }
  .buildbar-row { min-height: 80px; }
}
