/*
 * Obernote mobile and touch styles (mobile-usage).
 *
 * Linked on every page AFTER app.css and the page's own <style> block as
 *   <link rel="stylesheet" href="/mobile.css" media="(pointer: coarse)">
 * so it never loads on a desktop with a mouse (AC-001-5); app.css is not edited.
 *
 * Two kinds of rules (plan AD-1):
 *   - touch:   (pointer: coarse) alone; applies to every touch screen, iPad landscape included
 *              (tap targets, hidden delete). The link's media attribute already gates this file,
 *              so touch rules can sit at the top level of a section.
 *   - compact: nested in the AD-1 compact query below (iPhone in both orientations, iPad in
 *              portrait). The same query string is used by js/shell.js through matchMedia.
 *
 * Shell markup (#menu-btn, #back-btn, #nav-backdrop, .nav-account) carries the hidden attribute;
 * only rules inside the compact query may override it.
 *
 * Each task edits only its own section below, so parallel edits do not collide.
 */

/* Shared shell: drawer, menu button, Back button, safe areas (mobile-usage::T003) */

/* -- Touch: every touch screen, iPad landscape included (no [hidden] overrides here) -- */

/* Keep iOS from enlarging text after a rotation (AC-001-3). */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* The visible viewport, not the one behind Safari's toolbars. */
.layout {
  height: 100vh;
  height: 100dvh;
}

/*
 * .main as a grid: menu button, Back button, and the topbar share the first row,
 * the page content fills the rest. With both buttons hidden (iPad landscape in a
 * browser tab) the first two columns collapse and the page looks as on the desktop.
 */
.main {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}
.main > .topbar {
  grid-column: 3;
  grid-row: 1;
}
.main > .content {
  grid-column: 1 / -1;
  grid-row: 2;
}

/* Search stays at the top of the page while the keyboard is open (EC-001-2). */
.topbar {
  position: sticky;
  top: 0;
}

/* Menu and Back buttons: placement and look only; whether they show is decided elsewhere. */
#menu-btn,
#back-btn {
  grid-row: 1;
  align-self: stretch;
  min-width: 44px;
  min-height: 44px;
  padding: 0 0.75rem;
  background: var(--surface);
  color: var(--text);
  border-radius: 0;
  border-bottom: 1px solid var(--border);
  -webkit-tap-highlight-color: transparent;
}
#menu-btn {
  grid-column: 1;
  font-size: 20px;
  line-height: 1;
}
#back-btn {
  grid-column: 2;
  font-size: 15px;
  font-weight: 500;
  color: var(--primary);
  white-space: nowrap;
}

/* Tap targets of at least 44 x 44 (AC-001-4). */
.sidebar a,
.sidebar button {
  min-height: 44px;
  min-width: 44px;
}
.sidebar a {
  display: flex;
  align-items: center;
}
.sidebar .clear-btn {
  padding: 0 0.6rem;
}

/* Safe areas on the sidebar (left edge, home indicator). */
.sidebar {
  padding-left: env(safe-area-inset-left);
  padding-bottom: env(safe-area-inset-bottom);
  overscroll-behavior: contain;
}

/* Long names wrap instead of widening the page (EC-001-1). */
.sidebar,
.content {
  overflow-wrap: anywhere;
}

/* The 440 px Console panel never gets wider than the screen (AC-001-1, AC-001-6). */
.console-panel {
  max-width: 100vw;
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  #nav-backdrop {
    transition: none;
  }
}

