/*
  device-shell.css — mobile-app-design-fundamental
  Frames the app in a simple phone on desktop and gets out of the way on a phone.
  Markup (static, in index.html):

    <div class="dsh" data-device-shell data-width="412" data-height="915">
      <div class="dsh-screen">
        <div id="app" data-app-root></div>
      </div>
    </div>

  The shell is the only code allowed to read the browser viewport. The app sizes itself against
  [data-app-root] (a size container) and reads its insets from --app-inset-top / --app-inset-bottom.
*/

:root {
  --dsh-w: 412px;            /* logical screen size, set from data-width / data-height */
  --dsh-h: 915px;
  --dsh-bezel: 12px;
  --dsh-radius: 48px;
  --dsh-scale: 1;            /* set by device-shell.js in frame mode */
  --dsh-status-h: 40px;
  --dsh-gesture-h: 24px;
  --dsh-desk: #e8e6e1;
  --dsh-body: #111214;
  --dsh-ink: #1b1b1f;        /* status bar and gesture handle on a light screen */

  /* insets the app reads; native mode takes the device's own */
  --app-inset-top: env(safe-area-inset-top, 0px);
  --app-inset-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root { --dsh-desk: #1c1c1f; }
  /* a thin rim so the dark phone body still reads against a dark desk */
  html[data-shell="frame"] .dsh { outline: 1px solid rgb(255 255 255 / 0.12); outline-offset: -1px; }
  /* status bar follows the scheme unless the app sets a tone (DeviceShell.setStatusBar) or pins light */
  html:not([data-statusbar]):not([data-theme="light"]) { --dsh-ink: #f4f4f6; }
  /* the modern-web-guidance toggle pins it with <meta name="color-scheme">; data-theme wins over it,
     as in tokens.css. Own rules: a browser without :has() drops only these */
  html:not([data-statusbar], [data-theme]):has(> head > meta[name="color-scheme"][content="light"]) { --dsh-ink: #1b1b1f; }
}

html[data-theme="dark"]:not([data-statusbar]),
html[data-statusbar="light"] { --dsh-ink: #f4f4f6; }
html:not([data-statusbar], [data-theme]):has(> head > meta[name="color-scheme"][content="dark"]) { --dsh-ink: #f4f4f6; }

/* ---------------------------------------------------------------- shared */

html[data-shell] body { margin: 0; }

[data-app-root] {
  position: relative;
  inline-size: 100%;
  block-size: 100%;
  overflow: hidden;
  /* a size container: the app sizes with cqw/cqh and @container app (...) instead of the viewport */
  container: app / size;
  /* layout containment makes it the containing block of position: fixed descendants, so bars,
     sheets and toasts stay inside the phone in both modes (container-type alone does not do this) */
  contain: layout;
}

/* no scrollbars anywhere in the app, as on a phone, in the desktop frame too: every scroller (the
   root, screen bodies, sheets, chip rows, chat lists) still scrolls by touch, wheel, keyboard and the
   frame's mouse-drag; cut-off content or a fade shows there is more (modern-web-guidance
   scrollability-affordance-hints). The ::-webkit-scrollbar fallback only where scrollbar-width is missing */
[data-app-root],
[data-app-root] * { scrollbar-width: none; }
@supports not (scrollbar-width: none) {
  [data-app-root]::-webkit-scrollbar,
  [data-app-root] ::-webkit-scrollbar { display: none; }
}

/* text is not selectable anywhere in the app by default: a long-press or drag never starts a text
   selection (or iOS's callout menu), as in a native app. Fields stay editable and selectable (iOS
   cannot type into a field under an inherited -webkit-user-select: none). Content the project chose
   to make copyable (DESIGN.md "Text selection") opts back in with [data-selectable]; controls and
   drag regions inside it stay unselectable. Zero specificity: a base the app may override */
:where([data-app-root]),
:where([data-app-root]) * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
:where([data-app-root]) :where(input, textarea, [contenteditable]:not([contenteditable="false"]),
  [data-selectable], [data-selectable] *) {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}
:where([data-app-root] [data-selectable]) :where(button, [role="button"], [role="tab"], [role="menuitem"], [role="option"],
  [role="switch"], [role="checkbox"], [role="radio"], summary, nav, [role="navigation"], [role="tablist"], label,
  [data-shell-nodrag]),
:where([data-app-root] [data-selectable]) :where(button, [role="button"], [role="tab"], [role="menuitem"], [role="option"],
  [role="switch"], [role="checkbox"], [role="radio"], summary, nav, [role="navigation"], [role="tablist"], label,
  [data-shell-nodrag]) :where(:not(input, textarea)) {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* ---------------------------------------------------------------- native: a real phone */

html[data-shell="native"] .dsh,
html[data-shell="native"] .dsh-screen {
  position: relative;
  inline-size: 100%;
  block-size: 100dvh;
}

html[data-shell="native"] .dsh-statusbar,
html[data-shell="native"] .dsh-gesture { display: none; }

/* a tablet in hand is native too, but the app is phone-sized wherever it runs: on a wide viewport
   it keeps a phone-width column, centred on the desk colour (the shell may read the viewport) */
@media (width > 600px) {
  html[data-shell="native"] body { background: var(--dsh-desk); }
  html[data-shell="native"] .dsh-screen {
    max-inline-size: 30rem;
    margin-inline: auto;
  }
  /* top-layer UI centres on the viewport; keep it over the column (same box as the app).
     Popovers at zero specificity (the whole selector in :where), so an anchored one keeps its own insets */
  html[data-shell="native"] dialog:is(:modal, [data-dsh-closing]),
  :where(html[data-shell="native"] [popover]:is(:popover-open, .\:popover-open, [data-dsh-closing])) {
    left: var(--dsh-screen-left, 0px);
    right: var(--dsh-screen-right, 0px);
  }
  :where(html[data-shell="native"]) :where(dialog:is(:modal, [data-dsh-closing])) { max-inline-size: calc(100cqi - 2em - 6px); }
}

/* ---------------------------------------------------------------- frame: desktop */

html[data-shell="frame"] {
  --app-inset-top: var(--dsh-status-h);
  --app-inset-bottom: var(--dsh-gesture-h);
}

html[data-shell="frame"] body {
  min-block-size: 100dvh;
  overflow: hidden;
  background: var(--dsh-desk);
}

html[data-shell="frame"] .dsh {
  /* fixed and centred, scaled as one piece: the ratio never changes, the phone only gets smaller */
  position: fixed;
  left: 50%;
  top: 50%;
  box-sizing: content-box;
  inline-size: var(--dsh-w);
  block-size: var(--dsh-h);
  padding: var(--dsh-bezel);
  border-radius: var(--dsh-radius);
  background: var(--dsh-body);
  box-shadow: 0 32px 80px rgb(0 0 0 / 0.22), 0 8px 24px rgb(0 0 0 / 0.12);
  transform: translate(-50%, -50%) scale(var(--dsh-scale));
  transform-origin: 50% 50%;
}

html[data-shell="frame"] .dsh-screen {
  position: relative;
  inline-size: 100%;
  block-size: 100%;
  overflow: clip;
  border-radius: calc(var(--dsh-radius) - var(--dsh-bezel));
  background: Canvas;
}

/* status bar: time left, signal / wifi / battery right; drawn over the app, which pads by
   --app-inset-top exactly like it pads for a real status bar */
.dsh-statusbar {
  position: absolute;
  inset: 0 0 auto;
  z-index: 2147483000;
  block-size: var(--dsh-status-h);
  padding-inline: 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--dsh-ink);
  font: 500 14px/20px system-ui, "Roboto", sans-serif;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  user-select: none;
}
.dsh-statusbar__icons { display: flex; gap: 4px; align-items: center; }
.dsh-statusbar svg { display: block; inline-size: 16px; block-size: 16px; fill: currentColor; }

.dsh-gesture {
  position: absolute;
  inset: auto 0 0;
  z-index: 2147483000;
  block-size: var(--dsh-gesture-h);
  display: grid;
  place-items: center;
  pointer-events: none;
}
.dsh-gesture::before {
  content: "";
  inline-size: 108px;
  block-size: 4px;
  border-radius: 4px;
  background: var(--dsh-ink);
  opacity: 0.6;
}

/* ---------------------------------------------------------------- frame: top layer
   A modal <dialog> (showModal) and an open [popover] render in the top layer: out of the phone's
   transform and clip, positioned against the browser viewport. device-shell.js publishes the
   screen's drawn rect (--dsh-screen-left/-top/-width/-height, plus -right/-bottom insets), and the
   rules below put the layer back over the phone.

   Approach: the insets make the layer's containing area (its inset-modified containing block) the
   phone screen exactly, and `zoom` (not `scale`) draws its contents at the phone's scale. Zoom
   changes layout, so the app's own placement keeps working unchanged: margin: auto centres a dialog
   on the phone, margin-block-end: 0 docks a bottom sheet to the phone's bottom edge, and 100% /
   max-inline-size resolve against the phone screen, the same as on a real phone. Zoom also
   multiplies the element's own px lengths, insets included, hence the division by the scale.
   (A full-screen transparent dialog box with scale would also fit, but forces every app to place
   an inner wrapper and breaks closedby="any", since clicks land on the dialog, not the backdrop.)

   One catch: a top-layer element's containing block is still the browser viewport, so its own % sizes
   (inline-size: 100%) resolve against the desktop window, not the phone. Size top-layer UI in
   container units of the app root instead (inline-size: 100cqi, max-block-size: 100cqb), which is
   the skill's rule anyway; they work the same in native mode. The UA's % max sizes are replaced
   below with the same values in cq units, at zero specificity.

   App rules this implies: place dialogs and sheets with margin, never with inset / top / bottom;
   size them in cqi / cqb, never %.
   Dialogs take these insets at a strong specificity (0,2,2), on purpose: confining a modal to the
   phone is the shell's job, and a .drawer { inset: 0 } or dialog.sheet[open] must not throw it back
   over the desk (the rule above: place with margin). Only position, zoom and inset are taken, all
   three needed for that; the max sizes are at zero specificity below. Popovers take the whole rule
   at zero specificity (the html[data-shell] part inside :where too), so an anchored menu or tooltip
   whose own single-class rule sets inset keeps it (it then places itself, against the window).
   Exit animations: close() / hidePopover() drop :modal / :popover-open on the first frame while
   an allow-discrete exit still draws the element. device-shell.js marks it [data-dsh-closing]
   until its animations finish, so it leaves from the phone, not from the desktop window.
   .\:popover-open is the class the popover polyfill sets where :popover-open is missing.
   The backdrop is clipped to the screen's rounded corners; the layer itself is not clipped, so a
   sheet's square bottom corners can show a few px past the screen's curve. Native mode: unchanged. */

html[data-shell="frame"] dialog:is(:modal, [data-dsh-closing]),
:where(html[data-shell="frame"] [popover]:is(:popover-open, .\:popover-open, [data-dsh-closing])) {
  position: fixed;
  zoom: var(--dsh-scale);
  inset:
    calc(var(--dsh-screen-top, 0px) / var(--dsh-scale))
    calc(var(--dsh-screen-right, 0px) / var(--dsh-scale))
    calc(var(--dsh-screen-bottom, 0px) / var(--dsh-scale))
    calc(var(--dsh-screen-left, 0px) / var(--dsh-scale));
}

/* the UA's max sizes, against the phone (app root) instead of the desktop viewport; any app rule wins */
:where(html[data-shell="frame"]) :where(dialog:is(:modal, [data-dsh-closing])) {
  max-inline-size: calc(100cqi - 2em - 6px);
  max-block-size: calc(100cqb - 2em - 6px);
}

/* the scrim covers the phone screen only, not the desk, with the screen's corners. ::backdrop
   inherits its element's effective zoom (getComputedStyle still reports zoom: 1), so its lengths
   are divided by the scale like the insets above; in logical px the radius is simply the screen's */
html[data-shell="frame"] dialog::backdrop,
html[data-shell="frame"] [popover]::backdrop {
  position: fixed;
  inset: auto;
  left: calc(var(--dsh-screen-left, 0px) / var(--dsh-scale));
  top: calc(var(--dsh-screen-top, 0px) / var(--dsh-scale));
  inline-size: var(--dsh-w);
  block-size: var(--dsh-h);
  border-radius: calc(var(--dsh-radius) - var(--dsh-bezel));
}

/* ---------------------------------------------------------------- mouse as a finger */

/* a soft grey dot like the browser's touch emulation; darker while pressed */
html[data-shell="frame"] .dsh-screen,
html[data-shell="frame"] .dsh-screen * {
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32'%3E%3Ccircle cx='16' cy='16' r='12' fill='%23000' fill-opacity='.18' stroke='%23fff' stroke-opacity='.9' stroke-width='2'/%3E%3C/svg%3E") 16 16, default !important;
}
html[data-shell="frame"] .dsh-screen.dsh-pressing,
html[data-shell="frame"] .dsh-screen.dsh-pressing * {
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32'%3E%3Ccircle cx='16' cy='16' r='10' fill='%23000' fill-opacity='.34' stroke='%23fff' stroke-opacity='.9' stroke-width='2'/%3E%3C/svg%3E") 16 16, default !important;
}

/* no text selection while a drag scrolls, as on a phone, even in [data-selectable] content: there,
   selection is for a deliberate hold, not a scroll */
html[data-shell="frame"] .dsh-screen.dsh-dragging,
html[data-shell="frame"] .dsh-screen.dsh-dragging * { user-select: none; -webkit-user-select: none; }
