/* Phones and tablets. Loaded after every other stylesheet; nothing here applies to a desktop-sized window.
   Screens covered (static/js/mobile.js uses the same two conditions):
     * "stacked":  a phone or tablet held upright (or any window 700px wide or narrower): map on top, the two sidebars below it, one scrolling page;
     * "sideways": a touch screen held landscape: the desktop's three columns, with narrower sidebars (narrowest on a phone). */

/* ---------- every phone / tablet-sized screen ---------- */
@media (max-width: 700px), (max-width: 1366px) and (pointer: coarse) {
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  /* Nothing may make the page wider than the screen (a phone would zoom the whole page out to fit it): a tooltip near the edge is cut off instead. */
  html, body { overflow-x: hidden; overflow-x: clip; }
  body { overscroll-behavior: none; }
  .map-tooltip { max-width: min(18rem, 82vw); white-space: normal; }
  .topbar { gap: 0.35rem; padding: 0.4rem 0.6rem; flex-wrap: wrap; row-gap: 0.35rem; }
  .topbar .brand { font-size: 0.92rem; margin-right: 0; }
  .remote-group { gap: 0.4rem; }
  .calendar-btn { margin-right: 0; }
  .topbar .settings-btn, .topbar .fullscreen-btn { width: 2.1rem; height: 2.1rem; }
  .nav-dropdown-trigger, .polls-nav-link { font-size: 0.72rem; padding: 0.3rem 0.5rem; min-height: 2rem; white-space: nowrap; }
  .national-office-toggle { gap: 0.35rem; flex-wrap: wrap; }
  .topbar [class$="-topbar-label"], .topbar .state-name { font-size: 0.8rem; min-width: 0; }
  /* A menu opens as a panel under the top bar, the width of the screen, so it can never hang off an edge. */
  .topbar .nav-dropdown { position: static; }
  .topbar .nav-dropdown-menu { position: fixed; top: var(--topbar-height); left: 0.5rem; right: 0.5rem; margin-top: 0.15rem; min-width: 0; max-height: 70vh; overflow-y: auto; z-index: 3100; }
  .topbar .nav-dropdown-menu a, .topbar .nav-dropdown-menu button { font-size: 0.9rem; padding: 0.65rem 0.8rem; white-space: normal; }
  .settings-menu a, .settings-menu .settings-menu-item { padding: 0.65rem 0.8rem; }
  :root { --ticker-height: 1.8rem; }
  .ticker-item { font-size: 0.72rem; padding: 0 1rem; }
  .ticker-item::after { margin-left: 1rem; }

  /* A finger on the map scrolls the page until the map is zoomed in; then it moves the map. Two fingers always zoom it. */
  .app-svg { touch-action: pan-y; cursor: default; }
  .app-layout.map-zoomed .app-svg { touch-action: none; }

  /* Fields: 16px text keeps iPhones from zooming the page when one is tapped. */
  input[type="search"], input[type="text"], input[type="number"], select, textarea { font-size: 16px !important; max-width: 100%; }

  /* Plain pages (polls, poll grading, FAQ): narrow margins; wide tables scroll sideways inside their own box. */
  .faq-page, .polls-page, .poll-grading-page, .race-polling-page { margin: 0.75rem auto; padding: 0 0.75rem; max-width: 100%; }
  .race-polling-table-wrap, .poll-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
  .polls-filter-bar { gap: 0.6rem; }
  .polls-filter-group, .polls-filter-group select, .polls-filter-group input[type="search"], .poll-grading-search input[type="search"] { min-width: 0; width: 100%; }
}

