/* Tablet responsive layer for molecules/.
   Keep selectors scoped to body.molecule-editor-page. */

body.molecule-editor-page {
  --molecule-tablet-css-loaded: 1;
  --molecule-topbar-height: var(--molecule-tablet-topbar-height);
  --molecule-tablet-gap: clamp(12px, 2vw, 20px);
  --molecule-tablet-panel-width: clamp(250px, 30vw, 340px);
  --molecule-tablet-panel-padding: clamp(14px, 2vw, 20px);
  --molecule-tablet-topbar-height: clamp(58px, 7svh, 76px);
  --molecule-tablet-canvas-min-height: clamp(420px, 58dvh, 780px);
  --molecule-tablet-touch-target: 44px;
  --molecule-tablet-modal-max-height: calc(100dvh - 36px);
}

body.molecule-editor-page #graphCanvas {
  min-width: 0;
  touch-action: none;
}

@media (min-width: 600px) and (max-width: 1439px) {
  body.molecule-editor-page .topbar-app-title {
    padding-inline: 8px 10px;
    font-size: 9px;
    letter-spacing: 0.08em;
  }

  body.molecule-editor-page .topbar-menu-bar {
    justify-content: flex-start;
    overflow: visible;
  }

  body.molecule-editor-page .topbar-menu-bar::-webkit-scrollbar {
    display: none;
  }

  body.molecule-editor-page .topbar-menu-trigger {
    height: 36px;
    padding-inline: 12px;
  }

  body.molecule-editor-page .topbar-menu-panel {
    min-width: 220px;
  }

  body.molecule-editor-page .topbar-btn,
  body.molecule-editor-page #toolbar :is(.tool-button-grid > button, .history-button-row > button, .modify-bond-order-row > button, .modify-wide-action, .preset-catalog-open, .template-catalog-open),
  body.molecule-editor-page .preset-btn,
  body.molecule-editor-page .history-button-row button,
  body.molecule-editor-page .camera-button-grid .hud-btn,
  body.molecule-editor-page .shortcut-key,
  body.molecule-editor-page .json-modal-actions button,
  body.molecule-editor-page .signature-modal-actions button,
  body.molecule-editor-page .export-modal-actions button,
  body.molecule-editor-page .tutorial-actions button,
  body.molecule-editor-page .legend-item,
  body.molecule-editor-page .bond-order-menu button {
    line-height: 1.15;
    white-space: normal;
    overflow-wrap: anywhere;
    text-wrap: balance;
  }

  body.molecule-editor-page .molecule-formula,
  body.molecule-editor-page .molecule-weight-row,
  body.molecule-editor-page #status,
  body.molecule-editor-page .tutorial-card p,
  body.molecule-editor-page .json-modal textarea,
  body.molecule-editor-page .signature-modal input,
  body.molecule-editor-page .export-modal input {
    overflow-wrap: anywhere;
    word-break: break-word;
  }
}

