/* Shared page shell — layout, header, main, footer, nav */

*, *::before, *::after {
  box-sizing: border-box;
}

/* The dark theme is painted by .site-shell, a div inside body, so the document behind it stayed at
   the browser default: the default body margin showed as a white frame around every page, and each
   navigation flashed white before the shell painted. Painting the document itself fixes both, and
   color-scheme keeps form controls and scrollbars dark instead of light-on-dark. Deliberately not
   resetting the body margin — the frame is now the same colour, and pages keep their current
   spacing. */
html {
  background: var(--bg, #f4f6fa);
  color-scheme: light;
}

html[data-theme="dark"] {
  color-scheme: dark;
}

.site-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: clip;
  max-width: 100%;
  background: var(--bg, #f4f6fa);
  color: var(--text, #1a2332);
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.5;
}

.site-header {
  padding: 1.25rem 1.25rem 0;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.site-main {
  flex: 1;
  padding: 0 1.25rem 2rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.site-footer {
  padding: 1rem 1.25rem 1.5rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
  font-size: 0.85rem;
  color: var(--muted, #8b9cb3);
  border-top: 1px solid var(--border, #2a3548);
}

.site-footer a {
  color: var(--accent, #4d9fff);
  text-decoration: none;
}

.site-footer a:hover {
  text-decoration: underline;
}

.site-nav {
  font-size: 0.85rem;
  margin: 0.5rem 0 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.75rem;
  align-items: center;
}

.site-nav a {
  color: var(--accent, #4d9fff);
  text-decoration: none;
}

.site-nav a:hover {
  text-decoration: underline;
}

.site-nav a.vt-nav-active {
  font-weight: 600;
  text-decoration: underline;
}

/* UA04: keyboard focus visible (Tab path) */
.site-shell a:focus-visible,
.site-shell button:focus-visible,
.site-shell input:focus-visible,
.site-shell textarea:focus-visible,
.site-shell select:focus-visible,
.site-shell summary:focus-visible,
.site-shell [role="tab"]:focus-visible {
  outline: 2px solid var(--accent, #4d9fff);
  outline-offset: 2px;
}

.site-shell .skip-link:focus-visible {
  outline: 3px solid var(--accent, #4d9fff);
  outline-offset: 2px;
}

/* --- Overflow guard (marketplace / builder / shell pages) --- */
.site-shell:not(.chat-page) .site-main,
.site-shell:not(.chat-page) .site-header,
.site-shell:not(.chat-page) .site-footer {
  min-width: 0;
}

.site-shell:not(.chat-page) img,
.site-shell:not(.chat-page) iframe,
.site-shell:not(.chat-page) pre,
.site-shell:not(.chat-page) table {
  max-width: 100%;
}

/* --- prefers-reduced-motion --- */
@media (prefers-reduced-motion: reduce) {
  .site-shell:not(.chat-page) .pack {
    transition: none;
  }

  .site-shell:not(.chat-page) .pack:hover {
    transform: none;
  }

  .vt-detail-panel {
    animation: none !important;
  }
}

/* --- Tablet: marketplace + builder (max-width 768px) --- */
@media (max-width: 768px) {
  .site-header {
    padding: 1rem 1rem 0;
  }

  .site-main {
    padding: 0 1rem 1.5rem;
  }

  .site-footer {
    padding: 0.85rem 1rem 1.25rem;
  }

  .site-brand {
    flex-wrap: wrap;
    gap: 0.65rem;
  }

  .site-nav {
    gap: 0.45rem 0.65rem;
    font-size: 0.82rem;
    line-height: 1.35;
  }

  .site-nav a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 0.35rem 0.2rem;
  }

  /* Marketplace grids & form rows */
  .site-shell:not(.chat-page) .grid {
    grid-template-columns: 1fr;
    gap: 0.85rem;
    min-width: 0;
  }

  .site-shell:not(.chat-page) .row {
    align-items: stretch;
    flex-direction: column;
    grid-template-columns: 1fr;
  }

  .site-shell:not(.chat-page) .row .grow {
    flex: 1 1 auto;
    min-width: 0;
  }

  .site-shell:not(.chat-page) .card {
    padding: 1.05rem;
  }

  .site-shell:not(.chat-page) .pack-foot {
    flex-wrap: wrap;
    gap: 0.65rem;
  }

  .site-shell:not(.chat-page) .pack-foot button,
  .site-shell:not(.chat-page) .pack-foot .btn {
    min-height: 44px;
    flex: 1 1 calc(50% - 0.35rem);
  }

  /* Marketplace injected controls */
  .site-shell:not(.chat-page) .vt-controls {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }

  .site-shell:not(.chat-page) .vt-controls select {
    width: 100%;
    min-width: 0;
  }

  .site-shell:not(.chat-page) .vt-lang {
    margin-left: 0;
    margin-top: 0.35rem;
    align-self: flex-start;
  }

  .site-shell:not(.chat-page) .vt-lang button {
    min-height: 44px;
    min-width: 44px;
    padding: 0.45rem 0.75rem;
  }

  /* Detail overlay & panel (marketplace.js) */
  .vt-detail-overlay {
    padding: 1rem 0.75rem;
    align-items: flex-end;
  }

  .vt-detail-panel {
    max-width: none;
    width: 100%;
    max-height: 92vh;
    overflow-y: auto;
    padding: 1rem 1rem 1.25rem;
    border-radius: var(--radius, 12px) var(--radius, 12px) 0 0;
  }

  .vt-detail-close {
    top: 0.25rem;
    right: 0.25rem;
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.35rem;
  }

  /* Paywall (marketplace / assistant cards) */
  .vt-paywall {
    flex-direction: column;
    gap: 0.65rem;
  }

  .vt-paywall-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .vt-paywall-actions button,
  .vt-paywall-actions .btn,
  .vt-paywall-billing {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.65rem 1rem;
  }

  /* Builder steps / wizard */
  .site-shell:not(.chat-page) .steps {
    gap: 0.4rem;
    margin: 1rem 0;
  }

  .site-shell:not(.chat-page) .step-pill {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 0.85rem;
  }

  .site-shell:not(.chat-page) #stepMap {
    margin-bottom: 0.75rem;
  }

  .site-shell:not(.chat-page) .tpl-grid {
    grid-template-columns: 1fr;
  }

  .site-shell:not(.chat-page) .lang-switch {
    float: none;
    display: flex;
    justify-content: flex-end;
    gap: 0.35rem;
    margin-bottom: 0.5rem;
  }

  .site-shell:not(.chat-page) .lang-switch button {
    min-height: 44px;
    min-width: 44px;
    padding: 0.35rem 0.65rem;
  }

  .site-shell:not(.chat-page) .btn,
  .site-shell:not(.chat-page) button:not(.vt-detail-close):not(.prompt-hint) {
    min-height: 44px;
  }

  .site-shell:not(.chat-page) .prompt-hint {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 0.75rem;
  }

  .site-shell:not(.chat-page) .flowise-frame {
    height: min(420px, 55vh);
  }

  .site-shell:not(.chat-page) table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Catalog composer panel (builder.js #catalogComposer) */
  #catalogComposer {
    margin-bottom: 1rem;
    min-width: 0;
  }

  #catalogComposer .tpl-grid {
    grid-template-columns: 1fr;
  }

  #catalogComposer .row {
    grid-template-columns: 1fr;
  }

  #catalogComposer #catalogModuleList {
    max-height: none;
  }
}

/* --- Mobile: marketplace + builder (max-width 640px) --- */
@media (max-width: 640px) {
  .site-header {
    padding: 0.85rem 0.75rem 0;
  }

  .site-main {
    padding: 0 0.75rem 1.25rem;
  }

  .site-footer {
    padding: 0.75rem 0.75rem 1rem;
  }

  .site-shell:not(.chat-page) header h1 {
    font-size: 1.25rem;
  }

  .site-shell:not(.chat-page) header p {
    font-size: 0.85rem;
  }

  .site-shell:not(.chat-page) .card {
    padding: 0.95rem;
    margin-bottom: 0.85rem;
  }

  .site-nav {
    gap: 0.35rem 0.5rem;
  }

  .site-shell:not(.chat-page) .grid {
    gap: 0.75rem;
    margin-top: 0.75rem;
  }

  .vt-detail-overlay {
    padding: 0;
  }

  .vt-detail-panel {
    border-radius: 12px 12px 0 0;
    max-height: 94vh;
    padding: 0.95rem 0.85rem 1.15rem;
  }

  .site-shell:not(.chat-page) .steps {
    flex-direction: column;
    align-items: stretch;
  }

  .site-shell:not(.chat-page) .step-pill {
    width: 100%;
    justify-content: center;
    text-align: center;
  }

  .site-shell:not(.chat-page) .chk-row {
    flex-direction: column;
    gap: 0.65rem;
  }

  .site-shell:not(.chat-page) .chk-row label {
    min-height: 44px;
  }

  .site-shell:not(.chat-page) .kb-ingest-status .kb-status-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .site-shell:not(.chat-page) .kb-ingest-status .kb-status-actions .btn {
    width: 100%;
    min-height: 44px;
    margin: 0;
  }

  .site-shell:not(.chat-page) .pack-foot button,
  .site-shell:not(.chat-page) .pack-foot .btn {
    flex: 1 1 100%;
  }

  #catalogComposer details summary {
    min-height: 44px;
    display: flex;
    align-items: center;
  }
}

/* --- Narrow mobile: marketplace + builder (max-width 390px) --- */
@media (max-width: 390px) {
  #catalog {
    grid-template-columns: 1fr;
    gap: 0.65rem;
    min-width: 0;
    width: 100%;
  }

  #catalog .pack {
    min-width: 0;
    width: 100%;
  }

  .site-shell:not(.chat-page) #catalog .pack-foot {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .site-shell:not(.chat-page) #catalog .pack-foot button,
  .site-shell:not(.chat-page) #catalog .pack-foot .btn {
    flex: 1 1 auto;
    width: 100%;
    min-height: 44px;
  }

  .vt-detail-panel {
    max-height: 92vh;
    max-height: 92dvh;
    padding-top: max(0.95rem, env(safe-area-inset-top, 0px));
    padding-right: max(0.85rem, env(safe-area-inset-right, 0px));
    padding-bottom: max(1.15rem, env(safe-area-inset-bottom, 0px));
    padding-left: max(0.85rem, env(safe-area-inset-left, 0px));
  }

  .vt-detail-close {
    min-width: 44px;
    min-height: 44px;
  }

  .site-shell:not(.chat-page) #vt-controls,
  .site-shell:not(.chat-page) .vt-controls {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .site-shell:not(.chat-page) #vt-controls select,
  .site-shell:not(.chat-page) .vt-controls select {
    width: 100%;
    min-width: 0;
  }

  .site-shell:not(.chat-page) .pack-title,
  .site-shell:not(.chat-page) .pack-desc {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .site-shell:not(.chat-page) .vt-author-ledger {
    width: 100%;
    min-width: 0;
  }
}

/* --- Extra-narrow mobile: marketplace + builder (max-width 360px) --- */
@media (max-width: 360px) {
  .site-header {
    padding: 0.75rem 0.65rem 0;
  }

  .site-main {
    padding: 0 0.65rem 1.15rem;
  }

  .site-footer {
    padding: 0.65rem 0.65rem 0.85rem;
  }

  #catalog {
    gap: 0.55rem;
  }

  .site-shell:not(.chat-page) .card {
    padding: 0.85rem;
  }

  .vt-detail-panel {
    padding-top: max(0.85rem, env(safe-area-inset-top, 0px));
    padding-right: max(0.65rem, env(safe-area-inset-right, 0px));
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    padding-left: max(0.65rem, env(safe-area-inset-left, 0px));
  }

  .site-shell:not(.chat-page) #vt-controls,
  .site-shell:not(.chat-page) .vt-controls {
    gap: 0.45rem;
  }

  .site-shell:not(.chat-page) #catalog .pack-foot {
    gap: 0.45rem;
  }
}

/* Print — content must not depend on motion JS or decorative mesh */
@media print {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }

  .vt-mesh-hero__layers,
  .site-header::before,
  .site-header::after {
    display: none !important;
  }

  .vt-mesh-hero,
  .site-main,
  .site-header,
  .card,
  .hub-wrap {
    background: #fff !important;
    color: #000 !important;
    border-color: #ccc !important;
    box-shadow: none !important;
  }

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.85em;
    color: #444;
  }

  .pwa-hint,
  .theme-toggle,
  [hidden] {
    display: none !important;
  }
}
