/* ── lattice.study · mobile layer ──────────────────────────────────────────────
   Loaded after style.css / home.css. Every rule is scoped to a phone breakpoint
   or a coarse pointer, so desktop is untouched. Some components inject their
   CSS at runtime (app.js, dashboard.js, graphy.js); rules that must beat those
   carry an extra ID/element in the selector rather than !important.
   Map: docs/mobile-overhaul.md                                                  */

/* ── Touch basics ──────────────────────────────────────────────────────────── */
@media (pointer: coarse) {
  /* No double-tap-to-zoom delay on controls (pinch zoom stays available). */
  button, a, [role="button"], select, label, .cs-card { touch-action: manipulation; }
  /* iOS zooms into any focused field under 16px now that zoom is allowed. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not(.auth-otp-input),
  textarea, select { font-size: 16px !important; }
  /* Hold-to-pin is a mouse-first gesture; its hint toast floats over the cards. */
  .cs-pin-toast.cs-pin-hint { display: none !important; }
}

/* ── App header (≤768) ─────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  header #nav-up, header #search-mobile-btn, header #auth-btn,
  header #streak-badge, header .lv-chip { min-height: 40px; min-width: 40px; }
}

/* ── Bottom nav + More sheet (≤768, where #bottom-nav shows) ───────────────── */
@media (max-width: 768px) {
  #bottom-nav .bn-btn { position: relative; min-width: 0; gap: 4px; }
  #bottom-nav .bn-icon { font-size: 18px; }
  #bottom-nav .bn-label { font-size: 9.5px; letter-spacing: .6px; white-space: nowrap; }
  /* Active tab: colour plus a bar, never colour alone. */
  #bottom-nav .bn-btn.active::before {
    content: ''; position: absolute; top: 0; left: 28%; right: 28%; height: 2px;
    border-radius: 0 0 2px 2px; background: var(--accent);
  }
  #bn-more { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; padding: 6px 0; }
  #bn-more[hidden] { display: none; }
  #bn-more.active, #bn-more[aria-expanded="true"] { color: var(--accent); }
  #bn-more .bn-icon { font-size: 20px; line-height: .9; }

  #bn-more-backdrop { position: fixed; inset: 0; z-index: 640; background: rgba(0,0,0,.5);
    opacity: 0; pointer-events: none; transition: opacity var(--dur-2, .2s); }
  #bn-more-backdrop.open { opacity: 1; pointer-events: auto; }
  /* The floating Graphy launcher would sit on the sheet; Graphy is in the list. */
  html.bnm-open #graphy-btn, html.bnm-open .graphy-nudge { opacity: 0; pointer-events: none; }
  #bn-more-sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 650;
    max-height: min(78dvh, 640px); overflow-y: auto; overscroll-behavior: contain;
    background: var(--bg2); border-top: 1px solid var(--border2); border-radius: 18px 18px 0 0;
    box-shadow: 0 -16px 48px rgba(0,0,0,.45);
    padding: 6px 12px calc(14px + env(safe-area-inset-bottom, 0px));
    transform: translateY(105%); visibility: hidden;
    transition: transform var(--dur-2, .22s) var(--ease-out, ease-out), visibility 0s linear var(--dur-2, .22s);
  }
  #bn-more-sheet.open { transform: translateY(0); visibility: visible; transition: transform var(--dur-2, .22s) var(--ease-out, ease-out); }
  .bnm-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 4px 6px; }
  .bnm-title { font-family: var(--font-mono); font-size: 11px; letter-spacing: 2.5px; text-transform: uppercase; color: var(--muted); }
  .bnm-close { width: 44px; height: 44px; border-radius: 10px; background: none; border: 1px solid var(--border2);
    color: var(--muted); font-size: 14px; cursor: pointer; }
  .bnm-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .bnm-item { display: flex; align-items: center; gap: 10px; min-height: 60px; padding: 10px 12px; text-align: left;
    background: var(--bg3, rgba(var(--inkw),.03)); border: 1px solid var(--border2); border-radius: 12px;
    color: var(--text); cursor: pointer; }
  .bnm-item.is-active { border-color: var(--accent); background: rgba(var(--accent-rgb), .08); }
  .bnm-ic { flex-shrink: 0; width: 32px; height: 32px; border-radius: 9px; display: flex; align-items: center; justify-content: center;
    font-size: 16px; color: var(--accent); background: rgba(var(--accent-rgb), .1); }
  .bnm-tx { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
  .bnm-l { font-family: var(--font-display); font-size: 14px; font-weight: 700; line-height: 1.2; }
  .bnm-s { font-family: var(--font-body); font-size: 11.5px; line-height: 1.3; color: var(--muted); }
}
@media (max-width: 340px) { .bnm-list { grid-template-columns: 1fr; } }
@media (prefers-reduced-motion: reduce) {
  #bn-more-sheet, #bn-more-sheet.open, #bn-more-backdrop { transition: none; }
}

