/* Touch layouts share the desktop artwork, with their own navigation and pacing. */
.studio-mode-switch { display: none; }
.modal-open { overflow: hidden; }

@media (max-width: 800px) {
  .studio-mode-switch {
    display: flex; padding: 4px; gap: 4px; border: 1px solid var(--line);
    border-radius: 18px; background: #edf1e7;
  }
  .studio-mode-switch .icon-button { width: 44px; height: 40px; border-radius: 13px; }
  .studio-mode-switch [aria-pressed=true] { background: var(--white); color: var(--green); box-shadow: 0 2px 5px #263a3310; }
  .draft-badge { display: none; }
  .studio-layout { min-height: 0; }
  body[data-studio-panel=build] .chat-panel,
  body[data-studio-panel=chat] .studio-workbench { display: none; }
  .studio-workbench { min-height: 0; }
  .chat-panel { height: calc(var(--visible-height, 100dvh) - 208px); min-height: 380px; max-height: none; }
  .chat-messages { min-height: 0; overscroll-behavior-y: contain; }
  .chat-header, .chat-compose { flex-shrink: 0; }
  .chat-bubble { font-size: 15px; }
  .prompt-seeds { flex-wrap: wrap; justify-content: center; }
  .prompt-seed { min-height: 44px; font-size: 12px; }
  .source-zone textarea, .chat-compose textarea, .input-field, .explanation-input { font-size: 16px; }
}

@media (max-width: 560px) {
  :root { --phone-nav: 80px; }
  html { scroll-padding-top: 140px; scroll-padding-bottom: 100px; }
  body { overflow-wrap: break-word; }
  .app-shell { padding-bottom: calc(var(--phone-nav) + env(safe-area-inset-bottom)); }
  .rail {
    padding: 8px max(16px, env(safe-area-inset-left)) calc(8px + env(safe-area-inset-bottom));
    height: calc(var(--phone-nav) + env(safe-area-inset-bottom));
    background: #f7f8f2f2; box-shadow: 0 -8px 28px #263a3306; z-index: 40;
  }
  .rail nav { align-items: center; max-width: 460px; }
  .nav-button { width: 48px; height: 48px; border-radius: 17px; }
  .nav-button .icon { width: 23px; height: 23px; }
  .nav-button[href="#explore"] { order: 1; }
  .nav-button[href="#library"] { order: 2; }
  .nav-button[href="#create"] { order: 3; }
  .nav-button[href="#saved"] { order: 4; }
  .nav-button[href="#community"] { order: 5; }
  .nav-button.create-nav { width: 54px; height: 54px; border-radius: 19px; box-shadow: 0 5px 12px #d9653130; }
  .nav-button.create-nav[aria-current=page] { outline: 3px solid #f5d6c7; outline-offset: 3px; }
  .topbar {
    position: sticky; top: 0; z-index: 25; display: grid;
    grid-template-columns: 1fr auto; gap: 8px 14px; height: auto; min-height: 120px;
    padding: calc(10px + env(safe-area-inset-top)) 20px 12px;
    background: #f7f8f2f5; backdrop-filter: blur(16px);
  }
  .wordmark { font-size: 22px; width: fit-content; }
  .topbar-actions { grid-row: 1; grid-column: 2; }
  .avatar { width: 44px; height: 44px; border-width: 4px; }
  .search-box, .search-box:focus-within {
    position: static; grid-row: 2; grid-column: 1 / -1; width: 100%; max-width: none;
    min-width: 0; min-height: 44px; gap: 10px; padding: 10px 14px; margin: 0;
    background: #eaeee3; border-radius: 15px; cursor: text;
  }
  .search-box input { display: block; font-size: 16px; min-width: 0; }
  .search-box input::placeholder { font-size: 13px; }
  .search-box kbd { display: none; }
  #main { padding: 22px 20px 30px; min-height: calc(100svh - 230px); }
  .view-heading { gap: 12px; margin-bottom: 18px; }
  .view-heading h1 { font-size: clamp(23px, 6.1vw, 27px); line-height: 1.2; letter-spacing: -1px; }
  .hero-grid { gap: 12px; margin-bottom: 22px; }
  .feature { height: 222px; border-radius: 24px; }
  .feature-copy { padding: 22px; max-width: 90%; }
  .feature h2 { max-width: 225px; margin-top: 18px; font-size: 30px; }
  .feature-art { width: 54%; height: 75%; right: -17px; bottom: -9px; }
  .feature-bottom { font-size: 10px; gap: 10px; }
  .play-circle { width: 40px; height: 40px; }
  .create-banner { height: 78px; gap: 20px; padding: 12px 18px; border-radius: 20px; }
  .banner-plus { top: 25px; right: 14px; }
  .source-doodles { gap: 7px; }
  .paper-doodle { width: 28px; height: 34px; }
  .magic-spark { width: 36px; height: 36px; }
  .output-doodles { gap: 4px; }
  .output-doodles > span { height: 23px; width: 25px; }
  .filter-row { margin-bottom: 10px; }
  .format-filters { overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none; justify-content: space-between; gap: 4px; }
  .filter-button { flex: 0 0 44px; min-width: 44px; height: 44px; border-radius: 14px; }
  .filter-separator { flex-shrink: 0; margin: 0; }
  .topic-filters { margin: 0 -20px 22px; padding: 0 20px; gap: 7px; overscroll-behavior-x: contain; }
  .topic-pill { min-height: 44px; font-size: 12px; padding: 10px 16px; }
  .content-grid { gap: 26px 14px; }
  .card-art { aspect-ratio: 1 / 1.06; border-radius: 19px; }
  .card-title { font-size: 14px; line-height: 1.4; padding-right: 0; }
  .card-topics { font-size: 8px; letter-spacing: .35px; flex-wrap: wrap; line-height: 1.5; }
  .card-author { font-size: 11px; min-height: 28px; padding-right: 33px; margin-top: 6px; }
  .bookmark-button { width: 44px; height: 44px; bottom: -8px; right: -9px; }
  .bookmark-button .icon { width: 19px; height: 19px; }
  .card-time { font-size: 10px; bottom: 10px; right: 10px; }
  .mini-meme { font-size: 10px; bottom: 14px; }
  .site-footer { margin: 0 20px; padding: 18px 0; }
  .site-footer a { display: inline-flex; align-items: center; min-height: 44px; }
  .icon-button, .icon-button.small { min-width: 44px; min-height: 44px; }
  .source-chip { padding: 0 0 0 10px; font-size: 12px; }
  .source-chip > span { max-width: min(210px, 48vw); }
  .source-chip > .icon { width: 17px; height: 17px; }

  /* One focused pane at a time; switching panes keeps drafts intact. */
  body[data-view=create] .topbar { position: relative; min-height: 68px; }
  body[data-view=create] .search-box { display: none; }
  body[data-view=create] #main { padding-top: 16px; padding-bottom: 14px; }
  body[data-view=create] .view-heading { margin-bottom: 14px; }
  body[data-view=create] .site-footer { display: none; }
  .studio-mode-switch .icon-button { width: 44px; height: 44px; }
  .studio-workbench { padding: 14px 14px 84px; border-radius: 23px; }
  .workbench-header { margin: -4px 0 5px; min-height: 36px; }
  .step-number { font-size: 10px; letter-spacing: 1.6px; }
  .workbench-header .icon-button { margin: -4px -4px -4px 0; }
  .source-zone { min-height: 140px; padding: 12px; border-radius: 18px; }
  .source-actions { margin: 2px 0 12px; gap: 18px; }
  .source-actions .icon-button { width: 48px; height: 50px; border-radius: 14px; }
  .source-zone textarea { min-height: 50px; padding: 4px 0; line-height: 1.5; resize: vertical; }
  .source-zone textarea::placeholder { font-size: 14px; }
  .source-list:empty, .source-count:empty { display: none; }
  .source-list { margin-top: 10px; }
  .source-count { margin-top: 6px; font-size: 11px; min-height: 0; }
  .connector-line { height: 25px; }
  .connector-line:before, .connector-line:after { height: 5px; }
  .format-choices { gap: 8px; }
  .format-choice { min-height: 58px; padding: 12px 5px; border-radius: 15px; }
  .format-choice .icon { width: 25px; height: 25px; }
  .format-hint { font-size: 10px!important; }
  .palette-row { gap: 2px; margin: 9px 0 6px; }
  .palette { width: 44px; height: 44px; border: 0; padding: 12px; background-clip: content-box; outline: 0; position: relative; }
  .palette[aria-pressed=true]::after { content: ""; position: absolute; inset: 8px; border: 1px solid var(--ink); border-radius: 50%; }
  .generate-row {
    position: fixed; left: 0; right: 0;
    bottom: calc(var(--phone-nav) + env(safe-area-inset-bottom));
    padding: 16px 20px 12px; z-index: 30; pointer-events: none;
    background: linear-gradient(transparent, var(--paper) 25%);
  }
  .generate-button { height: 54px; max-width: 460px; border-radius: 17px; pointer-events: auto; }
  #generation-error:empty { display: none; }
  .chat-panel { height: calc(var(--visible-height, 100dvh) - 248px - env(safe-area-inset-bottom) - env(safe-area-inset-top)); min-height: 280px; border-radius: 23px; }
  .chat-header { padding: 12px 14px; }
  .chat-messages { padding: 15px; }
  .chat-empty { gap: 18px; min-height: 160px; }
  .chat-empty svg { width: 160px; height: 130px; }
  .chat-bubble { padding: 13px 15px; border-radius: 17px; line-height: 1.6; }
  .chat-compose { margin: 0 10px 10px; padding: 8px; border-radius: 18px; }
  .chat-compose textarea { min-height: 44px; height: 46px; padding: 5px; resize: none; }
  .chat-tools .icon-button { width: 44px; height: 44px; }
  .chat-tools .icon { width: 20px; height: 20px; }
  .chat-sources a { min-height: 36px; font-size: 12px; }
  .keyboard-open .rail, .keyboard-open .generate-row { display: none; }
  .keyboard-open .app-shell { padding-bottom: 0; }
  body[data-view=create][data-studio-panel=chat].keyboard-open .topbar { display: none; }
  body[data-view=create][data-studio-panel=chat].keyboard-open #main { padding-top: 8px; }
  body[data-view=create][data-studio-panel=chat].keyboard-open .view-heading { margin-bottom: 8px; }
  body[data-view=create][data-studio-panel=chat].keyboard-open .chat-panel { height: calc(var(--visible-height, 100dvh) - 88px); min-height: 200px; }

  /* Sheets keep their close control visible and respect phone safe areas. */
  dialog, dialog.small-dialog {
    width: 100%; max-width: 100%; max-height: calc(var(--visible-height, 100dvh) - 10px);
    margin: auto 0 0; border-radius: 27px 27px 0 0; border-bottom: 0; overscroll-behavior: contain;
  }
  .modal-header { padding: 20px 16px 10px; gap: 12px; border-bottom-color: #e1e6dcaa; }
  .modal-header::before { content: ""; position: absolute; width: 34px; height: 4px; border-radius: 4px; background: #cbd4c2; top: 8px; left: calc(50% - 17px); }
  .modal-header h2 { font-size: 17px; line-height: 1.4; }
  .modal-header .icon-button { width: 44px; height: 44px; background: #e9eee2; border-radius: 50%; }
  .modal-body, .small-dialog .modal-body { padding: 16px 16px calc(22px + env(safe-area-inset-bottom)); }
  .player-stage { aspect-ratio: 16 / 11; }
  .player-stage.short-stage { height: min(430px, calc(100svh - 310px)); min-height: 240px; }
  .player-controls { padding: 8px; gap: 4px; }
  .player-controls .icon-button { width: 44px; height: 44px; }
  .player-controls input { height: 44px; }
  .player-time { font-size: 10px; }
  .scene-strip { scroll-snap-type: x proximity; scrollbar-width: none; }
  .scene-thumb { width: 72px; scroll-snap-align: start; }
  .scene-thumb svg { height: 48px; }
  .viewer-grid { gap: 10px; }
  .viewer-actions { order: -1; margin: 0 0 16px; padding: 0 0 14px; border: 0; border-bottom: 1px solid var(--line); gap: 8px; }
  .viewer-actions .icon-button { flex: 1; height: 48px; max-width: 60px; border-radius: 15px; }
  .viewer-topic { margin-bottom: 8px; }
  .transcript { font-size: 15px; line-height: 1.7; max-height: none!important; overflow: visible; }
  .viewer-bottom { flex-direction: column; align-items: stretch; gap: 10px; }
  .viewer-bottom .viewer-actions { margin-bottom: 0; }
  .source-links a { min-height: 44px; display: inline-flex; align-items: center; font-size: 12px; }
  .practice-tabs { justify-content: center; gap: 12px; margin-bottom: 14px; }
  .practice-tabs .icon-button { width: 52px; height: 48px; border-radius: 15px; }
  .practice-surface { padding: 18px 14px; border-radius: 22px; min-height: 320px; }
  .practice-surface h3 { font-size: 22px; margin: 16px 0; }
  .answer-button { min-height: 58px; padding: 14px 12px; font-size: 15px; border-radius: 15px; }
  .answer-explanation, .poster-panel p, .visual-transcript { font-size: 15px; }
  .flashcard { min-height: 230px; padding: 22px; font-size: 22px; }
  .practice-footer .icon-button { width: 54px; height: 54px; border-radius: 17px; }
  .share-grid { flex-wrap: wrap; gap: 12px; }
  #toast { bottom: calc(var(--phone-nav) + env(safe-area-inset-bottom) + 12px); width: max-content; max-width: calc(100vw - 32px); font-size: 13px; }
  .keyboard-open #toast { bottom: 12px; }
  button:active:not(:disabled), a.nav-button:active { transform: scale(.96); }
}

@media (max-width: 359px) {
  .topbar { padding-left: 16px; padding-right: 16px; }
  #main { padding-left: 16px; padding-right: 16px; }
  .feature-copy { padding: 20px; }
  .feature h2 { max-width: 200px; font-size: 28px; }
  .create-banner { gap: 14px; padding-left: 14px; }
  .banner-plus { display: none; }
  .card-title { font-size: 13px; }
  .card-topics { font-size: 7px; }
  .palette-row { gap: 0; }
  .palette { width: 40px; height: 44px; padding: 12px 10px; }
  .studio-workbench { padding: 12px 12px 84px; }
  .studio-mode-switch { gap: 0; padding: 3px; }
  .studio-mode-switch .icon-button { min-width: 42px; width: 42px; }
}
