/* Chat workspace v4. Load after the native chat stylesheet. The existing layer
   is intentional: native !important rules already live in this cascade layer. */
@layer sw-studio-refinement {
  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] {
    --chat-v4-reading-width: 860px;
    min-inline-size: 0;
    color: var(--ds-ink, var(--text-primary, #f1f4fa));
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-stage {
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-destinations {
    gap: 4px !important;
    padding: 4px 16px !important;
    min-height: 52px !important;
    background: var(--ds-paper, var(--sw-layer-default)) !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar {
    min-height: 64px !important;
    padding: 8px 20px !important;
    gap: 12px !important;
    flex-wrap: wrap !important;
    background: var(--ds-canvas, var(--sw-layer-canvas)) !important;
    backdrop-filter: none !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity {
    min-width: 0;
    flex: 1 1 180px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity > div {
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 12px !important;
    min-width: 0;
    width: 100%;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity > div > span {
    flex-basis: 100%;
    font-size: 11px !important;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity h1 {
    margin: 0 !important;
    font-size: 14px !important;
    line-height: 1.4 !important;
    max-width: min(34ch, 100%);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-project {
    min-width: 0;
    max-width: 100%;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-project select {
    max-width: min(32ch, 100%) !important;
    min-height: 44px !important;
    color: var(--ds-ink, var(--text-primary)) !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px !important;
    min-width: 0;
    max-width: 100%;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-status {
    display: inline-flex !important;
    min-width: 0;
    font-size: 11px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-workspace-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    max-width: 100%;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-workspace-actions > button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 44px !important;
    min-width: 44px !important;
    padding: 8px 11px !important;
    border: 1px solid var(--ds-line, var(--sw-border-muted-color)) !important;
    border-radius: 11px !important;
    background: var(--ds-paper, var(--sw-layer-default)) !important;
    color: var(--ds-ink, var(--text-primary)) !important;
    font-size: 12px !important;
    line-height: 1.4;
    cursor: pointer;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-workspace-actions small {
    min-width: 1.25em;
    color: var(--ds-muted, var(--text-secondary));
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    text-align: center;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(.chat-v4-panel-trigger[aria-expanded="true"], .chat-v4-panel-trigger:hover) {
    background: var(--ds-raised, var(--sw-layer-raised)) !important;
    border-color: var(--ds-blue, var(--accent-primary)) !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-messages {
    padding: 24px 24px 16px !important;
    min-width: 0;
    overscroll-behavior: contain;
    overflow-wrap: anywhere;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(.chat-modern-messages__inner, .chat-modern-composer, .chat-v4-response-controls, .chat-v4-starting-points) {
    width: 100%;
    max-width: var(--chat-v4-reading-width) !important;
    margin-inline: auto;
    box-sizing: border-box;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer-zone {
    min-width: 0;
    padding: 10px 24px calc(12px + env(safe-area-inset-bottom)) !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer {
    padding: 10px !important;
    border-radius: 20px !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer textarea {
    font-size: 16px !important;
    line-height: 1.55 !important;
    min-width: 0;
    max-height: min(28dvh, 220px) !important;
    overflow-y: auto !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(.chat-modern-icon-button, .chat-modern-send, .chat-modern-composer button) {
    min-height: 44px !important;
    min-width: 44px !important;
    flex-shrink: 0;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-response-options {
    margin-top: 4px;
    min-width: 0;
    border-radius: 12px;
    color: var(--ds-muted, var(--text-secondary));
    font-size: 12px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(.chat-v4-response-options, .chat-v4-starting-points) > summary {
    min-height: 44px;
    box-sizing: border-box;
    padding: 11px 10px;
    cursor: pointer;
    border-radius: 10px;
    color: var(--ds-muted, var(--text-secondary));
    font-size: 12px;
    line-height: 1.65;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-response-options > summary > small {
    margin-inline-start: 12px;
    font-size: 11px;
    opacity: .9;
    white-space: normal;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-response-options[open] {
    border: 1px solid var(--ds-line, var(--sw-border-muted-color));
    padding: 0 8px 6px;
    background: var(--ds-paper, var(--sw-layer-default));
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-execution {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 12px !important;
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 2px;
    box-sizing: border-box;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-execution label {
    display: grid;
    min-width: 0;
    gap: 5px;
    font-size: 12px;
    text-align: left;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-execution select {
    min-width: 0;
    width: 100%;
    max-width: 100%;
    min-height: 44px !important;
    font-size: 14px !important;
    box-sizing: border-box;
    color: var(--ds-ink, var(--text-primary)) !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-connections-link {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding-inline: 4px;
    color: var(--ds-blue, var(--accent-primary));
    text-decoration: underline;
    text-underline-offset: 3px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-review-status {
    display: block;
    padding: 4px 10px;
    color: var(--ds-muted, var(--text-secondary));
    font-size: 12px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer__meta {
    margin-top: 0 !important;
    font-size: 11px !important;
    line-height: 1.5;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-starting-points {
    margin-top: 4px;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-starting-points .chat-modern-starters {
    margin-top: 2px !important;
    max-width: none !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-history__inner {
    padding: 14px 12px 10px !important;
    gap: 12px !important;
    min-width: 0;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-history :is(button, summary, input) {
    min-height: 44px !important;
  }

  :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(button, summary, a, select, textarea):focus-visible,
  .chat-modern-portal[data-chat-workspace-version="4"] :is(button, summary, a, select, input):focus-visible {
    outline: 2px solid var(--ds-blue, var(--accent-primary, #3697ff)) !important;
    outline-offset: 3px !important;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] :is(.chat-modern-context, aside.chat-modern-mobile-history) {
    inline-size: min(520px, 100vw) !important;
    max-inline-size: 100vw !important;
    block-size: 100dvh !important;
    min-block-size: 0;
    overflow: hidden;
    box-sizing: border-box;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] aside.chat-modern-mobile-history {
    inline-size: min(380px, 100vw) !important;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__header {
    padding: 16px 20px !important;
    gap: 12px;
    flex-shrink: 0;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__header h2 {
    font-size: 20px !important;
    line-height: 1.4;
    margin: 0;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__tabs {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin: 10px 12px !important;
    padding: 4px !important;
    gap: 2px !important;
    flex-shrink: 0;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__body {
    overflow-y: auto;
    overscroll-behavior: contain;
    min-height: 0;
    padding: 12px 16px calc(20px + env(safe-area-inset-bottom)) !important;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] :is(button, summary, select, .chat-settings-account-links a) {
    min-height: 44px !important;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__tabs button {
    padding: 8px 4px !important;
    font-size: 12px !important;
    min-width: 0;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-v4-panel-intro {
    margin: 0 0 16px;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-secondary, #adb5c1);
  }

  .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context__cards article {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .chat-modern-portal[data-chat-workspace-version="4"] [hidden] {
    display: none !important;
  }

  @media (max-width: 760px) {
    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar {
      display: grid !important;
      grid-template-columns: 44px minmax(0, 1fr) !important;
      grid-template-rows: auto auto !important;
      gap: 4px 8px !important;
      padding: 8px 12px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity {
      grid-column: 2;
      order: 0;
      width: 100%;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__identity > div > span {
      display: none !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-topbar__controls {
      grid-column: 1 / -1;
      order: 1;
      width: 100%;
      justify-content: flex-end;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-workspace-actions {
      flex: 1 1 auto;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-panel-trigger {
      flex: 1 1 0;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-view-switch {
      grid-column: 1 / -1;
      order: 2;
      justify-self: start;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-status {
      width: 100%;
      padding-inline: 4px;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-messages {
      padding: 16px 12px 12px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer-zone {
      padding: 6px 10px calc(8px + env(safe-area-inset-bottom)) !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer {
      padding: 6px !important;
      border-radius: 16px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer__input-row {
      gap: 4px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] :is(.chat-modern-project select, .chat-modern-execution select) {
      font-size: 16px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-execution {
      grid-template-columns: minmax(0, 1fr);
      gap: 10px !important;
    }

    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-v4-response-options > summary > small {
      display: block;
      margin-inline-start: 14px;
    }

    .chat-modern-portal[data-chat-workspace-version="4"] .chat-modern-context {
      border-radius: 0 !important;
    }

    .chat-modern-portal[data-chat-workspace-version="4"] :is(input, select, textarea) {
      font-size: 16px !important;
    }
  }

  @media (max-height: 600px) {
    :is(#workspace-chat-os, #dashboard-chat-os)[data-chat-os-modern="true"][data-chat-workspace-version="4"] .chat-modern-composer-zone {
      max-height: 58dvh;
      overflow-y: auto;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .chat-modern-portal[data-chat-workspace-version="4"] :is(.chat-modern-context, .chat-modern-portal__backdrop, aside.chat-modern-mobile-history) {
      animation: none !important;
      transition: none !important;
    }
  }
}
