/* Interaction ink follows the rendered surface, independently of the OS scheme.
   MathLive otherwise chooses its dark selection ink from prefers-color-scheme,
   even when Chalk is showing a light theme or a dark chalk surface in light mode. */
math-field {
  --chalk-math-highlight-ink: #17384c;
  --chalk-math-highlight-fill: #dcecf4;
  --chalk-math-selection-ink: #ffffff;
  --chalk-math-selection-fill: #244b68;
  --contains-highlight-color: var(--chalk-math-highlight-ink) !important;
  --contains-highlight-background-color: var(--chalk-math-highlight-fill) !important;
  --selection-color: var(--chalk-math-selection-ink) !important;
  --selection-background-color: var(--chalk-math-selection-fill) !important;
  --highlight-text: var(--chalk-math-highlight-fill) !important;
}
body[data-theme="dark"] math-field,
body[data-paper="chalkboard"] #note-editor math-field {
  --chalk-math-highlight-ink: #f4f8fb;
  --chalk-math-highlight-fill: #354e60;
  --chalk-math-selection-ink: #17384c;
  --chalk-math-selection-fill: #dcecf4;
}
.touch-reference-picker, .touch-object-actions { display: none; }


@media (max-width: 820px), (hover: none) and (pointer: coarse) {
  body[data-tools="hidden"] .workspace-toolbar > #tool-library-button { display: inline-flex; }
  .workspace-toolbar > #tool-library-button[aria-expanded="true"] { color: var(--on-pen); background: var(--pen); }
  .notebook-tabs { position: sticky; top: 0; z-index: 40; background: var(--sheet); }
  .touch-reference-picker {
    display: flex; align-items: center; gap: 8px; min-width: 0; width: 100%;
    color: var(--ink); font-size: 12px; font-weight: 650;
  }
  .touch-reference-picker select {
    min-width: 0; flex: 1; min-height: 44px; padding: 6px 28px 6px 10px;
    color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 7px; font: inherit;
  }
  .graph-pane .pane-bar:has(.touch-reference-picker) > .right-pane-tabs { display: none; }
  .note-editor .board-object { touch-action: pan-y pinch-zoom; max-width: calc(100% - 44px); }
  .note-editor .board-object :is(.geo-canvas, .lewis-canvas, .skeletal-canvas) { touch-action: pan-y pinch-zoom; }
  .note-editor .board-object :is(.lewis-atom, .lewis-slot, .skeletal-atom, .skeletal-bond, .geo-vertex, .geo-label) { touch-action: none; }
  .note-editor .board-object .skeletal-canvas:is([data-draw-tool="chain"], [data-draw-tool="bond"]) { touch-action: none; }
  .note-editor :is(.object-move, .object-resize, .math-drag) { touch-action: none; }
  .touch-object-actions:not([hidden]) {
    position: fixed; z-index: 68; left: 8px; right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    display: flex; align-items: center; gap: 4px; padding: 4px 6px;
    color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lift);
  }
  .touch-object-actions strong { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: normal; font-size: 12px; }
  .touch-object-actions button { min-width: 44px; min-height: 44px; padding: 6px 8px; color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: 6px; font: inherit; font-size: 12px; }
  .touch-object-actions [data-touch-object-action="move"], .touch-object-actions [data-touch-object-action="resize"] { touch-action: none; }
  body:not([data-mobile-layer="none"]) .touch-object-actions { display: none; }
  .editor-scroll { scroll-padding-top: 18px; scroll-padding-bottom: 76px; }
  .editor-scroll { padding-bottom: calc(76px + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)) + env(safe-area-inset-bottom, 0px)); }
  #object-dialog[open]:not(:modal) {
    bottom: calc(max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)) + 8px + env(safe-area-inset-bottom, 0px));
    max-height: calc(var(--touch-viewport-height, 100dvh) - var(--math-keyboard-inset, 0px) - 24px);
  }
}

