/* ── rStack Shell Layout ── */

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  background: var(--rs-bg-page);
  color: var(--rs-text-primary);
}

/* ── Header bar ── */

.rstack-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 9999;
  border-bottom: 1px solid var(--rs-glass-border);
  color: var(--rs-text-primary);
}
/* backdrop-filter on pseudo-element so the header doesn't create a
   containing block that traps position:fixed children (e.g. app-switcher sidebar) */
.rstack-header::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  background: var(--rs-glass-bg);
}

.rstack-header__left {
  display: flex;
  align-items: center;
  gap: 4px;
}

.rstack-header__center {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  min-width: 0;
}

.rstack-header__right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.rstack-header__demo-btn {
  display: inline-flex;
  align-items: center;
  padding: 5px 14px;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s, opacity 0.15s;
  background: var(--rs-gradient-cta);
  color: #fff;
  box-shadow: 0 1px 4px rgba(20, 184, 166, 0.25);
}
.rstack-header__demo-btn:hover {
  opacity: 0.88;
}
/* Hide the demo button when already on demo space */
.rstack-header__demo-btn[data-hide] { display: none; }

.rstack-header__logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  flex-shrink: 0;
}

.rstack-header__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-size: 1.25rem;
  font-weight: 700;
  color: inherit;
}

.rstack-header__brand-gradient {
  background: var(--rs-gradient-brand);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* ── Header icon buttons (settings gear, history clock) ── */

.rstack-header__settings-btn,
.rstack-header__history-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--rs-text-secondary);
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}

.rstack-header__settings-btn:hover,
.rstack-header__history-btn:hover {
  color: var(--rs-text-primary);
  background: var(--rs-btn-secondary-bg, rgba(255,255,255,0.08));
}

.rstack-header__history-btn.active {
  color: #fff;
  background: var(--rs-accent, #14b8a6);
}

/* ── Tab row (below header) ── */

.rstack-tab-row {
  position: fixed;
  top: 56px;
  left: 0;
  right: 0;
  z-index: 9998;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--rs-border-subtle);
  background: var(--rs-glass-bg);
}

/* ── Main content area ── */

#app {
  padding-top: 92px; /* Below fixed header (56px) + tab row (36px) */
  min-height: 100vh;
}

/* When canvas module is active, make it fill the viewport */
#app.canvas-layout {
  padding-top: 92px;
  height: 100vh;
  overflow: hidden;
}

/* ── Standalone mode (no app/space switcher) ── */

.rstack-header--standalone .rstack-header__left {
  gap: 0;
}

/* ── Shared in-app navigation bar (used by module components) ── */

.rapp-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding: 0;
  min-height: 36px;
}

.rapp-nav__back {
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--rs-border);
  background: transparent;
  color: var(--rs-text-secondary);
  cursor: pointer;
  font-size: 13px;
  text-decoration: none;
  transition: color 0.15s, border-color 0.15s;
}

.rapp-nav__back:hover {
  color: var(--rs-text-primary);
  border-color: var(--rs-border-strong);
}

.rapp-nav__title {
  font-size: 15px;
  font-weight: 600;
  color: var(--rs-text-primary);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rapp-nav__actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.rapp-nav__btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: none;
  background: var(--rs-primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
}

.rapp-nav__btn:hover {
  background: var(--rs-primary-hover);
}

.rapp-nav__btn--secondary {
  background: transparent;
  border: 1px solid var(--rs-border);
  color: var(--rs-text-secondary);
}

.rapp-nav__btn--secondary:hover {
  border-color: var(--rs-border-strong);
  color: var(--rs-text-primary);
}

.rapp-nav__badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--rs-warning);
  background: rgba(251,191,36,0.15);
  border: 1px solid rgba(251,191,36,0.3);
  border-radius: 4px;
  padding: 2px 8px;
}

/* ── Iframe embed for external apps ── */

.rspace-iframe-wrap {
  position: fixed;
  top: 92px; /* header 56px + tab row 36px */
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

.rspace-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: var(--rs-bg-page);
}

.rspace-iframe-loading {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--rs-bg-page);
  color: var(--rs-text-secondary);
  font-size: 0.9rem;
  z-index: 2;
}

