/* ── Mobile / small-screen layer (python/index.html only) ──
   Loaded LAST so it can override the desktop layout. Every layout rule
   lives behind a media query: desktop (> 900px) renders exactly as
   before this file existed.

   The shell strategy on small screens:
   - The sidebar becomes an off-canvas drawer, toggled by the hamburger
     button in the title bar (body.sidebar-open drives it, see ui.js).
   - The Excel / Python split rotates from side-by-side to a single
     scrollable column: spreadsheet + sticky note on top, editor +
     terminal below. Only .panels scrolls; the chrome stays put.
   - 100dvh instead of 100vh where supported: mobile browsers' collapsing
     URL bar makes 100vh taller than the visible area, which pushed the
     run bar / terminal behind the browser chrome. */

/* New shell elements (hamburger + drawer backdrop). Hidden on desktop —
   these exist only for the drawer pattern. */
.menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 15px;
  cursor: pointer;
  transition: color var(--transition-fast), background var(--transition-fast);
}

.menu-btn:hover {
  color: var(--text);
  background: var(--border-light);
}

.sidebar-backdrop {
  display: none;
}

/* ═══ Tablet portrait & phones: drawer + stacked panels ═══ */
@media (max-width: 900px) {

  .app {
    grid-template-columns: 1fr;
    height: 100vh;
    height: 100dvh; /* ignored where unsupported; the 100vh line above stands */
  }

  /* Sidebar → off-canvas drawer. Same inner layout (brand / progress /
     nav / footer), just parked off-screen until body.sidebar-open.
     visibility:hidden rides along because a transform alone keeps the
     drawer's links and buttons (Reset Progress!) in the tab order and
     screen-reader flow — invisible but focusable. The delayed visibility
     flip waits for the slide-out to finish; on open it flips instantly. */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 300;
    width: min(320px, 85vw);
    height: 100vh;
    height: 100dvh;
    border-right: none;
    transform: translateX(-105%); /* 105 so the box-shadow never peeks */
    visibility: hidden;
    transition: transform var(--transition-slow),
      visibility 0s linear var(--transition-slow);
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.45);
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
    visibility: visible;
    transition-delay: 0s;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 290;
    background: rgba(15, 23, 42, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-slow);
  }

  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .menu-btn {
    display: inline-flex;
  }

  /* Title bar hosts the hamburger; the lesson title must truncate instead
     of pushing the counter off-screen. */
  .title-bar {
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-3);
  }

  .title-text {
    flex: 1;
    min-width: 0;
  }

  .title-text h2 {
    font-size: var(--text-lg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .title-text p {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .tab-bar {
    padding: 0 var(--sp-3);
    overflow-x: auto;
    scrollbar-width: none; /* three tabs almost always fit; no scrollbar chrome */
  }

  .tab-btn {
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* Panels: side-by-side grid → one scrollable column. Panels take their
     natural height; the container is the single scroller (no nested
     scrollbars fighting the finger). */
  .panels {
    display: flex;
    flex-direction: column;
    min-height: 0;
    margin: 0;
    border-radius: 0;
    gap: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .panels.panels--solo {
    display: flex; /* .panels--solo only resets grid columns; keep flex */
  }

  .excel-panel {
    flex: none;
    overflow: visible; /* the outer .panels scrolls; this panel just flows */
  }

  .python-panel {
    flex: none;
    border-top: 1px solid var(--border);
  }

  /* In the stacked column the panel height is content-driven, so the
     editor's flex:1 would collapse to zero — give it a real height.
     Monaco's automaticLayout picks the change up on its own. */
  .monaco-container {
    flex: none;
    height: 44vh;
    height: 44dvh;
    min-height: 200px;
  }

  /* Desktop welcome blocks fill and scroll the panel; in the stacked
     column they just flow with the page. */
  .welcome-block {
    height: auto;
    overflow: visible;
  }

  /* Lesson grids are authored at desktop widths — pan them horizontally
     instead of letting them blow the column open. */
  .spreadsheet-container {
    overflow-x: auto;
  }

  /* Fingers need a fatter grab area than a 6px strip. */
  .terminal-resize-handle {
    height: 16px;
  }

  .resize-grip {
    width: 44px;
    height: 3px;
  }

  /* Button rows: wrap instead of clip. */
  .code-actions,
  .run-bar {
    flex-wrap: wrap;
  }

  /* Docking the DataFrame viewer next to the editor needs a wide screen. */
  .df-modal-dock {
    display: none;
  }

  .df-modal--docked .df-modal-card {
    width: calc(100vw - 32px);
  }

  /* iOS zooms the page when focusing inputs whose font-size is < 16px.
     Monaco types into a hidden textarea, so bumping it has no visual
     effect — it only suppresses the zoom jump. Same for jexcel's cell
     editor, where 16px during editing is fine on a phone. */
  .monaco-container .monaco-editor .inputarea {
    font-size: 16px !important;
  }

  .spreadsheet-container .jexcel tbody td input {
    font-size: 16px !important;
  }
}

/* ═══ Phones ═══ */
@media (max-width: 560px) {

  .title-bar {
    padding: var(--sp-2);
  }

  /* No physical keyboard — the Ctrl+Enter hint is noise. */
  .run-hint {
    display: none;
  }

  /* Run is THE action on this screen: full-width tap target, comment
     helpers drop to their own row underneath. */
  .run-btn {
    flex: 1 1 100%;
    justify-content: center;
    padding: var(--sp-2) var(--sp-4);
  }

  .excel-note-wrap {
    padding: 12px 10px 16px;
  }

  .excel-note {
    padding: 18px 16px 14px;
  }

  .welcome-block {
    padding: 18px 16px;
  }

  /* Modals: edge-to-edge cards. The DataFrame card's desktop min-width
     (360px) is wider than some phones, and CSS resize needs a mouse. */
  .df-modal-card {
    min-width: 0;
    width: calc(100vw - 16px);
    height: 85vh;
    height: 85dvh;
    resize: none;
  }

  .df-modal-card::after {
    display: none; /* the fake resize grip advertises a mouse-only feature */
  }

  .df-modal-body,
  .cheatsheet-modal-body,
  .faq-modal-body {
    padding: 14px;
  }

  .cheatsheet-modal-card,
  .faq-modal-card {
    width: calc(100vw - 16px);
    max-height: 88vh;
    max-height: 88dvh;
  }

  .img-zoom-overlay {
    padding: 12px;
  }

  .loading-overlay {
    padding: 0 var(--sp-6);
  }

  .loading-steps {
    min-width: 0;
    width: 100%;
    max-width: 300px;
  }
}

/* ═══ Touch devices (any width) ═══ */
@media (hover: none) {

  /* Hover tooltips get stuck open after a tap — the accessible names and
     button labels already carry the meaning. */
  [data-tooltip]::after {
    display: none;
  }

  /* Comfortable tap targets for the small text buttons. */
  .code-action-btn {
    padding: 6px 10px;
  }

  .excel-note-btn {
    padding: 7px 12px;
  }
}

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