/* Dedicated touch layout. Desktop controls return to their original slots. */
#mobileWorkspace, #mobilePageJump { display: none; }
.ai-markdown-table-wrap { max-width: 100%; overflow-x: auto; margin: 12px 0; }
.ai-markdown-table { border-collapse: collapse; min-width: 100%; width: max-content; max-width: none; font: inherit; }
.ai-markdown-table th, .ai-markdown-table td { min-width: 120px; max-width: 320px; padding: 9px 11px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; word-break: normal; overflow-wrap: break-word; }
.ai-markdown-table th { background: #eef2ff; color: #25324b; font-weight: 650; }
.ai-markdown-paragraph { margin: .6em 0; }
.ai-markdown-heading { margin: 1em 0 .45em; line-height: 1.35; }
.ai-message h1.ai-markdown-heading, .ai-message h2.ai-markdown-heading { font-size: 1.25em; }
.ai-message h3.ai-markdown-heading, .ai-message h4.ai-markdown-heading { font-size: 1.1em; }
.ai-markdown-list { margin: .6em 0; padding-left: 1.4em; }
.ai-markdown-code { max-width: 100%; overflow-x: auto; padding: 12px; background: #eef2f7; color: #24314a; border-radius: 8px; font-size: .9em; white-space: pre; }

@media (max-width: 900px), (hover: none) and (pointer: coarse) {
  body.mobile-workspace-active {
    --mobile-header-height: 54px;
    --mobile-dock-height: calc(112px + env(safe-area-inset-bottom, 0px));
    background: #272b33 !important;
  }
  body.mobile-workspace-active #mobileWorkspace { display: contents; }
  body.mobile-workspace-active #topbar,
  body.mobile-workspace-active #topbarScrollRail { display: none !important; }
  body.mobile-workspace-active #main {
    top: var(--mobile-header-height) !important;
    bottom: var(--mobile-dock-height) !important;
    height: auto !important;
    background: #272b33 !important;
    scrollbar-gutter: auto !important;
    transition: none !important;
  }
  body.mobile-workspace-active #canvasWrapper {
    margin: 12px auto !important;
    padding: 4px !important;
    min-height: 0 !important;
    box-shadow: none !important;
    border: 0 !important;
  }
  body.mobile-workspace-active #sheet { box-shadow: 0 2px 12px #0003; border-radius: 0; }
  body.mobile-workspace-active.dashboard-mode #main { bottom: 0 !important; }
  body.mobile-workspace-active #projectsView { top: 0 !important; height: 100% !important; background: #1a1f2a; }
  body.mobile-workspace-active.dashboard-mode #mobileDock,
  body.mobile-workspace-active.dashboard-mode #mobileToolContext { display: none !important; }
  body.mobile-workspace-active #mobileWorkspace [hidden] { display: none !important; }
  body.mobile-workspace-active .mobile-header {
    position: fixed; inset: 0 0 auto; z-index: 2200;
    display: flex; align-items: center; gap: 8px;
    height: var(--mobile-header-height); padding: 4px 8px; box-sizing: border-box;
    background: #141923; border-bottom: 1px solid #ffffff16; color: #f1f5f9;
  }
  body.mobile-workspace-active .mobile-project-title { flex: 1; min-width: 0; }
  body.mobile-workspace-active #currentProjectName {
    display: block !important; max-width: 100%; min-width: 0; padding: 0;
    font: 600 13px/1.35 system-ui, sans-serif; color: #e2e8f0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  body.mobile-workspace-active #mobileViewCaption { display: block; padding-top: 2px; font: 10px/1.15 system-ui, sans-serif; color: #9cabc1; }
  body.mobile-workspace-active.dashboard-mode #mobileViewCaption { display: none; }
  body.mobile-workspace-active #mobilePanelTitle { flex: 1; font: 650 16px/1.3 system-ui, sans-serif; }
  body.mobile-workspace-active #mobileHeader #backToProjects {
    width: 44px !important; min-width: 44px !important; max-width: 44px !important;
    height: 44px !important; min-height: 44px !important; padding: 0 !important; flex: 0 0 44px;
  }
  body.mobile-workspace-active #mobileHeader #aiToolBtn { min-width: 110px; height: 44px; padding: 0 12px; }
  body.mobile-workspace-active .mobile-control,
  body.mobile-workspace-active #mobileWorkspace .btn,
  body.mobile-workspace-active #mobileWorkspace .seg-btn {
    min-height: 44px; border: 1px solid #94a3b833; border-radius: 10px;
    color: #e8edf5; background: #222937; box-shadow: none;
    font: 600 14px/1.25 system-ui, sans-serif; cursor: pointer;
    padding: 8px 12px; touch-action: manipulation;
  }
  body.mobile-workspace-active #mobileWorkspace button:disabled { opacity: .4; cursor: default; }
  body.mobile-workspace-active #mobileWorkspace .btn-label-text {
    max-width: none !important; opacity: 1 !important; overflow: visible !important;
    margin-left: 0 !important; position: static; transition: none !important;
  }
  body.mobile-workspace-active #mobileWorkspace button:focus-visible,
  body.mobile-workspace-active #pageInfo:focus-visible { outline: 2px solid #b5a0ff; outline-offset: -3px; }
  body.mobile-workspace-active #mobileViewToggle { width: 112px; min-width: 112px; height: 44px; max-height: 44px; padding: 6px; white-space: nowrap; font-size: 12px; }
  body.mobile-workspace-active .mobile-dock {
    position: fixed; inset: auto 0 0; z-index: 2100; box-sizing: border-box;
    height: var(--mobile-dock-height); padding: 4px 8px calc(4px + env(safe-area-inset-bottom, 0px));
    background: #141923; border-top: 1px solid #ffffff20; color: #e2e8f0;
  }
  body.mobile-workspace-active .mobile-page-nav .seg {
    display: grid !important; grid-template-columns: 54px minmax(0, 1fr) 54px;
    width: 100%; height: 48px; min-height: 48px; border: 0; background: transparent;
  }
  body.mobile-workspace-active #mobilePageNav .seg-btn {
    width: 54px !important; min-width: 54px !important; height: 48px !important;
    padding: 0; font-size: 30px; background: #262d3a;
  }
  body.mobile-workspace-active #pageInfo {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    min-width: 0; min-height: 48px; padding: 0 8px; color: #f4f5f9;
    font: 700 17px/1.15 system-ui, sans-serif; letter-spacing: .02em; cursor: pointer;
  }
  body.mobile-workspace-active #pageInfo::before { content: 'Страницы PDF ▾'; font: 500 11px/1.5 system-ui, sans-serif; color: #aebcd0; letter-spacing: 0; }
  body.mobile-workspace-active .mobile-tool-nav { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; margin-top: 4px; }
  body.mobile-workspace-active #mobileToolNav .btn,
  body.mobile-workspace-active #mobileMore {
    display: flex !important; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    width: 100% !important; min-width: 0 !important; max-width: none !important;
    height: 52px !important; min-height: 52px !important; padding: 4px 2px !important;
    margin: 0 !important; box-sizing: border-box;
    border-color: transparent; background: transparent; font-size: 12px;
  }
  body.mobile-workspace-active #mobileToolNav svg { width: 21px; height: 21px; flex: 0 0 21px; }
  body.mobile-workspace-active #mobileToolNav .btn-label-text,
  body.mobile-workspace-active #mobileHeader .btn-label-text { display: block !important; font-size: 12px; line-height: 1.2; white-space: nowrap; }
  body.mobile-workspace-active #mobileToolNav #aiToolBtn .btn-label-text { font-size: 0 !important; }
  body.mobile-workspace-active #mobileToolNav #aiToolBtn .btn-label-text::after { content: 'AI чат'; font-size: 12px; }
  body.mobile-workspace-active #mobileToolNav .btn.active,
  body.mobile-workspace-active #mobileMore[aria-expanded="true"] { background: #6d50b938 !important; color: #dfd2ff !important; border-color: #9579d9 !important; }
  body.mobile-workspace-active #mobileMore > span:first-child { height: 21px; font-size: 16px; letter-spacing: 2px; }
  body.mobile-workspace-active.ai-open #mobileDock { visibility: hidden; pointer-events: none; }
  body.mobile-workspace-active.ai-open #mobileHeader #backToProjects { display: none !important; }
  body.mobile-workspace-active .mobile-panel-backdrop { position: fixed; inset: var(--mobile-header-height) 0 var(--mobile-dock-height); z-index: 2250; background: #0007; }
  body.mobile-workspace-active.mobile-panel-open #main { pointer-events: none !important; }
  body.mobile-workspace-active .mobile-tools-panel {
    position: fixed; top: calc(var(--mobile-header-height) + 8px); right: 8px; left: 8px;
    bottom: calc(var(--mobile-dock-height) + 8px); z-index: 2300;
    padding: 14px; overflow-y: auto; overscroll-behavior: contain;
    border: 1px solid #7b879d55; border-radius: 16px; background: #1a202c; color: #e8edf5;
    box-shadow: 0 12px 40px #0006; box-sizing: border-box;
  }
  body.mobile-workspace-active .mobile-panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
  body.mobile-workspace-active .mobile-panel-heading h2 { margin: 0; font: 650 19px/1.3 system-ui, sans-serif; }
  body.mobile-workspace-active .mobile-panel-heading button { min-width: 44px; width: 44px; }
  body.mobile-workspace-active .mobile-tools-panel h3 { margin: 18px 0 9px; font: 600 13px/1.3 system-ui, sans-serif; color: #b3c1d5; }
  body.mobile-workspace-active .mobile-tools-note { color: #9dabc1; font: 13px/1.45 system-ui, sans-serif; margin: 8px 0; }
  body.mobile-workspace-active .mobile-tool-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  body.mobile-workspace-active #mobileDocumentTools .btn {
    display: flex !important; align-items: center; justify-content: flex-start; gap: 8px;
    width: auto !important; max-width: none !important; min-width: 0 !important;
    min-height: 48px !important; height: auto !important; padding: 9px !important; text-align: left;
  }
  body.mobile-workspace-active #mobileDocumentTools .btn-label-text { display: block !important; font-size: 13px; white-space: normal; }
  body.mobile-workspace-active #mobileDocumentTools svg { width: 20px; height: 20px; flex: 0 0 20px; }
  body.mobile-workspace-active .mobile-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
  body.mobile-workspace-active #mobileZoomTools > .seg { width: 100%; display: flex; height: 48px; }
  body.mobile-workspace-active #mobileZoomTools .seg-btn { width: 56px; min-width: 56px; font-size: 24px; padding: 0; }
  body.mobile-workspace-active #zoomLevel { flex: 1; min-width: 64px; display: flex; align-items: center; justify-content: center; min-height: 44px; font-size: 16px; }
  body.mobile-workspace-active #mobileEditTools .seg-btn { width: 48px; min-width: 48px; padding: 0; font-size: 24px; }
  body.mobile-workspace-active #mobileEditTools > .seg { display: flex; flex: 0 0 96px; width: 96px; min-width: 96px; height: 48px; }
  body.mobile-workspace-active #mobileEditTools #clearPageBtn { display: flex !important; align-items: center; gap: 8px; width: auto !important; min-width: 112px !important; max-width: none !important; height: 48px; padding: 8px 12px !important; flex: 0 0 auto; }
  body.mobile-workspace-active #mobileEditTools #clearPageBtn svg { width: 20px; height: 20px; flex: 0 0 20px; }
  body.mobile-workspace-active #mobileEditTools .btn-label-text { display: block !important; }
  body.mobile-workspace-active #mobileEditTools #counter { display: flex; padding: 8px; width: 100%; min-height: 44px; font-size: 13px; }
  body.mobile-workspace-active #mobileSearchSection .project-search {
    display: grid !important; grid-template-columns: minmax(0, 1fr) 64px; width: 100% !important;
    min-width: 0 !important; max-width: none !important; padding: 0 !important; gap: 7px;
    background: transparent; border: 0; height: auto !important;
  }
  body.mobile-workspace-active #mobileSearchSection .project-search > svg { display: none; }
  body.mobile-workspace-active #projectSearchInput { width: 100% !important; min-width: 0; min-height: 46px; height: 46px; padding: 10px; font-size: 16px; border: 1px solid #71819b66; border-radius: 10px; background: #0f1520; color: #f1f5f9; }
  body.mobile-workspace-active #projectSearchRun { min-height: 46px; padding: 4px; border-radius: 10px; font-size: 13px; }
  body.mobile-workspace-active #projectSearchStatus { grid-column: 1 / -1; font-size: 13px; }
  body.mobile-workspace-active #projectSearchStatus:empty { display: none; }
  body.mobile-workspace-active .project-search-nav { width: 100%; justify-content: space-between; min-height: 44px; }
  body.mobile-workspace-active .project-search-nav button,
  body.mobile-workspace-active #projectSearchClear { min-width: 44px; min-height: 44px; width: 44px; height: 44px; font-size: 22px; }
  body.mobile-workspace-active #projectSearchCounter { font-size: 14px; }
  body.mobile-workspace-active #thumbsPanel {
    position: fixed; top: calc(var(--mobile-header-height) + 8px) !important; bottom: calc(var(--mobile-dock-height) + 8px) !important;
    left: 8px !important; right: 8px !important; width: auto !important; height: auto !important;
    max-width: none; max-height: none; padding: 12px; box-sizing: border-box;
    z-index: 2300; transform: none !important; border-radius: 16px; background: #1a202c;
    display: none; flex-direction: column; gap: 10px;
  }
  body.mobile-workspace-active #thumbsPanel.active { display: flex; }
  body.mobile-workspace-active .thumbs-header { flex: 0 0 auto; padding: 0; min-height: 44px; }
  body.mobile-workspace-active .thumbs-title { font-size: 18px; }
  body.mobile-workspace-active #thumbsLeft,
  body.mobile-workspace-active #thumbsRight { display: none; }
  body.mobile-workspace-active #thumbsClose { width: 44px; height: 44px; min-width: 44px; font-size: 18px; }
  body.mobile-workspace-active #mobilePageJump { display: block; flex: 0 0 auto; margin: 0; }
  body.mobile-workspace-active #mobilePageJump label { display: block; margin-bottom: 6px; color: #c6d1e1; font: 13px/1.3 system-ui, sans-serif; }
  body.mobile-workspace-active #mobilePageJump > div { display: flex; gap: 8px; }
  body.mobile-workspace-active #mobilePageNumber { min-width: 0; width: 100%; min-height: 44px; padding: 8px 12px; box-sizing: border-box; font-size: 16px; color: #fff; border: 1px solid #94a3b844; background: #101621; border-radius: 10px; }
  body.mobile-workspace-active #mobilePageJumpStatus { color: #fca5a5; margin: 4px 0 0; font-size: 13px; }
  body.mobile-workspace-active #mobilePageJumpStatus:empty { display: none; }
  body.mobile-workspace-active #thumbnails {
    position: relative; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px; min-height: 0; flex: 1; overflow-y: auto; overflow-x: hidden;
    padding: 2px; align-content: start; overscroll-behavior: contain;
  }
  body.mobile-workspace-active #thumbnails .thumb { width: 100%; min-width: 0; padding: 5px; box-sizing: border-box; height: auto; }
  body.mobile-workspace-active #thumbnails .thumb-preview { width: 100%; height: 110px; aspect-ratio: auto; background: #fff; }
  body.mobile-workspace-active #thumbnails .thumb-preview img,
  body.mobile-workspace-active #thumbnails .thumb-preview canvas { width: 100%; height: 100%; object-fit: contain; }
  body.mobile-workspace-active #thumbnails .thumb-label { padding: 7px 0 3px; font: 650 15px/1.2 system-ui, sans-serif; }
  body.mobile-workspace-active #thumbnails .thumb-label::before { content: 'PDF '; color: #a9b6c9; font-weight: 400; }
  body.mobile-workspace-active .mobile-tool-context {
    position: fixed; left: 8px; right: 8px; bottom: calc(var(--mobile-dock-height) + 8px); z-index: 2050;
    padding: 4px; background: #172030f5; border: 1px solid #8194b655; border-radius: 12px; color: #e7edf6;
  }
  body.mobile-workspace-active .mobile-ruler-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  body.mobile-workspace-active .mobile-ruler-actions .mobile-control { font-size: 12px; padding: 4px 2px; }
  body.mobile-workspace-active #mobileRulerChain[aria-pressed="true"] { background: #6942b0; border-color: #b79bef; }
  body.mobile-workspace-active #mobileCalibrate.needs-calibration { color: #ffce7a; }
  body.mobile-workspace-active #mobileCalibrationActions { display: flex; align-items: center; justify-content: space-between; gap: 8px; font: 13px/1.3 system-ui, sans-serif; padding-left: 8px; }
  body.mobile-workspace-active #calibHint { top: calc(var(--mobile-header-height) + 8px); bottom: auto; width: max-content; max-width: calc(100vw - 24px); box-sizing: border-box; padding: 6px 10px; font-size: 12px; border-radius: 10px; }
  body.mobile-workspace-active.mobile-calibrating #calibHint { display: none !important; }
  body.mobile-workspace-active #floatingBtn { display: none !important; }
  body.mobile-workspace-active #assistant-widget:not(.assistant-panel-visible),
  body.mobile-workspace-active #assistant-launcher { display: none !important; }
  body.mobile-workspace-active #assistant-widget.assistant-panel-visible {
    right: 8px !important; bottom: calc(var(--mobile-dock-height) + 8px) !important;
  }
  body.mobile-workspace-active #assistant-panel {
    position: fixed !important; left: auto !important; top: auto !important; right: 8px !important;
    bottom: calc(var(--mobile-dock-height) + 8px) !important;
    width: min(440px, calc(100vw - 16px)) !important;
    max-height: calc(var(--emil-visual-viewport-height, 100dvh) - var(--mobile-header-height) - var(--mobile-dock-height) - 16px);
    overflow-y: auto;
  }
  body.mobile-workspace-active #assistant-close,
  body.mobile-workspace-active #assistant-mic { min-width: 44px; min-height: 44px; }
  body.mobile-workspace-active #mobileVoiceCommand { width: 100%; }
  body.mobile-workspace-active .ai-sidebar {
    top: var(--mobile-header-height) !important;
    height: calc(var(--emil-visual-viewport-height, 100dvh) - var(--mobile-header-height)) !important;
    max-height: calc(var(--emil-visual-viewport-height, 100dvh) - var(--mobile-header-height)) !important;
    z-index: 2150; transition: transform .16s ease-out !important;
  }
  body.mobile-workspace-active .ai-sidebar .ai-message { font-size: 16px !important; line-height: 1.55 !important; }
  body.mobile-workspace-active .ai-sidebar .ai-chat-area { padding: 12px !important; }
  body.mobile-workspace-active .ai-context-toolbar > button { min-height: 44px !important; font-size: 13px !important; }
  body.mobile-workspace-active .ai-context-toolbar > button span { white-space: normal; overflow: visible; text-overflow: clip; }
  body.mobile-workspace-active .attachment-viewer-btn { min-width: 44px !important; min-height: 44px !important; height: 44px !important; }
  body.mobile-workspace-active .attachment-viewer-table-wrap th,
  body.mobile-workspace-active .attachment-viewer-table-wrap td { font-size: 15px !important; min-width: 100px; }
  body.mobile-workspace-active #aiMicBtn,
  body.mobile-workspace-active #aiSendBtn { width: 46px !important; min-width: 46px !important; height: 46px !important; min-height: 46px !important; }
  body.mobile-workspace-active .selection-panel { bottom: calc(var(--mobile-dock-height) + 8px) !important; max-height: calc(var(--emil-visual-viewport-height, 100dvh) - var(--mobile-header-height) - var(--mobile-dock-height) - 16px) !important; }
  body.mobile-workspace-active .selection-panel { box-sizing: border-box; width: calc(100vw - 16px); padding: 8px; gap: 8px; overflow-y: auto; backdrop-filter: none; transition: none; }
  body.mobile-workspace-active .selection-panel-head { position: sticky; top: -8px; z-index: 1; background: #141e2e; min-height: 44px; }
  body.mobile-workspace-active #selectionContextMeta { display: none; }
  body.mobile-workspace-active #selectionCloseBtn { width: auto; min-width: 76px; height: 44px; flex: 0 0 auto; border-radius: 10px; gap: 6px; font: 600 14px/1.2 system-ui, sans-serif; color: #f8fafc; touch-action: manipulation; }
  body.mobile-workspace-active .selection-close-label { display: inline; }
  body.mobile-workspace-active .selection-panel .selection-preview,
  body.mobile-workspace-active .selection-panel .selection-preview img { max-height: 80px; }
  body.mobile-workspace-active .selection-voice-btn,
  body.mobile-workspace-active .selection-ai-btn { min-height: 44px; }
}