/* -- Compact: iPhone in both orientations, iPad in portrait (AD-1) -- */
@media (pointer: coarse) and (max-width: 1023px), (pointer: coarse) and (orientation: portrait) {
  /* .main takes the full width; the safe area on the left moves from the sidebar to .main. */
  .main {
    width: 100%;
    padding-left: env(safe-area-inset-left);
  }

  .topbar {
    padding-left: 0.5rem;
    padding-right: 0.75rem;
  }

  /* Off-canvas drawer: out of the page flow, off screen until body.nav-open. */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 300;
    width: 280px;
    min-width: 0;
    max-width: 85vw;
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0s linear 0.2s;
  }
  body.nav-open .sidebar {
    transform: none;
    visibility: visible;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.35);
    transition: transform 0.2s ease, visibility 0s;
  }

  /* Backdrop under the drawer while it is open; a tap on it closes the drawer (shell.js). */
  body.nav-open #nav-backdrop {
    display: block;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 290;
    background: rgba(0, 0, 0, 0.45);
  }

  /* Shell markup shown on compact screens only. */
  #menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .nav-account {
    display: block;
  }

  /* Wide tables on secondary pages scroll inside their own box, not the page (AC-001-6). */
  .user-table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
  }
  #tag-content {
    max-width: 100%;
    overflow-x: auto;
  }

  /* GAP-004: while a note is open, only the notes page's own "Back to list" shows. */
  body.note-open #back-btn {
    display: none;
  }
}

/* ==== section: files ==== */

/* Files page on iPhone and iPad (mobile-usage::T004). Scoped to the Files page's own markup:
   tags.js also renders a .file-table, and upload.html has a #drop-zone (builder-notes-T010). */

/* -- Touch: every touch screen, iPad landscape included -- */

/* No drag and drop on touch; the Upload button (file picker) stays (AC-001-1). */
.drop-zone {
  display: none;
}
.upload-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 1rem;
  border-radius: var(--radius);
  font-size: 16px;
}

/* Tap targets (AC-001-4): the document name and the sort toggles (beat .btn-sm). */
#file-list-container .file-open {
  display: flex;
  align-items: center;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}
/* The name sits in .file-name-wrap (file-page-refresh-button-email-polling US-002). Touch has no
   row buttons, so the name fills the wrapper: the whole width stays one tap target (AC-002-4). */
#file-list-container .file-name-wrap .file-open {
  flex: 1 1 auto;
}
#file-list-container .sort-toggle {
  min-height: 44px;
  padding: 0 0.75rem;
  font-size: 14px;
}
#file-list-container .file-table tr:hover td {
  background: none;
}
/* Untagged section controls (Refresh, Poll now; file-page-refresh-button-email-polling US-003):
   44 px tap targets (beat .btn-sm), wrapping onto a second line on narrow screens. */
#file-list-container .section-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}
#file-list-container .section-controls button {
  min-height: 44px;
  padding: 0 0.75rem;
  font-size: 14px;
}
/* Load everything below the newest 50 tagged documents (files-page-tagged-limit): 44 px tap target. */
#file-list-container .load-everything {
  min-height: 44px;
  padding: 0 0.75rem;
  font-size: 14px;
}

/* Document viewer overlay (spike T012): top bar with Back and Share, the document below. */
.file-viewer {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
/* The display rule above beats the hidden attribute, so hidden needs its own rule. */
.file-viewer[hidden] {
  display: none;
}
.file-viewer-bar {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.file-viewer-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.75rem;
  background: none;
  color: var(--primary);
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.file-viewer-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  font-weight: 600;
  font-size: 15px;
}
.file-viewer-loading {
  flex-shrink: 0;
  padding: 0.5rem 1rem;
  text-align: center;
  font-size: 14px;
  color: var(--text-muted);
  background: var(--bg);
}
.file-viewer-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom);
}
.file-viewer-doc {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  border: 0;
}
img.file-viewer-doc {
  object-fit: contain;
}
.file-viewer-fallback {
  padding: 2rem 1rem;
  text-align: center;
}

/* Types the viewer cannot show: name and "Share / Open in…" (EC-004-2). */
.file-viewer-other {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 2rem 1rem;
  text-align: center;
  overflow-wrap: anywhere;
}
.file-viewer-name {
  font-weight: 600;
  font-size: 16px;
}
.file-viewer-other .file-viewer-openin {
  color: #fff;
  background: var(--primary);
  padding: 0 1.25rem;
}

