/* ============================================================
   RESPONSIVE — TABLET / MOBILE PRO
============================================================ */

@media (max-width: 960px) {

  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  .shell {
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    min-height: 100dvh;
  }

  /* ==========================================================
     MOBILE SIDEBAR
  ========================================================== */

  .side {
    display: none;
  }

  .side.open {
    display: flex;
    flex-direction: column;

    position: fixed;
    z-index: 100;

    top: 0;
    bottom: 0;
    left: 0;

    width: min(320px, 88vw);
    max-width: 100%;

    height: 100dvh;

    overflow-y: auto;
    overscroll-behavior: contain;

    background: var(--cream);
    border-right: 1px solid var(--cream-3);

    box-shadow:
      16px 0 48px rgba(28, 28, 28, 0.12),
      32px 0 96px rgba(28, 28, 28, 0.08);

    animation: mobileSideIn 0.24s ease both;

    -webkit-overflow-scrolling: touch;
  }

  @keyframes mobileSideIn {
    from {
      opacity: 0;
      transform: translateX(-18px);
    }

    to {
      opacity: 1;
      transform: translateX(0);
    }
  }

  /* ==========================================================
     MOBILE HEADER
  ========================================================== */

  .mob {
    position: sticky;
    top: 0;
    z-index: 90;

    display: flex;
    align-items: center;
    justify-content: space-between;

    gap: 12px;

    width: 100%;
    min-height: 56px;

    padding:
      max(8px, env(safe-area-inset-top))
      max(14px, env(safe-area-inset-right))
      8px
      max(14px, env(safe-area-inset-left));

    border-bottom: 1px solid var(--cream-3);

    background: rgba(247, 244, 237, 0.92);

    -webkit-backdrop-filter: blur(18px) saturate(140%);
    backdrop-filter: blur(18px) saturate(140%);

    box-shadow: 0 1px 0 rgba(28, 28, 28, 0.03);
  }

  .mobile-brand {
    display: flex;
    align-items: center;

    min-width: 0;

    gap: 9px;

    font-size: 11px;
    font-weight: 700;
    line-height: 1.2;

    letter-spacing: 0.07em;
    text-transform: uppercase;
  }

  .mobile-brand .brand-mark {
    flex: 0 0 auto;

    width: 30px;
    height: 30px;

    border-radius: 8px;
  }

  .mobile-brand span,
  .mobile-brand strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mob button,
  .mob .btn,
  .mob .icon-btn {
    min-width: 40px;
    min-height: 40px;

    display: inline-flex;
    align-items: center;
    justify-content: center;

    touch-action: manipulation;
  }

  /* ==========================================================
     TOP BAR
  ========================================================== */

  .top {
    top: 56px;

    width: 100%;
    max-width: 100%;

    overflow-x: hidden;
  }

  .top-actions {
    min-width: 0;
  }

  /* ==========================================================
     WORKSPACE
  ========================================================== */

  .workspace-head {
    padding: 30px 20px 16px;
  }

  .workspace-head h2 {
    max-width: 100%;

    font-size: clamp(30px, 5vw, 42px);
    line-height: 1.05;

    overflow-wrap: anywhere;
  }

  .workspace-head p {
    max-width: 720px;

    font-size: 14px;
    line-height: 1.65;
  }

  .work {
    width: 100%;
    max-width: 100%;

    padding:
      10px
      20px
      calc(64px + env(safe-area-inset-bottom));

    overflow-x: hidden;
  }

  .workspace-metrics {
    display: none;
  }

  /* ==========================================================
     GRID
  ========================================================== */

  .split,
  .grid2 {
    grid-template-columns: minmax(0, 1fr);
  }

  .split > *,
  .grid2 > * {
    min-width: 0;
  }

  /* Prevent cards / code / tables from forcing page width */
  .panel,
  .card,
  .widget,
  .section,
  .workspace,
  .workspace-body {
    min-width: 0;
    max-width: 100%;
  }

  pre,
  code,
  textarea,
  input,
  select {
    max-width: 100%;
  }

  pre {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table {
    min-width: 700px;
  }

  .table-wrap,
  .table-scroll {
    width: 100%;
    max-width: 100%;

    overflow-x: auto;
    overscroll-behavior-inline: contain;

    -webkit-overflow-scrolling: touch;
  }
}


/* ============================================================
   PHONE
============================================================ */

@media (max-width: 640px) {

  .mob {
    min-height: 54px;

    padding:
      max(7px, env(safe-area-inset-top))
      max(12px, env(safe-area-inset-right))
      7px
      max(12px, env(safe-area-inset-left));
  }

  .top {
    top: 54px;

    padding: 8px 12px;
  }

  .top-actions {
    gap: 6px;
  }

  .top-actions .tool-pill span,
  .top-actions .tool-pill kbd,
  #fps-stat {
    display: none;
  }

  .top-actions .tool-pill {
    min-width: 38px;
    min-height: 38px;

    padding: 8px 10px;
  }

  /* ==========================================================
     WORKSPACE HEADER
  ========================================================== */

  .workspace-head {
    padding: 24px 14px 12px;
  }

  .workspace-head h2 {
    font-size: clamp(29px, 9vw, 36px);
    line-height: 1.02;

    letter-spacing: -0.035em;
  }

  .workspace-head p {
    margin-top: 9px;

    font-size: 13px;
    line-height: 1.6;
  }

  .work {
    padding:
      6px
      14px
      calc(56px + env(safe-area-inset-bottom));
  }

  h1 {
    font-size: clamp(30px, 9vw, 36px);
    line-height: 1.05;
  }

  h2 {
    line-height: 1.12;
  }

  h3 {
    line-height: 1.2;
  }

  /* ==========================================================
     BUTTONS
  ========================================================== */

  .row {
    gap: 8px;
  }

  .row > .btn {
    flex: 1 1 auto;
    min-height: 42px;
  }

  .btn,
  button {
    touch-action: manipulation;
  }

  /* ==========================================================
     PANEL
  ========================================================== */

  .panel-b {
    padding: 13px;
  }

  .panel,
  .card {
    border-radius: 10px;
  }

  /* ==========================================================
     TABS
  ========================================================== */

  .tabs-h {
    display: flex;

    flex-wrap: nowrap;
    gap: 6px;

    width: 100%;
    max-width: 100%;

    overflow-x: auto;
    overflow-y: hidden;

    scrollbar-width: none;

    overscroll-behavior-inline: contain;
    scroll-snap-type: x proximity;

    -webkit-overflow-scrolling: touch;
  }

  .tabs-h::-webkit-scrollbar {
    display: none;
  }

  .tabs-h > * {
    flex: 0 0 auto;
    scroll-snap-align: start;
  }

  /* ==========================================================
     COMMAND BOX
  ========================================================== */

  .cmd-box {
    width: calc(100% - 20px);
    max-width: 560px;

    margin: 7vh auto 0;

    max-height: calc(100dvh - 14vh);

    overflow-y: auto;

    border-radius: 12px;

    -webkit-overflow-scrolling: touch;
  }

  /* ==========================================================
     FORMS
  ========================================================== */

  input,
  textarea,
  select {
    width: 100%;

    font-size: 16px;
  }

  textarea {
    min-height: 110px;
    resize: vertical;
  }

  /* 16px prevents unwanted iOS zoom */
  input,
  select,
  textarea {
    font-size: max(16px, 1em);
  }

  /* ==========================================================
     TABLE MOBILE
  ========================================================== */

  table {
    min-width: 640px;
  }

  th,
  td {
    white-space: nowrap;
  }
}


/* ============================================================
   SMALL PHONE
============================================================ */

@media (max-width: 420px) {

  .mobile-brand {
    gap: 7px;
    font-size: 10px;
    letter-spacing: 0.055em;
  }

  .mobile-brand .brand-mark {
    width: 28px;
    height: 28px;
  }

  .workspace-head {
    padding-left: 12px;
    padding-right: 12px;
  }

  .work {
    padding-left: 12px;
    padding-right: 12px;
  }

  .panel-b {
    padding: 11px;
  }

  .row {
    flex-wrap: wrap;
  }

  .row > .btn {
    flex: 1 1 100%;
  }

  .cmd-box {
    width: calc(100% - 16px);
    margin-top: 5vh;
  }
}


/* ============================================================
   LANDSCAPE MOBILE
============================================================ */

@media (max-width: 960px) and (orientation: landscape) {

  .side.open {
    width: min(300px, 72vw);
  }

  .cmd-box {
    margin-top: 3vh;
    max-height: 90dvh;
  }

  .workspace-head {
    padding-top: 20px;
  }
}


/* ============================================================
   ACCESSIBILITY / REDUCED MOTION
============================================================ */

@media (prefers-reduced-motion: reduce) {

  .side.open {
    animation: none;
  }

  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
  }
}