/* Mobile presentation. Sources, role checks and commands are shared with desktop. */
:root { --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px); --app-header-height: calc(56px + var(--safe-top)); }
.app-header { padding-top: calc(5px + var(--safe-top)); height: var(--app-header-height); }
.orders-filter-panel { margin-bottom: 16px; }
.chat-search input { font-size: max(16px, .9rem); }
.chat-composer { padding-bottom: calc(10px + var(--safe-bottom)); }
.chat-attachment-picker label, .chat-attachment-picker select { min-width: 0; max-width: 100%; }
.chat-attachment-picker select { width: 100%; }

@media (max-width: 900px), (max-width: 1100px) and (max-height: 500px) and (pointer: coarse) {
  /* Lock the outer document; the directory and timeline retain native scrolling. */
  html.has-chat-workspace, html.has-chat-workspace body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  .has-chat-page { overflow: hidden; }
  /* visualViewport follows the keyboard on Safari, where 100dvh alone does not. */
  .has-chat-page { position: fixed; inset: var(--visual-top, 0px) 0 auto; height: var(--visual-height, 100dvh); display: flex; flex-direction: column; }
  .has-chat-page > .app-header { flex: 0 0 var(--app-header-height); }
  .has-chat-page > .layout { flex: 1; min-height: 0; display: flex; }
  .has-chat-page .chat-page { flex: 1; width: 100%; height: auto; min-width: 0; min-height: 0; padding-inline: max(6px, env(safe-area-inset-left)) max(6px, env(safe-area-inset-right)); }
  .chat-layout { display: flex; }
  .chat-directory { width: 100%; border: 0; }
  .chat-layout:not(.has-conversation) .chat-conversation { display: none; }
  .chat-layout.has-conversation .chat-directory { display: none; }
  .chat-conversation { flex: 1; min-width: 0; min-height: 0; }
  .chat-mobile-back { display: grid; place-items: center; flex: 0 0 44px; width: 44px; height: 44px; }
  .chat-heading { padding: 8px; gap: 5px; min-height:56px; }
  .chat-heading-identity { flex: 1; }
  .chat-heading h2 { font-size: .95rem; }
  .chat-heading-actions { gap: 4px; margin-left: 0; }
  .chat-heading .chat-create-task, .chat-heading [data-chat-sound] { display: inline-grid; place-items: center; width: 44px; height: 44px; min-height: 44px; padding: 9px; flex-shrink: 0; }
  .chat-heading .chat-create-task > span, .chat-events-word { display: none; }
  .chat-heading-actions .chat-events-link { display: inline-flex; justify-content: center; align-items: center; gap: 3px; min-width: 44px; height: 44px; padding: 5px; }
  .chat-composer { padding-bottom: calc(8px + var(--safe-bottom)); }
  .chat-messages, .chat-directory { overscroll-behavior-y: contain; }
  .chat-send { width: 44px; padding: 8px; justify-content: center; }
  .chat-send > span { display: none; }
  .chat-attachment-picker { max-height: min(440px, calc(var(--visual-height, 100dvh) - var(--app-header-height) - 120px)); overflow-y: auto; overscroll-behavior-y: contain; }
  .chat-attachment-tools[data-mention-tools] .chat-attachment-picker { left: -52px; }
  .chat-composer [data-compose-context] { max-height: 120px; overflow-y: auto; }
}

@media (max-width: 760px) {
  :root { --app-header-height: calc(56px + var(--safe-top)); }
  .app-header { height: var(--app-header-height); padding: calc(5px + var(--safe-top)) max(8px, env(safe-area-inset-right)) 5px max(8px, env(safe-area-inset-left)); gap: 5px; }
  .app-header .menu-toggle { width: 44px; height: 44px; min-height: 44px; }
  .app-header .header-actions { gap: 0; padding-right: 3px; }
  .app-header .header-actions > a { width: 44px; height: 44px; }
  .app-header .brand { flex: 1; min-width: 0; font-size: .9rem; }
  
  .app-menu { padding-bottom: calc(12px + var(--safe-bottom)); }
  main:not(.chat-page):not(.auth-page) { padding: 16px max(12px, env(safe-area-inset-right)) calc(24px + var(--safe-bottom)) max(12px, env(safe-area-inset-left)); }
  input:not([type=checkbox]):not([type=radio]), select, textarea { font-size: max(16px, 1em); }
  input[type=file] { overflow: hidden; }
  button, summary, .open-link, .back { min-height: 44px; }
  .back { display: inline-flex; align-items: center; }
  .tabs, .client-tabs { scroll-padding-inline: 12px; overscroll-behavior-x: contain; }
  .tabs > *, .client-tabs > * { flex-shrink: 0; }
  .work-card, .order-detail, .task-editor, form, fieldset { min-width: 0; }
  .order-actions { gap: 10px; }
  .order-actions > button, .order-actions > a { white-space: normal; text-align: center; justify-content: center; }
  .order-route { padding: 18px; }
  .order-route-step { min-height: 42px; padding-bottom: 12px; }
  .order-route-label { font-size: .9rem; }
  dialog:not(.app-menu) { max-width: calc(100% - 16px); max-height: calc(var(--visual-height, 100dvh) - 16px - var(--safe-top) - var(--safe-bottom)); margin: auto; overflow-y: auto; overscroll-behavior: contain; }
  .order-dialog-heading { gap: 12px; align-items: center; }
  .order-dialog-heading h2 { min-width: 0; font-size: 1.15rem; }
  .order-dialog-heading > button { flex-shrink: 0; }
  .file-viewer .file-view-stage { min-height: 0; max-height: 45dvh; }
  .file-view-tools { flex-wrap: wrap; gap: 8px; }
  .task-calendar-panel { position: static; min-width: 0; width: 100%; max-width: 100%; margin-top: 8px; }
  .task-calendar-grid button { min-height: 36px; }
}