@media (min-width: 600px) and (max-width: 900px) {
  body.molecule-editor-page .topbar-app-title {
    display: none;
  }

  body.molecule-editor-page .topbar-menu-bar {
    overflow-x: auto;
    overflow-y: visible;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  body.molecule-editor-page .topbar-menu-panel {
    position: fixed;
    top: calc(var(--molecule-tablet-topbar-height) + 10px);
    left: 18px;
    right: 18px;
    width: auto;
    min-width: 0;
    max-height: calc(100dvh - var(--molecule-tablet-topbar-height) - 28px);
    overflow: auto;
  }

  body.molecule-editor-page .topbar-submenu-panel {
    position: static;
    min-width: 0;
    margin: 4px 0 7px 10px;
    border-color: rgba(234, 179, 8, 0.22);
    box-shadow:
      inset 0 0 14px rgba(234, 179, 8, 0.04),
      0 0 12px rgba(56, 189, 248, 0.08);
  }

  body.molecule-editor-page .topbar-submenu.open .topbar-submenu-arrow,
  body.molecule-editor-page .topbar-submenu:hover .topbar-submenu-arrow {
    transform: rotate(90deg);
  }
}

@media (min-width: 600px) and (max-width: 900px) and (orientation: portrait) {
  body.molecule-editor-page .panel-collapse-toggle {
    display: none;
  }

  html,
  body.molecule-editor-page {
    min-height: 100dvh;
    height: auto;
  }

  body.molecule-editor-page {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows:
      auto
      minmax(var(--molecule-tablet-canvas-min-height), 62dvh)
      auto;
    grid-template-areas:
      "topbar"
      "canvas"
      "left";
    padding:
      max(6px, env(safe-area-inset-top, 0px))
      max(0px, env(safe-area-inset-right, 0px))
      max(14px, env(safe-area-inset-bottom, 0px))
      max(0px, env(safe-area-inset-left, 0px));
    overflow-x: hidden;
    overflow-y: auto;
  }

  body.molecule-editor-page,
  body.molecule-editor-page * {
    cursor: auto !important;
  }

  body.molecule-editor-page .molecule-topbar {
    grid-template-columns: minmax(120px, auto) minmax(0, 1fr);
    grid-template-rows: auto auto;
    gap: 10px 16px;
    height: auto;
    min-height: var(--molecule-tablet-topbar-height);
    padding: 12px max(18px, env(safe-area-inset-right, 0px)) 12px max(18px, env(safe-area-inset-left, 0px));
  }

  body.molecule-editor-page .topbar-status-strip {
    display: none;
  }

  body.molecule-editor-page .topbar-app-title {
    display: none;
  }

  body.molecule-editor-page .topbar-brand {
    white-space: nowrap;
  }

  body.molecule-editor-page .topbar-menu-bar {
    grid-column: 1 / -1;
    width: 100%;
  }

  body.molecule-editor-page .topbar-link {
    white-space: nowrap;
  }

  body.molecule-editor-page .topbar-title-block {
    min-width: 0;
  }

  body.molecule-editor-page .topbar-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.molecule-editor-page .topbar-actions {
    grid-column: 1 / -1;
    width: 100%;
    gap: 10px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  body.molecule-editor-page .topbar-actions::-webkit-scrollbar {
    display: none;
  }

  body.molecule-editor-page .topbar-btn {
    flex: 1 0 116px;
    min-width: 0;
    min-height: var(--molecule-tablet-touch-target);
    height: var(--molecule-tablet-touch-target);
  }

  body.molecule-editor-page #graphCanvas {
    width: 100%;
    height: auto;
    min-height: var(--molecule-tablet-canvas-min-height);
    max-height: 68dvh;
    border-top: 1px solid rgba(56, 189, 248, 0.18);
    border-bottom: 1px solid rgba(56, 189, 248, 0.18);
  }

  body.molecule-editor-page #graphCanvas::before {
    top: 14px;
    right: 18px;
  }

  body.molecule-editor-page .molecule-panel {
    width: 100%;
    height: auto;
    min-height: 0;
    max-height: none;
    padding: var(--molecule-tablet-panel-padding);
    border-radius: 0;
    overflow: visible;
  }

  body.molecule-editor-page .left-panel {
    border-right: 1px solid rgba(56, 189, 248, 0.18);
    border-bottom: 1px solid rgba(56, 189, 248, 0.18);
    overscroll-behavior: contain;
  }

  body.molecule-editor-page .right-panel {
    display: none;
  }

  body.molecule-editor-page .tool-section,
  body.molecule-editor-page .panel-section {
    margin-bottom: var(--molecule-tablet-gap);
    padding: clamp(14px, 2.2vw, 18px);
  }

  body.molecule-editor-page .tool-button-grid,
  body.molecule-editor-page .history-button-row,
  body.molecule-editor-page .preset-grid,
  body.molecule-editor-page .camera-button-grid,
  body.molecule-editor-page .shortcut-pad {
    gap: 10px;
  }

  body.molecule-editor-page #toolbar :is(.tool-button-grid > button, .history-button-row > button, .modify-bond-order-row > button, .modify-wide-action, .preset-catalog-open, .template-catalog-open),
  body.molecule-editor-page .preset-btn,
  body.molecule-editor-page .history-button-row button,
  body.molecule-editor-page .camera-button-grid .hud-btn,
  body.molecule-editor-page .shortcut-key {
    min-height: var(--molecule-tablet-touch-target);
  }

  body.molecule-editor-page .atom-picker-button {
    min-height: 68px;
  }

  body.molecule-editor-page .molecule-formula {
    overflow-wrap: anywhere;
    font-size: clamp(32px, 6vw, 46px);
    line-height: 1.12;
    letter-spacing: 0;
  }

  body.molecule-editor-page .molecule-weight-row {
    gap: 10px;
  }

  body.molecule-editor-page .molecule-weight-row strong,
  body.molecule-editor-page #status {
    overflow-wrap: anywhere;
  }

  body.molecule-editor-page #status {
    margin: 12px 0 0;
    padding: 12px 14px;
    min-height: var(--molecule-tablet-touch-target);
    border: 1px solid rgba(34, 197, 94, 0.2);
    border-radius: 14px;
    background: rgba(2, 6, 23, 0.5);
    position: sticky;
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    z-index: 2;
  }

  body.molecule-editor-page .json-modal,
  body.molecule-editor-page .signature-modal,
  body.molecule-editor-page .export-modal,
  body.molecule-editor-page .periodic-modal {
    padding: 18px;
    align-items: start;
    overflow-y: auto;
  }

  body.molecule-editor-page .json-modal-panel,
  body.molecule-editor-page .signature-modal-panel,
  body.molecule-editor-page .export-modal-panel,
  body.molecule-editor-page .periodic-panel {
    max-height: var(--molecule-tablet-modal-max-height);
    overflow: auto;
  }

  body.molecule-editor-page .json-modal textarea {
    max-height: 50dvh;
  }

  body.molecule-editor-page .periodic-search-wrap {
    min-width: 0;
    max-width: 100%;
  }

  body.molecule-editor-page .periodic-grid {
    min-width: 900px;
    grid-template-columns: repeat(18, minmax(44px, 1fr));
    grid-template-rows: repeat(9, 60px);
  }

  body.molecule-editor-page .tutorial-card {
    max-height: calc(100dvh - 36px);
    overflow-y: auto;
  }

  body.molecule-editor-page .tutorial-actions button {
    min-height: var(--molecule-tablet-touch-target);
  }

}

