/* Only enabled on phones and tablets by device.js. */
/* PWA controls share the existing card, button and status styles. */
.pwa-panel { display: grid; gap: 1rem; min-width: 0; }
.pwa-panel section + section { border-top: 1px solid var(--border); padding-top: 1rem; }
.pwa-panel .actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.offline-photos { display: flex; flex-wrap: wrap; gap: .6rem; min-width: 0; }
.offline-photos > * { max-width: 100%; overflow-wrap: anywhere; }
.offline-photos img { max-width: 110px; max-height: 100px; object-fit: contain; }
#app-settings { flex-shrink: 0; }
.mobile-row-field { display: contents; }
.mobile-row-label { display: none; }

/* Everyday navigation stays within thumb reach; the menu holds account/admin tools. */
@media screen {
  :root {
    --mobile-tabs-height: 64px;
    --mobile-tabs-bottom: max(8px, env(safe-area-inset-bottom));
    --mobile-tabs-left: max(10px, env(safe-area-inset-left));
    --mobile-tabs-right: max(10px, env(safe-area-inset-right));
    --mobile-tabs-top: calc(var(--mobile-tabs-height) + var(--mobile-tabs-bottom));
  }
  button, .btn, .btn-sm, .seg, .sbox, .fac-opt, .chip-check, .choice, .fac, .q-photo-option { min-height: 44px; }
  input:not([type=checkbox]):not([type=radio]):not([type=hidden]), select, textarea, .q-main input { font-size: 16px; min-height: 44px; min-width: 0; }
  .bottom-tabs {
    position: fixed; inset: auto var(--mobile-tabs-right) var(--mobile-tabs-bottom) var(--mobile-tabs-left);
    z-index: 25; display: grid; grid-template-columns: repeat(6, minmax(0, 1fr));
    width: auto; max-width: none; height: var(--mobile-tabs-height); margin: 0; padding: 4px;
    border: 1px solid rgba(255, 255, 255, .9); border-radius: 999px;
    background: rgba(255, 255, 255, .94);
    box-shadow: 0 6px 24px rgba(15, 42, 68, .14), 0 1px 4px rgba(15, 42, 68, .06), inset 0 1px 0 rgba(255, 255, 255, .95);
    isolation: isolate;
  }
  .bottom-tabs > a, .bottom-tabs > button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .2rem; min-width: 0;
    min-height: 44px; height: 100%; padding: .3rem .1rem; color: #334155; text-align: center;
    font-size: .68rem; font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; text-decoration: none;
    border: 0; border-radius: 999px; background: transparent;
    appearance: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  }
  .bottom-tabs > a.active, .bottom-tabs > button[aria-expanded=true] { color: #16436f; background: rgba(31, 95, 168, .12); box-shadow: inset 0 1px 0 rgba(255, 255, 255, .8), 0 1px 3px rgba(15, 42, 68, .05); }
  .bottom-tabs > a:active, .bottom-tabs > button:active { background: rgba(31, 95, 168, .18); }
  .bottom-tabs > a:focus-visible, .bottom-tabs > button:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
  .bottom-tabs .tab-icon { display: grid; place-items: center; width: 24px; height: 24px; flex-shrink: 0; line-height: 1; }
  .bottom-tabs .tab-icon svg { display: block; width: 22px; height: 22px; }
  .bottom-tabs > a > span:last-child, .bottom-tabs > button > span:last-child { max-width: 100%; white-space: nowrap; overflow-wrap: normal; font-size: clamp(.56rem, 2.7vw, .68rem); }
  body:has(.bottom-tabs) main { padding-top: max(1rem, env(safe-area-inset-top)); padding-bottom: calc(var(--mobile-tabs-top) + 1rem); }
  body:has(.bottom-tabs) .savebar { bottom: calc(var(--mobile-tabs-top) + .5rem); }
  body:has(.bottom-tabs) .proto-progress { position: static; }
  body:has(.bottom-tabs) .check-item, body:has(.bottom-tabs) .q-field, body:has(.bottom-tabs) #proto-body > section { scroll-margin-top: 24px; }
  body:has(.bottom-tabs) input, body:has(.bottom-tabs) select, body:has(.bottom-tabs) textarea { scroll-margin-bottom: calc(var(--mobile-tabs-top) + 1rem); }
  body:has(.bottom-tabs) #toast { bottom: calc(var(--mobile-tabs-top) + .75rem); }
  body:has(.bottom-tabs) .dev-banner { position: static; width: fit-content; max-width: calc(100% - 2rem); margin: 0 1rem calc(var(--mobile-tabs-top) + 1rem) auto; overflow-wrap: anywhere; }

  .nav.app-drawer {
    display: none; position: fixed; top: auto; left: auto; right: var(--mobile-tabs-right);
    bottom: calc(var(--mobile-tabs-top) + .5rem); z-index: 30;
    width: min(360px, calc(100% - var(--mobile-tabs-left) - var(--mobile-tabs-right)));
    max-height: min(70dvh, calc(100dvh - var(--mobile-tabs-top) - env(safe-area-inset-top) - 1.5rem));
    flex-direction: column; gap: .25rem; margin: 0; padding: .75rem; overflow-y: auto; overscroll-behavior: contain;
    white-space: normal; color: #fff; background: var(--navy); border: 1px solid var(--navy-2); border-radius: 12px; box-shadow: 0 8px 35px rgba(15, 42, 68, .25);
  }
  .nav.app-drawer.open { display: flex; }
  .app-drawer > * { min-width: 0; }
  .app-drawer header, .app-drawer .drawer-header, .app-drawer .drawer-head { display: flex; align-items: center; flex-wrap: wrap; gap: .75rem; margin-bottom: .5rem; padding: .25rem .25rem .75rem; border-bottom: 1px solid rgba(255,255,255,.2); }
  .app-drawer .who, .app-drawer .drawer-user { min-width: 0; overflow-wrap: anywhere; }
  .app-drawer .who span, .app-drawer .drawer-user small { display: block; color: #cfdbe8; font-size: .8rem; }
  .app-drawer .drawer-header .who { flex: 1 1 160px; }
  .drawer-user-title { display: flex; align-items: center; gap: .5rem; }
  .drawer-user-title strong { min-width: 0; }
  .app-drawer .connection-indicator { display: inline-block; width: 8px; height: 8px; flex-shrink: 0; border-radius: 50%; background: #94a3b8; box-shadow: 0 0 0 3px rgba(148,163,184,.12); }
  .app-drawer .connection-indicator[data-state=connected] { background: #4ade80; box-shadow: 0 0 0 3px rgba(74,222,128,.12); }
  .app-drawer .connection-indicator[data-state=pending] { background: #fbbf24; box-shadow: 0 0 0 3px rgba(251,191,36,.12); }
  .app-drawer .connection-indicator[data-state=error] { background: #f87171; box-shadow: 0 0 0 3px rgba(248,113,113,.12); }
  .app-drawer .who .connection-status { margin-top: .3rem; font-size: .75rem; line-height: 1.4; }
  .drawer-tools, .drawer-account { display: grid; gap: .25rem; }
  .drawer-account { border-top: 1px solid rgba(255,255,255,.2); margin-top: .4rem; padding-top: .5rem; }
  .app-drawer a, .app-drawer button, .app-drawer summary { min-height: 44px; }
  .app-drawer button { width: 100%; justify-content: flex-start; text-align: left; color: #cfdbe8; font-weight: 500; background: transparent; border: 0; border-radius: 6px; padding: .55rem .75rem; white-space: normal; }
  .app-drawer button:hover { color: #fff; background: var(--navy-2); }
  .app-drawer #app-settings, .app-drawer #pw-btn { justify-content: flex-start; }
  .app-drawer .nav-menu .menu { position: static; min-width: 0; background: transparent; border: 0; box-shadow: none; padding: 0 0 0 .75rem; }
  .app-drawer .nav-menu .menu a { display: flex; align-items: center; color: #cfdbe8; white-space: normal; }
  .app-drawer .nav-menu .menu a:hover, .app-drawer .nav-menu .menu a.active { color: #fff; background: var(--navy-2); }
}

@supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .bottom-tabs {
    background: linear-gradient(165deg, rgba(255,255,255,.86), rgba(245,249,255,.76));
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    backdrop-filter: blur(24px) saturate(180%);
  }
}

@media (prefers-reduced-transparency: reduce), (prefers-contrast: more) {
  .bottom-tabs { background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none; border-color: var(--border); }
  .bottom-tabs > a.active, .bottom-tabs > button[aria-expanded=true] { background: var(--open-bg); }
}

@media (hover: hover) {
  .bottom-tabs > a:hover, .bottom-tabs > button:hover { background: rgba(31,95,168,.1); }
}

/* Screen-only rules leave the document's separate paper layout intact. */
@media screen and (max-width: 900px) {
  main, .card, .grid > *, .form-grid > *, .proto-meta > *, .q-main { min-width: 0; }
  main { padding-left: max(1rem, env(safe-area-inset-left)); padding-right: max(1rem, env(safe-area-inset-right)); padding-bottom: max(4rem, env(safe-area-inset-bottom)); }
  .topbar { padding-left: max(.5rem, env(safe-area-inset-left)); padding-right: max(.5rem, env(safe-area-inset-right)); }
  button, .btn, .btn-sm, .seg, .sbox, .fac-opt { min-height: 44px; }
  button, .btn { max-width: 100%; overflow-wrap: anywhere; }
  input:not([type=checkbox]):not([type=radio]):not([type=hidden]), select, textarea, .q-main input { font-size: 16px; min-height: 44px; min-width: 0; }
  input[type=checkbox], input[type=radio] { flex-shrink: 0; }
  .chip-check, .choice, .fac, .q-photo-option { min-height: 44px; }
  .thumb .del, .q-actions .btn-sm, .modal header button { min-width: 44px; min-height: 44px; justify-content: center; }
  .nav a, .nav-menu summary { min-height: 44px; display: flex; align-items: center; }
  h1, h2, h3, .crumbs, .card, .report, .page-head, .proto-meta, .proto-head { overflow-wrap: anywhere; }
  .page-head > *, .section-title > *, .section-head > * { min-width: 0; }
  .page-head .actions, .section-title, .section-title .bulk, .section-head { flex-wrap: wrap; }
  .section-title .bulk { margin-left: 0; }
  .toolbar > *, .savebar > *, .print-actions > * { min-width: 0; }
  .print-actions { flex-wrap: wrap; padding: 0; }
  .print-actions .btn, .print-actions button { white-space: normal; }
  dl.info { grid-template-columns: minmax(0, 1fr); gap: .2rem; }
  dl.info dd { min-width: 0; margin-bottom: .65rem; overflow-wrap: anywhere; }
  .person { max-width: 100%; }
  .person > span:last-child { min-width: 0; }

  .table-wrap:has(> .mobile-list) { overflow: visible; }
  table.mobile-list, table.mobile-list > tbody, table.mobile-list > tbody > tr { display: block; width: 100%; min-width: 0; }
  table.mobile-list > thead { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  /* Keep the original sort and selection controls reachable above the cards. */
  table.mobile-list > thead:has(.section-sort, .mobile-select-all) {
    display: block; position: static; width: auto; height: auto; overflow-x: auto; clip-path: none;
    border-bottom: 1px solid var(--border); margin-bottom: .5rem;
  }
  table.mobile-list > thead:has(.section-sort, .mobile-select-all) > tr { display: flex; align-items: center; gap: .5rem; }
  table.mobile-list > thead:has(.section-sort, .mobile-select-all) th:not(:has(.section-sort, .mobile-select-all)) {
    position: absolute; width: 1px; height: 1px; padding: 0; clip-path: inset(50%); overflow: hidden;
  }
  table.mobile-list > thead th:has(.section-sort, .mobile-select-all) {
    position: static; width: auto; flex: none; padding: 0; border: 0; box-shadow: none;
  }
  table.mobile-list .section-sort, .mobile-select-all {
    display: inline-flex; align-items: center; min-height: 44px; padding: 0 .4rem; color: var(--primary);
  }
  .mobile-select-all { gap: .4rem; cursor: pointer; }
  .mobile-select-all::after { content: attr(data-caption); }
  .section-scroll:has(> .mobile-list), .wide-table .table-wrap:has(> .mobile-list) { max-height: none; min-height: 0; }
  .section-scroll .mobile-list > tbody > tr > td { position: static; box-shadow: none; }
  .jeeves-check { max-width: 100%; white-space: normal; min-height: 44px; }
  table.mobile-list > tbody > tr { border-bottom: 1px solid var(--border); padding: .65rem 0; }
  table.mobile-list > tbody > tr:first-child { padding-top: 0; }
  table.mobile-list > tbody > tr:last-child { border-bottom: 0; }
  table.mobile-list > tbody > tr > td, table.mobile-list > tbody > tr > td.hide-sm {
    display: block; width: auto; min-width: 0 !important; border: 0; padding: .35rem .5rem; white-space: normal; overflow-wrap: anywhere;
  }
  table.mobile-list > tbody > tr > td::before { content: attr(data-label); display: block; margin-bottom: .15rem; font-size: .76rem; font-weight: 600; color: var(--muted); }
  table.mobile-list > tbody > tr > td.mobile-table-placeholder::before { content: none; }
  table.mobile-list > tbody > tr > td .badge { white-space: normal; }
  .audit-detail summary { min-height: 44px; display: flex; align-items: center; }

  .proto-progress { position: static; padding: .65rem .75rem; gap: .5rem; }
  .proto-progress .grow { flex-basis: 100%; }
  .savebar { position: static; padding-bottom: max(.75rem, env(safe-area-inset-bottom)); }
  .savebar .state { flex-basis: 100%; }
  .savebar > .btn, .savebar > button { white-space: normal; }
  .savebar .hide-sm { display: inline-flex; }
  .check-item, .q-field, #proto-body > section { scroll-margin-top: 24px; }
  .sig-wrap { min-width: 0; }
  .sig-wrap canvas { max-width: 100%; }
  .sig-wrap .ph { padding: .5rem; text-align: center; }
  .modal-bg { padding: max(.5rem, env(safe-area-inset-top)) max(.5rem, env(safe-area-inset-right)) max(.5rem, env(safe-area-inset-bottom)) max(.5rem, env(safe-area-inset-left)); overscroll-behavior: contain; }
  .modal { min-width: 0; max-height: calc(100dvh - max(1rem, env(safe-area-inset-top) + env(safe-area-inset-bottom))); display: flex; flex-direction: column; }
  .modal header, .modal footer { padding: .65rem .8rem; flex-shrink: 0; gap: .5rem; }
  .modal header h2 { min-width: 0; overflow-wrap: anywhere; }
  .modal .body { min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: .8rem; }
  .modal footer { flex-wrap: wrap; }
  .modal footer button { white-space: normal; }
  body:has(.modal-bg) { overflow: hidden; }
  #toast { bottom: max(1rem, env(safe-area-inset-bottom)); }
  /* A footer in document flow cannot cover the last form controls. */
  .dev-banner { position: static; width: fit-content; max-width: calc(100% - 2rem); margin: 0 1rem 1rem auto; overflow-wrap: anywhere; }
}

@media screen and (max-width: 760px) {
  .card { padding: .85rem; }
  .check-item { grid-template-columns: minmax(1.6rem, max-content) minmax(0, 1fr); gap: .35rem .5rem; }
  .check-item .opts, .check-item .extra { grid-column: 1 / -1; min-width: 0; }
  .check-item .extra { grid-template-columns: minmax(0, 1fr); }
  .status-boxes { gap: .4rem; }
  .sbox { white-space: normal; padding: .4rem .6rem; }
  .q-row { grid-template-columns: 1.5rem minmax(0, 1fr); gap: .5rem; }
  .q-actions { flex-wrap: wrap; }
  .q-field-row strong { min-width: 0; }
  .table-row:not(.head) { grid-template-columns: minmax(0, 1fr) auto; padding: .75rem 0; border-bottom: 1px solid var(--border); }
  .table-row .mobile-row-field { display: block; grid-column: 1 / -1; min-width: 0; }
  .table-row .mobile-row-label { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: .25rem; }
  .table-row .center { justify-content: flex-start; gap: .65rem; min-height: 44px; }
  .table-row .center .mobile-row-label { margin: 0; }
  .table-row > button { grid-column: 2; justify-self: end; min-width: 44px; justify-content: center; }
  .remarks-row { grid-template-columns: minmax(0, 1fr) auto; }
  .remarks-row > input:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
  .remarks-row > button { grid-column: 2; grid-row: 1; }
  .tiles, .tiles.tpl { grid-template-columns: minmax(0, 1fr); }
  .print-doc, .kund-doc { padding: .85rem; min-width: 0; overflow-wrap: anywhere; }
  .print-doc header { flex-wrap: wrap; gap: .5rem; }
  .print-doc header .t { margin-left: 0; text-align: left; width: 100%; }
  .print-doc table, .ktab { table-layout: fixed; }
  .print-doc th, .print-doc td, .ktab th, .ktab td { overflow-wrap: anywhere; }
  .print-doc .box, .ktab .pos { width: 36px; }
  .ktab .kc { width: 48px; }
  .kf { grid-template-columns: minmax(0, 1fr); gap: .1rem; }
  .kf > span:first-child { min-width: 0; text-align: left; color: var(--muted); }
  .utkast { font-size: clamp(32px, 12vw, 80px); letter-spacing: 2px; max-width: 100%; }
  .kcert { max-width: 100%; }
}

@media screen and (max-width: 480px) {
  .topbar { gap: .35rem; }
  .brand-logo img { height: 30px; }
  .brand-logo { padding: 2px 4px; }
  .userbox { gap: .25rem; }
  .userbox > .avatar { display: none; }
  .topbar .btn-sm { padding-left: .5rem; padding-right: .5rem; }
  #menu-btn, #pw-btn, #app-settings { min-width: 44px; justify-content: center; }
  .section-title h2 { flex-basis: 100%; }
  .print-actions > .btn, .print-actions > button { width: 100%; justify-content: center; }
  .audit-table td { grid-template-columns: minmax(0, 1fr); }
  .audit-table td > .audit-code { grid-column: 1; }
  .login .card { padding: 1.25rem; }
}

@media screen and (max-width: 760px) {
  .report-toolbar {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: .35rem;
    margin: 0 auto .65rem;
  }
  .report-toolbar > .btn, .report-toolbar > button {
    width: auto;
    min-height: 44px;
    padding: .4rem .3rem;
    justify-content: center;
    text-align: center;
    font-size: .85rem;
  }
  .report-toolbar .report-back { justify-self: start; grid-row: 1; grid-column: 1 / 3; border-color: transparent; background: transparent; }
  .report-toolbar #print { justify-self: end; grid-row: 1; grid-column: 3; border-color: transparent; background: transparent; }
  .report-toolbar #finish-report, .report-toolbar #mail { grid-column: 3; }
}

@media screen and (max-height: 500px) and (orientation: landscape) {
  .topbar, .proto-progress, .savebar { position: static; }
  .topbar { min-height: 48px; height: auto; padding-top: env(safe-area-inset-top); }
  .topbar .nav { top: 48px; max-height: calc(100dvh - 48px); }
  .proto-progress .grow { flex: 1 1 180px; }
  .sig-wrap canvas { height: 130px; }
  .modal-bg { padding-top: .25rem; padding-bottom: .25rem; }
}

@media screen and (max-height: 500px) and (orientation: landscape) {
  :root { --mobile-tabs-height: 56px; }
  .bottom-tabs > a, .bottom-tabs > button { flex-direction: row; gap: .35rem; font-size: .72rem; padding: .25rem; }
}

@media print {
  .pwa-panel, .offline-photos, #app-settings, .bottom-tabs, .app-drawer { display: none !important; }
  .mobile-row-label { display: none !important; }
}

@media screen and (max-width: 900px) {
  .journal-entry { grid-template-columns: minmax(0, 1fr); align-items: stretch; }
  .journal-fire { grid-template-columns: minmax(0, 1fr); }
  .journal-entry .field-head { align-items: center; }
  .journal-entry .btn-link { min-height: 44px; }
  .journal-entry .field input, .journal-entry .field select { width: 100%; max-width: 100%; }
  .journal-entry .field > label { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); }
  .journal-entry .field select {
    appearance: none;
    min-height: 44px;
    padding-right: 2rem;
    background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 17px) center, calc(100% - 12px) center;
    background-size: 5px 5px;
    background-repeat: no-repeat;
  }
  .journal-entry .field select + input { margin-top: .35rem; }
  .journal-sign { white-space: normal; }
  .journal-details { margin-top: 1rem; }
}