@media (max-width: 700px) {
  .orders-heading { padding: 14px; flex-wrap: wrap; gap: 12px; margin-bottom: 14px; }
  .orders-heading-copy { width: auto; flex: 1; }
  .orders-heading-actions { width: auto; }
  .orders-heading h1 { font-size: 1.4rem; }
  .orders-heading p { display: none; }
  .orders-heading .orders-create { padding: 9px 12px; }
  .orders-heading .orders-import { flex-basis: 100%; }
  .orders-filter-panel .filters { gap: 12px; padding: 14px; margin-bottom: 0; }
  .toolbar { gap: 12px; margin-bottom: 14px; }
  .view-tools { width: 100%; justify-content: flex-end; }
    .popover .popup { max-width: calc(100vw - 32px); }
  .orders-table-frame .table-pan, .finance-table-frame .table-pan { display: none; }
  .orders-table-frame .table-scroll, .finance-table-frame .table-scroll, .clients-table-scroll, .calc-table-scroll, .invoice-table-scroll { overflow: visible; border: 0; background: transparent; box-shadow: none; }
  /* One DOM/projection/command path: only table presentation changes on a phone. */
  .mobile-record-table { display: block; width: 100%; min-width: 0; border: 0; }
  .mobile-record-table thead { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .mobile-record-table tbody { display: grid; gap: 14px; }
  .mobile-record-table tbody tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); position: relative; gap: 12px; padding: 16px; border: 1px solid var(--card-outline); border-radius: 12px; background: var(--surface); }
  .mobile-record-table tbody td, .mobile-record-table tbody td:first-child, .mobile-record-table tbody td:nth-child(2), .mobile-record-table tbody td:last-child { display: block; position: static; min-width: 0; width: auto; max-width: none; padding: 0; border: 0; box-shadow: none; background: transparent; overflow-wrap: anywhere; }
  .mobile-record-table tbody td:first-child, .mobile-record-table tbody td:last-child { grid-column: 1 / -1; }
  .mobile-record-table tbody td[data-label]::before { content: attr(data-label); display: block; color: var(--muted); font-size: .8rem; font-weight: 400; margin-bottom: 6px; }
  .mobile-record-table tbody td:last-child { display: flex; justify-content: flex-end; }
  .mobile-record-table .order-status-card, .mobile-record-table .order-task-card { min-width: 0; max-width: none; width: 100%; }
  .orders-table tbody tr { grid-template-columns: minmax(0,1fr) 44px; }
  .orders-table tbody td:not(:first-child):not(:nth-child(2)) { grid-column: 1 / -1; }
  .orders-table tbody td:first-child { grid-column: 2; grid-row: 1 / 3; }
  .orders-table tbody td:nth-child(2) { grid-column: 1; grid-row: 1; }
  .orders-table tbody tr.attention-red { background: var(--danger-soft); }
  .orders-table tbody tr.attention-yellow { background: var(--warning-soft); }
  .orders-table .order-signals { gap: 8px; }
  .orders-table .row-actions.orders-row-actions { display: flex; width: 100%; gap: 10px; }
  .orders-table .orders-row-actions > * { flex: 1; min-height: 44px; }
  .orders-table .signal-add, .orders-table .signal-attention-button { width: 44px; height: 44px; }
  .orders-table .signal-tip { left: auto; right: 0; top: 38px; width: min(280px, calc(100vw - 80px)); }
  .calc-table tbody td:nth-child(2) { grid-column: 1 / -1; }
  .clients-table .clients-identity { gap: 12px; }
  .clients-table .clients-name { font-size: 1.05rem; }
  .clients-table .clients-manager { font-size: .9rem; }
  .invoice-table .invoice-data-row td:nth-child(2) { grid-column: 1 / -1; }
}

@media (max-width: 380px) {
  .app-header .brand { gap: 4px; font-size: .83rem; }
  
  .chat-heading { gap: 3px; padding-inline: 5px; }
  .chat-heading-actions { gap: 3px; }
  .chat-heading-identity h2 { font-size: .85rem; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; } }

