/* Touch layout: the canvas keeps the full screen width; editing panels are sheets. */
#mobile-tools,
#library-backdrop,
.library-close {
  display: none;
}

@media (max-width: 850px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  body {
    overscroll-behavior: none;
  }

  .topbar {
    gap: 12px;
    padding-inline: 14px;
  }

  .workspace-label,
  .top-divider,
  .beta,
  .avatar,
  .project-version {
    display: none;
  }

  .top-right {
    min-width: 0;
    gap: 5px;
    font-size: 9px;
  }

  #save-status {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .top-right .icon-btn {
    width: 40px;
    height: 40px;
    margin: 0;
    flex-shrink: 0;
  }

  .projectbar {
    padding-inline: 14px;
    gap: 8px;
  }

  .project-title {
    min-height: 40px;
    display: flex;
    align-items: center;
  }

  .project-actions {
    gap: 5px;
  }

  .project-actions .btn {
    height: 40px;
    min-width: 40px;
    padding-inline: 10px;
  }

  .app-layout {
    display: block;
    width: 100%;
    height: calc(100dvh - 155px);
    min-height: 0;
    overflow: hidden;
  }

  .editor-panel {
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
  }

  .editor-tabs {
    min-height: 44px;
    padding-inline: 14px;
  }

  .view-tabs {
    gap: 22px;
  }

  .view-tab {
    min-height: 44px;
    font-size: 11px;
  }

  .view-tab svg {
    width: 15px;
    height: 15px;
  }

  .validation-btn {
    min-width: 40px;
    min-height: 44px;
  }

  #mobile-tools {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 48px;
    height: 48px;
    padding: 2px 10px;
    border-bottom: 1px solid var(--border);
    background: #fff;
    z-index: 6;
  }

  #mobile-tools button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    flex: 1 1 0;
    min-width: 0;
    height: 44px;
    padding: 0 7px;
    border: 1px solid #dfe5d7;
    border-radius: 7px;
    background: #f8faf4;
    color: #536c3d;
    font-size: 11px;
    line-height: 1.3;
    white-space: nowrap;
    touch-action: manipulation;
  }

  #mobile-tools button.active,
  #mobile-tools button[aria-pressed="true"],
  #mobile-tools button[aria-expanded="true"] {
    border-color: #a7c572;
    background: #e7f4cb;
    color: #365421;
  }

  #mobile-tools button svg {
    width: 17px;
    height: 17px;
  }

  .canvas-wrap {
    width: 100%;
    min-width: 0;
    min-height: 0;
  }

  .canvas-topline {
    top: 13px;
    left: 13px;
    right: 13px;
  }

  .canvas-tag {
    font-size: 7px;
  }

  .canvas-legend {
    left: 10px;
    right: auto;
    bottom: 67px;
    max-width: calc(100% - 20px);
    padding: 8px 10px;
    pointer-events: none;
  }

  .canvas-legend .legend-title {
    display: none;
  }

  .canvas-legend .legend-items {
    gap: 8px 10px;
    flex-wrap: wrap;
  }

  .canvas-legend .legend-item {
    font-size: 8px;
  }

  .canvas-legend .legend-item i {
    width: 15px;
  }

  .canvas-bottom {
    left: 10px;
    right: 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
    justify-content: flex-end;
  }

  .canvas-controls {
    height: 46px;
    padding: 1px;
    gap: 0;
    border-radius: 8px;
  }

  .canvas-controls button {
    width: 40px;
    height: 42px;
    min-width: 40px;
    font-size: 20px;
    touch-action: manipulation;
  }

  .canvas-controls button svg {
    width: 17px;
    height: 17px;
  }

  .canvas-controls #zoom-value {
    min-width: 37px;
    font-size: 11px;
  }

  .control-separator {
    margin: 0 2px;
  }

  .canvas-help {
    display: none;
  }

  .editor-footer {
    height: 28px;
    flex-basis: 28px;
    padding-inline: 12px;
    font-size: 9px;
  }

  .editor-footer > span:last-child {
    display: none;
  }

  .library-panel {
    display: none;
    position: fixed;
    inset: min(220px, 24dvh) 0 0;
    z-index: 40;
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: 0;
    border: 1px solid #dce3d3;
    border-bottom: 0;
    border-radius: 17px 17px 0 0;
    box-shadow: 0 -12px 50px #283b2a33;
    padding-bottom: env(safe-area-inset-bottom);
    overscroll-behavior: contain;
  }

  body.library-open .library-panel {
    display: flex;
  }

  #library-backdrop {
    position: fixed;
    inset: 0;
    z-index: 39;
    background: #1e2c1c66;
    touch-action: none;
  }

  body.library-open #library-backdrop {
    display: block;
  }

  .library-panel .panel-heading {
    position: sticky;
    top: 0;
    z-index: 2;
    flex-shrink: 0;
    padding: 12px 16px;
    background: #fbfcf9;
  }

  .library-panel .panel-heading > div:first-child {
    flex: 1;
  }

  .library-panel .panel-heading h2 {
    margin-top: 2px;
    font-size: 17px;
  }

  .library-panel .eyebrow {
    font-size: 8px;
  }

  .library-panel .count-badge {
    margin-right: 8px;
    font-size: 11px;
  }

  .library-panel .library-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    min-width: 44px;
    border-radius: 8px;
    background: #eef2e8;
    font-size: 24px;
    touch-action: manipulation;
  }

  .library-panel .search-field {
    margin-inline: 15px;
    min-height: 44px;
    padding: 8px 10px;
    flex-shrink: 0;
  }

  .library-panel .search-field input {
    min-height: 26px;
    font-size: 16px;
  }

  .library-panel .search-field .key {
    display: none;
  }

  .library-panel .category-filters {
    flex-shrink: 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding: 9px 15px;
    gap: 6px;
    overscroll-behavior-x: contain;
  }

  .library-panel .category-chip {
    flex-shrink: 0;
    min-height: 40px;
    padding-inline: 12px;
    font-size: 11px;
    touch-action: manipulation;
  }

  .library-panel .library-toolbar {
    flex-shrink: 0;
    min-height: 44px;
    padding: 0 16px;
    font-size: 11px;
  }

  .library-panel .library-toolbar select {
    width: 120px;
    min-height: 40px;
    font-size: 14px;
  }

  .library-panel .device-list {
    flex: 1 1 0;
    min-height: 0;
    padding: 0 12px 10px;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .library-panel .library-device {
    min-height: 82px;
    padding: 11px 9px;
    gap: 12px;
    border-color: #e4e9dc;
    background: #fff;
    margin-bottom: 7px;
    cursor: pointer;
    touch-action: pan-y;
  }

  .library-panel .device-thumb {
    display: flex;
    width: 58px;
    height: 47px;
  }

  .library-panel .device-thumb .device-art {
    width: 55px;
    height: 39px;
  }

  .library-panel .library-device .device-model {
    font-size: 12px;
  }

  .library-panel .library-device .device-brand {
    font-size: 10px;
  }

  .library-panel .device-meta span {
    font-size: 8px;
  }

  .library-panel .add-device {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    background: #eef5e3;
    color: #5e7f35;
    opacity: 1;
    font-size: 25px;
    touch-action: manipulation;
  }

  .library-panel .custom-device-btn {
    flex-shrink: 0;
    min-height: 51px;
    margin: 8px 15px 10px;
    padding: 8px 12px;
    font-size: 12px;
  }

  .library-panel .custom-device-btn small {
    font-size: 9px;
  }

  .library-footer {
    display: none;
  }

  .inspector-panel {
    display: none;
  }

  .inspector-panel.mobile-open {
    display: flex;
    position: fixed;
    inset: auto 0 0;
    width: 100%;
    max-width: 100%;
    max-height: 75dvh;
    z-index: 50;
    border: 1px solid #dce3d3;
    border-bottom: 0;
    border-radius: 17px 17px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: 0 -12px 60px #283b2a44;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .inspector-panel #inspector {
    flex-shrink: 0;
  }

  .inspector-heading {
    position: sticky;
    top: 0;
    z-index: 3;
    height: 58px;
    background: #fff;
    padding-inline: 17px;
  }

  .inspector-heading h2 {
    font-size: 14px;
  }

  .inspector-heading > span {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 10px;
  }

  .inspector-heading [data-action="close-inspector"] {
    min-width: 44px;
    min-height: 44px;
    background: #f1f5eb;
  }

  .inspector-panel .field > span,
  .inspector-panel .section-label,
  .inspector-panel .spec-row,
  .inspector-panel .port-spec,
  .inspector-panel .source-link {
    font-size: 12px;
  }

  .inspector-panel .field input,
  .inspector-panel .field select,
  .inspector-panel .field textarea {
    min-height: 44px;
    font-size: 16px;
  }

  .inspector-panel .cable-choice,
  .inspector-panel .btn {
    min-height: 44px;
    min-width: 44px;
    font-size: 12px;
  }

  .inspector-bottom {
    display: none;
  }

  .schedule-view {
    min-height: 0;
    padding: 18px 12px;
    overscroll-behavior: contain;
  }

  .cable-table {
    min-width: 650px;
    font-size: 11px;
  }

  .cable-table th {
    font-size: 10px;
  }

  .schedule-header .btn,
  .dropdown button,
  .modal-footer .btn,
  .modal-header .icon-btn {
    min-height: 44px;
    min-width: 44px;
  }

  .modal-body .field input,
  .modal-body .field select,
  .modal-body .field textarea {
    min-height: 44px;
    font-size: 16px;
  }

  .toast {
    bottom: max(22px, env(safe-area-inset-bottom));
    font-size: 13px;
  }
}

