/*
 * EXPERIENCE — phone layout (mobile launch rescue, 2026-10-22).
 *
 * Every rule here is scoped to html[data-xp-layout="mobile"], which
 * src/windows/mobileLayout.ts sets from MOBILE_LAYOUT_QUERY (narrow screens,
 * and touch screens on their side). The desktop layout never matches a rule
 * in this file.
 *
 * The phone layout is an OVERRIDE of the desktop one, not a second system:
 * windows and icons keep the inline geometry the window manager and the icon
 * grid wrote, and the rules below win over it with !important. Nothing is
 * rewritten, so leaving the phone layout puts every window and icon back
 * exactly where it was.
 *
 * Loaded last (after xp.css), so equal-specificity XP rules lose to it.
 */

html[data-xp-layout="mobile"] {
  /* The taskbar, plus the home-indicator strip on notched phones. */
  --xp-taskbar-height: calc(30px + env(safe-area-inset-bottom, 0px));
  /* What is actually on screen above the taskbar. --xp-viewport-height is the
     VISUAL viewport (mobileLayout.ts): it shrinks with the virtual keyboard
     and with a browser toolbar, where 100vh does not. */
  --xp-usable-height: calc(var(--xp-viewport-height, 100dvh) - var(--xp-taskbar-height));
}

/* With the keyboard up, the taskbar would sit under it anyway: give its strip
   to the window being typed into. */
html[data-xp-layout="mobile"][data-xp-keyboard="open"] {
  --xp-taskbar-height: 0px;
}

html[data-xp-layout="mobile"][data-xp-keyboard="open"] .taskbar,
html[data-xp-layout="mobile"][data-xp-keyboard="open"] .start-menu {
  display: none !important;
}

html[data-xp-layout="mobile"] .desktop {
  height: 100vh;
  height: 100dvh;
}

/* ------------------------------------------------------------- desktop icons */

/* The desktop grid becomes a vertically scrolling grid that always fits the
   width; the icons' inline left/top are kept for the desktop layout. */
html[data-xp-layout="mobile"] .icons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  grid-auto-rows: 100px;
  justify-items: center;
  align-content: start;
  box-sizing: border-box;
  height: calc(100% - var(--xp-taskbar-height));
  padding: 12px max(6px, env(safe-area-inset-right, 0px)) 12px max(6px, env(safe-area-inset-left, 0px));
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

html[data-xp-layout="mobile"] .icons .icon {
  position: relative !important;
  left: auto !important;
  top: auto !important;
  cursor: pointer;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html[data-xp-layout="mobile"] .icons .icon img {
  -webkit-user-drag: none;
}

/* ------------------------------------------------------------------- windows */

/* Every ordinary window fills the usable viewport: full width, from the top
   to the taskbar. Error dialogs (Close only) and Winamp's frameless skin keep
   their own size and only have to fit the width. */
html[data-xp-layout="mobile"] .desktop > .window:not(.winamp-frameless):has(> .title-bar button[aria-label="Minimize"]) {
  left: env(safe-area-inset-left, 0px) !important;
  top: 0 !important;
  width: calc(100% - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)) !important;
  height: var(--xp-usable-height) !important;
  min-width: 0 !important;
  max-width: none !important;
  min-height: 0 !important;
  max-height: none !important;
  box-sizing: border-box;
}

html[data-xp-layout="mobile"] .desktop > .window {
  max-width: calc(100vw - 8px);
}

html[data-xp-layout="mobile"] .window .resize-edge,
html[data-xp-layout="mobile"] .window .resize-handle {
  display: none !important;
}

/* The XP title bar stays, sized for a finger. The window cannot be moved or
   resized here, and it is always as large as it can be, so Maximize goes. */
html[data-xp-layout="mobile"] .window > .title-bar {
  height: 30px;
  cursor: default !important;
}

html[data-xp-layout="mobile"] .window > .title-bar .title-bar-left {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}