/* ── Explore on phones (≤600) ──────────────────────────────────────────────── */
@media (max-width: 600px) {
  /* The 65px left rail becomes one horizontal strip under the header, freeing
     the full width for the graph and staying clear of the bottom sheet. */
  #view-explore #toolbar {
    top: 8px; left: 8px; right: 8px; transform: none;
    flex-direction: row; align-items: center; gap: 6px; padding: 6px 8px;
    overflow-x: auto; overflow-y: hidden; scrollbar-width: none; overscroll-behavior-x: contain;
    touch-action: pan-x; border-radius: 14px;
  }
  #view-explore #toolbar::-webkit-scrollbar { display: none; }
  #view-explore #toolbar .tool-label { display: none; }
  #view-explore #toolbar #zoom-level-lbl { display: flex; align-items: center; flex-shrink: 0; margin: 0 2px;
    font-size: 10px; letter-spacing: 1px; }
  #view-explore #toolbar .tool-divider { width: 1px; height: 26px; margin: 0 2px; flex-shrink: 0; }
  #view-explore #sf-filters { display: flex; flex-direction: row; gap: 6px; flex-shrink: 0; }
  #view-explore #toolbar .tool-btn, #view-explore #toolbar .sf-btn { width: 44px; height: 44px; flex-shrink: 0; }
  #view-explore #toolbar .tool-btn { font-size: 17px; border-radius: 11px; }
  #view-explore #toolbar .sf-btn { font-size: 12px; }
  /* Hover tooltips have no hover on touch and would be clipped by the strip. */
  #view-explore #toolbar .tool-btn::after, #view-explore #toolbar .sf-btn::after { display: none; }
  #view-explore #toolbar .sf-btn:hover, #view-explore #toolbar .tool-btn:hover { transform: none; }

  /* First-visit tour: under the tool strip, clear of the Graphy button and the sheet. */
  #view-explore #explore-tour-card { top: 70px; bottom: auto; left: 10px; right: 10px; width: auto; max-width: none;
    transform: none; animation: mobTourIn .22s var(--ease-out, ease-out); padding: 14px 16px; }
  #view-explore .tour-text { font-size: 15px; }
  #view-explore .tour-step-lbl { font-size: 10px; }
  #view-explore .tour-skip, #view-explore .tour-next { min-height: 44px; min-width: 64px; }
}
@keyframes mobTourIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* Labels counter-scaled by --inv (= max(1, 1/zoom), set in app.js applyCamera):
   unit names sit under the bubble at a fixed on-screen size instead of shrinking
   inside it. .explore-compact is set by buildGraph() with the phone layout. */
#view-explore.explore-compact .unit-bubble { overflow: visible; }
#view-explore.explore-compact .u-num { font-size: calc(12px * var(--inv, 1)); opacity: .9; letter-spacing: 1px; margin: 0; font-weight: 700; }
#view-explore.explore-compact .u-period { display: none; }
#view-explore.explore-compact .u-name {
  position: absolute; top: calc(100% + 5px); left: 50%; width: 116px; max-width: none; padding: 0;
  transform: translateX(-50%) scale(var(--inv, 1)); transform-origin: 50% 0;
  font-size: 11.5px; line-height: 1.25; color: var(--text); pointer-events: none;
  text-shadow: 0 0 4px rgb(var(--canvas-bg)), 0 0 8px rgb(var(--canvas-bg)), 0 1px 2px rgb(var(--canvas-bg));
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
#view-explore.explore-compact .sub-bubble { overflow: visible; padding: 0; }
#view-explore.explore-compact .sub-bubble .sb-txt {
  position: absolute; top: calc(100% + 4px); left: 50%; width: 100px;
  transform: translateX(-50%) scale(var(--inv, 1)); transform-origin: 50% 0;
  font-size: 10.5px; line-height: 1.2; text-align: center; color: var(--text); pointer-events: none;
  text-shadow: 0 0 4px rgb(var(--canvas-bg)), 0 0 8px rgb(var(--canvas-bg));
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
#view-explore.explore-compact .subsub-node { font-size: 10px; }

