/* ==========================================================================
   aicademi.ai — mobile composer (Round 4, 2026-04-13)
   Activates only at <=820px. Replaces sprawling desktop composer with a
   single sticky-bottom composer that fits one thumb. Injected by JS.
   ========================================================================== */

/* ----- R4 palette tokens ----- */
:root {
  --cr4-bg: #ffffff;
  --cr4-ink: #1f2937;
  --cr4-muted: #6b7280;
  --cr4-border: #e5e7eb;
  --cr4-shadow: 0 -8px 28px rgba(0,0,0,0.07);
  --guide-calm:      #10b981;
  --guide-calm-soft: #ecfdf5;
  --guide-calm-ink:  #064e3b;
  --guide-amber:      #f59e0b;
  --guide-amber-soft: #fffbeb;
  --guide-amber-ink:  #78350f;
  --offline-badge-bg: #f3f4f6;
  --offline-badge-ink: #4b5563;
}

/* ----- Cascade-kill desktop sprawl on mobile ----- */
@media (max-width: 820px) {
  /* Hide desktop composer + hint card + workspace tabs. JS also forces
     data-active="chat" on .workspace-panels so swipes don't flip panels. */
  .panel-chat > .chat-input-area { display: none !important; }
  #hintCardSlot { display: none !important; }
  .workspace-tabs { display: none !important; }

  /* Reserve room so last message isn't hidden under the sticky composer. */
  .panel-chat .chat-messages {
    padding-bottom: calc(var(--cr4-reserve, 160px) + env(safe-area-inset-bottom, 0px));
  }
}

