﻿/* Mobile / tablet adaptations. Desktop styles stay untouched. */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

@media (pointer: coarse) {
  html,
  body {
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
  }

  .board,
  .node,
  .port,
  .resize-handle,
  .su-preview,
  .pano-frame,
  .pano-canvas,
  .share-board {
    touch-action: none;
  }

  input,
  textarea,
  select,
  .node-panel,
  .mobile-sheet-host {
    touch-action: auto;
  }

  .node-panel textarea,
  .node-panel input,
  .node-panel select {
    font-size: 16px;
  }
  .prop-panel textarea,
  .prop-panel input,
  .prop-panel select {
    font-size: 16px;
  }

  .context-menu .ctx-item {
    min-height: 44px;
  }

  .context-menu .ctx-hint {
    display: none;
  }

  .pano-modal {
    padding: calc(16px + var(--safe-top)) calc(12px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(12px + var(--safe-left));
  }

  .pano-canvas,
  .pano-flat {
    width: 100%;
    height: min(72dvh, 820px);
  }

  .pano-close {
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    height: 40px;
    padding: 0 16px;
  }

  .pano-modal .pano-hint {
    padding-bottom: calc(4px + var(--safe-bottom));
  }

  .node-head .close-btn,
  .node-head .props-btn {
    width: 32px;
    height: 32px;
  }

  .node-gen-btn {
    width: 32px;
    height: 32px;
    opacity: 1;
  }

  .resize-handle {
    width: 26px;
    height: 26px;
  }

  .port {
    width: 30px;
    height: 30px;
  }
}

@media (max-width: 1024px) {
  body,
  .shell {
    height: 100dvh;
    min-height: 100dvh;
  }

  .topbar {
    top: calc(10px + var(--safe-top));
    left: calc(10px + var(--safe-left));
    right: calc(10px + var(--safe-right));
    gap: 8px;
  }

  .canvas-nav {
    max-width: min(300px, calc(100vw - 210px));
  }

  .canvas-nav .canvas-nav-meta {
    opacity: 1;
    max-width: 150px;
  }

  .canvas-nav .pin-toggle {
    display: none;
  }

  .toolbar {
    max-width: calc(100vw - 330px);
    padding: 8px;
  }

  .sidebar {
    left: calc(8px + var(--safe-left));
    width: 48px;
    border-radius: 16px;
    padding: 9px 0;
  }

  .sidebar-item {
    width: 40px;
    height: 40px;
  }

  .sidebar-user {
    width: 44px;
    height: 44px;
  }

  .sidebar-avatar {
    width: 34px;
    height: 34px;
  }

  .minimap {
    left: calc(68px + var(--safe-left));
    bottom: calc(92px + var(--safe-bottom));
    width: 150px;
    height: 100px;
  }

  .view-dock {
    left: calc(68px + var(--safe-left));
    bottom: calc(48px + var(--safe-bottom));
  }

  .hint {
    display: none;
  }

  .multi-toolbar {
    max-width: calc(100vw - 24px);
  }

  .rail-panel.rail-expanded {
    width: min(1000px, calc(100vw - 32px)) !important;
  }

  /* Node panel is portaled to #mobileSheetHost by renderer.js. */
  #mobileSheetHost.open {
    display: block;
  }

  .mobile-sheet-host {
    position: fixed;
    inset: 0;
    z-index: 90;
    display: none;
    pointer-events: none;
    padding-bottom: var(--safe-bottom);
  }

  .mobile-sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .48);
    pointer-events: auto;
  }

  .mobile-sheet-close {
    position: absolute;
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    z-index: 3;
    height: 36px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--text);
    display: inline-flex;
    align-items: center;
    gap: 5px;
    pointer-events: auto;
    box-shadow: 0 8px 24px var(--shadow);
  }

  .mobile-sheet-close i,
  .mobile-sheet-close svg {
    width: 14px;
    height: 14px;
  }

  .mobile-sheet-host .node-panel {
    position: absolute !important;
    left: 50% !important;
    top: auto !important;
    bottom: 0 !important;
    width: min(720px, 100%) !important;
    max-width: 100% !important;
    margin: 0 !important;
    transform: translateX(-50%) !important;
    z-index: 2;
    pointer-events: auto;
  }

  .mobile-sheet-host .node-panel-inner {
    max-height: 70dvh;
    overflow-y: auto;
    border-radius: 24px 24px 0 0;
    padding: 18px 16px calc(16px + var(--safe-bottom));
    box-shadow: 0 -18px 48px var(--shadow);
  }

  .mobile-sheet-host .node-panel-inner::before {
    content: "";
    display: block;
    width: 38px;
    height: 4px;
    margin: -8px auto 6px;
    border-radius: 99px;
    background: var(--line-strong);
    flex: none;
  }

  .prop-panel {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: min(78dvh, 720px);
    border-radius: 24px 24px 0 0;
    border-bottom: 0;
    z-index: 92;
    box-shadow: 0 -18px 48px var(--shadow);
    padding-bottom: var(--safe-bottom);
  }

  .prop-panel.open {
    display: flex;
  }

  .prop-panel .prop-head {
    position: relative;
    padding-top: 18px;
  }

  .prop-panel .prop-head::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    width: 38px;
    height: 4px;
    border-radius: 99px;
    background: var(--line-strong);
    transform: translateX(-50%);
  }

  .prop-panel .prop-body {
    max-height: calc(78dvh - 62px);
    padding: 16px 16px calc(18px + var(--safe-bottom));
    overscroll-behavior: contain;
  }

  .prop-panel .prop-close {
    width: 34px;
    height: 34px;
  }
  .mobile-sheet-host .node-panel .np-pop {
    position: fixed;
    left: 50% !important;
    top: auto !important;
    bottom: calc(12dvh + var(--safe-bottom));
    width: min(420px, calc(100vw - 24px)) !important;
    min-width: 0 !important;
    max-width: calc(100vw - 24px) !important;
    transform: translateX(-50%);
    max-height: 46dvh;
    z-index: 5;
  }
}