/* ── Notes / Vocab (≤700, where the units sheet replaces the sidebar) ─────── */
@media (max-width: 700px) {
  .mobile-units-open { min-height: 44px; }
  .sidebar-mobile-close { min-width: 44px; min-height: 44px; }
  .notes-chapter-btn, .notes-unit-btn, .vocab-sidebar-btn { min-height: 44px; }

  /* Vocab toolbar: title + count on one line, then the four study modes as an
     even segmented row, then filter + shuffle. No ragged wrap. */
  #view-vocab #vocab-toolbar { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 16px; }
  #view-vocab #vocab-unit-title { font-size: 15px; }
  #view-vocab #vocab-toolbar-right { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  #view-vocab #vocab-toolbar-right .vocab-ctrl-btn,
  #view-vocab #vocab-toolbar-right .vocab-filter-select { min-height: 42px; font-size: 10.5px; letter-spacing: .5px; padding: 6px 4px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
  #view-vocab #vocab-cards-btn, #view-vocab #vocab-list-btn, #view-vocab #vocab-type-btn, #view-vocab #vocab-match-btn { order: 1; }
  #view-vocab #vocab-status-filter { order: 2; grid-column: span 2; text-align: left; padding-left: 12px;
    background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
    background-position: calc(100% - 15px) 50%, calc(100% - 10px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
  #view-vocab #vocab-shuffle-btn { order: 3; }
  #view-vocab #vocab-progress { order: 4; min-width: 0; align-self: center; text-align: center; font-size: 12px; }

  /* Card: the arrows shrink and the stage tightens so the card gets the width,
     and long terms wrap instead of running off the card. */
  #view-vocab #vocab-stage { gap: 8px; padding: 14px 10px; }
  #view-vocab .vocab-nav-btn { width: 40px; height: 56px; flex-shrink: 0; }
  #view-vocab .vocab-card-term { font-size: clamp(19px, 5.6vw, 30px); overflow-wrap: break-word; max-width: 100%; }
  #view-vocab .vocab-card-hint { font-size: 10px; letter-spacing: 1.5px; }
  #view-vocab .vocab-card-unit-tag, #view-vocab .vocab-card-num { font-size: 10px; }
}

/* ── Dashboard EXAMPLE chips (≤600) ────────────────────────────────────────── */
@media (max-width: 600px) {
  #view-dashboard .dsc-label { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; font-size: 10px; letter-spacing: 1.5px; line-height: 1.3; }
  #view-dashboard .dsc-example { margin-left: 0; font-size: 9px; letter-spacing: 1.2px; padding: 2px 6px; vertical-align: 0; }
  #view-dashboard .dash-stat-card { gap: 8px; }
}

/* ── Opening page + course picker (≤600) ───────────────────────────────────── */
@media (max-width: 600px) {
  /* Readable floor for the mono micro-copy (was 7.5–10px). */
  .lp-kicker { font-size: 11px; }
  .lp-hero-note { font-size: 12px; letter-spacing: .6px; }
  .lp-map-cap { font-size: 10.5px; letter-spacing: 1px; }
  .lp-stat span { font-size: 10.5px; letter-spacing: 1px; }
  .lp-cmp-row > span:first-child, .lp-cmp-head > span { font-size: 11px; letter-spacing: 1px; }
  .lpd-tag { font-size: 9.5px; }
  .lpd-row { font-size: 11px; }
  .lpd-scale { font-size: 10px; }
  .cs-footer { font-size: 10px; letter-spacing: 1.2px; }

  /* Picker controls */
  .cs-tab { font-size: 11px; letter-spacing: 1.5px; min-height: 40px; }
  .cs-cat-pill { font-size: 10px; letter-spacing: 1px; min-height: 36px; padding: 8px 12px; }
  #cs-topnav .cs-topnav-btn { min-height: 40px; min-width: 40px; }

  /* Course cards: names fit their column (no clipped "Macroeconomic…"), the
     descriptor and badges are legible. */
  .cs-card { padding: 14px 12px 12px; gap: 6px; }
  .cs-card-name { font-size: clamp(12px, 3.3vw, 15px); line-height: 1.22; overflow-wrap: break-word; hyphens: auto; }
  .cs-card-desc { font-size: 10px; letter-spacing: .2px; line-height: 1.45; }
  .cs-badge, .cs-status { font-size: 9px; }
  .cs-card-icon { width: 34px; height: 34px; }
}

/* ── Remaining small touch targets found by the QA audit (≤768) ────────────── */
@media (max-width: 768px) {
  /* Dashboard unit rows: Notes / Vocab chips (were 20px tall). */
  #view-dashboard .duc-go-btn { min-height: 36px; padding: 8px 12px; font-size: 10px; }
  /* Timeline era / theme filters. */
  #view-timeline .tl-filter-btn { min-height: 36px; padding: 8px 13px; font-size: 10px; }
  /* Map controls (inline-styled in map.js, hence min-* and the scoped !important). */
  #map-layers-btn, #map-play-btn { min-height: 40px; font-size: 10px !important; }
  #map-speed-btns { gap: 6px !important; }
  #map-speed-btns .map-speed-btn { min-height: 40px; min-width: 40px; font-size: 10px !important; }
  /* Year slider: same 2px track, but a 32px tall hit area and a bigger thumb. */
  #map-year-slider { height: 32px; background: linear-gradient(var(--border2), var(--border2)) center / 100% 2px no-repeat; }
  #map-year-slider::-webkit-slider-thumb { width: 22px; height: 22px; }
  #map-year-slider::-moz-range-thumb { width: 22px; height: 22px; }
  /* Practice / exam hub buttons. */
  #view-practice #exam-go-practice, #view-practice #exam-start-diag, #view-practice .etc-btn,
  #view-exam .etc-btn { min-height: 44px; }
}
@media (max-width: 768px) {
  /* Modal close buttons (auth, upgrade, save-progress, bottom-sheet modal): ~23px before. */
  #auth-modal-close, #upgrade-modal-close, #save-progress-close, #btsm-close {
    min-width: 44px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center;
  }
}
@media (max-width: 768px) { .auth-close { top: 2px; right: 2px; } }
