/* FTJ Mobile Cockpit — phone-first layout.
 * Tab panels stack full-screen above the bottom nav. 44px+ touch targets. */

#tabs {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.tab {
  flex: 1 1 auto;
  min-height: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.tab[hidden] { display: none; }
.tab.active { display: flex; }

.tab-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6em max(1em, env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.tab-header h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
}
/* Tab-headers that only hold controls (no redundant h1 — the active tab in
   the nav already tells you where you are). Used by Docs + Drafts. */
.tab-header-controls {
  padding: 0.5em 0.6em;
  gap: 0.5em;
}
.tab-header-spacer { flex: 1; }

/* Tab nav at TOP. NO position:sticky — body has overflow:hidden which breaks
   sticky on iOS Safari (collapses to zero height; the iOS notch then eats
   the content below). Flex flow naturally places it at top. The
   safe-area-inset-top padding clears the notch. */
#tab-nav {
  flex: 0 0 auto;
  display: flex;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0);
  z-index: 50;
}
#tab-nav .tab-link {
  flex: 1;
  text-align: center;
  padding: 0.7em 0.4em;
  color: var(--fg-dim);
  font-size: 0.85rem;
  min-height: var(--touch);
  display: flex;
  align-items: center;
  justify-content: center;
}
#tab-nav .tab-link.active {
  color: var(--accent);
  font-weight: 600;
  background: var(--bg-elev);
  border-bottom: 3px solid var(--accent);
}

/* Floating mic FAB at bottom-right — thumb-reachable. Sits above the chat
   input strip, fading when other tabs are active (only shows on chat). */
#mic-fab-container {
  position: fixed;
  right: 1em;
  bottom: calc(env(safe-area-inset-bottom, 0) + 88px); /* clears the chat input */
  display: flex;
  align-items: center;
  gap: 0.6em;
  z-index: 60;
}
#mic-fab-container[hidden] { display: none; }
.voice-fab {
  width: 64px;
  height: 64px;
  min-height: 64px;
  border-radius: 50%;
  font-size: 1.7rem;
  padding: 0;
  background: var(--accent);
  color: var(--bg);
  border: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  /* Prevent iOS from treating the emoji as selectable text — was causing
     the mic glyph to show selection handles when pressed. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}
.voice-fab.recording {
  background: var(--danger);
  color: white;
  animation: pulse 1.4s ease-in-out infinite;
}
.voice-fab.recording.auto {
  background: var(--accent);
  animation: pulse-auto 1.6s ease-in-out infinite;
}
.prefs-fab {
  width: 40px;
  height: 40px;
  min-height: 40px;
  border-radius: 50%;
  font-size: 1.1rem;
  padding: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--fg-dim);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}
/* Stop iOS from selecting button-emoji text in the chat input row too. */
.attach-inline-btn, .send-btn, .row-actions, .sheet-action, .upload-btn, .refresh-btn {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

/* ── Chat tab ───────────────────────────────────────────────────────────── */
.stream {
  flex: 1;
  overflow-y: auto;
  padding: 0.8em;
  display: flex;
  flex-direction: column;
  gap: 0.7em;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}
.turn {
  max-width: 92%;
  padding: 0.7em 0.9em;
  border-radius: var(--radius);
  word-wrap: break-word;
}
.turn.user {
  align-self: flex-end;
  background: var(--accent-dim);
  color: white;
}
.turn.assistant {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--border);
}
.turn.error {
  align-self: stretch;
  background: rgba(255, 92, 92, 0.12);
  border: 1px solid var(--danger);
}

.chat-input {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 0.4em;
  padding: 0.5em;
  padding-bottom: calc(0.5em + env(safe-area-inset-bottom, 0));
  background: var(--panel);
  border-top: 1px solid var(--border);
  width: 100%;
  box-sizing: border-box;
}
.chat-input textarea {
  flex: 1 1 auto;
  min-width: 0;
  min-height: var(--touch);
  max-height: 30vh;
}
.chat-input .send-btn,
.chat-input .attach-inline-btn {
  flex: 0 0 auto;
}
.send-btn {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 600;
  padding: 0 1.2em;
}
.send-btn.primary { padding: 0 1.5em; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 92, 92, 0.4); }
  50%      { box-shadow: 0 0 0 14px rgba(255, 92, 92, 0); }
}
@keyframes pulse-auto {
  0%, 100% { box-shadow: 0 0 0 0 rgba(61, 220, 151, 0.4); }
  50%      { box-shadow: 0 0 0 16px rgba(61, 220, 151, 0); }
}

