/* css/mobile.css — Phone support. Loaded LAST (see css/style.css) so these rules win ties. The design template is a
   phone held sideways: 800 x 360 CSS px (an iPhone 13 is 844 x 390 in landscape; the rest is Safari's bars and the
   notch). Preview it on a desktop with tools/mobile-preview.html. Three independent switches:
     - orientation: landscape + max-height: 520px  -> a SHORT screen: compact header, tight margins, scrolling modals
     - max-width: 900px                            -> a NARROW screen: boxes may shrink below their desktop width
     - pointer: coarse                             -> a touch screen: bigger tap targets, no hover-only behaviour
   Portrait phones get the narrow rules plus a full-screen "turn your phone sideways" note (ui/orientation-hint-ui.js). */

/* iOS enlarges text in landscape unless told not to. */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* As a Home Screen web app the page draws under the status bar (black-translucent, see index.html), so keep the
   content below it. Zero in a normal browser tab and in landscape, where iOS hides the status bar. */
body {
  padding-top: env(safe-area-inset-top);
}

/* ---- Short landscape screens (a sideways phone) ---- */
@media (orientation: landscape) and (max-height: 520px) {
  body {
    margin: 0.25rem;
    /* Keep content out from under the notch / rounded corners (index.html sets viewport-fit=cover). */
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    /* A quarter of the side gap on top (never less than the status-bar inset). */
    padding-top: max(env(safe-area-inset-top), calc(env(safe-area-inset-left) / 4));
    /* And the same at the bottom, so the page doesn't end flush against the combat log. */
    padding-bottom: max(env(safe-area-inset-bottom), env(safe-area-inset-left));
  }

  /* One compact header row instead of three stacked ones. */
  .top-bar {
    gap: 0.25rem;
    margin-bottom: 0.25rem;
  }

  .top-bar-row {
    flex-wrap: nowrap;
    gap: 0.4rem;
  }

  #main-nav {
    flex-wrap: nowrap;
    flex: 1 1 0;
    min-width: 0;
  }

  /* Every tab the same width whatever its label ("???" or "Adventure"), so the row doesn't jump as tabs unlock. */
  #main-nav button {
    width: auto;
    min-width: 0;
    flex: 1 1 0;
    max-width: 84px;
    margin-right: 0.25rem;
    font-size: 0.75rem;
    padding: 0.25rem 0.1rem;
    min-height: 40px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* iOS draws plain buttons as pills at this size; a small radius reads as a tab/button on a small screen. */
  button {
    border-radius: 4px;
  }

  .header-right-group {
    flex-wrap: nowrap;
    gap: 0.4rem;
    flex: 0 0 auto;
  }

  #header-stats {
    gap: 0.4rem;
    font-size: 0.8rem;
  }

  #header-button-row {
    gap: 0.3rem;
  }

  /* Party > hero roster: only as wide as the Adventure view's left half (half the panel minus that half's 1.5rem
     right padding, see .split-half), so its boxes are the same size in both places instead of stretching across. */
  #party-panel > .slot-row {
    width: calc(50% - 1.5rem);
  }

  #header-button-row button {
    width: 40px;
    height: 40px;
  }

  #header-button-row button .icon {
    width: 20px;
    height: 20px;
  }

  .header-badge {
    top: -6px;
    right: -6px;
  }

  /* The three party buttons: a single tighter row. */
  .party-switcher-row {
    gap: 0.25rem;
    margin-bottom: 0.25rem;
  }

  .party-switcher-button {
    padding: 0.2rem 0.4rem;
  }

  .party-switcher-name {
    font-size: 0.8rem;
  }

  .party-switcher-status {
    font-size: 0.7rem;
  }

  .party-switcher-fatigue {
    font-size: 0.65rem;
    gap: 0.5rem;
  }

  /* Party / zone sub-tab rows: smaller buttons so the seven Party tabs fit one line. */
  .party-subnav-button {
    width: auto;
    flex: 1 1 auto;
    padding: 0.25rem 0.4rem;
    font-size: 0.8rem;
  }

  .zone-menu-subnav-button {
    padding: 0.25rem 0.5rem;
    font-size: 0.85rem;
  }

  /* The Adventure header box has a fixed 125px height on desktop; here it only needs its content. */
  .adventure-header-box {
    height: auto;
    padding: 0.4rem 0.6rem;
  }

  /* Smaller, FIXED-size hero/enemy boxes so a row of six fits the half-width column and the action menu fits a
     short screen. Everything a box can hold (name, HP bar, buff + debuff bubbles) is sized to fit inside it. */
  .slot-box {
    width: 100%;
    box-sizing: border-box;
    height: 82px;
    font-size: 0.65rem;
  }

  /* A row of six boxes is a grid, so together they use the whole width of their half instead of a fixed 58px each. */
  .slot-row {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 0.3rem;
  }

  .actor-slot-box {
    padding: 3px;
    gap: 2px;
  }

  .actor-name {
    font-size: 0.6rem;
    line-height: 1.15;
  }

  .hp-track {
    height: 12px;
  }

  /* Follower portraits: smaller where the screen is short. */
  .follower-detail-portrait {
    height: 100px;
  }

  .follower-box-portrait {
    top: -2.3rem;
    height: 7.5rem;
  }

  .unlock-intro-image-npc {
    height: 120px;
  }

  /* The hero card's half is narrow here: a smaller fixed label width, still the same for every line. */
  .level-line-label {
    flex-basis: 8.5em;
    width: 8.5em;
  }

  .hp-text {
    font-size: 0.55rem;
  }

  /* Buffs and debuffs stack (a side-by-side pair would be wider than the box) in tiny bubbles. */
  .status-indicator-row {
    flex-direction: column;
    align-items: stretch;
    flex-wrap: nowrap;
    gap: 1px;
    margin-top: 1px;
  }

  .status-indicator-badge {
    font-size: 0.5rem;
    padding: 0 2px;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: center;
  }

  /* Modals use the whole short screen and scroll inside instead of running off it. */
  .modal-overlay {
    box-sizing: border-box;
    padding: 0.25rem;
    padding-left: max(0.25rem, env(safe-area-inset-left));
    padding-right: max(0.25rem, env(safe-area-inset-right));
  }

  .modal-box {
    max-height: 100%; /* of the overlay, which follows the visible area (see ui/modal-utils.js) */
    overflow-y: auto;
    padding: 0.75rem 1rem;
  }

  /* Compact modal text so the box plus the on-screen keyboard fit a 360px-tall screen (the overlay itself is
     pinned to the visible area while the keyboard is up, see ui/modal-utils.js). */
  .modal-overlay {
    align-items: flex-start;
  }

  .modal-box h3 {
    margin-bottom: 0.25rem;
    font-size: 1rem;
  }

  .modal-box p {
    margin: 0.25rem 0;
    font-size: 0.8rem;
  }

  .modal-box textarea {
    height: 4.5rem;
    min-height: 3rem;
    font-size: 16px; /* under 16px iOS Safari zooms the page when a field is focused */
  }

  /* The Close/Cancel row stays visible while a long modal scrolls. */
  .modal-box .modal-actions {
    position: sticky;
    bottom: -0.75rem;
    padding: 0.35rem 0;
    background: var(--color-white);
  }

  /* Toasts hug the bottom edge instead of floating 4rem above it (a short screen has no room to spare, and a
     toast at the top would cover the tab row). */
  #toast-container {
    bottom: 0.5rem;
  }
}

