/*
 * Phone and tablet layout. Loaded last, after matrix/dashboard/brand.
 * Goals: no sideways scrolling, touch targets of at least 40 px, the synoptic and the tables
 * re-flowed for a narrow column, the page scrolls with one finger everywhere.
 */

/* ------------------------------------------------------------ synoptic phone layout (hidden on wide screens) */
.pm-synm { display: none; }
.pm-hint-touch { display: none; }
@media (pointer: coarse) {
  .pm-hint-mouse { display: none; }
  .pm-hint-touch { display: inline; }
}

.pm-synm { flex-direction: column; gap: 8px; margin: 8px 0 12px; }
.pm-synm-row { display: grid; grid-template-columns: minmax(0, 1fr) 24px minmax(0, 1fr); align-items: center; gap: 4px; }
.pm-synm-node {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 56px; padding: 10px 12px; text-align: left;
  border-radius: var(--mx-radius-control); border: 2px solid var(--mx-neutral-line-border-strong);
  background: var(--pm-surface); color: var(--pm-text); font: inherit; cursor: pointer;
}
.pm-synm-icon { font-size: 22px; color: var(--pm-muted); flex: none; }
.pm-synm-icon.is-spinning { animation: pm-spin 1.2s linear infinite; }
.pm-synm-text { display: flex; flex-direction: column; min-width: 0; }
.pm-synm-name { font-size: 13px; line-height: 18px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pm-synm-status { font-size: 12px; line-height: 16px; color: var(--pm-secondary); }
.pm-synm-node.state-run { border-color: var(--pm-ok); background: var(--pm-ok-bg); }
.pm-synm-node.state-run .pm-synm-icon, .pm-synm-node.state-run .pm-synm-status { color: var(--pm-ok-text); }
.pm-synm-node.state-info { border-color: var(--mx-info-solid); background: var(--mx-info-canvas); }
.pm-synm-node.state-info .pm-synm-status { color: var(--mx-info-text); }
.pm-synm-node.state-alarm { border-color: var(--pm-crit); background: var(--pm-crit-bg); }
.pm-synm-node.state-alarm .pm-synm-icon, .pm-synm-node.state-alarm .pm-synm-status { color: var(--pm-crit-text); font-weight: 700; }
.pm-synm-node.state-unknown { border-style: dashed; color: var(--pm-muted); }
.pm-synm-arrow, .pm-synm-down { display: flex; align-items: center; justify-content: center; gap: 4px; color: var(--mx-neutral-line-border-strong); font-size: 11px; font-style: italic; }
.pm-synm-arrow .material-symbols-outlined, .pm-synm-down .material-symbols-outlined { font-size: 18px; font-style: normal; }
.pm-synm-arrow.is-active, .pm-synm-down.is-active { color: var(--mx-info-solid); }
.pm-synm-split { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.pm-synm-branch { display: flex; flex-direction: column; gap: 8px; }
.pm-synm-note { text-align: center; font-size: 11px; font-style: italic; color: var(--pm-muted); }
@media (prefers-reduced-motion: reduce) { .pm-synm-icon.is-spinning { animation: none; } }

@media (max-width: 720px) {
  .pm-syn-scroll { display: none; }
  .pm-synm { display: flex; }
  .pm-syn-legend .pm-syn-key { display: none; }
}

/* ------------------------------------------------------------ phones */
@media (max-width: 640px) {
  /* header: the environment badge is for developers, keep room for language + theme */
  .pm-env { display: none; }
  .pm-userlinks { gap: 8px; }
  .pm-lang-btn { height: 36px; }
  .pm-theme-toggle { width: 40px; height: 40px; }

  /* page head */
  .pm-pagehead { align-items: stretch; }
  .pm-pagehead .pm-btn { justify-content: center; width: 100%; }
  .pm-pagehead-title p { font-size: 13px; }

  /* status line: pill on its own row, then the three numbers side by side */
  .pm-statusbar { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; padding: 12px 16px; }
  .pm-statusbar > .pm-pill { grid-column: 1 / -1; justify-self: start; }
  .pm-statusbar-item.pm-grow { grid-column: 1 / -1; }

  /* measure cards two per row */
  .pm-grid-analog { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .pm-tile-analog { padding: 12px; }
  .pm-tile-analog .pm-tile-value { font-size: 20px; line-height: 26px; }
  .pm-tile-foot { font-size: 11px; }

  /* touch targets: at least 40 px */
  .pm-chip { font-size: 12px; }
  .pm-chip-remove { width: 40px; height: 40px; }
  /* periods: one row that scrolls sideways instead of three rows */
  .pm-segmented { display: flex; flex-wrap: nowrap; width: 100%; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity; }
  .pm-segmented::-webkit-scrollbar { display: none; }
  .pm-segmented button { flex: none; min-height: 40px; justify-content: center; padding: 6px 14px; white-space: nowrap; scroll-snap-align: center; }
  .pm-btn-sm { min-height: 40px; }
  .pm-sig { min-height: 44px; font-size: 14px; }
  .pm-link { display: inline-flex; align-items: center; min-height: 40px; }
  .pm-chart-controls { width: 100%; }
  .pm-chart-bar { gap: 8px; }
  /* chips and actions: chips first, then a compact row of actions on the right */
  .pm-chart-toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .pm-chips { gap: 6px 12px; }
  .pm-chip { min-height: 36px; padding: 6px 10px; gap: 6px; }
  .pm-chipgroup { gap: 6px; }
  .pm-chart-bar:has(.pm-focus-note[hidden]) { display: none; } /* the hint is hidden on phones: no empty row */
  .pm-chart-actions { margin-left: 0; justify-content: flex-end; }
  .pm-chart-actions .pm-btn { flex: none; justify-content: center; min-height: 40px; }
  .pm-chart-actions .pm-btn-label { display: none; }
  .pm-custom { flex-direction: column; align-items: stretch; }
  .pm-custom input { width: 100%; height: 44px; }
  .pm-custom .pm-btn { justify-content: center; min-height: 44px; }

  /* tables become stacked cards: one event / one variable per card, labels from data-label */
  .pm-table-wrap { margin: 0; overflow: visible; }
  .pm-table-cards thead { display: none; }
  .pm-table-cards, .pm-table-cards tbody { display: block; width: 100%; }
  .pm-table-cards tr {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 12px;
    padding: 12px 0; border-bottom: 1px solid var(--pm-hairline);
  }
  .pm-table-cards tr:last-child { border-bottom: 0; }
  .pm-table-cards td { display: block; padding: 0 !important; border: 0 !important; text-align: left !important; white-space: normal; font-size: 13px; }
  .pm-table-cards td[data-label]::before {
    content: attr(data-label); display: block; font-size: 11px; line-height: 14px; font-weight: 500; color: var(--pm-muted);
  }
  .pm-table-cards td:first-child:not([data-label]), .pm-table-cards td.pm-cell-main { grid-column: 1 / -1; font-weight: 700; font-size: 14px; }
  .pm-table-cards td.pm-cell-main::before { display: none !important; }
  .pm-row-active td { background: transparent; }
  .pm-table-cards tr.pm-row-active { background: var(--pm-crit-bg); border-radius: var(--mx-radius-item); padding: 12px; }
  .pm-stats-table { background: transparent; }
  .pm-stats-table tr { padding: 12px; background: var(--pm-surface-2); border-radius: var(--mx-radius-control); margin-bottom: 8px; border: 0; grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .pm-stats-table tr td:first-child { grid-column: 1 / -1; }
  .pm-stats-grid { overflow: visible; }

  /* synoptic card and legend */
  .pm-syn-card { padding: 16px; }
  .pm-syn-legend { width: 100%; }
}

/* very narrow phones (320-360 px) */
@media (max-width: 360px) {
  .pm-grid-analog { grid-template-columns: 1fr; }
  .pm-synm-row { grid-template-columns: 1fr; }
  .pm-synm-arrow { transform: rotate(90deg); height: 16px; }
  .pm-statusbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* phone refinements */
.pm-synm-name { white-space: normal; overflow: visible; text-overflow: clip; overflow-wrap: anywhere; }
@media (max-width: 640px) {
  /* state statistics: the share column holds a meter + value, give it more room */
  .pm-stats-table tr:has(.pm-meter) { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr) minmax(0, 1fr); }
  .pm-stats-table .pm-meter { width: 48px; }
  .pm-table-cards td[data-label]::before { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}
/* no sticky hover backgrounds after a tap on touch screens */
@media (hover: none) {
  .pm-table tbody tr:hover td, button.pm-sig:hover, .pm-chip:hover, .pm-segmented button:hover { background: inherit; }
  .pm-chip.is-active:hover { background: var(--mx-neutral-surface-solid); }
  .pm-segmented button.is-active:hover { background: var(--pm-surface); }
  button.pm-tile:hover { box-shadow: var(--mx-elevation-1); }
}
/* phones: the chart runs into the card padding, every pixel goes to the plot */
@media (max-width: 640px) {
  #pm-chart { margin-inline: -8px; width: calc(100% + 16px) !important; }
}
/* phones: event cards keep the notes button at the end of the card */
@media (max-width: 640px) {
  .pm-table-cards td.pm-cell-notes { grid-column: 1 / -1; width: auto; text-align: left !important; }
  .pm-alarm { flex-wrap: nowrap; }
}
.pm-synm-node.state-warning { border-color: var(--pm-warn); background: var(--pm-warn-bg); }
.pm-synm-node.state-warning .pm-synm-icon, .pm-synm-node.state-warning .pm-synm-status { color: var(--pm-warn-text); font-weight: 700; }
