/* Phone / small tablet — desktop styles unchanged above 768px */

@media (max-width: 768px), (max-width: 1100px) and (pointer: coarse) {
  html,
  body {
    font-size: 15px;
    overflow-x: hidden;
    overflow-y: auto;
    height: auto;
    min-height: 100%;
  }

  .app {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .main {
    flex: 1 1 auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
  }

  .panel {
    padding: 10px 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  button,
  .btn,
  .tab {
    min-height: 44px;
  }

  button,
  .btn {
    padding: 10px 14px;
    font-size: 14px;
  }

  button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
  }

  /* ── Top bar: stacked rows — actions never overlap tab strip ── */
  .race-topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--panel-hi);
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.4);
  }

  .race-topbar-inner {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: unset !important;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) 10px 8px;
    min-height: 0;
  }

  .tb-center {
    order: 1 !important;
    position: relative !important;
    left: auto !important;
    top: auto !important;
    transform: none !important;
    display: flex;
    flex-direction: column;
    align-items: center !important;
    justify-content: center;
    gap: 2px;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    text-align: center !important;
    padding: 0;
    pointer-events: auto;
  }

  .tb-left {
    order: 2 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    width: 100% !important;
    min-width: 0;
    gap: 8px;
  }

  .tb-header-actions {
    order: 1;
    position: static !important;
    display: flex !important;
    flex-direction: row !important;
    align-items: center;
    justify-content: flex-end;
    align-self: stretch !important;
    width: 100% !important;
    min-width: 0;
    flex-shrink: 0;
    gap: 8px;
    margin: 0;
    padding: 0;
    z-index: auto;
  }

  .tb-header-actions-sep {
    display: none;
  }

  .tb-fullscreen,
  .tb-signout {
    min-height: 40px;
    min-width: 44px;
    padding: 8px 10px;
    font-size: 9px;
    flex: 0 0 auto;
  }

  .race-topbar .tab-grid {
    order: 2;
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    padding: 4px 2px 6px;
    width: 100% !important;
    min-width: 0;
    grid-template-columns: unset !important;
    grid-template-rows: unset !important;
    box-sizing: border-box;
  }

  .tb-series-logo {
    flex: 0 0 auto;
    width: auto;
    max-width: min(58vw, 180px);
    max-height: 34px;
    margin: 0;
  }

  .tb-event {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 11px;
    text-align: left;
    margin-top: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
  }

  .tb-circuit {
    font-size: 10px;
    text-align: left;
    margin-top: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
    width: 100%;
  }

  .race-topbar .tab-grid::-webkit-scrollbar {
    display: none;
  }

  .race-topbar .tab-grid button.tab {
    flex: 0 0 auto;
    scroll-snap-align: center;
    min-width: 74px;
    min-height: 42px;
    padding: 8px 12px;
    font-size: 11px;
    letter-spacing: 0.03em;
    white-space: nowrap;
  }

  .tb-right {
    order: 3 !important;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    width: 100%;
  }

  .tb-card {
    padding: 6px 8px;
    min-height: 52px;
  }

  .tb-metric-val,
  .tb-fuel-val {
    font-size: 15px;
  }

  .tb-alert-btn {
    min-height: 48px;
    font-size: 10px;
    padding: 8px 6px;
  }

  /* ── Selection / data tables (touch-friendly) ── */
  .table-wrap,
  .dv-table-wrap {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    border: 1px solid var(--border);
    border-radius: 4px;
  }

  .dv-table-wrap {
    min-height: 160px;
    max-height: min(52vh, 440px);
  }

  .dv-table,
  table.data,
  table.timing-results-sheet,
  table.timing-data {
    font-size: 13px;
  }

  .dv-table th,
  .dv-table td,
  table.data th,
  table.data td,
  #fuel-table th,
  #fuel-table td,
  .pit-log-table th,
  .pit-log-table td,
  .drv-stint-table th,
  .drv-stint-table td {
    padding: 12px 10px;
    vertical-align: middle;
  }

  .dv-table tbody tr,
  table.data tbody tr,
  #fuel-table tbody tr,
  .pit-log-table tbody tr,
  .drv-stint-table tbody tr {
    min-height: 48px;
    cursor: pointer;
    -webkit-tap-highlight-color: rgba(0, 210, 190, 0.15);
  }

  .dv-table tbody tr.selected {
    background: rgba(10, 158, 142, 0.28);
    box-shadow: inset 4px 0 0 var(--cyan);
  }

  table.data tbody tr.selected td {
    outline: none;
    box-shadow: inset 4px 0 0 var(--cyan);
  }

  #fuel-table tbody tr.selected td {
    box-shadow: inset 4px 0 0 var(--cyan), inset 0 0 0 1px rgba(0, 210, 190, 0.35);
    outline: none;
  }

  .pit-log-table tbody tr.selected,
  .drv-stint-table tbody tr.selected {
    box-shadow: inset 4px 0 0 var(--cyan);
    background: rgba(10, 158, 142, 0.22);
  }

  /* Saved sessions: hide Key column; let Summary wrap */
  #dv-sessions-table th:first-child,
  #dv-sessions-table td:first-child {
    display: none;
  }

  #dv-sessions-table th:nth-child(2),
  #dv-sessions-table td:nth-child(2) {
    white-space: nowrap;
    width: 72px;
    max-width: 88px;
  }

  #dv-sessions-table td:nth-child(3) {
    white-space: normal;
    word-break: break-word;
    line-height: 1.35;
    min-width: 0;
  }

  .dv-table th {
    position: sticky;
    top: 0;
    z-index: 2;
  }

  /* ── Data viewer layout ── */
  .data-viewer-wrap {
    grid-template-columns: 1fr;
    min-height: 0;
    gap: 12px;
  }

  #dv-left {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    gap: 8px;
  }

  .dv-list {
    min-height: 108px;
    max-height: 150px;
    font-size: 14px;
    line-height: 1.4;
  }

  .dv-list option {
    padding: 8px 6px;
  }

  #dv-previews {
    grid-template-rows: repeat(4, minmax(110px, 150px));
  }

  .dv-preview-section textarea {
    font-size: 12px;
  }

  .dv-filter-row {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  .dv-filter-row label {
    min-width: 0;
  }

  .dv-actions,
  .dv-actions-row {
    flex-direction: column;
    gap: 10px;
  }

  .dv-actions button,
  .dv-actions-row button {
    width: 100%;
  }

  .dv-detail-wrap {
    max-height: 220px;
  }

  /* ── Forms & toolbars ── */
  .field-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .v5-toolbar,
  .tb-actions,
  .toolbar,
  .form-actions,
  .v5-toolbar .tb-field,
  .lt-form-grid .tb-field {
    gap: 10px;
  }

  .v5-toolbar .tb-field,
  .lt-form-grid .tb-field {
    flex: 1 1 100%;
    min-width: 0;
  }

  .form-row {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  .form-row label {
    min-width: 0;
  }

  .form-pressure-row {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .form-car-row {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .form-actions {
    flex-direction: column;
  }

  .form-actions button {
    width: 100%;
  }

  .toast {
    left: 12px;
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
    max-width: none;
  }

  /* ── Live timing: full-width stacked layout (not side-by-side) ──
     Keep page scroll on phones, but put the map block first so crews
     do not have to scroll past chrome / tables to find the track. */
  body.timing-tab-active,
  .main:has(#panel-timing.active),
  .main.main-locked {
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  #panel-timing.panel.panel-fill {
    overflow: visible !important;
    flex: 1 0 auto !important;
    max-height: none !important;
    min-height: auto !important;
    padding-top: 6px;
  }

  #panel-timing.panel-fill > .lt-root {
    overflow: visible !important;
    flex: 1 0 auto !important;
    min-height: auto !important;
  }

  .lt-root {
    overflow: visible !important;
    gap: 8px;
  }

  /* Map row first — chrome / session bars follow underneath. */
  .lt-root > .lt-main {
    order: -1;
  }

  .lt-root > .lt-link-bar {
    display: none;
  }

  .lt-form-grid {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px;
  }

  .lt-form-grid .tb-field {
    flex-direction: column !important;
    align-items: stretch !important;
    width: 100%;
    gap: 4px;
  }

  .lt-form-grid .tb-field span {
    white-space: normal;
  }

  .lt-form-grid .tb-field .select-ctl,
  .lt-form-grid .tb-field .input-ctl {
    width: 100%;
  }

  .lt-form-grid .tb-actions {
    margin-left: 0 !important;
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .lt-form-grid .tb-actions button {
    width: 100%;
  }

  .lt-form-grid .tb-status {
    justify-content: center;
    padding: 6px 0;
  }

  .lt-link-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .lt-screen-links {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 10px;
  }

  .lt-official-header {
    grid-template-columns: 1fr !important;
    gap: 6px;
    text-align: left;
  }

  .lt-official-title {
    text-align: left !important;
    white-space: normal;
    line-height: 1.25;
  }

  .lt-flag-badge {
    justify-self: start !important;
    min-width: 0;
    width: 100%;
    max-width: 220px;
  }

  .lt-official-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    line-height: 1.4;
  }

  .lt-screen-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    padding-bottom: 4px;
  }

  .lt-screen-bar::-webkit-scrollbar {
    display: none;
  }

  .lt-screen-btn {
    flex: 0 0 auto;
    min-height: 40px;
    padding: 8px 14px;
    font-size: 12px;
  }

  .lt-main,
  .lt-main.layout-overview,
  .lt-main.layout-tracker,
  .lt-main.layout-results,
  .lt-main.layout-panel-only {
    display: flex !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
    overflow: visible !important;
    flex: 0 0 auto !important;
    min-height: auto !important;
    gap: 10px;
  }

  .lt-map-host {
    order: 1;
    flex: 0 0 auto !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: min(52vh, 480px) !important;
    height: min(52vh, 480px) !important;
  }

  .lt-main.layout-tracker .lt-map-host {
    min-height: min(62vh, 560px) !important;
    height: min(62vh, 560px) !important;
  }

  .lt-pit-panel {
    order: 2;
    width: 100% !important;
    max-width: none !important;
    flex: 0 0 auto !important;
    align-self: stretch;
    min-height: 120px;
    max-height: 240px;
  }

  .lt-pit-table td,
  .lt-pit-table th {
    max-width: none;
    padding: 8px 6px;
  }

  .lt-screen-panels {
    order: 3;
    flex: 0 0 auto !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: auto !important;
    overflow: visible !important;
    gap: 10px;
  }

  .lt-main.layout-overview .lt-screen-panels {
    display: flex !important;
    flex-direction: column !important;
  }

  .lt-main.layout-overview #lt-screen-results,
  .lt-main.layout-overview #lt-screen-messages {
    flex: 0 0 auto !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow: visible !important;
    display: flex !important;
  }

  .lt-results-card,
  .lt-msg-card {
    min-height: 0;
  }

  #lt-screen-results .lt-results-wrap,
  #lt-screen-results .table-wrap {
    min-height: 220px;
    max-height: min(44vh, 420px);
  }

  #lt-screen-messages .lt-messages-wrap {
    min-height: 160px;
    max-height: min(36vh, 300px);
  }

  .lt-main.layout-panel-only .lt-screen-panel.active,
  .lt-main.layout-results .lt-screen-panel.active {
    min-height: 280px;
  }

  .lt-main.layout-panel-only .lt-screen-panel.active .table-wrap,
  .lt-main.layout-results .lt-results-wrap {
    min-height: 240px;
    max-height: min(50vh, 480px);
  }

  .lt-stats-official {
    overflow: visible;
    max-height: none;
  }

  .lt-stats-summary {
    grid-template-columns: 1fr !important;
    gap: 12px;
  }

  .lt-sheet-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .lt-sheet-field {
    width: 100%;
  }

  .lt-sheet-field .select-ctl,
  .lt-sheet-field .input-ctl {
    min-width: 0;
    width: 100%;
  }

  table.timing-results-sheet {
    font-size: 12px;
  }

  .lt-map-controls {
    top: 6px;
    right: 6px;
  }

  .lt-map-ctrl-body {
    width: min(260px, calc(100vw - 24px));
  }
}