/* ---------- upright phones: logo and buttons on the first row; country and menus below ---------- */
@media (max-width: 700px) {
  .topbar .brand { order: -3; }
  .topbar > .remote-group { order: -2; margin-left: auto; }
  .topbar::after { content: ""; order: -1; flex: 0 0 100%; height: 0; margin-top: -0.35rem; }
  .topbar .national-office-toggle:not(.hidden) { display: contents; }
}
@media (max-width: 480px) {
  .topbar { column-gap: 0.25rem; }
  .topbar .brand { font-size: 0.82rem; }
  .topbar > [id$="-topbar-label"] span { max-width: calc(100vw - 8rem); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .topbar > #nationalshell-topbar-label, .topbar > #poll-country-topbar-label { display: none; }     /* the U.S. menus need the row; the map says which country */
  .nav-dropdown-trigger, .polls-nav-link { font-size: 0.62rem; padding: 0.3rem 0.35rem; gap: 0.15rem; }
}

@media (max-width: 380px) {
  .topbar { padding-left: 0.4rem; padding-right: 0.4rem; column-gap: 0.2rem; }
  .nav-dropdown-trigger, .polls-nav-link { font-size: 0.57rem; padding: 0.3rem 0.25rem; }
}
@media (min-width: 701px) and (max-width: 900px) and (pointer: coarse) and (orientation: portrait) {
  .topbar { column-gap: 0.25rem; }
  .topbar .brand { font-size: 0.8rem; }
  .topbar > #nationalshell-topbar-label span, .topbar > #poll-country-topbar-label span { display: none; }
  .nav-dropdown-trigger, .polls-nav-link { font-size: 0.66rem; padding: 0.3rem 0.4rem; }
}

/* ---------- stacked: upright phones and tablets ---------- */
@media (max-width: 700px), (max-width: 1100px) and (pointer: coarse) and (orientation: portrait) {
  main { -webkit-overflow-scrolling: touch; }
  .app-layout,
  .app-layout.state-mode, .app-layout.country-mode, .app-layout.margin-map-mode, .app-layout.margin-map-mode.scenario-active,
  .app-layout.sleg-mode.sleg-chamber-level, .app-layout.germany-mode.germany-states-view, .app-layout.canada-mode:not(.canada-province-selected) {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-auto-rows: auto; align-content: start; height: auto; min-height: 100%; transition: none;
  }
  .app-layout > #map { grid-row: 1; grid-column: 1 / -1; width: 100%; min-width: 0; height: calc(100vh - var(--topbar-height) - var(--ticker-height)); height: calc(100svh - var(--topbar-height) - var(--ticker-height)); }
  /* With sidebars on screen the map takes the upper part and the page scrolls on to them. */
  .app-layout.state-mode > #map, .app-layout.country-mode > #map, .app-layout.margin-map-mode > #map { height: 58vh; height: 58svh; min-height: 300px; }
  .app-layout.germany-mode.germany-states-view > #map, .app-layout.canada-mode:not(.canada-province-selected) > #map {
    height: calc(100vh - var(--topbar-height) - var(--ticker-height)); height: calc(100svh - var(--topbar-height) - var(--ticker-height)); }
  .app-layout > .race-selector { grid-row: 2; grid-column: 1 / -1; }
  .app-layout > .county-panel { grid-row: 3; grid-column: 1 / -1; }
  .app-layout > .race-selector, .app-layout > .county-panel {
    width: auto; min-width: 0; height: auto; max-height: none; overflow: visible; border-left: 0; border-right: 0; border-top: 1px solid #d5d8de; padding: 0.75rem;
  }
  .app-layout .candidate-list-scroll { max-height: none !important; overflow: visible !important; }
  /* The sidebars cannot be folded away here: they are simply further down the page. */
  .app-layout .sidebar-toggle { display: none !important; }
  .race-selector button, .county-panel button { min-height: 2.4rem; }
}