/* Width never enables these actions on a desktop mouse/trackpad. */
@media (hover: none), (pointer: coarse) {
  .note-editor .inline-math { max-width: 100%; }
  .note-editor .inline-math math-field { max-width: 100%; min-width: 0; }
  .note-editor .inline-math > .math-actions {
    position: fixed; top: var(--touch-math-y); left: var(--touch-math-x, 8px);
    margin: 0; right: auto; bottom: auto;
    box-sizing: border-box; width: var(--touch-math-width, calc(100vw - 16px)); max-width: none;
    flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; scrollbar-width: none;
    transform: none; gap: 2px; padding: 2px; z-index: 68;
    color: var(--ink); background: var(--paper); border-color: var(--line);
  }
  .note-editor .inline-math > .math-actions button { flex: 0 0 auto; box-sizing: border-box; min-width: 44px; height: 44px; min-height: 44px; line-height: 20px; padding: 8px 10px; color: var(--ink); font-size: 12px; }
  .note-editor .inline-math > .math-actions .math-action-dismiss { display: block; position: sticky; right: -2px; order: -1; left: -2px; background: var(--paper); }
  .note-editor .inline-math:focus-within > .math-actions:not([data-dismissed]) { opacity: 1; visibility: visible; pointer-events: auto; }
  .note-editor .inline-math > .math-actions[data-dismissed] { opacity: 0; visibility: hidden; pointer-events: none; }
  .note-editor .inline-math:hover > .math-actions,
  .note-editor .inline-math:focus-within > .math-actions { transform: none; }
}
@media print {
  .touch-reference-picker, .touch-object-actions { display: none !important; }
}
@media (max-height: 500px) and (max-width: 820px),
       (max-height: 500px) and (hover: none) and (pointer: coarse) {
  #object-dialog[open]:not(:modal) .object-field { margin-block: 5px; }
  #object-dialog[open]:not(:modal) .object-field math-field { min-height: 32px; padding: 2px 5px; font-size: 17px; }
}

/* Selected controls stay discoverable independently of the hold feature flag. */
@media (hover: none), (pointer: coarse) {
  .toast {
    z-index: 80; box-sizing: border-box; width: max-content; max-width: calc(100% - 24px);
    bottom: calc(12px + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)));
  }
  .toast button { min-width: 44px; min-height: 44px; }
  .note-editor .inline-math > .math-actions button { white-space: nowrap; }
  .note-editor .board-object.is-picked :is(.object-move, .object-resize) {
    display: grid; opacity: 1; width: 28px; height: 28px;
  }
  .note-editor .board-object.is-picked :is(.object-move, .object-resize)::before { inset: -8px; }
  .note-editor .inline-math:is(:focus-within, .is-picked) > .math-drag {
    display: grid; opacity: 1; width: 24px; left: -24px; top: 0; height: 100%; transform: none;
  }
}
.note-editor .lifting { touch-action: none; transform: scale(1.02); box-shadow: var(--shadow-lift); }
.note-editor .lifting, .note-editor .lifting * { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

/* Phone dock: its measured height includes the safe area exactly once. */
@media (max-width: 600px) {
  body[data-visual-theme] .workspace-toolbar {
    position: fixed; left: 0; right: 0; top: auto;
    bottom: max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px));
    box-sizing: border-box; width: auto; flex-wrap: nowrap;
    overflow-x: auto; overflow-y: hidden;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
    background: var(--sheet); border-top: 1px solid var(--line);
  }
  .workspace-toolbar > button { flex: 0 0 auto; min-height: 44px; }
  .workspace-toolbar > :is(.chip-optional, .chip-optional-late) { display: inline-flex; }
  .workspace-toolbar .chip-primary { order: -1; }
  .workspace-toolbar > #tool-library-button { position: static; flex: 0 0 auto; }
  .editor-scroll {
    padding-bottom: calc(var(--phone-toolbar-height, 68px) + 76px + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)));
    scroll-padding-bottom: calc(var(--phone-toolbar-height, 68px) + 76px);
  }
  .touch-object-actions:not([hidden]), .toast {
    bottom: calc(var(--phone-toolbar-height, 68px) + 8px + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)));
  }
}

/* Short touch landscapes share one navigation row. The document actions keep
   their four 44px targets; the tools scroll within the remaining width. Keep
   this query in sync with shortLandscape in workspace/mobile-workspace.js. */