@media (min-width: 901px) and (max-width: 1439px) and (min-height: 560px) {
  body.molecule-editor-page {
    --molecule-left-panel-width: clamp(220px, 23vw, 280px);
    --molecule-right-panel-width: clamp(210px, 22vw, 260px);
    grid-template-columns:
      var(--molecule-left-panel-width)
      minmax(0, 1fr)
      var(--molecule-right-panel-width);
    grid-template-rows: var(--molecule-tablet-topbar-height) minmax(0, 1fr);
    grid-template-areas:
      "topbar topbar topbar"
      "left canvas right";
    min-height: 100dvh;
    overflow: hidden;
  }

  body.molecule-editor-page .molecule-topbar {
    grid-template-columns: 160px minmax(0, 1fr) minmax(150px, auto);
    height: var(--molecule-tablet-topbar-height);
    padding-inline: clamp(12px, 1.5vw, 18px);
    gap: clamp(10px, 1.4vw, 18px);
  }

  body.molecule-editor-page .topbar-brand {
    min-width: 0;
  }

  body.molecule-editor-page .topbar-brand-subtitle {
    display: none;
  }

  body.molecule-editor-page .topbar-status-strip {
    padding-inline: 8px;
  }

  body.molecule-editor-page .topbar-title-block {
    min-width: 0;
  }

  body.molecule-editor-page .topbar-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: clamp(14px, 1.6vw, 16px);
  }

  body.molecule-editor-page .topbar-actions {
    max-width: min(52vw, 560px);
    gap: 7px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  body.molecule-editor-page .topbar-actions::-webkit-scrollbar {
    display: none;
  }

  body.molecule-editor-page .topbar-btn {
    flex: 0 0 auto;
    min-width: 82px;
    min-height: 40px;
    height: 40px;
    padding-inline: 10px;
    font-size: 10px;
  }

  body.molecule-editor-page .molecule-panel {
    height: 100%;
    min-height: 0;
    padding: clamp(12px, 1.5vw, 18px);
    overflow-y: auto;
  }

  body.molecule-editor-page .left-panel {
    border-right: 1px solid rgba(56, 189, 248, 0.22);
    border-bottom: 0;
    overscroll-behavior: contain;
  }

  body.molecule-editor-page .right-panel {
    position: relative;
    display: block;
    right: auto;
    top: auto;
    width: auto;
    height: 100%;
    max-height: none;
    border-left: 1px solid rgba(56, 189, 248, 0.22);
    border-radius: 0;
    overscroll-behavior: contain;
  }

  body.molecule-editor-page #graphCanvas {
    width: 100%;
    height: 100%;
    min-height: 0;
  }

  body.molecule-editor-page .tool-section,
  body.molecule-editor-page .panel-section {
    margin-bottom: clamp(10px, 1.4vw, 16px);
    padding: clamp(12px, 1.5vw, 18px);
  }

  body.molecule-editor-page #toolbar :is(.tool-button-grid > button, .history-button-row > button, .modify-bond-order-row > button, .modify-wide-action, .preset-catalog-open, .template-catalog-open),
  body.molecule-editor-page .preset-btn,
  body.molecule-editor-page .history-button-row button,
  body.molecule-editor-page .camera-button-grid .hud-btn,
  body.molecule-editor-page .shortcut-key {
    min-height: 42px;
  }

  body.molecule-editor-page .molecule-formula {
    overflow-wrap: anywhere;
    font-size: clamp(28px, 3.3vw, 42px);
    line-height: 1.12;
    letter-spacing: 0;
  }

  body.molecule-editor-page .molecule-weight-row strong,
  body.molecule-editor-page #status {
    overflow-wrap: anywhere;
  }

}