@media (min-width: 700px) and (max-width: 900px), (min-width: 700px) and (hover: none) and (pointer: coarse) {
  body.mobile-workspace-active #thumbnails { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  body.mobile-workspace-active .mobile-tools-panel { left: max(12px, calc((100vw - 660px) / 2)); right: max(12px, calc((100vw - 660px) / 2)); }
  body.mobile-workspace-active #currentProjectName { font-size: 16px; }
}

@media (max-height: 450px) and (max-width: 900px), (max-height: 450px) and (hover: none) and (pointer: coarse) {
  body.mobile-workspace-active { --mobile-header-height: 48px; --mobile-dock-height: calc(60px + env(safe-area-inset-bottom, 0px)); }
  body.mobile-workspace-active .ai-context-toolbar { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  body.mobile-workspace-active .ai-context-toolbar > button { min-height: 38px !important; font-size: 12px !important; }
  body.mobile-workspace-active.ai-open #aiContextPreviewContainer,
  body.mobile-workspace-active.ai-open .ai-files-shelf,
  body.mobile-workspace-active.ai-open .ai-session-cost { display: none !important; }
  body.mobile-workspace-active.ai-open .ai-input-wrapper { max-height: 108px !important; padding: 6px !important; }
  body.mobile-workspace-active.ai-open .ai-sidebar .ai-chat-area { min-height: 64px !important; padding: 8px 10px !important; }
}

@media (min-width: 600px) and (max-height: 450px) and (max-width: 900px), (min-width: 600px) and (max-height: 450px) and (hover: none) and (pointer: coarse) {
  body.mobile-workspace-active #mobileDock { display: grid; grid-template-columns: minmax(160px, 1fr) minmax(240px, 1.3fr); gap: 8px; align-items: center; }
  body.mobile-workspace-active .mobile-tool-nav { margin: 0; }
}
@media (max-width: 599px) and (max-height: 450px) {
  body.mobile-workspace-active { --mobile-dock-height: calc(112px + env(safe-area-inset-bottom, 0px)); }
  body.mobile-workspace-active #thumbsPanel.active { bottom: 8px !important; }
}