@media (max-width: 640px) {
  .tb-series-logo {
    max-width: min(42vw, 140px);
    max-height: 28px;
  }

  .tb-event {
    font-size: 10px;
  }

  .tb-circuit {
    font-size: 9px;
  }

  .tb-right {
    grid-template-columns: 1fr 1fr;
  }

  .tb-right .tb-alert {
    grid-column: 1 / -1;
  }

  .wk-row0,
  .wk-row1 {
    flex-direction: column;
    align-items: stretch;
  }

  .fuel-top {
    gap: 10px;
  }

  .fuel-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .fuel-toolbar button {
    width: 100%;
  }

  .fuel-toolbar-sep {
    display: none;
  }

  .tele-layout {
    gap: 10px;
  }

  .tele-perf-list {
    max-height: none;
  }

  #dv-left {
    grid-template-columns: 1fr;
  }

  .lt-map-host {
    min-height: min(46vh, 400px) !important;
    height: min(46vh, 400px) !important;
  }

  .lt-main.layout-tracker .lt-map-host {
    min-height: min(55vh, 460px) !important;
    height: min(55vh, 460px) !important;
  }
}

/* Landscape phone */
@media (max-width: 1100px) and (pointer: coarse) and (orientation: landscape),
       (max-width: 768px) and (orientation: landscape) {
  .race-topbar-inner {
    gap: 6px;
    padding-top: max(4px, env(safe-area-inset-top));
  }

  .tb-series-logo {
    max-height: 26px;
  }

  .tb-right {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .tb-right .tb-alert {
    grid-column: auto;
  }

  .dv-table-wrap {
    max-height: min(38vh, 280px);
  }

  .lt-map-host {
    min-height: min(40vh, 360px) !important;
    height: min(40vh, 360px) !important;
  }

  .lt-main.layout-tracker .lt-map-host {
    min-height: min(48vh, 420px) !important;
    height: min(48vh, 420px) !important;
  }
}
