/* ===========================================================================
   OPERA · Phone layout
   iPhone users get OPERA through the browser (there is no iOS app), so this is
   their whole product. Loaded after dashboard.css; everything that changes the
   desktop layout sits inside the 720px query below.
   =========================================================================== */

/* Phone-only pieces stay out of the desktop layout. */
.phone-nav, .phone-sheet-overlay, .phone-only { display: none; }
/* The sidebar carries the brand above phone width. */
.topbar .brand { display: none; }

/* Tasks loses its filter rail below 1100px, so tablets need the button too. */
.narrow-only { display: none; }
@media (max-width: 1100px) {
  /* The page keeps its width; the open sidebar floats over it. */
  .app-shell.side-open { padding-left: var(--side-rail); }
  .side-open .side { box-shadow: var(--shadow-lg); }
  .side-scrim { display: block; position: fixed; inset: 0; z-index: 29; background: rgba(15, 23, 42, 0.32); }
  .narrow-only { display: inline-flex; }
  .tasks-layout { flex-direction: column; align-items: stretch; }
  .tasks-side.is-open {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    position: static; width: 100%; padding: 14px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  }
}

/* A hover state sticks after a tap on touch screens. */
@media (hover: none) {
  .btn:hover { transform: none; filter: none; }
  .ocard-undo { opacity: 1; }
}