@media (max-width: 1000px) and (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
  body[data-visual-theme] > .toolbar { height: 52px; min-height: 52px; padding-block: 4px; }
  .toolbar .brand { display: none; }
  body[data-visual-theme] .workspace-toolbar {
    position: fixed; top: 0; left: 0; right: 198px; bottom: auto;
    width: auto; height: 52px; min-height: 52px; padding: 4px 8px; border: 0;
  }
  body[data-visual-theme] .workspace-toolbar > button { min-width: 44px; min-height: 44px; }
  body[data-visual-theme]:not([data-focus="board"]) .app-shell {
    height: calc(var(--touch-viewport-height, 100dvh) - 52px);
  }
  body[data-visual-theme] .editor-scroll {
    min-height: 0; padding-top: 8px;
    padding-bottom: calc(76px + max(var(--math-keyboard-inset, 0px), var(--touch-viewport-bottom, 0px)));
  }
  /* Keyboard dismissal restores navigation; editing gets the scarce height. */
  html[data-math-keyboard="open"] body:has(#note-editor math-field:focus) :is(header.toolbar, .workspace-toolbar, .notebook-tabs) { display: none; }
  html[data-math-keyboard="open"] body:has(#note-editor math-field:focus) .app-shell { height: var(--touch-viewport-height, 100dvh); }
}

/* The board keeps its authored footprint; phones edit factors at readable size. */
.dimensional-open-editor { display: none !important; }
@media (max-width: 820px), (hover: none) and (pointer: coarse) {
  .note-editor .board-object[data-kind="dimensional"] > .object-body { overflow: hidden; pointer-events: none; }
  .note-editor .board-object[data-kind="dimensional"] .dimensional-keep-quantity { display: none; }
  .note-editor .dimensional-open-editor {
    display: flex !important; position: absolute; inset: auto 4px 4px auto;
    width: auto; height: 44px; min-width: 110px; padding: 8px 12px;
    transform: none; opacity: 1; z-index: 5; font: 600 14px/1.2 var(--ui-font,system-ui);
    background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  }
}
.dimensional-mobile-overlay { padding: 0 0 var(--science-keyboard-inset); }
.dimensional-mobile-editor { width: min(760px,100%); height: calc(100dvh - var(--science-keyboard-inset)); max-height: calc(100dvh - var(--science-keyboard-inset)); }
.dimensional-mobile-editor .science-studio-footer { flex-direction: row; justify-content: flex-end; padding: 8px 12px; }
.dimensional-mobile-editor button { min-height: 44px; min-width: 44px; font-size: 14px; }
.dimensional-mobile-draft { display: block !important; position: relative !important; inset: auto !important; width: 100% !important; height: auto !important; min-width: 0; margin: 0 !important; padding: 0 !important; border: 0 !important; }
.dimensional-mobile-draft > .object-body { height: auto !important; }
.dimensional-mobile-draft .dimensional-stage { width: 100%; transform: none; }
.dimensional-mobile-draft .dimensional-board { padding: 0; gap: 16px; }
.dimensional-mobile-draft .dimensional-toolbar { display: grid; gap: 12px; }
.dimensional-mobile-draft .dimensional-toolbar > strong { display: none; }
.dimensional-mobile-draft .dimensional-target-label { display: grid; gap: 6px; font-size: 14px; }
.dimensional-mobile-draft .dimensional-target { width: 100% !important; box-sizing: border-box; min-height: 44px; font-size: 16px; }
.dimensional-mobile-draft .dimensional-board-actions { display: grid; grid-template-columns: 1fr 1fr 44px; gap: 6px; }
.dimensional-mobile-draft .dimensional-chain { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(260px,100%),1fr)); gap: 16px; counter-reset: factor; }
.dimensional-mobile-draft :is(.dimensional-given-group,.dimensional-factor-group,.dimensional-factor,.dimensional-given) { min-width: 0; width: 100%; box-sizing: border-box; }
.dimensional-mobile-draft :is(.dimensional-factor,.dimensional-given) { padding: 10px; border: 1px solid var(--line); border-radius: 8px; align-content: start; }
.dimensional-mobile-draft .dimensional-given::before { content: "Given quantity"; font-size: 14px; font-weight: 600; }
.dimensional-mobile-draft .dimensional-factor::before { counter-increment: factor; content: "Factor " counter(factor); font-size: 14px; font-weight: 600; }
.dimensional-mobile-draft :is(.dimensional-separator,.dimensional-given-spacer) { display: none; }
.dimensional-mobile-draft .dimensional-given .dimensional-fraction { display: block; min-height: 0; }
.dimensional-mobile-draft .dimensional-side { gap: 6px; min-width: 0; padding-block: 8px; }
.dimensional-mobile-draft .dimensional-value { width: 72px; min-height: 44px; font-size: 16px; box-sizing: border-box; }
.dimensional-mobile-draft .dimensional-units { width: 100%; min-width: 0; min-height: 44px; font-size: 16px; box-sizing: border-box; }
.dimensional-mobile-draft .dimensional-exact-label { font-size: 13px; min-height: 44px; }
.dimensional-mobile-draft .dimensional-exact-label input { width: 20px; height: 20px; }
.dimensional-mobile-draft :is(.dimensional-edit-units,.dimensional-unit-token) { min-height: 44px !important; font-size: 14px !important; }
.dimensional-mobile-draft .dimensional-factor-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.dimensional-mobile-draft .dimensional-feedback { font-size: 14px; }
/* Mobile: tapping a Math box already exposes its action menu.
   Hide the redundant per-row ellipsis toggle. */
@media (hover: none), (pointer: coarse) {
  .note-editor .inline-math > .math-menu-toggle {
    display: none !important;
  }
}