html[data-xp-layout="mobile"] .window > .title-bar .title-bar-text {
  min-width: 0;
  margin-right: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

html[data-xp-layout="mobile"] .window > .title-bar .title-bar-controls {
  display: flex;
  flex-shrink: 0;
}

html[data-xp-layout="mobile"] .window > .title-bar .title-bar-controls button {
  width: 30px;
  height: 30px;
  min-width: 30px;
  min-height: 30px;
  margin-left: 8px;
  background-size: 100% 100%;
}

/* By class: the label reads "Restore" on a window maximized before the switch. */
html[data-xp-layout="mobile"] .window > .title-bar .title-bar-controls .toggle-maximize {
  display: none;
}

/* Content scrolls inside its window and never drags the page with it. */
html[data-xp-layout="mobile"] .window .window-content,
html[data-xp-layout="mobile"] .window .window-body {
  overscroll-behavior: contain;
}

/* ------------------------------------------------------------------- taskbar */

html[data-xp-layout="mobile"] .taskbar {
  height: var(--xp-taskbar-height);
  box-sizing: border-box;
  padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

/* Running applications scroll sideways instead of being clipped. */
html[data-xp-layout="mobile"] .taskbar-buttons {
  height: 30px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}

html[data-xp-layout="mobile"] .taskbar-buttons::-webkit-scrollbar {
  display: none;
}

html[data-xp-layout="mobile"] .taskbar-button {
  flex: 0 0 auto;
  width: 120px;
  min-width: 0;
  height: 26px;
  overflow: hidden;
  white-space: nowrap;
  background-size: 100% 100% !important;
}

html[data-xp-layout="mobile"] .taskbar-button span {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Portrait phones: icon-only buttons, so several running apps fit at once.
   The icon's alt text is the button's name. */
@media (max-width: 480px) {
  html[data-xp-layout="mobile"] .taskbar-button {
    width: 44px;
    justify-content: center !important;
  }

  html[data-xp-layout="mobile"] .taskbar-button img {
    margin-right: 0 !important;
  }

  html[data-xp-layout="mobile"] .taskbar-button span {
    display: none;
  }
}

/* ---------------------------------------------------------------- start menu */

/* The header goes on top, as on a real XP Start menu, so both columns get the
   width; the menu scrolls when the phone is too short for it. */
html[data-xp-layout="mobile"] .start-menu {
  flex-direction: column;
  left: env(safe-area-inset-left, 0px);
  bottom: var(--xp-taskbar-height);
  box-sizing: border-box;
  width: min(400px, calc(100vw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)));
  max-height: calc(var(--xp-usable-height) - 4px);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

html[data-xp-layout="mobile"] .start-menu-header {
  flex: 0 0 auto;
  padding: 10px 12px;
}

html[data-xp-layout="mobile"] .start-menu-header span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

html[data-xp-layout="mobile"] .start-menu-items {
  flex: 0 0 auto;
  height: auto;
}

html[data-xp-layout="mobile"] .start-menu-left,
html[data-xp-layout="mobile"] .start-menu-right {
  flex: 1 1 50%;
  min-width: 0;
}

html[data-xp-layout="mobile"] .start-menu-item,
html[data-xp-layout="mobile"] .start-menu-all-programs {
  box-sizing: border-box;
  min-height: 44px;
  overflow-wrap: anywhere;
}

html[data-xp-layout="mobile"] .start-menu-all-programs {
  display: flex;
  align-items: center;
  padding: 0 8px;
}

/* -------------------------------------------------------------- context menu */

html[data-xp-layout="mobile"] .context-menu-item {
  padding: 10px 14px;
}

/* --------------------------------------------------------- desktop assistant */

/* On a phone every window fills the screen, so a character above windows
   would always be on top of somebody's content. It lives on the desktop
   instead: above the icons, below every window. */
html[data-xp-layout="mobile"] .clippy-agent,
html[data-xp-layout="mobile"] .clippy-balloon {
  z-index: 0;
}

/* ---------------------------------------------------------------------- games */

/* The EXPERIENCE duel's 16:9 arena letterboxes to a thin strip on a portrait phone.
   The game is untouched; this only says how to get the bigger view. */
@media (orientation: portrait) {
  html[data-xp-layout="mobile"] .robinhood-game .rh-stage-wrap {
    position: relative;
  }

  html[data-xp-layout="mobile"] .robinhood-game .rh-stage-wrap::after {
    content: "\21BB  Turn your phone sideways for a bigger arena";
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 16px;
    padding: 6px 10px;
    border: 2px solid rgba(255, 255, 255, 0.8);
    background: rgba(7, 22, 56, 0.85);
    color: #fff8a5;
    font: 700 12px/1.3 Tahoma, sans-serif;
    text-align: center;
    pointer-events: none;
  }
}