/* -- Compact: one entry per document instead of the wide table (AC-004-2); iPad landscape keeps it -- */
@media (pointer: coarse) and (max-width: 1023px), (pointer: coarse) and (orientation: portrait) {
  #file-list-container .file-table,
  #file-list-container .file-table tbody {
    display: block;
    width: 100%;
  }
  #file-list-container .file-table thead {
    display: none;
  }
  #file-list-container .file-table tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem 0.75rem;
    padding: 0.4rem 0.25rem 0.6rem;
    border-bottom: 1px solid var(--border);
  }
  #file-list-container .file-table td {
    display: block;
    padding: 0;
    border-bottom: none;
  }
  #file-list-container .file-table .file-name-cell {
    flex: 1 1 100%;
    min-width: 0;
  }
  #file-list-container .file-table .date-cell {
    order: 1;
    font-size: 13px;
  }
  #file-list-container .file-table .tags-cell {
    order: 2;
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
  }
  /* Size and OCR status are desktop details. */
  #file-list-container .file-table .size-cell,
  #file-list-container .file-table .ocr-cell {
    display: none;
  }
  #file-list-container .file-section .sort-toggle {
    margin-bottom: 0.25rem;
  }
}


/* ==== section: notes ==== */

/* Notes on iPhone and iPad (mobile-usage::T005, AD-8). */

/* -- Touch: every touch screen, iPad landscape included -- */

/* Tap targets (AC-001-4). Edit and Done carry the hidden attribute; notes.js decides which shows. */
#note-edit-btn,
#note-done-btn,
#note-back-btn {
  min-width: 44px;
  min-height: 44px;
  padding: 0 0.9rem;
  font-size: 14px;
}
.note-item {
  min-height: 44px;
}

/* Checklist boxes large enough to tick with a finger (beats app.css .editor-body li.task-item input). */
#editor-body input[type="checkbox"] {
  width: 24px;
  height: 24px;
  margin-top: 2px;
  flex-shrink: 0;
}

/* Images and tables fit the note width (AC-005-2). */
#editor-body img {
  max-width: 100%;
  height: auto;
}
#editor-body table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
}

/* Reading mode: no caret; the text can still be selected and copied. */
#editor-body[contenteditable="false"] {
  caret-color: transparent;
}

/* "This note is empty" (EC-005-1); shown by notes.js in reading mode only. */
.note-empty-msg {
  padding: 1rem 1.25rem;
  font-style: italic;
}

/* -- Compact: the note list or the open note, not both (AC-005-1); iPad landscape keeps both (AC-005-7) -- */
@media (pointer: coarse) and (max-width: 1023px), (pointer: coarse) and (orientation: portrait) {
  .note-list {
    width: 100%;
    min-width: 0;
    border-right: none;
  }
  body.note-open .note-list {
    display: none;
  }

  /* notes.js sets the editor's display inline, so hiding it needs !important. */
  body:not(.note-open) .editor-area {
    display: none !important;
  }
  .no-note-msg {
    display: none !important;
  }

  body.note-open #note-back-btn {
    display: inline-flex;
    align-items: center;
  }

  .editor-header {
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
  }
  .title-input {
    min-width: 0;
    flex-basis: 100%;
  }
  .toolbar {
    padding: 0.4rem 0.75rem;
  }
  .editor-body {
    padding: 0.75rem;
  }
}

/* ==== section: account ==== */

/* Account page on iPhone and iPad (mobile-usage::T008, US-001/AC-1, US-006). */

/* -- Touch: every touch screen, iPad landscape included -- */

/* 16 px text: iOS does not zoom in when a field gets focus (AC-001-1). */
#device-name,
#new-password,
#confirm-password,
#totp-confirm-code {
  font-size: 16px;
}
#device-name {
  min-height: 44px;
}