.attach-inline-btn {
  font-size: 1.4rem;
  width: var(--touch);
  min-width: var(--touch);
  height: var(--touch);
  min-height: var(--touch);
  padding: 0;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.attach-inline-btn:active {
  background: var(--accent-dim);
}
.compose-tip {
  background: var(--bg-elev);
  border-left: 3px solid var(--accent);
  padding: 0.7em 1em;
  margin: 0.6em 0.8em;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  color: var(--fg-dim);
}
.compose-tip strong { color: var(--accent); }
.compose-tip a { color: var(--link); }
.compose-from {
  display: flex;
  gap: 0.6em;
  padding: 0.6em 0.8em;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  align-items: center;
}
.compose-from .meta { color: var(--fg-dim); }
.compose-from strong { color: var(--accent); font-weight: 600; }

.attachments-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4em;
  padding: 0 0.6em 0.6em;
}
.attachments-strip:empty { padding: 0; }
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.3em 0.6em;
  font-size: 0.85rem;
}
.attachment-chip button {
  background: transparent;
  border: none;
  padding: 0;
  color: var(--fg-dim);
  min-height: 0;
  cursor: pointer;
}

/* ── Docs tab ───────────────────────────────────────────────────────────── */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.3em;
  padding: 0.6em 0.8em;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  white-space: nowrap;
  font-size: 0.9rem;
}
.breadcrumb .crumb {
  color: var(--link);
  cursor: pointer;
}
.breadcrumb .sep {
  color: var(--fg-muted);
}

.listing {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}
.listing-row {
  display: flex;
  align-items: center;
  gap: 0.8em;
  padding: 0.8em 1em;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  min-height: var(--touch);
}
.listing-row:active {
  background: var(--bg-elev);
}
.listing-row .icon {
  font-size: 1.3rem;
  color: var(--accent);
  flex-shrink: 0;
}
.listing-row .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.listing-row .meta {
  font-size: 0.78rem;
  color: var(--fg-muted);
}

.upload-btn, .refresh-btn {
  font-size: 1.2rem;
  width: var(--touch);
  height: var(--touch);
  padding: 0;
  background: var(--bg-elev);
}

#docs-search {
  flex: 1;
  margin: 0 0.5em;
  min-height: 36px;
  font-size: 0.9rem;
  background: var(--bg-elev);
  padding: 0.4em 0.7em;
}
#docs-search::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  background: var(--fg-dim);
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/></svg>") center / 14px no-repeat;
  cursor: pointer;
}

.row-actions {
  font-size: 1.4rem;
  width: 32px;
  min-width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  background: transparent;
  border: none;
  color: var(--fg-dim);
  margin-left: 0.4em;
}
.row-actions:hover, .row-actions:active {
  color: var(--accent);
}

/* ── Bottom sheet (file action chooser) ─────────────────────────────────── */
.bottom-sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade-in 0.15s ease-out;
}
.bottom-sheet {
  width: 100%;
  max-width: 520px;
  background: var(--panel);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  padding: 0.6em 0.6em calc(0.6em + env(safe-area-inset-bottom, 0));
  display: flex;
  flex-direction: column;
  gap: 0.4em;
  animation: slide-up 0.2s ease-out;
}
.bottom-sheet-title {
  text-align: center;
  font-size: 0.9rem;
  color: var(--fg-dim);
  padding: 0.6em;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.2em;
  word-break: break-all;
}
.sheet-action.primary {
  background: var(--accent);
  color: var(--bg);
  font-weight: 600;
  border-color: var(--accent);
}
.sheet-action {
  text-align: left;
  padding: 0.9em 1em;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  min-height: var(--touch);
}
.sheet-action.cancel {
  margin-top: 0.3em;
  background: transparent;
  color: var(--fg-dim);
  text-align: center;
}
/* Voice-prefs toggle rows — checkbox left, label right, full-width tap. */
.sheet-action.voice-toggle {
  display: flex;
  align-items: center;
  gap: 0.8em;
  cursor: pointer;
}
.sheet-action.voice-toggle input[type="checkbox"] {
  width: 22px;
  height: 22px;
  min-height: auto;
  accent-color: var(--accent);
}
.voice-prefs-hint {
  font-size: 0.8rem;
  color: var(--fg-muted);
  padding: 0.3em 1em 0.5em;
  line-height: 1.4;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .bottom-sheet-overlay, .bottom-sheet { animation: none; }
}

/* ── Editor + PDF-viewer action bars ────────────────────────────────────── */
.editor-header, .pdf-header {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.5em 0.7em;
  padding-top: calc(0.5em + env(safe-area-inset-top, 0));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.editor-header .close-btn, .pdf-header .close-btn {
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  background: transparent;
  border: none;
  color: var(--fg-dim);
  font-size: 1.3rem;
}
.editor-title, .page-counter {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.95rem;
}
.editor-status {
  font-size: 0.78rem;
  color: var(--fg-muted);
}
.viewer-actions {
  display: flex;
  gap: 0.3em;
}
.viewer-actions button {
  min-width: 36px;
  min-height: 36px;
  padding: 0 0.5em;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
}
.editor-area {
  flex: 1;
  width: 100%;
  border: none;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-mono);
  font-size: 0.92rem;
  padding: 0.7em;
  min-height: 0;
}
.pdf-canvas-wrap {
  flex: 1;
  overflow: auto;
  display: flex;
  justify-content: center;
  background: #111;
}
.pdf-canvas-wrap canvas {
  max-width: 100%;
  height: auto;
}
.page-jump {
  width: 56px;
  min-width: 56px;
  height: 32px;
  min-height: 32px;
  padding: 0 0.3em;
  text-align: center;
  font-size: 0.85rem;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  -moz-appearance: textfield;
}
.page-jump::-webkit-outer-spin-button,
.page-jump::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

