/*
 * web-overrides.css — web-tool-only styles layered AFTER signing.css by
 * scripts/sync-sign-app.mjs. The extension always runs in a desktop window, so
 * its stylesheet never needed a real phone layout; the web tool does.
 * Keep overrides here — never edit the synced extension CSS.
 *
 * Two distinct phone layouts, because the page serves two states:
 *   1. UPLOAD state  — no document yet. Detected with
 *      `:has(#upload-interface:not(.hidden))`: signing.js appends that element
 *      in `?upload=true` mode and adds `.hidden` to it once a file is loaded.
 *      Everything that only makes sense with a document open (the empty canvas
 *      well, the tools sidebar, zoom, Download) is hidden so the drop zone
 *      fills the screen instead of sitting below a screenful of nothing.
 *   2. DOCUMENT state — document on top, tools as a strip underneath it.
 */

@media (max-width: 780px) {
  /* 100vh on a phone is the *large* viewport — the part under the browser's
     collapsing toolbar is unreachable. dvh tracks the visible area. */
  .container {
    height: 100dvh;
  }

  /* ---------------------------------------------------------------- toolbar */
  .toolbar {
    padding: 8px 10px;
    gap: 8px;
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .toolbar > .btn,
  .toolbar-actions .btn {
    height: 36px;
  }

  #back-btn {
    padding-left: 14px;
    padding-right: 16px;
  }

  .document-name {
    flex: 1 1 120px;
    min-width: 0;
    margin: 0;
  }

  .document-name input,
  #document-name-input {
    min-width: 0;
    max-width: none;
    width: 100%;
    height: 36px;
    font-size: 14px;
  }

  /* The controls drop to their own full-width row so nothing is pushed off
     the right edge (the Download button used to be half off-screen). */
  .toolbar-actions {
    order: 3;
    width: 100%;
    flex-wrap: wrap;
    gap: 6px;
  }

  /* "Saved" is reassurance, not a control — the first thing to go when the
     row is 390px wide. */
  #autosave-status {
    display: none;
  }

  /* An `appearance: none` <select> resolves `width: auto` to the full line
     box; on desktop the crowded row shrinks it back to its 100px min-width,
     but alone on a wrapped phone row it stretched to the whole screen. */
  .toolbar-actions select,
  #zoom-select {
    width: max-content;
    min-width: 0;
    height: 34px;
    padding: 0 28px 0 12px;
    font-size: 13px;
  }

  #zoom-out-btn,
  #zoom-in-btn {
    width: 34px;
    height: 34px;
  }

  #settings-btn-toolbar {
    width: 36px;
    height: 34px;
    padding: 0;
  }

  #finish-btn {
    margin-left: auto;
    height: 34px;
    padding-left: 18px;
    padding-right: 18px;
  }

  /* ----------------------------------------------------------- main content */
  .main-content {
    flex-direction: column;
    min-height: 0;
  }

  /* `.canvas-area` is the flex child of `.main-content` — `#canvas-container`
     is nested inside it, so ordering that one (as this file used to) never
     had any effect and the tools panel stayed above the document. */
  .canvas-area {
    order: 1;
    flex: 1 1 auto;
    min-height: 0;
    padding: 12px 8px;
  }

  .sidebar {
    order: 2;
    width: 100%;
    flex: 0 0 auto;
    max-height: 38dvh;
    padding: 10px 14px 12px;
    overflow-y: auto;
    border-right: none;
    border-top: 1px solid var(--sd-line, rgba(128, 128, 128, 0.25));
  }

  .sidebar-section {
    margin-bottom: 16px;
  }

  /* Tool cards and saved signatures read better as horizontal strips on a
     narrow screen — two columns of stacked cards eats the whole panel. */
  .tools-container {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 10px;
    padding-bottom: 6px;
    -webkit-overflow-scrolling: touch;
  }

  .tools-container .tool-card {
    flex: 0 0 auto;
    min-width: 78px;
    padding: 10px 8px;
    gap: 6px;
  }

  .signatures-container {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 6px;
    -webkit-overflow-scrolling: touch;
  }

  .signatures-container .signature-card {
    flex: 0 0 60%;
  }

  /* Touch has no hover, so the per-signature delete button would never
     appear; keep it visible on coarse pointers. */
  .signature-delete-btn {
    opacity: 1;
  }

  /* ---------------------------------------------------------- upload state */
  .container:has(#upload-interface:not(.hidden)) .sidebar,
  .container:has(#upload-interface:not(.hidden)) .canvas-area,
  .container:has(#upload-interface:not(.hidden)) .document-name,
  .container:has(#upload-interface:not(.hidden)) #zoom-out-btn,
  .container:has(#upload-interface:not(.hidden)) #zoom-select,
  .container:has(#upload-interface:not(.hidden)) #zoom-in-btn,
  .container:has(#upload-interface:not(.hidden)) #text-select-toggle,
  .container:has(#upload-interface:not(.hidden)) #finish-btn,
  .container:has(#upload-interface:not(.hidden)) .mobile-sign-bar {
    display: none;
  }

  /* Only the settings gear is left, so the row belongs beside Back. */
  .container:has(#upload-interface:not(.hidden)) .toolbar-actions {
    order: 0;
    width: auto;
    margin-left: auto;
  }

  .upload-interface {
    padding: 20px 16px 32px;
    height: auto;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
  }

  .upload-zone {
    padding: 32px 20px;
    max-width: none;
  }

  .upload-zone svg {
    width: 48px;
    height: 48px;
    margin-bottom: 14px !important;
  }

  .upload-zone h2 {
    font-size: 20px;
  }

  .upload-zone p {
    font-size: 14px;
    margin-bottom: 20px;
  }

  /* A file picker is a tap target, not a drop target, on a phone. */
  .upload-zone #upload-btn {
    width: 100%;
    max-width: 280px;
  }

  /* ---------------------------------------------------------------- modals */
  .modal-content {
    width: calc(100vw - 24px);
    max-width: none;
    max-height: 92dvh;
    overflow-y: auto;
  }

  /* The signature pad's own size is set inline by signature-manager.js (which
     fits it to the modal); this is only a backstop for the typed preview,
     whose 400px is a plain stylesheet value. */
  #typed-preview-canvas {
    max-width: 100%;
  }

  /* Four buttons in one row is ~30px each on a phone. */
  .draw-here-actions {
    flex-wrap: wrap;
    gap: 8px;
  }
}

/* ---------------------------------------------------- signature sheet (phone)
   Signing with a finger in a 288×150 box is the single worst thing about this
   screen on a phone. The QR hand-off page (phone/sign.html) gets this right by
   giving the pad the whole display, so the signature dialog becomes a
   full-screen sheet here and the canvas grows into whatever is left after the
   header, tabs and buttons. Every step of the chain needs to be a column flex
   box with min-height:0, or the canvas' own height wins and the sheet scrolls
   instead.

   Keyed on height as well as width: a phone turned sideways is 844×390 — wider
   than any phone breakpoint and shorter than every desktop one, and turning it
   is exactly what the pad asks people to do. */
@media (max-width: 780px), (max-height: 520px) {
  #signature-modal .modal-content {
    width: 100vw;
    height: 100dvh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* Every millimetre of width is width the signature gets to use. */
  #signature-modal .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px 12px 4px;
  }

  #signature-modal .modal-header {
    padding: 14px 16px 4px;
  }

  #signature-modal .modal-footer {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  }

  #signature-modal .tab-content.active {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  /* `.hidden` is display:none !important, so the QR view still hides. */
  #draw-here-section {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  #signature-modal .drawing-canvas-container {
    flex: 1 1 auto;
    min-height: 120px;
    margin-bottom: 12px;
  }

  /* The stylesheet caps the pad at 400×150 — the cap has to go, or a canvas
     sized to the sheet would be squashed back down and every stroke would land
     away from the finger. */
  #drawing-canvas {
    max-width: none;
    max-height: none;
  }

  #signature-modal .draw-here-actions {
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 0;
  }

  #signature-modal .modal-footer .btn {
    flex: 1;
    height: 46px;
  }
}

/* Sideways is where a signature actually fits, so the sheet spends nothing it
   doesn't have to on chrome: the instruction line goes, the pad keeps the rest. */
@media (max-height: 520px) {
  #signature-modal #draw-here-section .method-description,
  #signature-modal .draw-instruction {
    display: none;
  }

  #signature-modal .modal-header {
    padding: 8px 16px 0;
  }

  #signature-modal .modal-header h2 {
    font-size: 16px;
  }

  #signature-modal .modal-body {
    padding: 8px 12px 0;
  }

  #signature-modal .signature-tabs {
    margin-bottom: 8px;
  }

  #signature-modal .modal-footer {
    padding: 8px 16px calc(8px + env(safe-area-inset-bottom, 0px));
  }

  #signature-modal .modal-footer .btn {
    height: 40px;
  }
}

/* Fallback for engines without :has() — keep the empty canvas well from
   claiming half the screen before a document exists. */
@supports not selector(:has(*)) {
  @media (max-width: 780px) {
    .canvas-area {
      min-height: 0;
    }
  }
}
