/* ===========================================================================
   Phone layout. Loaded last in every layout so it wins over the desktop rules.

   Most views author their grids and widths inline (no class to hang a media
   query on), so the fixes here match those inline styles with attribute
   selectors: any new view that uses the same literal patterns is covered
   automatically. Tables become stacked cards on phones (mobile.js copies each
   column heading into a data-label on every cell).
   =========================================================================== */

/* Tablet and below: nothing is ever wider than the screen. */
@media (max-width: 900px) {
    html, body { max-width: 100%; overflow-x: clip; }
    img, video, canvas, iframe { max-width: 100%; }
    pre, code { overflow-wrap: anywhere; }
    pre { white-space: pre-wrap; max-width: 100%; }
    .page-body { min-width: 0; }
    .page-body > * { min-width: 0; max-width: 100%; }
    .card { min-width: 0; }
    /* The bottom bar shows five or six tabs; they share the width instead of spilling off the edge. */
    .bottom-nav a, .bottom-nav button { min-width: 0; flex: 1 1 0; padding: 6px 2px; font-size: .64rem; overflow: hidden; }
    .bottom-nav a span, .bottom-nav button span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
    .data-table { min-width: 0; }
}

@media (max-width: 640px) {
    .page-body { padding-left: 14px; padding-right: 14px; }

    /* ---- Grids authored inline ---- */
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="minmax(300px"], [style*="minmax(320px"], [style*="minmax(340px"], [style*="minmax(360px"], [style*="minmax(420px"],
    [style*="grid-template-columns:1fr 90px auto"] { grid-template-columns: 1fr !important; }
    [style*="grid-template-columns:repeat(4"], [style*="grid-template-columns:repeat(3"], [style*="grid-template-columns:repeat(2"] { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
    [style*="grid-template-columns"] > * { min-width: 0; }
    [style*="display:flex"], [style*="display: flex"] { min-width: 0; }
    form[style*="display:flex"], div[style*="display:flex"]:not(.data-tools):not([style*="flex-direction:column"]) { flex-wrap: wrap; }
    form[style*="display:flex"] > .form-group { flex: 1 1 100%; }
    input[type=checkbox], input[type=radio] { width: 20px; height: 20px; }

    /* ---- Fixed widths authored inline ---- */
    [style*="width:260px"], [style*="width:280px"], [style*="width:420px"], [style*="width:480px"], [style*="width:520px"],
    [style*="width:560px"], [style*="width:600px"], [style*="width:640px"], [style*="width:760px"], [style*="width:860px"],
    [style*="width: 640px"], [style*="width:340px"], [style*="width:320px"], [style*="width:360px"] { width: auto !important; max-width: 100% !important; }
    input.form-control[style*="width"], select.form-control[style*="width"], textarea.form-control[style*="width"] { width: 100% !important; max-width: 100% !important; }
    [style*="min-width:220px"], [style*="min-width:240px"], [style*="min-width:200px"] { min-width: 0 !important; }

    /* ---- Page headers and toolbars ---- */
    .page-header { flex-direction: column; align-items: stretch; gap: 12px; }
    .page-header > .btn { width: 100%; text-align: center; }
    .page-header .actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
    .page-header .actions .btn, .page-header .actions form { flex: 1 1 140px; }
    .page-header .actions form .btn { width: 100%; }
    .data-tools { flex-wrap: nowrap !important; overflow-x: auto; justify-content: flex-start !important; padding-bottom: 4px; -webkit-overflow-scrolling: touch; }
    .data-tools .btn { white-space: nowrap; flex: 0 0 auto; }
    .data-tools { scrollbar-width: none; }
    .data-tools::-webkit-scrollbar { display: none; }
    /* Summary tiles sit two to a row instead of one tall card each. */
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .stat-card { padding: 12px; }
    .stat-card .value { font-size: 1.45rem; }
    .stat-card .label { font-size: .74rem; }
    [style*="justify-content:space-between"] { flex-wrap: wrap; row-gap: 8px; }
    .topbar .btn { padding-left: 10px; padding-right: 10px; }
    .topbar { gap: 6px; }

    /* ---- Forms: no iOS zoom on focus, comfortable taps, full-width submit rows ---- */
    input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { font-size: 16px; }
    .btn, .form-control, select.form-control { min-height: 44px; }
    .btn-sm { min-height: 38px; }
    form .btn + .btn { margin-top: 8px; }
    .form-group { margin-bottom: 14px; }
    .card { padding: 14px; }
    label { overflow-wrap: anywhere; }
    h1, h2, h3, .card strong { overflow-wrap: anywhere; }

    /* ---- Tables become stacked cards ---- */
    .card:has(table.data-table) { padding: 10px !important; overflow: visible !important; }
    .card:has(> .table-tools) table.data-table { margin-top: 8px; }
    table.data-table { display: block; width: 100%; min-width: 0 !important; overflow: visible; background: transparent; font-size: .9rem; }
    table.data-table thead { display: none; }
    table.data-table tbody { display: block; }
    table.data-table tr { display: block; margin: 0 0 10px; padding: 4px 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
    table.data-table tbody tr:hover { background: var(--surface); }
    table.data-table td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 8px 14px; border-bottom: 1px dashed var(--border); text-align: right; overflow-wrap: anywhere; }
    table.data-table td:last-child { border-bottom: 0; }
    table.data-table td::before { content: attr(data-label); flex: 0 0 36%; max-width: 40%; text-align: left; color: var(--text-muted); font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
    table.data-table td[data-label=""]::before, table.data-table td:not([data-label])::before { content: none; }
    table.data-table td[colspan] { display: block; text-align: center; }
    table.data-table td form { margin: 0; }
    html[dir="rtl"] table.data-table td { text-align: left; }
    html[dir="rtl"] table.data-table td::before { text-align: right; }
    html[dir="rtl"] table.data-table td[colspan] { text-align: center; }
    table.data-table tr[onclick] { cursor: pointer; }

    /* ---- Boards, maps, charts ---- */
    .kanban-board { scroll-snap-type: x mandatory; gap: 12px; margin-inline: -4px; padding-inline: 4px; }
    .kanban-column { flex: 0 0 82vw; scroll-snap-align: start; }
    .leaflet-container { max-width: 100%; }
    [id$="-map"], #map, .map-canvas { max-height: 60vh; }
    canvas { max-width: 100% !important; height: auto !important; }

    /* ---- Pop-overs ---- */
    .notif-dropdown { left: 10px; right: 10px; width: auto; max-width: none; }
    .cmdk-hint, .pwa-install-btn { display: none; }
    .toast, .pwa-toast { max-width: calc(100vw - 24px); }

    /* ---- Marketing / auth / setup pages ---- */
    .auth-card, .register-card { width: 100%; max-width: 100%; padding: 20px 16px; }
    .landing-nav, .lp-footer, .lp-hero, .lp-section { padding-left: 16px; padding-right: 16px; }
    .lp-table, .landing-table { display: block; overflow-x: auto; }
    .wa-float { bottom: 78px; }
}

/* Very small phones. */
@media (max-width: 380px) {
    .page-body { padding-left: 10px; padding-right: 10px; }
    h1 { font-size: 1.2rem; }
    .stat-grid { grid-template-columns: 1fr; }
    [style*="grid-template-columns:repeat(4"], [style*="grid-template-columns:repeat(3"], [style*="grid-template-columns:repeat(2"] { grid-template-columns: 1fr !important; }
}
