/* SvgPlayground narrow layout (SVGPLAYG-184).
 *
 * Desktop is unchanged from 900px up. Below that, media queries restack the
 * editor so a phone or tablet can actually use it: no horizontal page scroll,
 * the canvas keeps most of the viewport, toolbars scroll inside themselves
 * with 40px touch targets, menus and dialogs stay on screen, and floating /
 * docked tool panels become a toggleable bottom sheet (see responsive.js,
 * which only toggles .svgp-sheet-open).
 *
 * Two bands:
 *   max-width: 899px  — tablet and phone (768px included)
 *   max-width: 599px  — phones: hide rulers so the canvas is wider
 */

@media (max-width: 899px) {
  :root {
    --svgp-touch-min: 40px;
    --svgp-sheet-height: 42vh;
    --svgp-mobile-chrome-bottom: var(--svgp-touch-min);
    --svgp-dock-left: 0px;
    --svgp-dock-right: 0px;
  }

  html,
  body {
    width: 100%;
    max-width: 100%;
    height: 100%;
    overflow: hidden;
  }

  #container {
    width: 100% !important;
    height: 100% !important;
    max-width: 100%;
    overflow: hidden;
  }

  /* Kill the reserved / JS-applied dock columns. Side panels become sheets
     over the canvas instead of shrinking it. */
  .svg_editor,
  .svg_editor.svgp-has-dock,
  .svg_editor.open {
    --svgp-dock-left: 0px !important;
    --svgp-dock-right: 0px !important;
    --svgp-toolbar-row-height: var(--svgp-touch-min);
    box-sizing: border-box;
    padding-left: 0 !important;
    padding-right: 0 !important;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    grid-template-rows: var(--svgp-toolbar-row-height) 15px 1fr var(--svgp-touch-min);
    grid-template-columns: var(--svgp-touch-min) 15px 50px 1fr;
    grid-template-areas:
      "main main main top"
      "left corner rulerX rulerX"
      "left rulerY workarea workarea"
      "left bottom bottom bottom";
  }

  #workarea {
    min-width: 0;
  }

  /* ----- Toolbars: one scrolling row / column, 40px targets ----- */

  #main_button {
    height: var(--svgp-toolbar-row-height);
    min-height: var(--svgp-touch-min);
  }

  #tools_top {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    min-width: 0;
    width: 100%;
    height: var(--svgp-toolbar-row-height);
    min-height: var(--svgp-touch-min);
    align-self: stretch;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  #tools_top > * {
    flex-wrap: nowrap;
    flex: 0 0 auto;
  }

  #tools_left {
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  #tools_bottom {
    min-height: var(--svgp-touch-min);
    height: var(--svgp-touch-min);
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }

  /* Shadow-DOM tool controls expose ::part(button). */
  se-button::part(button),
  se-flyingbutton::part(button),
  se-explorerbutton::part(button),
  se-colorpicker::part(button),
  se-list::part(button) {
    box-sizing: border-box;
    width: var(--svgp-touch-min);
    height: var(--svgp-touch-min);
    min-width: var(--svgp-touch-min);
    min-height: var(--svgp-touch-min);
    margin: 0;
    padding: var(--svgp-space-4);
  }

  se-spin-input::part(input),
  se-zoom::part(input),
  se-input::part(input),
  se-select::part(select) {
    min-height: var(--svgp-touch-min);
  }

  .tool_sep {
    height: calc(var(--svgp-touch-min) - 8px);
  }

  /* ----- Bottom sheets: docked and floating panels ----- */

  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window),
  #codemirror-floating-container:not(.fullscreen):not(.svgp-dialog__window) {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    bottom: var(--svgp-mobile-chrome-bottom) !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    margin: 0 !important;
    transform: none !important;
    resize: none !important;
    border-radius: var(--svgp-radius-xl) var(--svgp-radius-xl) 0 0 !important;
    border-width: 1px 0 0 0 !important;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.35);
    z-index: var(--svgp-z-floating-active) !important;
    /* Sit above the properties bar; stacked tabs use --svgp-sheet-offset. */
    margin-bottom: var(--svgp-sheet-offset, 0px) !important;
  }

  .svgp-panel.svgp-docked:not(.fullscreen) {
    top: auto !important;
    bottom: var(--svgp-mobile-chrome-bottom) !important;
    width: 100% !important;
    border-radius: var(--svgp-radius-xl) var(--svgp-radius-xl) 0 0 !important;
    border-width: 1px 0 0 0 !important;
  }

  /* Peek / tab: header only, until .svgp-sheet-open (responsive.js). */
  .svgp-panel:not(.svgp-sheet-open):not(.fullscreen),
  #codemirror-floating-container:not(.svgp-sheet-open):not(.fullscreen) {
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
  }

  .svgp-panel:not(.svgp-sheet-open):not(.fullscreen) > :not(.svgp-panel-header):not(.svgp-floating-panel__header) {
    display: none !important;
  }

  .svgp-panel:not(.svgp-sheet-open):not(.fullscreen) > .svgp-panel-header,
  .svgp-panel:not(.svgp-sheet-open):not(.fullscreen) > .svgp-floating-panel__header {
    cursor: pointer;
    min-height: var(--svgp-touch-min);
    padding-top: 14px;
  }

  /* Grabber on the peek tab and on an open sheet header. */
  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window) > .svgp-panel-header::before,
  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window) > .svgp-floating-panel__header::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: var(--svgp-color-border-strong);
    transform: translateX(-50%);
    pointer-events: none;
  }

  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window) > .svgp-panel-header,
  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window) > .svgp-floating-panel__header {
    position: relative;
    min-height: var(--svgp-touch-min);
  }

  .svgp-panel.svgp-sheet-open:not(.svgp-collapsed):not(.fullscreen),
  #codemirror-floating-container.svgp-sheet-open:not(.svgp-collapsed):not(.fullscreen) {
    height: var(--svgp-sheet-height) !important;
    min-height: 160px !important;
    max-height: calc(100vh - var(--svgp-toolbar-row-height) - var(--svgp-mobile-chrome-bottom) - 24px) !important;
  }

  .svgp-dock-splitter {
    display: none !important;
  }

  /* Docking is a side column; on a narrow screen the sheet replaces it. */
  .svgp-panel-dock-btn,
  #icons-dock-btn {
    display: none !important;
  }

  /* Animation timeline and snap toggle sit above the sheet tabs, and must
     not overflow the viewport (the timeline is 220px + labels wide). */
  #svgp-timeline,
  .svgp-timeline {
    left: 50% !important;
    right: auto !important;
    top: auto !important;
    bottom: calc(var(--svgp-mobile-chrome-bottom) + var(--svgp-sheet-stack, 72px) + 8px) !important;
    width: auto !important;
    max-width: calc(100vw - 16px);
    box-sizing: border-box;
  }

  .svgp-timeline-scrub {
    width: min(220px, calc(100vw - 180px));
  }

  #ext-snapping-toggle,
  .ext-snapping-toggle {
    top: auto !important;
    bottom: calc(var(--svgp-mobile-chrome-bottom) + var(--svgp-sheet-stack, 72px) + 8px) !important;
  }

  .svgp-floating-panel__btn,
  .svgp-panel-icon-btn {
    width: var(--svgp-touch-min);
    height: var(--svgp-touch-min);
    min-width: var(--svgp-touch-min);
    min-height: var(--svgp-touch-min);
  }

  /* ----- Menus and dialogs stay in the viewport ----- */

  #main_menu {
    max-width: calc(100vw - 16px);
    max-height: calc(100vh - 64px);
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
  }

  .svgp-dialog,
  .svgp-dialog::part(frame),
  #se-img-prop,
  #se-edit-prefs,
  #se-cmenu_canvas,
  se-cmenu,
  .elix-dialog,
  [role="dialog"] {
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100vh - 16px) !important;
    box-sizing: border-box;
  }

  #se-cmenu_canvas,
  se-cmenu {
    max-height: calc(100vh - 24px) !important;
    overflow-y: auto;
  }
}