/* ---- Narrow screens (any phone, either way up) ---- */
@media (max-width: 900px) {
  /* Automation rows: the switches drop under the description instead of squeezing it. */
  .automation-row {
    flex-wrap: wrap;
  }

  .automation-controls {
    max-width: 100%;
    justify-content: flex-start;
  }

  /* Boxes with a desktop fixed width may shrink to fit a half-width column. */
  .zone-box,
  .party-switcher-item,
  .patronage-box,
  .holy-palace-level-row {
    width: auto;
    max-width: 100%;
    min-width: 0;
    flex: 1 1 0;
  }

  /* The three skill cards: columns may shrink below their longest "Upgrade (cost)" button, which then wraps. */
  .skill-card-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
  }

  .skill-upgrade-button {
    white-space: normal;
    height: auto;
    min-height: 28px;
  }

  /* The 8-column Resources grid in a half-width column leaves each cell about 45px: icon and amount side by side
     left no room for the number, which was clipped to nothing. Stack the amount under its icon instead, so it gets
     the cell's whole width ("500", "1.2K", "+74"). The gear tab's 5-column list has room, so it keeps its row. */
  .resources-summary-list:not(.gear-resources-summary-list) .resources-summary-item {
    flex-direction: column;
    justify-content: center;
    gap: 0;
    padding: 2px 1px;
    line-height: 1.1;
  }

  .resources-summary-list:not(.gear-resources-summary-list) .resources-summary-item-amount {
    max-width: 100%;
    font-size: 0.65rem;
  }
}

/* ---- Touch screens ---- */
@media (pointer: coarse) {
  button {
    min-height: 32px;
    touch-action: manipulation; /* no double-tap-to-zoom delay */
  }

  /* A finger can't hit the 16px corner "A" of a quickbar button: it becomes a narrow full-height tab beside it. */
  .zone-quickbar-auto {
    position: static;
    width: 18px;
    height: auto;
    min-height: 32px;
    margin-left: 2px;
    border-radius: 4px;
  }

  .slot-box-clickable,
  .zone-box,
  .tutorial-box {
    touch-action: manipulation;
  }

  /* Hover-only decoration must not be the only way to read something (see ui/touch-tooltips-ui.js). */
  :not(.settings-toggle) > input[type="checkbox"] {
    width: 20px;
    height: 20px;
  }
}