/* Tap targets of at least 44 x 44 (AC-001-4). */
#device-create-btn,
#device-key-copy,
.device-revoke-btn,
#pw-btn,
#totp-reset-btn,
#totp-cancel-btn,
#totp-confirm-btn {
  min-height: 44px;
  min-width: 44px;
}
.device-revoke-btn {
  padding: 0 0.9rem;
  font-size: 14px;
}

/* "Add Shortcut" as a button-sized link; its paragraph carries the hidden attribute, not the link. */
#device-shortcut-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 1rem;
  font-size: 16px;
}

/* The 47-character key wraps at any character, so it is never cut off on a phone. */
#device-key {
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-all;
  font-size: 14px;
}

/* Long device names wrap instead of widening the page (EC-001-1). */
.device-name {
  overflow-wrap: anywhere;
}

/* -- Compact: iPhone in both orientations, iPad in portrait (AD-1) -- */
@media (pointer: coarse) and (max-width: 1023px), (pointer: coarse) and (orientation: portrait) {
  /* Field and Set up sharing stacked, both full width. */
  .device-create-row {
    flex-direction: column;
    align-items: stretch;
  }
  .device-create-row .form-group {
    flex-basis: auto;
  }
  #device-create-btn {
    width: 100%;
  }

  /* Key on its own line, Copy below it. */
  .device-key-row {
    flex-direction: column;
    align-items: stretch;
  }
  .device-key {
    flex-basis: auto;
  }

  /* Revoke under the name and dates, aligned right. */
  .device-info {
    flex-basis: 100%;
  }
  .device-revoke-btn {
    margin-left: auto;
  }
}


/* ==== section: forms ==== */

/*
 * Login and Upload pages (mobile-usage::T010). Touch rules (pointer: coarse, the link's media
 * attribute); every selector is scoped to .login-card, #qr-img, or .upload-page, because this file
 * is shared and files.html also has a #drop-zone.
 */

/* -- Login: card, 2FA step, first-time 2FA setup (AC-001-1) -- */

/* The card leaves a margin on narrow phones and scrolls with the page when taller than the screen. */
.login-card {
  width: calc(100% - 2rem);
  margin: 1rem 0;
  padding: 1.5rem;
}

/* 16 px text: iOS does not zoom in when a field gets focus. */
.login-card input {
  font-size: 16px;
}

/* The QR code never gets wider than the card. */
#qr-img {
  max-width: 100%;
  height: auto;
}

/* Sign in, Verify, Activate 2FA: full-width tap targets (AC-001-4). */
.login-card .btn-primary {
  width: 100%;
  min-height: 44px;
  font-size: 16px;
}

/* -- Upload: the file picker instead of drag and drop (AC-001-1, GAP-009) -- */

/*
 * The drop zone stays (it holds the picker and opens it on tap); only the drag-and-drop
 * text and the dashed border go away.
 */
.upload-page #drop-zone {
  border: none;
  padding: 0;
}
.upload-page #drop-zone p {
  display: none;
}

/* "Choose files" as the prominent, full-width action. */
.upload-page .choose-files-btn {
  display: block;
  width: 100%;
  min-height: 48px;
  font-size: 16px;
}

/* "Back to files" as a tap target, not an inline link (AC-001-4). */
.upload-page .back-link {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
}

/* Progress rows fit the screen width: the name column may shrink, long texts wrap. */
.upload-page .file-row {
  grid-template-columns: minmax(0, 1fr) auto;
  overflow-wrap: anywhere;
}
.upload-page .file-row .status {
  white-space: normal;
  max-width: 40vw;
  text-align: right;
}

/* An error message gets its own line under the progress bar. */
.upload-page .file-row[data-status="error"] .status {
  grid-column: 1 / -1;
  grid-row: 3;
  max-width: none;
  text-align: left;
}

/* Duplicate question and its buttons span the row. */
.upload-page .duplicate-row .duplicate-info,
.upload-page .duplicate-row .duplicate-actions {
  grid-column: 1 / -1;
}
.upload-page .duplicate-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.upload-page .duplicate-actions button {
  min-height: 44px;
  min-width: 44px;
}
