/* Phone overrides (<= 760px). Loaded after all other stylesheets so the desktop layout is untouched.
   Rules: nothing wider than the viewport, wide boards scroll in their own box, tabs become a
   swipeable strip, pop-ups stay on screen, controls get touch-sized targets. */
@media (max-width: 760px) {
  html, body { overflow-x: clip; max-width: 100vw; }
  img, svg, video, canvas { max-width: 100%; }
  .wrap, .wrap.wide, main { padding-left: 10px !important; padding-right: 10px !important; }
  .card { padding: 12px !important; border-radius: 12px; }
  h1 { font-size: 1.45rem; line-height: 1.2; word-break: break-word; }
  h2 { font-size: 1.15rem; }

  /* Fields: 16px text prevents iOS focus zoom; full width, touch-sized */
  input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { font-size: 16px !important; min-height: 40px; max-width: 100%; }
  button, .btn, .btn-sm, a.btn { min-height: 38px; }
  .btn-sm, button.btn-sm, a.btn-sm { padding: 6px 12px; }

  /* Tab bars: single horizontally scrolling strip instead of overflowing */
  .planbar { flex-wrap: wrap; gap: 6px; padding: 8px 10px; }
  .planbar-t { display: none; }
  /* Planner tabs collapse into a dropdown */
  .planbar #tabs { display: none; }
  #tabs-pick { display: block !important; width: 100%; font-weight: 700; }
  .planbar nav { width: 100%; min-width: 0; }
  .seg { display: flex; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; max-width: 100%; scroll-snap-type: x proximity; }
  .seg::-webkit-scrollbar { display: none; }
  .seg > button, .seg > a { flex: none; scroll-snap-align: start; white-space: nowrap; }
  .ptabs { scrollbar-width: none; }

  /* Page heads and toolbars wrap instead of overflowing */
  .plan-head, .row, .ev-tools, .form-row, .tb-zoom, .sc-tools, .ro-bar { flex-wrap: wrap; }
  .plan-head > * { min-width: 0; max-width: 100%; }
  .plan-head .row[style*="margin-left:auto"], .plan-head .row { margin-left: 0 !important; }
  .ev-tools form, .ev-tools input { max-width: 100%; }
  .ev-sep { display: none; }

  /* Two-column work areas stack */
  .planner, .sl-layout, .sl-top, .tri-top, .sc-layout, .tac-row, .pk-layout, .im-map, .cp-axis, .cp-row, .capline { grid-template-columns: 1fr !important; }
  .pool-col { position: static !important; max-height: 55vh; min-width: 0; width: 100%; }
  .pool-head, .pool-head .row { min-width: 0; flex-wrap: wrap !important; }
  .pool-head .row > * { flex: 1 1 auto; }
  .pool-head .spacer { display: none; }
  .rallies, .tri-groups, .sl-garrisons, .ro-cols, .hist, .def-heroes, .lay-legend, .mt-grid, .turrets { grid-template-columns: 1fr !important; }
  .sl-top .sl-loot { grid-template-columns: 1fr !important; }

  /* Wide boards scroll inside their own box; the page never scrolls horizontally */
  .coverage { grid-template-columns: repeat(10, minmax(30px, 1fr)); overflow-x: auto; max-width: 100%; }
  .sc-gridwrap, .tb-scroll, .md-table, .bb-plan, .jtimeline, .sync-list, .tri-table-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
  .card table:not(.sc-grid):not(.sc-tot) { display: block; overflow-x: auto; max-width: 100%; }
  .sync, .card.sync { min-width: 0 !important; width: 100% !important; max-width: 100%; }
  .sync-h { flex-wrap: wrap; }
  .sync-h .spacer { display: none; }

  /* Rally and player cards: compress inner grids */
  .pc.mini { grid-template-columns: minmax(0, 1fr) auto auto !important; }
  .pc.mini .av, .pc.mini .tgs { display: none; }
  .pool-col .pc { grid-template-columns: minmax(0, 1fr) !important; min-width: 0; max-width: 100%; }
  .pool-col .pc > * { min-width: 0; }
  .pc .sub { white-space: normal; overflow-wrap: anywhere; }
  .shift-edit .r { grid-template-columns: 1fr 1fr !important; }
  .tm-row { grid-template-columns: 1fr auto !important; }

  /* Keep pop-ups, menus and dialogs within the viewport */
  .k-dialog, .kdialog, dialog, .modal, .drop-menu, .sl-pick, .sb-online-panel, .menu-pop, [role="dialog"] {
    max-width: calc(100vw - 16px) !important; box-sizing: border-box; }
  .drop-menu, .sl-pick { left: 8px !important; right: 8px !important; width: auto !important; }
  .sl-pick { position: fixed !important; top: 70px !important; max-height: calc(100vh - 90px); overflow-y: auto; z-index: 60; }
  .toast, #toast { left: 8px !important; right: 8px !important; max-width: none !important; transform: none !important; }

  /* Swordland / Tri planners */
  .sl-row.sub { grid-template-columns: 26px minmax(0, 1fr) !important; }
  .sl-row.sub .sl-cover { grid-column: 2; }
  .sl-legion-tabs > button, .tri-legion-tabs > button { padding: 6px 10px; }
  .sl-map { max-width: 100%; }

  /* Sanctuaries: grid scrolls horizontally in its box; pills wrap */
  .sc-palette { gap: 6px; }
  .sc-chip { padding: 6px 11px; }

  /* Transfer desk */
  .td-layout, .td-grid { grid-template-columns: 1fr !important; }
  .td-pool { position: static !important; max-height: none; }
  .td-msg { max-width: 100%; }
  .tf-act { gap: 6px; }

  /* My profile gear tiles: always five per row */
  .mp-heroes { grid-template-columns: 1fr !important; }
}

/* --- Very small phones --- */
@media (max-width: 380px) {
  .wrap, .wrap.wide, main { padding-left: 6px !important; padding-right: 6px !important; }
  .card { padding: 10px !important; }
}
@media (max-width: 760px) {
  /* Discord bars: label + status on one line, each field and its Set button on its own line */
  .ev-tools { display: grid !important; grid-template-columns: auto minmax(0, 1fr); gap: 6px 8px; align-items: center; }
  .ev-tools > form, .ev-tools > .btn-gold, .ev-tools > button, .ev-tools > select, .ev-tools > a.btn-sm { grid-column: 1 / -1; }
  .ev-tools > .pill { justify-self: start; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
  .ev-tools > form { display: flex !important; gap: 6px; }
  .ev-tools > form input { flex: 1; min-width: 0; }
  .ev-tools > .muted.small { grid-column: auto; }
}

/* --- Touch devices --- */
/* Member lists scroll by touch. Cards are placed by tapping on touch screens, so a swipe over a
   card scrolls the list instead of starting a drag. */
@media (pointer: coarse) {
  .pool-col .pc { touch-action: pan-y; }
}
@media (max-width: 760px) {
  /* Give the list its own height so filters and search don't squeeze it */
  .pool-col { max-height: none !important; }
  .pool-col > .list { max-height: 60vh; min-height: 160px; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
}
@media (max-width: 760px) {
  /* Swordland planner: map fills the screen; building garrisons are listed below it */
  #tab-swordland .sl-top .sl-map, #tab-swordland .sl-pick { display: none !important; }
}
