/* ==========================================================================
   Phones and the installed app (PWA)
   1. No zooming        2. Bottom tab bar + "More" sheet
   3. Tables as cards   4. Screen-specific phone layouts
   Desktop (>= 992px) is unchanged.
   ========================================================================== */

/* ---------- 1. No zooming ---------- */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation; /* no double-tap zoom */
  overflow-x: clip;           /* nothing may widen the page, or phones zoom out / pan sideways */
}

/* iOS zooms into any field whose text is smaller than 16px, and the installed app stays zoomed. */
@media (max-width: 991.98px), (pointer: coarse) {
  .form-control,
  .form-select,
  .form-control-sm,
  .form-select-sm,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select,
  textarea { font-size: 16px !important; }
}

.mobile-tabbar { display: none; }
.more-sheet[hidden] { display: none !important; }

@media (max-width: 991.98px) {
  :root { --tabbar-height: 64px; --tabbar-gap: 10px; }

  /* ---------- 2. Bottom tab bar ---------- */
  .mobile-tabbar {
    position: fixed;
    left: var(--tabbar-gap);
    right: var(--tabbar-gap);
    bottom: calc(var(--tabbar-gap) + env(safe-area-inset-bottom));
    z-index: 1040;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    height: var(--tabbar-height);
    padding: 6px;
    border-radius: 22px;
    background: rgba(255, 255, 255, .9);
    backdrop-filter: saturate(180%) blur(18px);
    -webkit-backdrop-filter: saturate(180%) blur(18px);
    border: 1px solid rgba(226, 232, 240, .9);
    box-shadow: 0 16px 40px -14px rgba(15, 23, 42, .35), 0 2px 6px rgba(15, 23, 42, .05);
  }
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-width: 0;
    border: 0;
    border-radius: 16px;
    background: transparent;
    color: var(--muted);
    font-family: var(--font-body);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .01em;
    -webkit-tap-highlight-color: transparent;
    transition: color .2s, background .3s var(--ease), box-shadow .3s var(--ease);
  }
  .tab-item i { font-size: 20px; line-height: 1; transition: transform .35s var(--ease-spring); }
  .tab-item span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tab-item:hover { color: var(--primary-600); }
  .tab-item:active i { transform: scale(.85); }
  .tab-item.active {
    color: #fff;
    background: var(--gradient-primary);
    box-shadow: 0 10px 22px -10px rgba(14, 165, 233, .9);
  }
  .tab-item.active i { transform: translateY(-1px); }

  /* Room for the bar; the sidebar toggle is replaced by the "More" tab. */
  .app-main { padding-bottom: calc(var(--tabbar-height) + var(--tabbar-gap) * 3 + env(safe-area-inset-bottom)) !important; }
  #navToggle { display: none; }
  .pwa-card { bottom: calc(var(--tabbar-height) + var(--tabbar-gap) * 2 + env(safe-area-inset-bottom)); }
  .offline-banner { bottom: calc(var(--tabbar-height) + var(--tabbar-gap) * 2 + env(safe-area-inset-bottom)); transform: translate(-50%, calc(100% + 140px)); }
  .is-offline .offline-banner { transform: translate(-50%, 0); }
  body.sheet-open { overflow: hidden; }
  /* Drawers and dialogs cover the bar. */
  body:has(.app-sidebar.open) .mobile-tabbar { visibility: hidden; }

  /* ---------- "More" sheet ---------- */
  .more-sheet { position: fixed; inset: 0; z-index: 1600; }
  .more-sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, .5);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    animation: fadeIn .25s both;
  }
  .more-sheet-panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-radius: 28px 28px 0 0;
    box-shadow: 0 -24px 60px rgba(15, 23, 42, .25);
    padding-bottom: env(safe-area-inset-bottom);
    animation: sheetUp .38s var(--ease) both;
  }
  .more-sheet.closing .more-sheet-panel { animation: sheetDown .24s var(--ease) forwards; }
  .more-sheet.closing .more-sheet-backdrop { animation: fadeIn .24s reverse forwards; }
  .more-sheet-handle { width: 44px; height: 5px; border-radius: 5px; background: #d5dbe6; margin: 10px auto 2px; flex-shrink: 0; }
  .more-sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 18px 10px; flex-shrink: 0; }
  .more-sheet-head strong { display: block; font-family: var(--font-heading); font-size: 18px; color: var(--text); }
  .more-sheet-head small { color: var(--muted); font-size: 12.5px; }
  .more-sheet-body { overflow-y: auto; overscroll-behavior: contain; padding: 0 16px 20px; }
  .more-group { margin-top: 10px; }
  .more-group-label { margin: 6px 4px 10px; font-family: var(--font-body); font-size: 11px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--muted-2); }
  .more-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
  .more-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 14px 6px 12px;
    border-radius: 18px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text-2);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.25;
    text-align: center;
    -webkit-tap-highlight-color: transparent;
    transition: transform .2s var(--ease), border-color .2s, background .2s;
  }
  .more-tile:active { transform: scale(.96); }
  .more-tile.active { border-color: var(--primary); background: var(--primary-50); color: var(--primary-700); }
  .more-tile-icon {
    width: 44px;
    height: 44px;
    border-radius: 14px;
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 19px;
    background: var(--tile-gradient, var(--gradient-primary));
    box-shadow: 0 10px 18px -10px rgba(15, 23, 42, .5);
  }
  .tone-0 { --tile-gradient: var(--gradient-primary); }
  .tone-1 { --tile-gradient: var(--gradient-ocean); }
  .tone-2 { --tile-gradient: var(--gradient-emerald); }
  .tone-3 { --tile-gradient: var(--gradient-sunset); }
  .tone-4 { --tile-gradient: var(--gradient-rose); }
  .tone-5 { --tile-gradient: var(--gradient-slate); }
  .more-account { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
  .more-account .btn { width: 100%; }

  /* Segmented switches (route family, status, day…) fill the screen width; buttons share it.
     The small grid/list icon toggle in Media stays compact. */
  .segmented:not(#mediaView) { display: flex; width: 100%; }
  .segmented:not(#mediaView) > button { flex: 1 1 auto; min-width: 0; justify-content: center; padding: 9px 8px; white-space: normal; line-height: 1.2; text-align: center; }

  /* Page headers: buttons wrap neatly instead of squeezing. */
  .page-header { margin-bottom: 16px; gap: 12px; }
  .page-header-title h4 { font-size: 20px; }
  .page-header-actions { width: 100%; }
}

@keyframes sheetUp { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheetDown { from { transform: none; } to { transform: translateY(100%); } }

/* ---------- 3. Tables as cards ---------- */
@media (max-width: 767.98px) {
  /* The surrounding card becomes a plain section; each row becomes its own card. */
  .app-card:has(.app-table:not(.no-cards)) { background: transparent; border: 0; box-shadow: none; }
  .app-card:has(.app-table:not(.no-cards)) > .app-card-header { padding: 4px 2px 12px; border: 0; }
  .app-card:has(.app-table:not(.no-cards)) > .app-card-body { padding: 0; }
  .table-wrap:has(> .app-table:not(.no-cards)) { overflow: visible; border-radius: 0; }

  .app-table:not(.no-cards),
  .app-table:not(.no-cards) > tbody { display: block; width: 100%; }
  .app-table:not(.no-cards) > thead { display: none; }
  .app-table:not(.no-cards) > tbody > tr {
    position: relative;
    display: flex;
    flex-direction: column;
    margin-bottom: 12px;
    padding: 2px 16px 4px 18px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 8px 22px -16px rgba(15, 23, 42, .35);
    overflow: hidden;
  }
  .app-table:not(.no-cards) > tbody > tr::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: var(--gradient-primary);
  }
  .app-table:not(.no-cards) > tbody > tr:hover { background: var(--surface); }
  .app-table:not(.no-cards) > tbody > tr.row-danger { background: #fffafa; }
  .app-table:not(.no-cards) > tbody > tr.row-danger::before { background: var(--gradient-rose); }

  .app-table:not(.no-cards) > tbody > tr > td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-width: 0;
    padding: 10px 0;
    border: 0;
    border-bottom: 1px dashed var(--border);
    text-align: right;
    white-space: normal;
    word-break: break-word;
    color: var(--text);
  }
  .app-table:not(.no-cards) > tbody > tr > td:last-child { border-bottom: 0; }
  .app-table:not(.no-cards) > tbody > tr > td::before {
    content: attr(data-label);
    flex-shrink: 0;
    max-width: 46%;
    text-align: left;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--muted-2);
  }
  .app-table:not(.no-cards) > tbody > tr > td[data-label=""]::before,
  .app-table:not(.no-cards) > tbody > tr > td:not([data-label])::before { display: none; }
  .app-table:not(.no-cards) > tbody > tr > td[data-label=""] { justify-content: flex-start; text-align: left; }

  /* Card title (first column, or the one marked data-card-title) */
  .app-table:not(.no-cards) > tbody > tr > td.card-title-cell {
    order: -2;
    display: block;
    padding: 14px 40px 12px 0;
    border-bottom: 1px solid var(--border);
    text-align: left;
    font-size: 15px;
    font-weight: 600;
  }
  .app-table:not(.no-cards) > tbody > tr > td.card-title-cell::before { display: none; }
  .app-table:not(.no-cards) > tbody > tr > td.card-title-cell .cell-title { font-size: 15px; }

  /* "#" becomes a small badge in the corner */
  .app-table:not(.no-cards) > tbody > tr > td.card-index-cell {
    position: absolute;
    top: 12px;
    right: 14px;
    min-width: 28px;
    height: 28px;
    padding: 0 8px;
    justify-content: center;
    border: 0;
    border-radius: 999px;
    background: var(--primary-50);
    color: var(--primary-700);
    font-size: 12px;
    font-weight: 700;
  }
  .app-table:not(.no-cards) > tbody > tr > td.card-index-cell::before { display: none; }

  /* Buttons go to the bottom of the card */
  .app-table:not(.no-cards) > tbody > tr > td.card-actions-cell {
    order: 10;
    justify-content: flex-end;
    gap: 6px;
    padding: 8px 0 10px;
    border-top: 1px solid var(--border);
    border-bottom: 0;
  }
  .app-table:not(.no-cards) > tbody > tr > td.card-actions-cell::before { display: none; }
  .app-table:not(.no-cards) > tbody > tr > td.card-actions-cell .btn-icon { width: 38px; height: 38px; background: var(--surface-2); border: 1px solid var(--border); }

  /* Messages / loading rows spanning the table */
  .app-table:not(.no-cards) > tbody > tr:has(> td.card-full) { padding: 0; }
  .app-table:not(.no-cards) > tbody > tr:has(> td.card-full)::before { display: none; }
  .app-table:not(.no-cards) > tbody > tr > td.card-full { display: block; padding: 14px; border: 0; text-align: center; }
  .app-table:not(.no-cards) > tbody > tr > td.card-full::before { display: none; }

  /* Values keep their own look (badges, pills, people) but never overflow the card. */
  .app-table:not(.no-cards) > tbody > tr > td > * { max-width: 100%; }
  /* Fields inside a card row take all the space next to their label (labels share one width). */
  .app-table:not(.no-cards) > tbody > tr > td:has(> .form-select, > .form-control)::before { width: 30%; }
  .app-table:not(.no-cards) > tbody > tr > td > .form-select,
  .app-table:not(.no-cards) > tbody > tr > td > .form-control { flex: 1 1 0; width: auto; min-width: 0; }
}

/* ---------- 4. Screen-specific phone layouts ---------- */
@media (max-width: 575.98px) {
  /* Stops & Fares: each stop is a card on the route line; fare and buttons on their own row. */
  .route-line { padding: 14px 4px 10px; }
  .route-line::before { left: 25px; top: 28px; bottom: 28px; }
  .stop-item {
    grid-template-columns: 44px minmax(0, 1fr);
    row-gap: 8px;
    margin-bottom: 10px;
    padding: 12px 12px 10px 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 8px 20px -16px rgba(15, 23, 42, .4);
  }
  .stop-item:hover { background: var(--surface); }
  .stop-dot { grid-row: span 2; align-self: start; }
  .stop-name { font-size: 15px; line-height: 1.3; }
  .stop-meta { grid-column: 2; justify-content: space-between; flex-wrap: wrap; gap: 6px; }
  .stop-actions .btn-icon { width: 36px; height: 36px; background: var(--surface-2); border: 1px solid var(--border); }
  .stop-search { width: 100%; flex: 1 1 100%; }

  /* Segmented filters scroll sideways instead of widening the page. */
  .segmented { max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  .segmented::-webkit-scrollbar { display: none; }
  .segmented button { flex-shrink: 0; }
}
