/* All layout changes are confined to phones and short touch landscapes. */
.phone-studio-switch, .phone-preview-zoom { display: none; }
.phone-palette-open { display: none !important; }
.phone-move-button, .phone-sticker-button, .phone-image-button { display: none; }
.phone-history-actions { display: none; }
.geo-touch-hit { display: none; }
.phone-hide-keyboard { display: none; }
.phone-periodic-name, .phone-periodic-status, .phone-formula-category, .phone-board-management, .phone-unit-guidance { display: none; }
@media screen and (max-width: 600px),
       screen and (max-width: 1000px) and (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
  .quantity-conversion-controls input { min-height: 44px; font-size: 16px; }
  .quantity-conversion-controls button { min-height: 44px; }
  #note-editor :is(.geo-handle,.geo-touch-target,.lewis-atom *, .lewis-slot *) { touch-action: none; }
  #ink-bar:not([hidden]) { display: grid; grid-template-columns: repeat(6,minmax(0,1fr)); width: min(600px,calc(100vw - 16px)); box-sizing: border-box; gap: 4px; padding: 6px; bottom: calc(var(--phone-toolbar-height,64px) + 8px); }
  #ink-bar .ink-tools { display: contents; }
  #ink-bar :is(.ink-sep,[data-drawing-tool="line"],[data-drawing-tool="arrow"],[data-drawing-tool="triangle"],[data-drawing-tool="circle"],[data-drawing-tool="rectangle"]) { display: none; }
  #ink-shape, #ink-options > summary { display: block; box-sizing: border-box; width: 100%; min-width: 0; min-height: 44px; padding: 6px 2px; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: var(--r-tight,6px); font: 12px var(--ui-font,system-ui); }
  #ink-options { display: block; }
  #ink-options > summary { display: grid; place-items: center; list-style: none; cursor: pointer; }
  #ink-options > summary::-webkit-details-marker { display: none; }
  #ink-options > .ink-options-content { display: none; }
  #ink-options[open] > .ink-options-content { position: absolute; bottom: calc(100% + 6px); left: 0; right: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; padding: 12px; max-height: calc(100dvh - 180px); overflow: auto; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: var(--r-soft,10px); box-shadow: var(--shadow-lift); }
  #ink-bar > .ink-tool, #ink-bar .ink-tools > .ink-tool { min-width: 0; padding-inline: 2px; font-size: 12px; }
  #note-editor .geo-touch-hit { display: block; overflow: visible; }
  #note-editor .geo-touch-target { width: 100%; height: 100%; cursor: grab; }
  #note-editor .geo-handle { pointer-events: none; }
  .phone-periodic-searching > .phone-periodic-status { display: block; margin: 8px 0; font-size: 14px; }
  .ptable-grid.phone-periodic-search { grid-template-columns:repeat(2,minmax(0,1fr)) !important; grid-template-rows:none !important; grid-auto-rows:auto; min-width:0 !important; width:100%; gap:8px; padding:4px; box-sizing:border-box; }
  .ptable-grid.phone-periodic-search .ptable-cell:not([hidden]) { grid-column:auto !important; grid-row:auto !important; min-height:64px; display:grid; grid-template-columns:28px minmax(0,1fr); align-content:center; gap:3px; padding:8px; text-align:left; }
  .ptable-grid.phone-periodic-search .ptable-number { position:static; font-size:12px; }
  .ptable-grid.phone-periodic-search .ptable-symbol { font-size:18px; }
  .ptable-grid.phone-periodic-search .ptable-mass { display:none; }
  .ptable-grid.phone-periodic-search .phone-periodic-name { display:block; grid-column:1 / -1; font-size:14px; overflow-wrap:anywhere; }
  .phone-formula-category { display:grid; gap:6px; font-size:14px; margin-bottom:10px; }
  .phone-formula-category select, #formula-search { min-height:44px; font-size:16px; padding:8px; color:var(--ink); background:var(--paper); border:1px solid var(--line); border-radius:6px; min-width:0; }
  #formula-category-tabs { display:none; }
  #formula-view .reference-pin { min-width:44px; min-height:44px; align-self:start; }
  .phone-board-management { display:flex; flex-wrap:wrap; gap:6px; grid-column:1 / -1; grid-row:2; }
  .phone-board-management button { flex:1; min-height:44px; padding:8px; font-size:14px; }
  .phone-unit-guidance { display:block; font-size:14px; line-height:1.5; margin:0 0 12px; }
  .phone-hide-keyboard { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 6px 10px; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 6px; font: 14px var(--ui-font,system-ui); }
  .biology-studio-overlay { padding: 0; top: var(--phone-visible-top,0px); height: var(--phone-visible-height,100dvh); bottom: auto; }
  .biology-studio[data-phone-view] { width: 100%; border-radius: 0; }
  body[data-visual-theme] .workspace-toolbar {
    display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px;
    overflow: visible; padding-inline: max(10px, env(safe-area-inset-left));
  }
  body[data-visual-theme] .workspace-toolbar > :is(#tools-toggle,#toggle-check,#insert-graph,#insert-table,.workspace-toolbar-spacer) { display: none; }
  body[data-visual-theme] .workspace-toolbar > :is(#focus-notes,#add-line,#add-chem,#toggle-draw,#tool-library-button) {
    display: inline-flex; justify-content: center; gap: 3px; min-width: 0;
    min-height: 46px; padding-inline: 2px; font-size: 14px; order: 0;
  }
  body[data-visual-theme] .workspace-toolbar > #text-format-toolbar:not([hidden]) {
    display: flex !important; position: fixed; left: 8px; right: 8px;
    bottom: calc(var(--phone-toolbar-height, 64px) + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)) + 6px);
    overflow-x: auto; flex-wrap: nowrap; padding: 5px; z-index: 75;
    background: var(--paper); color: var(--ink);
  }
  #text-format-toolbar button { flex: 0 0 auto; min-height: 44px; min-width: 44px; font-size: 14px; }
  #reference-button { min-width: 44px; min-height: 44px; }
  #file-menu { max-height: calc(var(--phone-visible-height,100dvh) - 64px); overflow-y: auto; }
  .phone-history-actions { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 6px; border-top: 1px solid var(--line); }
  .phone-history-actions button { flex: 1; min-height: 44px; }
  .phone-move-button { display: inline-flex; align-items: center; justify-content: center; }
  body:has(#note-editor .board-object[data-kind="sticker"].is-picked) .phone-sticker-button { display: inline-flex; align-items: center; justify-content: center; }
  body:has(#note-editor .board-object[data-kind="sticker"].is-picked) .touch-object-actions strong { display: none; }
  body:has(#note-editor .board-object[data-kind="sticker"].is-picked) .touch-object-actions button { flex: 1; }
  .phone-sticker-button { order: -1; }
  body:has(#note-editor .board-object[data-kind="image"].is-picked) .phone-image-button { display: inline-flex; align-items: center; justify-content: center; }
  body:has(#note-editor .board-object[data-kind="image"].is-picked) .touch-object-actions strong { display: none; }
  #note-editor .image-frame-controls { display: none !important; }
  #note-editor .geo-controls > :not(.phone-palette-open) { display: none !important; }
  #note-editor .geo-controls > .phone-palette-open { display: inline-flex !important; align-items:center; justify-content:center; min-height:44px; padding:8px 12px; font-size:14px; }
  #note-editor .sticker-controls { display: none !important; }
  #note-editor :is(.lewis-palette,.skeletal-palette) { overflow: visible; min-height: 44px; }
  #note-editor :is(.lewis-palette,.skeletal-palette) > :not(.phone-palette-open) { display: none; }
  #note-editor :is(.lewis-palette,.skeletal-palette) > .phone-palette-open {
    display: inline-flex !important; min-height: 44px; min-width: 120px;
    align-items: center; justify-content: center; padding: 8px 12px; font-size: 14px;
  }
  .phone-palette-dialog[open] {
    box-sizing: border-box; width: calc(100% - 16px); max-width: 560px;
    max-height: calc(var(--phone-visible-height,100dvh) - 24px);
    margin: auto auto max(8px,env(safe-area-inset-bottom)); padding: 0;
    background: var(--paper); color: var(--ink); border: 1px solid var(--line); border-radius: 12px;
  }
  .phone-palette-dialog::backdrop { background: rgb(15 31 50 / .35); }
  .phone-palette-dialog header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; }
  .phone-palette-dialog h2 { margin: 0; font: 600 18px var(--ui-font,system-ui); }
  .phone-palette-commands { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; padding: 0 12px 12px; }
  .phone-palette-dialog button { min-height: 44px; min-width: 44px; padding: 8px; font: 14px var(--ui-font,system-ui); color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: var(--r-tight,6px); }
  .phone-palette-dialog button[aria-pressed="true"] { background: var(--pen); color: var(--on-pen); }
  .toolbar-actions > button, .modal-header > button { min-width: 44px; min-height: 44px; }
  #note-editor :is(.chem-palette,.geo-controls) button { min-width: 44px; min-height: 44px; font-size: 14px; }
  #note-editor .chem-palette {
    top: calc(100% + 28px); bottom: auto; /* clears the 44px ⋯ tools toggle at the box's corner */
    width: max(216px, 100%); max-width: calc(100vw - 112px);
    overflow-x: auto; overflow-y: hidden;
  }
  #note-editor .chem-palette > *, #note-editor .chem-palette button { flex-shrink: 0; }
  #note-editor .board-object[data-kind="unit-circle"] { min-height: 420px; }
  #note-editor .unit-circle-shell { grid-template-rows: minmax(210px,1fr) auto; }
  #note-editor .unit-circle-visual { padding: 36px 4px 0; }
  #note-editor .unit-circle-panel { overflow: auto; }
  #note-editor .unit-circle-values-toggle { min-height: 44px; font-size: 14px; }
  #note-editor .unit-circle-slider { min-height: 44px; }
  #note-editor .trig-exact { font-size: 14px; }

  /* Search and subjects stay reachable while the result list scrolls. */
  :is(#tools-dialog,#help-dialog)[open] {
    display: flex; flex-direction: column; width: 100%; max-width: none;
    height: var(--phone-visible-height, 100dvh); max-height: var(--phone-visible-height, 100dvh);
    margin: 0; top: var(--phone-visible-top, 0px); border-radius: 0; overflow: hidden;
  }
  #tools-dialog .modal-header { flex: 0 0 auto; padding: 10px 12px; }
  #tools-dialog .modal-header p { margin-bottom: 0; }
  #tools-dialog .tools-body {
    display: flex; flex-direction: column; min-height: 0; overflow: hidden;
    padding: 10px 12px max(12px, env(safe-area-inset-bottom)); gap: 10px; flex: 1;
  }
  #tools-dialog .tools-search-label, #tools-dialog .tools-tabs { flex-shrink: 0; }
  #tools-dialog .tools-tabs { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 4px; }
  #tools-dialog .tools-tabs button { min-width: 0; text-align: center; font-size: 14px; }
  #tools-dialog .tools-content { overflow-y: auto; overscroll-behavior: contain; min-height: 0; flex: 1; }
  #tools-dialog .tools-recent:has(#tools-clear-recent:disabled) { display: none; }
  #tools-dialog .tools-label { font-size: 16px; }
  #tools-dialog .tools-description { font-size: 14px; }

  .phone-studio-switch:not([hidden]) {
    display: flex; flex-shrink: 0; gap: 4px; padding: 6px 12px;
    border-bottom: 1px solid var(--line); background: var(--paper);
  }
  .phone-studio-switch button {
    flex: 1; min-height: 44px; color: var(--ink); background: transparent;
    border: 1px solid var(--line); border-radius: var(--r-tight,6px); font: 600 14px var(--ui-font,system-ui);
  }
  .phone-studio-switch button[aria-selected="true"] { background: var(--pen); color: var(--on-pen); border-color: var(--pen); }
  .phone-studio-switch button:focus-visible { outline: 2px solid var(--pen); outline-offset: 2px; }
  .biology-studio[data-phone-view], .graph-studio[data-phone-view] {
    height: calc(var(--phone-visible-height,100dvh) - var(--math-keyboard-inset,0px));
    max-height: calc(var(--phone-visible-height,100dvh) - var(--math-keyboard-inset,0px));
    top: var(--phone-visible-top,0px); bottom: auto;
  }
  .biology-studio[data-phone-view] { grid-template-rows: auto auto auto auto minmax(0,1fr) auto; }
  .biology-studio[data-phone-view] .biology-studio-workbench,
  .graph-studio[data-phone-view] .graph-studio-content { display: block; min-height: 0; overflow: auto; overscroll-behavior: contain; }
  [data-phone-view="edit"] > .biology-studio-workbench > .biology-preview-shell,
  [data-phone-view="preview"] > .biology-studio-workbench > .biology-studio-editor,
  [data-phone-view="edit"] > .graph-studio-content > .graph-studio-preview-shell,
  [data-phone-view="preview"] > .graph-studio-content > .graph-studio-controls { display: none; }
  .graph-studio[data-phone-view] .graph-mobile-jump { display: none; }
  .graph-studio[data-phone-view="preview"] .graph-studio-preview-shell { display: block; margin-top: 0; }
  .biology-studio[data-phone-view] .biology-preview-shell { overflow: auto; padding: 14px; }
  .biology-studio[data-phone-view] .biology-studio-preview { width: calc(100% * var(--phone-specimen-scale,1)); }
  .phone-preview-zoom { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; position: sticky; left: 0; }
  .phone-preview-zoom button { min-width: 44px; min-height: 44px; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: var(--r-tight,6px); font-size: 16px; }
  .phone-preview-zoom output { margin-left: auto; font: 14px var(--ui-font,system-ui); }
  .biology-studio[data-phone-view] .science-studio-footer { flex-shrink: 0; overflow: visible; padding-bottom: max(10px,env(safe-area-inset-bottom)); }
  .biology-studio[data-phone-view] .science-studio-footer .science-actions { display: flex; flex-wrap: wrap; gap: 6px; }
  .biology-studio[data-phone-view] .science-studio-footer button { min-width: 0; min-height: 44px; padding-inline: 10px; }
  .biology-studio[data-phone-view] :is(input,select,textarea) { font-size: 16px; }
  .biology-studio[data-phone-view] :is(button,summary) { min-height: 44px; }
  .biology-studio[data-phone-view] .science-tabs { flex-shrink: 0; }
}
@media screen and (max-width: 1000px) and (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
  #tools-dialog .tools-tabs { grid-template-columns: repeat(6,minmax(0,1fr)); }
  #tools-dialog .modal-header p { display: none; }
  .phone-studio-switch:not([hidden]) { padding-block: 2px; }
  .biology-studio[data-phone-view] .science-studio-header { padding-block: 4px; }
  .biology-studio[data-phone-view] .science-studio-header .biology-eyebrow { display: none; }
  .biology-studio[data-phone-view] .science-studio-footer { padding-block: 4px; }
  .biology-studio[data-phone-view] .science-studio-footer .science-status { max-width: 34%; }
}
@media print { .phone-studio-switch, .phone-palette-dialog, .phone-move-button, .phone-preview-zoom { display: none !important; } }
@media screen and (max-width:600px), screen and (max-width:1000px) and (max-height:500px) and (orientation:landscape) and (pointer:coarse) {
  .phone-palette-dialog header { position: sticky; top: 0; z-index: 1; background: var(--paper); }
  .phone-palette-dialog > select { box-sizing: border-box; display: block; margin: 0 12px 12px; width: calc(100% - 24px); min-height: 44px; padding: 8px; font-size: 16px; color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: var(--r-tight,6px); }
  .phone-palette-commands > [hidden] { display: none !important; }
  body:has(#text-format-toolbar:not([hidden])) .toast { bottom: calc(var(--phone-toolbar-height,64px) + 72px + max(var(--math-keyboard-inset,0px),var(--touch-viewport-bottom,0px))); }
  body:has(#ink-bar:not([hidden])) .toast { bottom: calc(var(--phone-toolbar-height,64px) + var(--phone-ink-height,320px) + 24px + max(var(--math-keyboard-inset,0px),var(--touch-viewport-bottom,0px))); }
}

/* Native iOS input focus must not enlarge the page beyond its phone layout. */
@media screen and (max-width:600px), screen and (max-width:1000px) and (max-height:500px) and (orientation:landscape) and (pointer:coarse) {
  #help-search, #tools-search, .math-visualizer .visualizer-control :is(input,select), .structure-exchange-dialog :is(textarea,select) { font-size:16px; }
}
body[data-ink="on"] .touch-object-actions { display:none; }