#docs-viewer {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 1050;
  display: flex;
  flex-direction: column;
}
#docs-viewer[hidden] { display: none; }

/* ── Compose tab ────────────────────────────────────────────────────────── */
.compose-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.6em;
  padding: 0.8em;
  overflow-y: auto;
}
.compose-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3em;
  font-size: 0.85rem;
  color: var(--fg-dim);
}
.compose-form textarea[name="body"] {
  min-height: 30vh;
  font-family: var(--font-mono);
  font-size: 0.95rem;
}
.compose-actions {
  display: flex;
  gap: 0.6em;
  flex-wrap: wrap;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.attach-btn {
  flex: 1;
  min-width: 140px;
}

/* ── Drafts tab ─────────────────────────────────────────────────────────── */
.draft-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 0.6em 0.8em;
  padding: 0.8em 1em;
}
.draft-card .preview {
  color: var(--fg-dim);
  margin: 0.5em 0;
  font-size: 0.92rem;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.draft-card .actions {
  display: flex;
  gap: 0.5em;
  margin-top: 0.5em;
}
.draft-card button { flex: 1; }
.draft-card .approve { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.draft-card .reject  { background: var(--danger); color: white; border-color: var(--danger); }

.push-banner {
  margin: 0.6em 0.8em;
  padding: 0.8em 1em;
  background: var(--bg-elev);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
}
.push-banner-body strong { display: block; margin-bottom: 0.2em; }
.push-banner-body p { margin: 0 0 0.6em; color: var(--fg-dim); font-size: 0.92rem; }
.push-banner-actions { display: flex; gap: 0.5em; }
.push-banner-actions button { flex: 1; }
.push-banner-actions .primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }

/* ── Fullscreen in-app document viewer ──────────────────────────────────── */
/* Overlay sits above everything else; iframe renders /preview which can
   serve PDF, image, text, video. iOS standalone PWA can't window.open
   cleanly so we keep all viewing inside the PWA. */
#doc-viewer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: none;
  flex-direction: column;
  z-index: 10000;
}
#doc-viewer-overlay.dv-open { display: flex; }
.dv-no-scroll { overflow: hidden; }
#doc-viewer-overlay .dv-bar {
  display: flex; align-items: center; gap: 0.5em;
  padding: env(safe-area-inset-top, 0.5em) 0.5em 0.5em 0.5em;
  background: #0a0a0a;
  border-bottom: 1px solid #222;
  flex: 0 0 auto;
}
#doc-viewer-overlay .dv-title {
  flex: 1 1 auto;
  font-size: 0.9rem;
  color: #ddd;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  padding: 0 0.4em;
}
#doc-viewer-overlay .dv-close,
#doc-viewer-overlay .dv-download {
  background: transparent;
  border: 1px solid #333;
  color: #ddd;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.1rem;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: manipulation;
  display: flex; align-items: center; justify-content: center;
  flex: 0 0 auto;
}
#doc-viewer-overlay .dv-close:active,
#doc-viewer-overlay .dv-download:active { background: #1a1a1a; }
#doc-viewer-overlay .dv-frame-wrap {
  flex: 1 1 auto;
  position: relative;
  background: #2a2a2a;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
#doc-viewer-overlay .dv-content {
  width: 100%;
  min-height: 100%;
}
#doc-viewer-overlay .dv-loader,
#doc-viewer-overlay .dv-error {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: #ddd;
  font-size: 0.95rem;
  background: #111;
  text-align: center;
  padding: 1em;
}
#doc-viewer-overlay .dv-error { color: #f88; }
#doc-viewer-overlay .dv-error[hidden] { display: none; }

/* ── Mobile-only overrides ──────────────────────────────────────────────── */
@media (max-width: 768px) {
  .tab-header h1 { font-size: 1.05rem; }
  .turn { max-width: 95%; }
}

/* Reduce motion respect */
@media (prefers-reduced-motion: reduce) {
  .voice-btn.recording { animation: none; }
}

/* When the on-screen keyboard would crush layout, give the input strip
   precedence over the message stream. */
@media (max-height: 500px) {
  .stream { padding: 0.4em; }
  .chat-input { padding: 0.4em; }
}