.rspace-iframe-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--rs-spinner-track);
  border-top-color: var(--rs-spinner-head);
  border-radius: 50%;
  animation: rspace-spin 0.8s linear infinite;
}

@keyframes rspace-spin {
  to { transform: rotate(360deg); }
}

.rspace-iframe-newtab {
  position: absolute;
  bottom: 12px;
  right: 16px;
  font-size: 0.72rem;
  color: var(--rs-text-muted);
  text-decoration: none;
  padding: 4px 10px;
  border-radius: 4px;
  background: var(--rs-glass-bg);
  border: 1px solid var(--rs-glass-border);
  z-index: 3;
  transition: color 0.15s, border-color 0.15s;
}

.rspace-iframe-newtab:hover {
  color: var(--rs-text-primary);
  border-color: var(--rs-border-strong);
}

/* "Open Full App" button used in module demo views */
.rapp-nav__btn--app-toggle {
  background: var(--rs-gradient-primary);
  color: #fff;
  font-size: 0.78rem;
  padding: 5px 14px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: opacity 0.15s;
}
.rapp-nav__btn--app-toggle:hover {
  opacity: 0.88;
}

/* ── Sidebar push offsets ── */

.rstack-tab-row,
#app,
.rspace-iframe-wrap,
#toolbar {
  transition: margin-left 0.25s ease, left 0.25s ease;
}

body.rstack-sidebar-open .rstack-tab-row {
  left: 280px;
}

body.rstack-sidebar-open #app {
  margin-left: 280px;
}

body.rstack-sidebar-open .rspace-iframe-wrap {
  left: 280px;
}

body.rstack-sidebar-open #toolbar {
  left: 292px; /* 280px sidebar + 12px original margin */
}

/* ── Mobile adjustments ── */

@media (max-width: 640px) {
  /* Switch header + tab row from fixed to sticky on mobile.
     This avoids needing a magic padding-top on #app and lets
     the header wrap naturally to two rows. */
  .rstack-header {
    position: sticky;
    padding: 6px 12px;
    height: auto;
    flex-wrap: wrap;
    gap: 0;
  }
  .rstack-header__left {
    flex: 1 1 0;
    min-width: 0;
    gap: 2px;
  }
  .rstack-header__center {
    order: 3;
    flex-basis: 100%;
    padding: 4px 0 2px;
    display: flex;
  }
  .rstack-header__right {
    flex: 0 0 auto;
    gap: 6px;
    margin-left: auto;
  }
  .rstack-header__demo-btn {
    padding: 4px 10px;
    font-size: 0.72rem;
  }
  .rstack-header__brand {
    font-size: 1rem;
    gap: 6px;
  }
  .rstack-header__logo {
    width: 24px;
    height: 24px;
  }
  .rstack-tab-row {
    position: sticky;
  }
  #app {
    padding-top: 0;
  }
  #app.canvas-layout {
    padding-top: 0;
  }
  .rspace-iframe-wrap {
    position: relative;
    top: 0;
    width: 100%;
    height: calc(100vh - 80px);
    min-height: 400px;
  }
  .rapp-nav {
    flex-wrap: wrap;
    gap: 6px;
  }
  /* Sidebar overlays on mobile — no push offsets */
  body.rstack-sidebar-open .rstack-tab-row { left: 0; }
  body.rstack-sidebar-open #app { margin-left: 0; }
  body.rstack-sidebar-open .rspace-iframe-wrap { left: 0; }
  body.rstack-sidebar-open #toolbar { left: 12px; }
}

@media (max-width: 480px) {
  #app {
    padding-left: 8px;
    padding-right: 8px;
  }
}

/* ── Tab pane caching (show/hide) ── */

.rspace-tab-pane {
  display: none;
}

.rspace-tab-pane--active {
  display: block;
}

/* When canvas is active, the tab pane must fill the viewport so #canvas height:100% works */
#app.canvas-layout > .rspace-tab-pane--active {
  height: 100%;
}

.rspace-tab-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 92px);
}

.rspace-tab-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--rs-spinner-track);
  border-top-color: var(--rs-spinner-head);
  border-radius: 50%;
  animation: rspace-spin 0.8s linear infinite;
}