/* The tap/long-press tooltip (ui/touch-tooltips-ui.js) and the mouse hover tooltip (ui/hover-tooltips-ui.js, shown 250 ms after
   the pointer settles; the browser's own title tooltip is never used). Both always defined; each only shown by its own input. */
#touch-tooltip,
#hover-tooltip {
  position: fixed;
  z-index: 3000;
  max-width: min(320px, calc(100vw - 16px));
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  background: var(--color-border-dark);
  color: var(--color-white);
  font-size: 0.8rem;
  line-height: 1.3;
  pointer-events: none;
  white-space: pre-line;
}

#hover-tooltip {
  z-index: 3500; /* above the modal windows (2000) */
}

#touch-tooltip[hidden],
#hover-tooltip[hidden] {
  display: none;
}

/* The "turn your phone sideways" screen (ui/orientation-hint-ui.js): covers the whole page on an upright phone, so only the
   zone art on <html> shows through, under a white veil. */
#orientation-hint {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  box-sizing: border-box;
  padding: max(1rem, env(safe-area-inset-top)) 1rem max(1rem, env(safe-area-inset-bottom));
  /* Solid white at the bottom, fading up so the zone art shows through near the top (like the zone fades). */
  background: linear-gradient(to top, rgba(255, 255, 255, 1) 35%, rgba(255, 255, 255, 0.7));
  color: var(--color-border-dark);
  text-align: center;
}

#orientation-hint[hidden] {
  display: none;
}

/* Everything but the note is hidden (not removed, so the game keeps running behind it). */
body.portrait-phone-note > *:not(#orientation-hint) {
  visibility: hidden;
}

.orientation-card {
  max-width: 420px;
  margin: auto 0;
}

.orientation-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.4rem;
}

.orientation-card h2.orientation-subheading {
  margin-top: 1.5rem;
  font-size: 1.15rem;
}

.orientation-card p {
  margin: 0 0 0.75rem;
  line-height: 1.4;
}

.orientation-install-block {
  margin: 0.5rem 0;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--color-border-light);
  border-radius: 8px;
  background: var(--color-white);
  text-align: left;
}

.orientation-install-block h3 {
  margin: 0 0 0.3rem;
  font-size: 1rem;
}

.orientation-install-block ol {
  margin: 0;
  padding-left: 1.3rem;
  line-height: 1.5;
}

.orientation-card p.orientation-note {
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

/* Quickbar icon buttons: a disabled one must read as clearly unavailable next to an usable one. */
.zone-quickbar-button:disabled {
  opacity: 0.4;
  filter: grayscale(100%);
  border-style: dashed;
  background: var(--color-bg-disabled);
}

.zone-quickbar-button:not(:disabled) {
  border-color: var(--color-active-border);
  background: var(--color-info-bg);
}

/* ---- Phone tweaks (narrow screens) ---- */
@media (max-width: 900px) {
  /* The unsold-loot pill is smaller so the shortcut buttons beside it stay on one line. */
  .party-loot-line {
    gap: 0.25rem;
    padding: 0 0.35rem;
    font-size: 0.7rem;
    white-space: nowrap;
  }

  .party-loot-line .help-hint {
    width: 0.9rem;
    height: 0.9rem;
    font-size: 0.6rem;
  }

  /* One line on a phone: the "Return safely to last safe zone (...)" button gives up its width first and ends in "..."
     (its full text is in its tooltip), so it never pushes the Loot pill onto a second line. */
  .zone-header-shortcut-row {
    gap: 0.25rem;
    flex-wrap: nowrap;
  }

  .zone-header-shortcut-button {
    white-space: nowrap;
    padding: 0 0.4rem;
    font-size: 0.7rem;
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .party-loot-line,
  .zone-quickbar {
    flex-shrink: 0;
  }

  /* The Battle view's hero card: the stat growth bars take whatever width is left instead of a fixed 160px, so the
     stats grid never runs off the screen (the Party > Hero view keeps its 160px bars). */
  .hero-detail-top-right {
    min-width: 0;
  }

  .hero-detail-top-right .stats-list {
    grid-template-columns: max-content max-content max-content max-content minmax(0, 1fr);
  }

  /* Party > Hero: a smaller portrait picker so it stays clear of the stats grid under it. */
  .portrait-picker {
    gap: 0.2rem;
  }

  .portrait-picker-image {
    height: 84px;
  }

  .portrait-picker-name {
    width: 4rem;
    font-size: 0.7rem;
  }
}

/* Home screen on a SHORT landscape phone (800 x 360): a smaller title and tighter slot buttons, so three saves plus
   Settings/Discord fit without scrolling. */
@media (orientation: landscape) and (max-height: 520px) {
  .game-title {
    font-size: 2.6rem;
  }

  .game-title-block {
    padding: 0.5rem 1.5rem;
  }

  .home-tagline {
    margin-bottom: 0.25rem;
    font-size: 0.8rem;
  }

  .home-content {
    gap: 0.4rem;
  }

  .home-slot-button {
    padding: 0.35rem 0.8rem;
  }
}