@media (max-width: 720px) {
  :root { --phone-top-h: 52px; --phone-nav-h: 58px; --phone-gutter: 14px; }

  html { -webkit-text-size-adjust: 100%; }
  body {
    background-attachment: scroll; /* fixed backgrounds repaint badly on iOS */
    min-height: 100dvh;
    -webkit-tap-highlight-color: transparent;
  }
  button, a, .tab, .ocard, .task-row, .select-line { touch-action: manipulation; }

  /* iOS zooms the page on focus for anything under 16px. */
  input, select, textarea, .modal textarea, .tasks-detail textarea, .utable td input[type="text"], .utable td select { font-size: 16px; }
  input[type="date"], input[type="datetime-local"], input[type="time"] { min-width: 0; text-align: left; }
  input::-webkit-date-and-time-value { text-align: left; }

  /* Comfortable tap targets. */
  .btn, .btn.sm { min-height: 44px; }
  .btn.sm { padding: 0 14px; font-size: 14px; }
  .pick-toggle { min-height: 36px; }
  .modal-pipeline-actions select { height: 44px; }
  .team-chip button, .row-chip button { min-width: 28px; min-height: 28px; }
  .ocard-undo { width: 32px; height: 32px; }

  /* --- Shell --- */
  .topbar {
    display: flex; flex-wrap: nowrap; align-items: center;
    min-height: calc(var(--phone-top-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) calc(var(--phone-gutter) + env(safe-area-inset-right)) 0 calc(var(--phone-gutter) + env(safe-area-inset-left));
  }
  .topbar > .row { min-width: 0; gap: 10px; }
  .app-shell, .app-shell.side-open { padding-left: 0; }
  .side, .side-scrim { display: none; }
  .topbar-right { display: none; }
  .topbar .brand { display: inline-flex; font-size: 0; letter-spacing: 0; padding: 0; gap: 0; }

  main {
    padding: 14px calc(var(--phone-gutter) + env(safe-area-inset-right))
             calc(var(--phone-nav-h) + env(safe-area-inset-bottom) + 24px)
             calc(var(--phone-gutter) + env(safe-area-inset-left));
  }

  .phone-nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
    display: flex; padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .phone-nav-item {
    position: relative; flex: 1 1 0; min-width: 0; height: var(--phone-nav-h);
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    border: none; background: none; color: var(--muted); font: inherit; font-size: 10.5px; font-weight: 600;
  }
  .phone-nav-item.active { color: var(--primary); }
  .phone-nav-item span:not(.tab-count) { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .phone-nav-item .tab-count { position: absolute; top: 5px; left: calc(50% + 6px); margin: 0; }

  .phone-sheet-overlay {
    position: fixed; inset: 0; z-index: 55; display: flex; align-items: flex-end;
    background: rgba(15, 23, 42, 0.55); animation: overlay-in .16s ease;
  }
  .phone-sheet {
    width: 100%; max-height: 85dvh; overflow-y: auto; overscroll-behavior: contain;
    padding: 8px 8px calc(10px + env(safe-area-inset-bottom));
    background: var(--surface); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    animation: phone-sheet-in .2s cubic-bezier(.16, 1, .3, 1);
  }
  .phone-sheet-who { padding: 10px 12px 12px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
  .phone-sheet-who b { display: block; font-size: 15px; }
  .phone-sheet-row {
    display: flex; align-items: center; gap: 12px; width: 100%; min-height: 48px; padding: 0 12px;
    border: none; border-radius: var(--radius-sm); background: none; color: var(--text);
    font: inherit; font-size: 15px; font-weight: 500; text-align: left;
  }
  .phone-sheet-row.active { background: var(--primary-tint); color: var(--primary); }
  .phone-sheet-row svg { flex: none; color: var(--muted); }
  .phone-sheet-row.active svg { color: inherit; }
  .phone-sheet-row .tab-count { margin-left: auto; }
  .phone-sheet-row.danger { color: var(--danger); }
  .phone-sheet-hint { margin: 6px 12px 4px; padding-top: 10px; border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted); }

  .phone-only { display: inline-flex; }
  .toast {
    bottom: calc(var(--phone-nav-h) + env(safe-area-inset-bottom) + 12px);
    width: max-content; max-width: calc(100vw - 28px); text-align: center;
  }

  /* --- Toolbars: filters wrap instead of pushing the page wide.
     !important beats the fixed widths set inline for the desktop toolbar. --- */
  .board-toolbar { flex-wrap: wrap; align-items: stretch; }
  .board-toolbar > *, .board-toolbar .row { min-width: 0; max-width: 100%; }
  .board-toolbar .row { flex-wrap: wrap; }
  .board-toolbar input, .board-toolbar select { width: 100% !important; min-width: 0 !important; }
  .board-toolbar .row > input, .board-toolbar .row > select, .board-toolbar .row > .pipeline-range { flex: 1 1 140px; min-width: 0; }
  .pipeline-range { flex-wrap: wrap; max-width: 100%; }
  .os-filter { overflow-x: auto; flex-wrap: nowrap !important; }
  .tabs { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tab { white-space: nowrap; }

  /* --- Boards: one column per swipe. The summary and the board share a
     scroller, so both must use the same column width to stay lined up. --- */
  .board-scroll {
    margin: 0 calc(var(--phone-gutter) * -1); padding: 0 var(--phone-gutter) 6px;
    scroll-snap-type: x proximity; scroll-padding: 0 var(--phone-gutter);
    -webkit-overflow-scrolling: touch;
  }
  .board.board, .pipeline-summary:not(.os-summary) {
    grid-template-columns: none !important; grid-auto-flow: column; grid-auto-columns: 84vw; gap: 12px;
  }
  .col, .psum { scroll-snap-align: start; }
  .pipeline-summary.os-summary { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  /* A dozen KPI cards would bury the board, so they wait behind a button and
     then run as the same ticker as on desktop. !important beats the x-show that reveals them. */
  .metrics-row:not(.is-open) { display: none !important; }
  .metrics-row { margin: 0 calc(var(--phone-gutter) * -1) 14px; }
  .metrics-toggle { width: 100%; margin: 14px 0 12px; justify-content: center; }

  /* --- Tables become labelled cards. labelTablesForPhone() in dashboard.js
     copies each column heading onto its cells as data-label. --- */
  .table-scroll { overflow-x: visible; }
  .utable, .leads-table { display: block; min-width: 0; width: 100%; background: none; border: none; box-shadow: none; }
  .utable > thead { display: none; }
  .utable > tbody { display: flex; flex-direction: column; gap: 10px; }
  .utable > tbody > tr {
    display: block; padding: 6px 14px; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius);
  }
  .utable > tbody > tr > td {
    display: flex; align-items: center; justify-content: space-between; gap: 14px;
    min-height: 34px; padding: 7px 0; border-bottom: 1px solid var(--border);
    text-align: right; overflow-wrap: anywhere;
  }
  .utable > tbody > tr > td:last-child { border-bottom: none; }
  .utable > tbody > tr > td:empty { display: none; }
  .utable > tbody > tr > td[data-label]::before {
    content: attr(data-label); flex: none; max-width: 42%; text-align: left;
    font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted);
  }
  .utable > tbody > tr > td[data-label=""]::before { content: none; }
  .utable > tbody > tr > td[data-label=""], .utable > tbody > tr > td[colspan] { justify-content: flex-start; text-align: left; }
  .utable td select, .utable td input[type="text"] { min-width: 0; }

  /* --- Popups: bottom sheets that use the whole width. --- */
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal, .modal.panel {
    width: 100%; max-width: none !important; max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0; border-bottom: none;
    padding: 18px var(--phone-gutter) calc(18px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .modal-actions { flex-wrap: wrap; }
  .panel { padding: 16px; max-width: none; }

  /* --- Tasks --- */
  .task-add input[type="text"] { min-width: 0; flex-basis: 100%; }
  .task-add .prod-search { flex-basis: 100% !important; min-width: 0 !important; }
  .task-add select, .task-add input[type="date"] { flex: 1 1 130px; width: auto; min-width: 0; }
  .task-cell-main { flex-wrap: wrap; }
  .tasks-detail .panel { min-height: 100%; border: none; border-radius: 0; box-shadow: none; }

  /* --- Chat: the list, then the thread, one at a time. --- */
  .chat-layout { display: block; }
  .chat-side { position: static; width: 100%; max-height: none; }
  .chat-layout.has-thread .chat-side { display: none; }
  .chat-layout:not(.has-thread) .chat-main { display: none; }
  .chat-thread {
    height: calc(100dvh - var(--phone-top-h) - var(--phone-nav-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 28px);
  }
  .chat-thread-head { display: flex; align-items: center; gap: 8px; padding: 6px 12px; }
  .chat-bubble { max-width: 85%; }
  .chat-compose { padding: 10px; }
  .chat-compose .row { flex-wrap: wrap; }
  .chat-compose select { order: 3; flex: 1 1 100%; max-width: none !important; }

  /* --- Maps and the rest --- */
  #map { height: calc(100dvh - var(--phone-top-h) - var(--phone-nav-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 40px); }
  #heatmap { height: 60dvh; }
  .map-side, .map-legend { flex-wrap: wrap; white-space: normal; min-width: 0; }
  .settings-layout { align-items: stretch; }
  .settings-sidebar { width: 100%; min-width: 0; -webkit-overflow-scrolling: touch; }
  .settings-nav-item { white-space: nowrap; flex: none; }
  .settings-layout > :not(.settings-sidebar) { min-width: 0; width: 100%; }
  .import-grid { grid-template-columns: minmax(0, 1fr); }
  .login-wrap { min-height: 100dvh; padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
  .login-card { padding: 28px 20px; }
}

@keyframes phone-sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Catalogue: the name gets the full card width; there's no room for a thumbnail. */
@media (max-width: 720px) {
  .utable > tbody > tr.cat-row > td:first-child { display: none; }
  .utable > tbody > tr.cat-row > td:nth-child(2) { display: block; text-align: left; }
  .utable > tbody > tr.cat-row > td:nth-child(2)::before { content: none; }
}
