/* Mobile workspace: stable header rows and one task per view. */
.mobile-navigation { display: none; }
.save-icon { display: none; }
@media (max-width: 780px) {
  body { min-width: 0; }
  .topbar {
    /* Keep dropdowns above the workspace while the header scrolls normally. */
    position: relative;
    display: grid;
    grid-template-columns: 28px minmax(74px, 1fr) repeat(4, 36px);
    gap: 6px;
    padding: 10px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
    background: var(--bg);
  }
  .brand { grid-area: 1 / 1; min-width: 0; gap: 0; }
  .topbar .session-section, .topbar .session-controls { display: contents; }
  .topbar .session-name, .topbar .session-switcher { grid-area: 1 / 2; min-width: 0; }
  .topbar .edit-session-name { grid-area: 1 / 3; }
  .topbar .new-session-button { grid-area: 1 / 4; }
  .session-name input { min-height: 44px; font-size: 16px; }
  .edit-session-name, .new-session-button { width: 36px; min-height: 40px; padding: 6px; justify-content: center; border-radius: 10px; font-size: 12px; }
  .new-session-button span { display: none; }
  .brand input { width: 100%; min-width: 0; padding: 10px 0; font-size: 16px; text-overflow: ellipsis; }
  .brand-logo { display: none; }
  .brand-icon { display: block; width: 28px; height: 36px; }
  .topbar .account-menu-wrapper { grid-area: 1 / 6; z-index: 4; }
  .account-trigger { width: 36px; height: 40px; min-height: 40px; padding: 0; justify-content: center; }
  .account-menu-label, .account-chevron { display: none; }
  .account-dropdown { max-width: calc(100vw - 32px); }
  .save-wrap { display: contents; }
  .save-label { display: none; }
  .topbar .save-wrap #save-retry { grid-area: 1 / 5; display: grid; place-items: center; width: 36px; height: 40px; min-height: 40px; padding: 0; border-radius: 10px; color: var(--accent-soft); }
  .save-icon { display: block; }
  .transport { grid-area: 2 / 1 / auto / -1; width: 100%; display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto; gap: 8px; padding: 10px 0; border: 0; border-block: 1px solid var(--line); border-radius: 0; background: transparent; box-shadow: none; }
  .transport .bpm-control { margin: 0; gap: 4px; flex-wrap: wrap; font-size: 11px; }
  .transport input { width: 54px; min-height: 44px; padding: 6px; font-size: 16px; }
  .transport-buttons { gap: 4px; padding-left: 0; border-left: 0; }
  .transport-buttons button { width: 44px; height: 44px; min-height: 44px; }
  .transport .loop-control { min-height: 44px; gap: 6px; padding: 0; font-size: 11px; }
  .transport .loop-control input { min-height: 0; width: 16px; height: 16px; }
  body.metronome-playing .topbar {
    padding-bottom: calc(12px + var(--mobile-transport-height, 66px));
    /* Keep the fixed transport anchored to the viewport, not this header. */
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  body.metronome-playing .transport {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 25;
    padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
    background: rgba(11, 16, 32, .96);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    animation: mobile-transport-in .32s cubic-bezier(.22, 1, .36, 1) both;
  }
  body.metronome-playing .mobile-navigation { top: var(--mobile-transport-height, 66px); }
  body[data-mobile-view="grid"] .topbar { padding-bottom: 12px; }
  body[data-mobile-view="grid"] .mobile-navigation { top: 0; }
  body[data-mobile-view="grid"] .transport {
    position: sticky;
    top: var(--mobile-navigation-height, 61px);
    z-index: 14;
    padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
    background: rgba(11, 16, 32, .96);
    animation: none;
  }
  @keyframes mobile-transport-in {
    from { opacity: 0; transform: translateY(-100%); }
    to { opacity: 1; transform: translateY(0); }
  }
  .mobile-navigation { position: sticky; top: 0; z-index: 15; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; margin: 0; padding: 8px 16px; background: var(--bg); border-bottom: 1px solid var(--line); }
  .mobile-navigation button { border-color: transparent; background: transparent; color: var(--muted); font-size: 14px; font-weight: 650; }
  .mobile-navigation button[aria-pressed="true"] { background: var(--accent-tint); border-color: #45639a; color: var(--text); }
  /* Allow the header to scroll away even when the active tab is empty. */
  .layout {
    min-height: calc(100vh - var(--mobile-navigation-height, 61px));
    min-height: calc(100dvh - var(--mobile-navigation-height, 61px));
  }
  body[data-mobile-view="words"] .workspace, body[data-mobile-view="words"] .rhymes-sidebar,
  body[data-mobile-view="grid"] .sidebar, body[data-mobile-view="grid"] .rhymes-sidebar,
  body[data-mobile-view="rhymes"] .sidebar, body[data-mobile-view="rhymes"] .workspace { display: none; }
  body[data-mobile-view] .sidebar-toggle { display: none; }
  body[data-mobile-view] .sidebar.collapsed #sidebar-content { display: block; }
  .sidebar, .workspace, .rhymes-sidebar { padding: 20px 16px max(24px, env(safe-area-inset-bottom)); border: 0; background: transparent; }
  .rhymes-sidebar { padding-top: 0; }
  .sound-group-search-sticky { background: transparent; }
  .stock-panel { display: flex; flex-direction: column; padding: 0; border: 0; }
  .section-head { margin-bottom: 12px; gap: 12px; }
  .section-head h2 { margin: 0; font-size: 14px; letter-spacing: .03em; }
  #search { top: var(--mobile-navigation-height, 61px); height: 40px; min-height: 40px; font-size: 16px; border-radius: 6px; }
  #clear-session { order: 2; margin-top: 16px; color: var(--muted); background: transparent; }
  .word-list { gap: 12px; margin-top: 16px; }
  .word-card { padding: 12px; border-radius: 14px; background: var(--panel); }
  .word-head { gap: 6px; }
  .word-text { white-space: normal; overflow-wrap: anywhere; line-height: 1.45; }
  .small-action { width: 44px; min-height: 44px; flex-shrink: 0; }
  .tokens { gap: 8px; margin-top: 8px; }
  .token { min-width: 44px; min-height: 44px; }
  .workspace-head { gap: 16px; }
  .workspace h1 { font-size: 24px; }
  .measure-actions { width: 100%; flex-direction: row; flex-wrap: wrap; gap: 8px; }
  .measure-actions button { flex: 1 1 auto; padding: 8px; font-size: 12px; }
  .selection-help { line-height: 1.6; }
  .grid { --grid-zoom: 1; max-width: 100%; overscroll-behavior-x: contain; touch-action: pan-x pan-y; }
  .grid .measure {
    grid-template-columns: 52px repeat(8, var(--cell));
    grid-template-rows: repeat(2, auto);
    zoom: var(--grid-zoom);
  }
  .grid .measure-label { grid-row: 1 / span 2; }
  .grid .cell:nth-child(9) {
    border-right: 1px solid var(--line);
    border-radius: 0 8px 8px 0;
  }
  .grid .cell:nth-child(9) .word-occurrence { border-radius: 0 7px 7px 0; }
  .grid .cell:nth-child(10) .word-occurrence { border-radius: 7px 0 0 7px; }
  .grid-remove { top: -9px; right: -9px; width: 30px; height: 30px; min-height: 30px; font-size: 20px; touch-action: manipulation; }
  .rhyme-row { display: flex; align-items: center; gap: 6px; }
  .rhyme-row [data-action="rhyme-name"] { flex: 0 0 54px; width: 54px; height: 36px; }
  .rhyme-row input[type="color"] { flex: 0 0 28px; width: 28px; height: 36px; }
  .rhyme-row select { height: 36px; }
  .rhyme-row .small-action { flex: 0 0 36px; width: 36px; height: 36px; min-height: 36px; }
  .rhyme-group-actions { justify-content: space-between; }
  .rhyme-style-info { display: grid; place-items: center; width: 36px; height: 36px; min-height: 36px; padding: 0; border-color: var(--accent-soft); border-radius: 50%; color: var(--accent-soft); font-family: Georgia, serif; font-size: 20px; font-weight: 700; }
  .rhyme-style-info[aria-expanded="true"] { background: var(--accent-tint); color: #fff; }
  input, textarea, select, .rhyme-row select, .rhyme-row input, .manual-syllable-input { font-size: 16px; }
  .account-form { width: 100%; }
  .dialog-head { gap: 12px; }
  .dialog-head h2 { font-size: 20px; }
  .dialog-actions { flex-wrap: wrap; }
  .sample-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .sample-actions .danger-button { grid-column: 1 / -1; justify-self: start; margin-right: 0; }
  .sample-actions button { min-width: 0; }
  .sample-actions #sample-cancel, .sample-actions #sample-submit { width: 100%; }
  .dictionary-page-header { position: relative; z-index: 10; display: grid; grid-template-columns: minmax(0, 1fr) 44px; height: auto; min-height: 0; align-items: center; gap: 8px; padding: 10px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left)); }
  #dictionary-page .dictionary-page-header { grid-template-columns: 40px minmax(74px, 1fr) repeat(3, 36px); gap: 6px; }
  .dictionary-page-header > div:not(.dictionary-header-actions) { grid-column: 1 / -1; grid-row: 3; display: grid; grid-template-columns: 1fr; grid-template-rows: auto; justify-content: start; margin-top: 8px; }
  #dictionary-page .dictionary-page-header > div:not(.dictionary-header-actions) { grid-row: 2; }
  .dictionary-page-header .eyebrow { text-align: left; }
  .dictionary-page-header .hint { grid-column: 1; grid-row: 3; margin-top: 6px; padding-left: 0; border-left: 0; white-space: normal; }
  .dictionary-page-header h1 { grid-column: 1; grid-row: 2; }
  .dictionary-page-header > .dictionary-header-actions { display: contents; }
  .dictionary-header-actions .session-section { grid-column: 1 / -1; grid-row: 2; max-width: none; }
  .dictionary-header-actions .account-menu-wrapper { grid-column: 2; grid-row: 1; justify-self: end; }
  .dictionary-back { grid-column: 1; grid-row: 1; width: fit-content; align-self: center; }
  #dictionary-page .dictionary-header-actions .session-section,
  #dictionary-page .dictionary-header-actions .session-controls { display: contents; }
  #dictionary-page .dictionary-header-actions .session-name,
  #dictionary-page .dictionary-header-actions .session-switcher { grid-area: 1 / 2; min-width: 0; }
  #dictionary-page .dictionary-header-actions .edit-session-name { grid-area: 1 / 3; width: 36px; min-height: 40px; padding: 6px; }
  #dictionary-page .dictionary-header-actions .new-session-button { grid-area: 1 / 4; width: 36px; min-height: 40px; padding: 6px; }
  #dictionary-page .dictionary-header-actions .account-menu-wrapper { grid-area: 1 / 5; }
  #dictionary-page .dictionary-header-actions .account-trigger { width: 36px; height: 40px; min-height: 40px; }
  #dictionary-page .dictionary-back { width: 40px; min-height: 40px; padding: 4px; justify-content: center; }
  #dictionary-page .dictionary-back > span:last-child { display: none; }
  #dictionary-page .dictionary-back-icon { width: 30px; height: 30px; }
  .dictionary-page-header h1 { font-size: 28px; }
  .dictionary-page-content { width: auto; margin: 0; padding: 16px 16px max(32px, env(safe-area-inset-bottom)); }
  .dictionary-tabs {
    top: 0;
    display: flex;
    gap: 0;
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .dictionary-tabs::-webkit-scrollbar { display: none; }
  .dictionary-tab {
    flex: 0 0 auto;
    min-width: 0;
    padding: 10px 14px;
    font-size: 13px;
    line-height: 1.3;
    white-space: nowrap;
  }
  .word-group-form > div, .word-group-item { grid-template-columns: 1fr; }
  .word-group-form > div button { width: 100%; }
  .word-groups-heading, .word-groups-search-and-filters { grid-template-columns: 1fr; align-items: stretch; }
  .word-groups-heading-actions { display: grid; grid-template-columns: 44px 44px minmax(0, 1fr); }
  #word-groups-panel .word-groups-heading { top:44px; grid-template-columns:minmax(0,1fr); gap:12px; padding:14px; }
  #word-groups-panel #word-groups-search { grid-column:1; grid-row:1; }
  #word-groups-panel .word-groups-heading-actions { grid-column:1; grid-row:2; }
  #word-groups-panel .sound-group-filters { grid-column:1; grid-row:3; grid-template-columns:minmax(0,1fr); gap:8px; padding-top:12px; }
  #word-groups-panel .word-groups-select-filters { grid-column:1; grid-template-columns:repeat(2,minmax(0,1fr)); }
  #word-groups-panel .word-groups-select-filters .sound-group-filter-select:last-child { grid-column:1/-1; }
  #word-groups-panel .word-groups-filters-reset { grid-column:1; justify-self:end; }
  #word-groups-panel .word-groups-vowel-filters, #word-groups-panel .word-groups-vowel-filters[open] { grid-column:1; grid-row:auto; }
  #word-groups-panel .word-groups-vowel-filters summary, #word-groups-panel .word-groups-vowel-filters[open] summary { width:100%; }
  #word-groups-panel .word-groups-vowel-filter-grid { grid-template-columns:repeat(2,minmax(0,1fr)); max-width:348px; }
  #word-groups-panel .word-groups-quick-filters { grid-column:1; }
  #word-groups-panel .word-groups-quick-filters .sound-super-group-filter { width:100%; }
  .word-group-actions { width: 100%; }
  .word-group-actions button { flex: 1; }
  .dictionary-controls { grid-template-columns: 1fr; top: 44px; margin-bottom: 16px; padding: 12px; }
  .library-ungrouped-words { top:calc(44px + var(--word-groups-heading-height,86px) + var(--word-groups-alphabet-height,50px) + 8px); }
  .dictionary-search-label { grid-column: 1; }
  .dictionary-toolbar { align-items: stretch; flex-direction: column; }
  .dictionary-toolbar label { width: 100%; }
  .dictionary-alphabet { grid-column: 1; padding-bottom: 2px; }
  #word-groups-alphabet { top:calc(44px + var(--word-groups-heading-height,86px)); }
  .dictionary-list { grid-template-columns: 1fr; }
  .dictionary-item { align-items: stretch; flex-direction: column; gap: 8px; }
  .dictionary-item-actions { width: 100%; }
  .dictionary-item-actions [data-action="dictionary-select"] { flex: 1; width: auto; }
  .toast { width: max-content; max-width: calc(100vw - 32px); bottom: max(16px, env(safe-area-inset-bottom)); text-align: center; }
}
/* Stock cards keep native scrolling. Grid words reserve the gesture so an
   appui long can become a drag without the browser cancelling the pointer. */
@media (pointer: coarse) {
  .word-card, .word-drag-handle { touch-action: auto; cursor: pointer; }
  .cell.occupied { touch-action: none; cursor: grab; }
  .cell.occupied:active { cursor: grabbing; }
  .rhyme-list .rhyme-word.word-drag-handle { touch-action: none; cursor: grab; }
  .rhyme-list .rhyme-word.word-drag-handle:active { cursor: grabbing; }
}

@media (max-width: 360px) {
  .topbar .session-picker-icon,
  #dictionary-page .session-picker-icon { display: none; }
}