/* Upright tablets have room for the two sidebars side by side under the map. */
@media (min-width: 701px) and (max-width: 1100px) and (pointer: coarse) and (orientation: portrait) {
  .app-layout.state-mode, .app-layout.country-mode, .app-layout.margin-map-mode, .app-layout.margin-map-mode.scenario-active { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important; }
  .app-layout.state-mode > #map, .app-layout.country-mode > #map, .app-layout.margin-map-mode > #map { height: 62vh; height: 62svh; }
  .app-layout > .race-selector { grid-column: 1; grid-row: 2; }
  .app-layout > .county-panel { grid-column: 2; grid-row: 2; border-left: 1px solid #d5d8de; }
  .app-layout.germany-mode.germany-states-view, .app-layout.canada-mode:not(.canada-province-selected), .app-layout.sleg-mode.sleg-chamber-level { grid-template-columns: minmax(0, 1fr) !important; }
}

/* ---------- sideways: touch screens held landscape ---------- */
@media (min-width: 701px) and (max-width: 1366px) and (pointer: coarse) and (orientation: landscape) {
  .app-layout { --left-col-width: 232px; --right-col-width: 264px; }
}
@media (min-width: 701px) and (max-height: 540px) and (pointer: coarse) and (orientation: landscape) {
  :root { --ticker-height: 1.5rem; }
  .app-layout { --left-col-width: 188px; --right-col-width: 212px; }
  .topbar { padding: 0.25rem 0.5rem; }
  .topbar .brand { font-size: 0.82rem; }
  .nav-dropdown-trigger, .polls-nav-link { font-size: 0.68rem; min-height: 1.8rem; padding: 0.2rem 0.45rem; }
  .topbar .settings-btn, .topbar .fullscreen-btn { width: 1.85rem; height: 1.85rem; }
  .topbar > #nationalshell-topbar-label span, .topbar > #poll-country-topbar-label span { display: none; }
  .app-layout > .race-selector, .app-layout > .county-panel { padding: 0.5rem; font-size: 0.82rem; overflow-y: auto; }
  .race-selector button, .county-panel button { font-size: 0.74rem; padding: 0.35rem 0.4rem; }
}
@media (pointer: coarse) and (orientation: landscape) and (max-height: 540px) and (max-width: 700px) {
  :root { --ticker-height: 1.5rem; }
}

/* ---------- what floats on the map ---------- */
@media (max-width: 700px), (pointer: coarse) and (max-height: 540px) {
  /* The drawing tools (pen, eraser, laser) need more room than a phone has. */
  .map-tools-panel { display: none !important; }
}
@media (max-width: 700px), (max-width: 1366px) and (pointer: coarse) {
  .back-to-world-btn { top: 0.4rem; left: 0.4rem; font-size: 0.7rem; padding: 0.35rem 0.55rem; }
  /* The seat-balance bar sits under the back button instead of on top of it. */
  .us-chamber-balance-dock, #state-legislature-balance-bar { top: 2.6rem !important; width: calc(100% - 0.8rem) !important; }
  #map:has(> .us-chamber-balance-dock:not(.hidden), > #state-legislature-balance-bar:not(.hidden)) > .map-tools-panel { top: 7.8rem; }
  .color-mode-toggle, .county-select-mode-toggle { flex-wrap: wrap; }
  .zoom-control { right: 0.4rem; bottom: 0.4rem; padding: 0.3rem; gap: 0.3rem; }
  .zoom-control button { font-size: 0.72rem; }
  .sidebar-toggle { top: 0.4rem; }
}
@media (max-width: 700px), (pointer: coarse) and (max-height: 540px) {
  .national-legend-panel, #margin-legend-panel, #country-key-panel, .cities-legend, .peru-legend, #swing-key { font-size: 0.62rem !important; max-width: 9.5rem !important; }
  .national-legend-row { white-space: normal !important; }
  .national-legend-panel .national-legend-body { max-height: 6.5rem; overflow-y: auto; }
  #country-key-panel .national-legend-body, #margin-legend-panel .margin-legend-body { max-height: 2.8rem; }
  .map-legends-stack { top: 0.4rem; right: 0.4rem; gap: 0.3rem; max-width: 9.5rem; }
  .app-layout.state-mode .map-legends-stack .cities-legend { display: none; }
}