@media (min-width: 901px) and (max-width: 1439px) and (max-height: 600px) {
  body.molecule-editor-page {
    grid-template-rows: 50px minmax(0, 1fr);
  }

  body.molecule-editor-page .molecule-topbar {
    height: 50px;
    padding-block: 5px;
  }

  body.molecule-editor-page .topbar-btn {
    min-height: 36px;
    height: 36px;
  }

  body.molecule-editor-page .molecule-panel {
    padding: 10px;
  }

  body.molecule-editor-page .tool-panel-header {
    margin-bottom: 10px;
  }

  body.molecule-editor-page #toolbar h2 {
    margin-bottom: 10px;
    font-size: 18px;
  }

  body.molecule-editor-page #toolbar h2::after {
    display: none;
  }

  body.molecule-editor-page .tool-section,
  body.molecule-editor-page .panel-section {
    margin-bottom: 9px;
    padding: 10px;
  }

  body.molecule-editor-page .tool-section-title,
  body.molecule-editor-page .panel-kicker {
    margin-bottom: 7px;
    font-size: 8px;
  }

  body.molecule-editor-page .panel-section h3 {
    margin-bottom: 10px;
    font-size: 18px;
  }

  body.molecule-editor-page .atom-picker-button {
    min-height: 54px;
  }

  body.molecule-editor-page .tool-button-grid,
  body.molecule-editor-page .history-button-row,
  body.molecule-editor-page .preset-grid,
  body.molecule-editor-page .camera-button-grid,
  body.molecule-editor-page .shortcut-pad {
    gap: 7px;
  }

  body.molecule-editor-page #toolbar :is(.tool-button-grid > button, .history-button-row > button, .modify-bond-order-row > button, .modify-wide-action, .preset-catalog-open, .template-catalog-open),
  body.molecule-editor-page .preset-btn,
  body.molecule-editor-page .history-button-row button,
  body.molecule-editor-page .camera-button-grid .hud-btn,
  body.molecule-editor-page .shortcut-key {
    min-height: 36px;
  }

  body.molecule-editor-page .molecule-formula-card {
    padding: 12px;
  }

  body.molecule-editor-page .molecule-formula {
    font-size: clamp(22px, 2.8vw, 34px);
  }

  body.molecule-editor-page .shortcut-hint {
    margin-top: 8px;
  }

  body.molecule-editor-page #status {
    margin-top: 8px;
    font-size: 11px;
    position: sticky;
    bottom: 0;
    z-index: 2;
  }

  body.molecule-editor-page .tutorial-card {
    max-height: calc(100dvh - 28px);
    overflow-y: auto;
  }

}

@media (min-width: 961px) and (max-width: 1439px) and (max-height: 600px) {
  body.molecule-editor-page .left-panel,
  body.molecule-editor-page .right-panel {
    overflow-y: auto;
  }

  body.molecule-editor-page .json-modal,
  body.molecule-editor-page .signature-modal,
  body.molecule-editor-page .export-modal,
  body.molecule-editor-page .periodic-modal {
    padding: 12px;
    align-items: start;
    overflow-y: auto;
  }

  body.molecule-editor-page .json-modal-panel,
  body.molecule-editor-page .signature-modal-panel,
  body.molecule-editor-page .export-modal-panel,
  body.molecule-editor-page .periodic-panel {
    max-height: calc(100dvh - 24px);
    padding: 16px;
    overflow: auto;
  }

  body.molecule-editor-page .json-modal textarea {
    min-height: 220px;
    max-height: 46dvh;
  }

  body.molecule-editor-page .json-modal-actions,
  body.molecule-editor-page .signature-modal-actions,
  body.molecule-editor-page .export-modal-actions {
    position: sticky;
    bottom: -1px;
    padding-top: 10px;
    background: linear-gradient(180deg, transparent, rgba(2, 6, 23, 0.94) 34%);
  }

  body.molecule-editor-page .periodic-grid {
    min-width: 880px;
    grid-template-columns: repeat(18, minmax(42px, 1fr));
    grid-template-rows: repeat(9, 58px);
  }

  body.molecule-editor-page .bond-order-menu {
    width: min(270px, calc(100vw - 24px));
    gap: 8px;
    padding: 10px;
  }

  body.molecule-editor-page .bond-order-menu button {
    min-height: 54px;
  }

  body.molecule-editor-page .tutorial-card {
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }
}

@media (pointer: coarse) and (min-width: 600px) and (max-width: 1439px) {
  body.molecule-editor-page .panel-collapse-toggle {
    display: none;
  }

  body.molecule-editor-page .right-panel {
    display: none;
  }
}

@media (pointer: coarse) and (min-width: 901px) and (max-width: 1439px) and (min-height: 560px) {
  body.molecule-editor-page {
    grid-template-columns:
      clamp(220px, 24vw, 300px)
      minmax(0, 1fr);
    grid-template-areas:
      "topbar topbar"
      "left canvas";
  }
}