@media (max-width: 720px), (pointer: coarse) and (max-height: 520px) and (orientation: landscape) {
  .canvas-nav {
    max-width: calc(100vw - 20px);
    padding: 7px 5px 7px 7px;
  }

  .canvas-nav .canvas-nav-meta {
    max-width: 145px;
  }

  .current-canvas-title {
    max-width: 145px;
    font-size: 11px;
  }

  .current-canvas-time {
    max-width: 145px;
    font-size: 9px;
  }

  .toolbar {
    display: none;
  }

  .sidebar {
    top: auto;
    bottom: calc(10px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    width: auto;
    max-width: calc(100vw - 16px);
    height: auto;
    flex-direction: row;
    padding: 6px;
    border-radius: 17px;
  }

  .sidebar-nav,
  .sidebar-bottom {
    flex-direction: row;
    gap: 2px;
    padding: 0;
    overflow: visible;
  }

  .sidebar-bottom {
    margin-left: 2px;
  }

  .sidebar-sep,
  .sidebar-brand,
  .sidebar .pin-toggle {
    display: none;
  }

  .sidebar-item,
  .sidebar-user {
    width: 44px;
    height: 44px;
  }

  .node {
    max-width: calc(100vw - 24px);
  }

  .empty-guide {
    left: calc(12px + var(--safe-left));
    right: calc(12px + var(--safe-right));
    top: calc(66px + var(--safe-top));
    max-width: none;
  }

  .empty-guide-card {
    max-height: calc(100dvh - 166px - var(--safe-top) - var(--safe-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding: 18px;
  }

  .empty-guide-actions {
    gap: 6px;
  }

  .empty-guide-actions .btn {
    flex: 1 1 100%;
  }

  .statusbar,
  .minimap {
    display: none !important;
  }

  .view-dock {
    left: auto;
    right: calc(10px + var(--safe-right));
    bottom: calc(78px + var(--safe-bottom));
  }

  .agent-fab {
    right: calc(10px + var(--safe-right));
    bottom: calc(124px + var(--safe-bottom));
    width: 44px;
    height: 44px;
  }

  #agentDrawer {
    top: calc(62px + var(--safe-top));
    right: calc(8px + var(--safe-right));
    left: calc(8px + var(--safe-left));
    bottom: calc(8px + var(--safe-bottom));
    width: auto;
    border-radius: 20px;
  }


  .mobile-sheet-host .node-panel-inner {
    max-height: 82dvh;
  }

  .gate-panel {
    padding: 0 14px 28px;
  }
  .gate-head {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "brand actions"
      "nav nav";
    align-items: center;
    row-gap: 8px;
    column-gap: 8px;
  }

  .gate-head-left {
    display: contents;
  }

  .gate-brand {
    grid-area: brand;
    align-self: center;
    font-size: 13px;
    letter-spacing: .12em;
    white-space: nowrap;
  }

  .gate-nav {
    grid-area: nav;
    justify-self: start;
  }

  .gate-nav-btn {
    height: 34px;
    padding: 0 12px;
    font-size: 12px;
    white-space: nowrap;
  }

  .gate-head-actions {
    grid-area: actions;
    justify-self: end;
    gap: 6px;
  }

  .gate-icon-btn {
    width: 34px;
    height: 34px;
  }

  .gate-head-actions .primary-btn {
    height: 34px;
    padding: 0 12px;
    font-size: 12px;
  }

  .gate-head {
    margin: 0 -14px 30px;
    padding: calc(14px + var(--safe-top)) 14px 0;
  }

  .gate-head-left {
    gap: 8px;
    flex-wrap: wrap;
  }

  .gate-slogan {
    font-size: 24px;
  }

  .gate-sub {
    font-size: 12px;
  }

  .gate-hero,
  .gate-composer,
  .gate-hero-note {
    max-width: 100%;
  }

  .gate-recent-row {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .gate-recent-row .canvas-item,
  .gate-create-tile {
    width: 100%;
    flex: 1 1 auto;
    aspect-ratio: 16 / 9;
  }

  .gate-list {
    grid-template-columns: 1fr;
  }

  .gate-project-search {
    width: 100%;
  }

  .gate-project-search-wrap {
    margin-left: 0;
    width: 100%;
  }

  .gate-scope {
    width: 100%;
    overflow-x: auto;
  }

  .auth-top {
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
  }

  .share-top {
    top: calc(10px + var(--safe-top));
    left: calc(10px + var(--safe-left));
    right: calc(10px + var(--safe-right));
  }

  .share-dock {
    bottom: calc(12px + var(--safe-bottom));
  }
}

@media (pointer: coarse) and (max-height: 520px) and (orientation: landscape) {
  .mobile-sheet-host .node-panel-inner {
    max-height: 88dvh;
  }

  .sidebar {
    bottom: calc(7px + var(--safe-bottom));
  }
}

@media (max-width: 820px) {
  .config-overlay {
    align-items: stretch;
    padding: calc(10px + var(--safe-top)) calc(10px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(10px + var(--safe-left));
  }

  .config-modal {
    width: 100%;
    max-width: none;
    height: 100%;
    max-height: none;
    border-radius: 20px;
  }

  .config-head { padding: 18px 18px 0; }
  .config-head h2 { font-size: 20px; }
  .config-head > p { font-size: 11px; }
  .config-import-export { flex-wrap: wrap; }
  .config-tabs { margin-top: 14px; padding: 0 18px; }
  .config-body { padding: 18px; }
  .config-row-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .config-footer { padding: 12px 18px calc(12px + var(--safe-bottom)); }
}

@media (max-width: 560px) {
  .config-head-top { flex-direction: column; gap: 10px; }
  .config-import-export { margin-top: 0; }
  .config-tab-btn { height: 36px; padding: 0 9px; font-size: 12px; }
  .config-tab-btn.active::after { left: 9px; right: 9px; }
  .config-row-4 { grid-template-columns: 1fr; }
  .config-channel-card {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }
  .config-channel-actions { margin-left: 0; flex-wrap: wrap; }
}

@media (max-width: 720px) {
  .rail-panel:has(.settings-panel) {
    left: calc(8px + var(--safe-left)) !important;
    right: calc(8px + var(--safe-right));
    top: auto !important;
    bottom: calc(72px + var(--safe-bottom)) !important;
    width: auto !important;
    max-width: none !important;
    max-height: min(72dvh, 620px);
    border-radius: 22px;
    box-shadow: 0 24px 70px rgba(0, 0, 0, .42);
  }

  .rail-panel:has(.settings-panel) .settings-panel {
    max-height: min(72dvh, 620px);
  }
}

@media (max-width: 560px) {
  .config-section-head {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .config-section-actions {
    justify-content: flex-start;
  }
  .config-section-actions .config-add-btn { flex: 1 1 auto; justify-content: center; }
  .config-tabs {
    flex-wrap: wrap;
    gap: 4px;
    padding-bottom: 8px;
    border-bottom: 0;
    overflow: visible;
  }
  .config-tab-btn {
    height: 32px;
    border: 1px solid var(--line-2);
    border-radius: 8px;
    background: var(--soft);
  }
  .config-tab-btn.active {
    background: var(--card-solid);
    border-color: var(--line-strong);
  }
  .config-tab-btn.active::after { display: none; }
}