/* A landscape phone with the keyboard open can be wider than the portrait cutoff. */
@media (max-width: 1100px) and (max-height: 500px) and (pointer: coarse) {
  :root { --app-header-height: calc(52px + var(--safe-top)); }
  .app-header { padding-top: calc(4px + var(--safe-top)); padding-bottom: 4px; }
  .chat-page { padding-block: 4px; }
  .chat-heading { min-height: 52px; padding-block: 4px; }
  .chat-messages { padding-block: 8px; }
  .chat-composer { padding: 6px 8px calc(6px + var(--safe-bottom)); }
  .chat-composer textarea { max-height: min(96px, 20dvh); }
  .chat-composer [data-compose-context] { max-height: min(72px, 18dvh); }
  .chat-attachment-picker { max-height: calc(var(--visual-height, 100dvh) - var(--app-header-height) - 72px); }
}

/* 19.11: compact client conversation; staff and desktop keep their own layout. */
@media (max-width: 900px), (max-width: 1100px) and (max-height: 500px) and (pointer: coarse) {
  .has-client-chat-page { --app-header-height: calc(52px + var(--safe-top)); }
  .has-client-chat-page > .app-header { padding-top: calc(2px + var(--safe-top)); padding-bottom: 2px; border-bottom: 0; }
  .has-client-chat-page .chat-page { padding: 0; }
  .has-client-chat-page .chat-layout { border: 0; border-radius: 0; }
  .has-client-chat-page .chat-heading { grid-template-columns: 44px minmax(0,max-content) minmax(0,1fr); align-items: center; min-height: 56px; padding: 4px 6px 6px; gap: 3px; border-bottom: 2px solid var(--text); box-shadow: none; }
  .has-client-chat-page .chat-mobile-back { grid-column: 1; grid-row: 1; }
  .has-client-chat-page .chat-heading-identity { grid-column: 2; grid-row: 1; }
  .has-client-chat-page .chat-heading h2 { margin: 0; font-size: .9rem; }
  .has-client-chat-page .chat-heading-tools { grid-column: 3; grid-row: 1; justify-self: start; justify-content: flex-start; margin-left: 6px; flex-wrap: nowrap; gap: 9px; }
  .has-client-chat-page .chat-heading-actions { margin-left: 0; gap: 3px; }
  .has-client-chat-page .chat-heading .chat-create-task { width: 44px; height: 44px; min-height: 44px; }
  .has-client-chat-page .chat-conversation { background: #c2d5d3; }
  .has-client-chat-page .chat-composer { margin: 0 4px 4px; padding: 8px 8px max(6px,var(--safe-bottom)); border-top: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
}

/* Mobile chat typography approximates the user-provided messenger reference. */
@media (max-width: 900px), (max-width: 1100px) and (max-height: 500px) and (pointer: coarse) {
  .chat-message { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; letter-spacing: normal; }
  .chat-message p, .chat-message blockquote { font-size: 17px; line-height: 1.25; font-weight: 400; letter-spacing: normal; }
}

@media (max-width: 900px), (max-width: 1100px) and (max-height: 500px) and (pointer: coarse) {
  .invoice-table .invoice-data-row { gap: 10px; padding: 14px; grid-template-columns: minmax(0,1fr) auto; }
  .invoice-table .invoice-data-row td,.invoice-table .invoice-data-row td:first-child { padding: 0; border: 0; box-shadow: none; min-width: 0; }
  .invoice-table .invoice-data-row.is-arrived td:first-child { box-shadow: none; }
  .invoice-table .invoice-data-row td:first-child { grid-column: 1 / -1; grid-row: 1; }
  .invoice-table .invoice-data-row td:nth-child(2) { display: none; }
  .invoice-table .invoice-data-row .invoice-route-cell { grid-column: 1 / -1; grid-row: 2; }
  .invoice-table .invoice-data-row .invoice-list-cost { grid-column: 1; grid-row: 3; align-self: center; font-size: 17px; }
  .invoice-table .invoice-data-row .invoice-payment-cell { grid-column: 1 / -1; grid-row: 4; }
  .invoice-table .invoice-data-row .invoice-list-actions { display: flex; align-items: center; grid-column: 2; grid-row: 3; }
  .invoice-table .invoice-data-row td[data-label]::before { display: none; }
  .invoice-card-meta { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin-top: 7px; }
  .invoice-card-meta .invoice-list-preview { margin: 0; min-width: 0; line-height: 1.4; font-size: 13px; }
  .invoice-list-name { font-size: 16px; line-height: 1.3; }
  .invoice-list-order { margin-top: 5px; line-height: 1.4; font-size: 13px; }
  .invoice-mobile-state { display: inline-flex; font-size: 22px; font-weight: 400; padding: 7px 9px; flex-shrink: 0; }
  .invoice-list-open,.invoice-pay-action { font-size: 13px; padding: 7px 10px; min-height: 40px; }
}
@media (max-width: 380px) {
  .invoice-table .invoice-data-row .invoice-list-actions { grid-row: 4; grid-column: 1 / -1; }
  .invoice-table .invoice-data-row .invoice-payment-cell { grid-row: 5; }
}

@media (max-width: 760px) { .app-header .profile { display: none; } .app-header .header-actions { border-right: 0; padding-right: 0; } .settings-session-exit button { width: 100%; } }