@media (max-width: 599px) {
  .svg_editor,
  .svg_editor.svgp-has-dock,
  .svg_editor.open {
    position: relative;
    grid-template-rows: var(--svgp-toolbar-row-height) 1fr var(--svgp-touch-min);
    grid-template-columns: var(--svgp-touch-min) 1fr;
    grid-template-areas:
      "top top"
      "left workarea"
      "bottom bottom";
  }

  /* The File menu is wider than the 40px tools column. Park it at the start
     of the top toolbar so the label stays readable. */
  #main_button {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 8;
    width: auto;
    min-width: 96px;
    height: var(--svgp-toolbar-row-height);
  }

  #tools_top {
    padding-left: 96px;
    box-sizing: border-box;
  }

  #ruler_x,
  #ruler_y,
  #ruler_corner {
    display: none;
  }

  /* A little extra room above the home indicator. */
  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window),
  #codemirror-floating-container:not(.fullscreen):not(.svgp-dialog__window) {
    bottom: calc(var(--svgp-mobile-chrome-bottom) + env(safe-area-inset-bottom, 0px)) !important;
  }
}

@media (max-width: 899px) and (prefers-reduced-motion: reduce) {
  .svgp-panel:not(.fullscreen):not(.svgp-dialog__window),
  #codemirror-floating-container:not(.fullscreen):not(.svgp-dialog__window) {
    box-shadow: none;
  }
}