/* ----- .composer-r4 (JS-injected) ----- */
@media (max-width: 820px) {
  .composer-r4 {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    display: flex; flex-direction: column;
    gap: 6px;
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--cr4-bg);
    border-top: 1px solid var(--cr4-border);
    box-shadow: var(--cr4-shadow);
    /* Lift above bottom tabbar if present (see mobile-app.css --tab-bar-height). */
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px) + var(--cr4-tabbar-lift, 0px));
  }
  body.has-app-tabbar .composer-r4 {
    --cr4-tabbar-lift: calc(var(--tab-bar-height, 64px));
  }

  .cr4-tools {
    display: flex; align-items: center; gap: 6px;
    /* R5: wrap instead of horizontal scroll — nothing scrolls sideways on phone. */
    flex-wrap: wrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .cr4-tools::-webkit-scrollbar { display: none; }

  .cr4-tool {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    display: inline-grid; place-items: center;
    background: transparent; border: 1px solid var(--cr4-border);
    border-radius: 12px;
    color: var(--cr4-ink);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
  }
  .cr4-tool:active { transform: scale(0.95); }
  .cr4-tool[aria-pressed="true"] {
    background: var(--purple-light, #ede9fe);
    border-color: var(--purple, #7c3aed);
    color: var(--purple, #7c3aed);
  }
  .cr4-tool svg { width: 20px; height: 20px; }
  .cr4-tool-label {
    font: 700 0.64rem/1 'Inter', sans-serif;
    letter-spacing: 0.02em;
  }
  .cr4-tool-spacer { flex: 1 1 auto; min-width: 6px; }

  .cr4-row {
    display: flex; align-items: flex-end; gap: 8px;
  }
  .cr4-textarea {
    flex: 1 1 auto;
    min-height: 84px;        /* 3-line min */
    max-height: 40dvh;
    padding: 10px 12px;
    border: 1px solid var(--cr4-border);
    border-radius: 14px;
    background: #f9fafb;
    color: var(--cr4-ink);
    font: 400 16px/1.4 'Inter', sans-serif; /* 16px prevents iOS zoom */
    resize: none;
    -webkit-appearance: none;
    outline: none;
    transition: border-color 120ms ease, background 120ms ease;
  }
  .cr4-textarea:focus { border-color: var(--purple, #7c3aed); background: #fff; }

  .cr4-send {
    flex: 0 0 auto;
    width: 48px; height: 48px;
    display: inline-grid; place-items: center;
    background: var(--gradient, linear-gradient(135deg, #7c3aed, #a855f7));
    color: #fff;
    border: 0; border-radius: 14px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 120ms ease, filter 120ms ease;
  }
  .cr4-send:disabled { filter: grayscale(0.6); opacity: 0.7; cursor: not-allowed; }
  .cr4-send:active { transform: scale(0.96); }
  .cr4-send svg { width: 20px; height: 20px; }

  .cr4-file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
}

/* ----- Inline guide cards ----- */
.guide-card {
  margin: 8px 12px;
  padding: 10px 12px;
  border-radius: 14px;
  font: 500 0.92rem/1.4 'Inter', sans-serif;
  display: flex; align-items: flex-start; gap: 10px;
  position: relative;
}
.guide-card-milestone {
  background: var(--guide-calm-soft);
  color: var(--guide-calm-ink);
  border: 1px solid rgba(16, 185, 129, 0.18);
}
.guide-card-nextup {
  background: var(--guide-amber-soft);
  color: var(--guide-amber-ink);
  border: 1px solid rgba(245, 158, 11, 0.22);
}
.guide-card-icon {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  display: inline-grid; place-items: center;
}
.guide-card-body { flex: 1 1 auto; }
.guide-card-title { font-weight: 700; margin-bottom: 2px; }
.guide-card-text { opacity: 0.92; }
.guide-card-dismiss {
  position: absolute; top: 4px; right: 4px;
  width: 32px; height: 32px;
  display: inline-grid; place-items: center;
  background: transparent; border: 0;
  color: inherit; opacity: 0.6;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  border-radius: 8px;
}
.guide-card-dismiss:hover { opacity: 1; }

.guide-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--guide-calm-soft);
  color: var(--guide-calm-ink);
  font: 600 0.78rem 'Inter', sans-serif;
}

/* ----- @-picker: AI tools section ----- */
.sheet-tools {
  margin-top: 10px;
  padding: 8px 10px 4px;
  border-top: 1px solid var(--cr4-border);
}
.sheet-tools > summary {
  list-style: none;
  display: flex; align-items: center; justify-content: space-between;
  font: 600 0.85rem 'Inter', sans-serif;
  color: var(--cr4-muted);
  padding: 6px 4px;
  min-height: 40px;
  cursor: pointer;
}
.sheet-tools > summary::-webkit-details-marker { display: none; }
.sheet-tools > summary::after {
  content: "▾"; transition: transform 120ms ease;
}
.sheet-tools[open] > summary::after { transform: rotate(180deg); }
.sheet-tools-list { padding: 4px 0 8px; }
.tool-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer;
  min-height: 48px;
  -webkit-tap-highlight-color: transparent;
}
.tool-row:active { background: #f3f4f6; }
.tool-row-main { flex: 1 1 auto; display: flex; flex-direction: column; gap: 2px; }
.tool-row-title { font: 600 0.95rem 'Inter', sans-serif; color: var(--cr4-ink); }
.tool-row-kind { font: 500 0.78rem 'Inter', sans-serif; color: var(--cr4-muted); }
.tool-offline-badge {
  flex: 0 0 auto;
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 999px;
  background: var(--offline-badge-bg); color: var(--offline-badge-ink);
  font: 600 0.72rem 'Inter', sans-serif;
  letter-spacing: 0.02em;
}

/* ----- Offline tool chat bubble ----- */
.chat-tool-offline {
  background: var(--offline-badge-bg) !important;
  color: var(--offline-badge-ink) !important;
}
.chat-tool-offline .chat-sender {
  color: var(--offline-badge-ink);
  font-weight: 700;
}

/* ----- Chat upload thumb ----- */
.chat-upload-thumb {
  display: inline-block;
  margin: 6px 0;
}
.chat-upload-thumb img {
  max-width: 150px; max-height: 150px;
  border-radius: 12px;
  border: 1px solid var(--cr4-border);
  display: block;
}
.chat-upload-thumb figcaption {
  font: 500 0.72rem 'Inter', sans-serif;
  color: var(--cr4-muted);
  margin-top: 4px;
}

/* ==========================================================================
   R5 — Draggable chat drawer over preview.
   Three snap states: .is-collapsed (60px handle), .is-half (50dvh), .is-full (90dvh).
   ========================================================================== */
@media (max-width: 820px) {
  .chat-drawer {
    position: fixed;
    left: 0; right: 0;
    bottom: calc(var(--tab-bar-height, 64px) + env(safe-area-inset-bottom, 0px));
    z-index: 42;
    display: flex;
    flex-direction: column;
    background: #fff;
    border-top-left-radius: 18px;
    border-top-right-radius: 18px;
    box-shadow: 0 -14px 40px rgba(0,0,0,0.18);
    max-width: 100vw;
    height: var(--drawer-half-h);
    transform: translateY(var(--cr5-drawer-offset, 0px));
    transition: height 240ms var(--drawer-spring);
    touch-action: none;      /* handle drags; messages re-enable pan-y */
    overflow: hidden;
  }
  .chat-drawer.is-collapsed { height: var(--drawer-collapsed-h); }
  .chat-drawer.is-half      { height: var(--drawer-half-h); }
  .chat-drawer.is-full      { height: var(--drawer-full-h); }
  .chat-drawer.is-dragging  { transition: none; }
  /* R6: hidden state — drawer slides off-screen so FAB is the only return. */
  .chat-drawer {
    transition: height 240ms var(--drawer-spring),
                transform 260ms var(--ease-ios, cubic-bezier(0.32, 0.72, 0, 1));
  }
  .chat-drawer.is-hidden {
    transform: translateY(calc(100% + var(--tab-bar-height, 64px) + env(safe-area-inset-bottom, 0px)));
    pointer-events: none;
  }

  .chat-drawer-handle {
    position: relative;
    height: 32px;
    display: grid; place-items: center;
    cursor: grab;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    flex: 0 0 auto;
  }
  .chat-drawer-handle::before {
    content: "";
    width: 44px; height: 4px; border-radius: 2px;
    background: var(--cr4-border);
    transition: background 150ms ease;
  }
  /* R6: close ✕ in handle row — slides drawer to hidden, FAB appears. */
  .chat-drawer-close {
    position: absolute;
    right: 6px; top: 50%;
    transform: translateY(-50%);
    width: 36px; height: 36px;
    border: 0; border-radius: 50%;
    background: transparent;
    color: var(--cr4-muted);
    display: inline-grid; place-items: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .chat-drawer-close:active { background: var(--purple-light, #ede9fe); color: var(--purple, #7c3aed); }
  .chat-drawer-close svg { width: 18px; height: 18px; }
  .chat-drawer.is-full .chat-drawer-handle::before { background: var(--purple, #7c3aed); }

  .chat-drawer-body {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    touch-action: pan-y;
    display: flex; flex-direction: column;
    min-height: 0;
  }
  /* In collapsed state, hide body so just the handle + messages summary show. */
  .chat-drawer.is-collapsed .chat-drawer-body { display: none; }

  /* Auto chip sticky above composer inside the drawer body. */
  .drawer-auto-chip {
    position: sticky; bottom: 0; left: 0; right: 0;
    margin: 0 10px 8px;
    display: flex; align-items: center; gap: 8px;
    padding: 8px 12px;
    border-radius: 999px;
    background: linear-gradient(135deg, #fde68a, #fbbf24);
    color: #78350f;
    font: 600 0.88rem 'Inter', sans-serif;
    border: 1px solid rgba(245, 158, 11, 0.35);
    box-shadow: 0 4px 14px rgba(251, 191, 36, 0.25);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    min-height: 40px;
    max-width: calc(100vw - 20px);
    overflow: hidden;
  }
  .drawer-auto-chip[hidden] { display: none; }
  .drawer-auto-chip-icon { flex: 0 0 auto; }
  .drawer-auto-chip-text {
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* R5 .preview-max-chip replaced in R6 by .chat-fab (see mobile-app.css). */

  /* When drawer is collapsed, the fixed composer-r4 must NOT stack;
     the drawer wraps it instead. Hide the floating composer and let the
     drawer's own copy show. (mount() decides which is present.) */
  body.has-chat-drawer .composer-r4.is-detached { display: none; }
}

@media (max-width: 820px) and (prefers-reduced-motion: reduce) {
  .chat-drawer { transition: none; }
}

/* When composer-r4 lives inside the drawer, it must NOT be viewport-fixed —
   let it flow at the bottom of the drawer body. */
@media (max-width: 820px) {
  .chat-drawer .composer-r4 {
    position: relative !important;
    left: auto; right: auto; bottom: auto;
    box-shadow: none;
    border-top: 1px solid var(--cr4-border);
    padding-bottom: 10px;
    z-index: auto;
    --cr4-tabbar-lift: 0px;
  }
  /* Tabbar and drawer together — drawer already sits above the tabbar via
     bottom offset, so a fixed composer on pages WITHOUT the drawer
     (non-workspace) still follows the old rule. */
}