@media (max-width: 560px) {
  .topbar {
    height: 54px;
    padding-inline: 12px;
    gap: 5px;
  }

  .brand {
    font-size: 22px;
  }

  .top-right {
    font-size: 8px;
  }

  #save-status {
    max-width: 103px;
  }

  .projectbar {
    height: 101px;
    padding: 5px 12px 8px;
    gap: 0;
    justify-content: space-between;
  }

  .project-title {
    min-height: 30px;
    font-size: 15px;
  }

  .project-subtitle {
    margin-top: 0;
    font-size: 9px;
  }

  .project-actions {
    width: 100%;
  }

  .project-actions .btn {
    height: 40px;
    font-size: 11px;
    padding-inline: 12px;
  }

  .project-actions .export-wrap {
    margin-left: auto;
  }

  .editor-tabs {
    height: 44px;
    padding-inline: 12px;
  }

  .view-tabs {
    gap: 20px;
  }

  .validation-btn svg {
    width: 18px;
    height: 18px;
  }

  #mobile-tools {
    padding-inline: 8px;
  }

  #mobile-tools button {
    font-size: 10px;
    gap: 4px;
    padding-inline: 4px;
  }

  .canvas-controls button {
    width: 40px;
  }

  .empty-state {
    width: calc(100% - 40px);
  }

  .empty-state > span {
    font-size: 44px;
  }

  .empty-state h2 {
    font-size: 17px;
  }

  .empty-state .btn {
    min-height: 44px;
  }
}

@media (max-width: 360px) {
  .canvas-controls button {
    width: 36px;
    min-width: 36px;
  }

  .canvas-controls #zoom-value {
    min-width: 32px;
  }

  .canvas-legend .legend-items {
    gap: 7px;
  }

  #mobile-tools button svg {
    width: 14px;
    height: 14px;
  }
}
