/**
 * 99tools SaaS Studio & A4 Document Simulator Framework
 * Generic, reusable studio workspace architecture for interactive tools
 * (Invoice Generator, Resume Builder, PDF Editor, Certificate Generator, etc.)
 *
 * @package 99tools
 */

/* ==========================================================================
   1. SAAS STUDIO APP SHELL & TOOLBAR
   ========================================================================== */
.saas-studio-app {
  display: flex;
  flex-direction: column;
  width: 100%;
  position: relative;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-md);
}

/* Studio Top Action Toolbar */
.saas-studio-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding: 0.75rem 1.25rem;
  background: var(--bg-surface-elevated);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 30;
}

.saas-toolbar-left,
.saas-toolbar-center,
.saas-toolbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.saas-tool-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.6rem;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 700;
  background: rgba(16, 185, 129, 0.12);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.25);
}

.saas-tool-status-pill .status-pulse {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #10b981;
}

/* Zoom Controls */
.saas-zoom-toolbar {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.15rem 0.35rem;
}

.saas-zoom-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-weight: 700;
  font-size: 0.85rem;
  padding: 0.2rem 0.45rem;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.saas-zoom-btn:hover {
  background: var(--bg-surface);
  color: var(--brand-primary);
}

.saas-zoom-label {
  font-size: 0.78rem;
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--text-muted);
  min-width: 44px;
  text-align: center;
}

/* ==========================================================================
   2. STUDIO BODY & COMPACT ACCORDION SIDEBAR
   ========================================================================== */
.saas-studio-body {
  display: grid;
  grid-template-columns: 350px 1fr;
  min-height: 850px;
  position: relative;
  transition: grid-template-columns var(--transition-normal);
}

.saas-studio-body.is-sidebar-collapsed {
  grid-template-columns: 0px 1fr;
}

/* Studio Left Sidebar (Refined 350px) */
.saas-studio-sidebar {
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  overflow-y: auto;
  overflow-x: hidden;
  max-height: calc(100vh - 140px);
  padding: 1.15rem 1rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  transition: opacity var(--transition-fast), transform var(--transition-fast);
  z-index: 20;
  /* Modern Sleek Scrollbar */
  scrollbar-width: thin;
  scrollbar-color: rgba(212, 168, 67, 0.35) transparent;
}

.saas-studio-sidebar::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

.saas-studio-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.saas-studio-sidebar::-webkit-scrollbar-thumb {
  background: rgba(212, 168, 67, 0.3);
  border-radius: 9999px;
}

.saas-studio-sidebar::-webkit-scrollbar-thumb:hover {
  background: rgba(212, 168, 67, 0.65);
}

/* Suppress redundant outer workspace toolbar & padding when studio tool is present */
.tool-workspace-container.is-studio-workspace,
.tool-workspace-container:has(.saas-studio-app) {
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  padding: 0 !important;
}

.tool-workspace-container.is-studio-workspace > .workspace-toolbar,
.tool-workspace-container:has(.saas-studio-app) > .workspace-toolbar {
  display: none !important;
}

.tool-workspace-container.is-studio-workspace > .workspace-canvas,
.tool-workspace-container:has(.saas-studio-app) > .workspace-canvas {
  padding: 0 !important;
}

.saas-studio-body.is-sidebar-collapsed .saas-studio-sidebar {
  width: 0 !important;
  min-width: 0 !important;
  padding: 0 !important;
  border-right: none !important;
  opacity: 0;
  pointer-events: none;
  overflow: hidden !important;
  transform: translateX(-20px);
}

/* ==========================================================================
   2.1 SIDEBAR TABS & TAB PANELS (Scalable Horizontal Scroll Rail)
   ========================================================================== */
.saas-sidebar-tabs {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: rgba(15, 23, 42, 0.65);
  padding: 0.35rem 0.4rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  margin-bottom: 0.65rem;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE/Edge */
  -webkit-overflow-scrolling: touch;
  flex-shrink: 0;
}

.saas-sidebar-tabs::-webkit-scrollbar {
  display: none; /* Chrome/Safari */
}

.saas-tab-nav-btn {
  flex: 0 0 auto;
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.42rem 0.85rem;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--transition-fast);
  user-select: none;
}

.saas-tab-nav-btn span {
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.01em;
}

.saas-tab-nav-btn svg {
  width: 14px;
  height: 14px;
  stroke-width: 2.2;
  flex-shrink: 0;
  color: inherit;
}

.saas-tab-nav-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.1);
}

.saas-tab-nav-btn.is-active {
  background: rgba(212, 168, 67, 0.15);
  color: var(--brand-primary);
  border-color: rgba(212, 168, 67, 0.45);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.saas-tab-nav-btn.is-active span {
  font-weight: 700;
}

/* Tab Panels */
.saas-tab-panels {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.saas-tab-panel {
  display: none;
  animation: saasTabFade 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.saas-tab-panel.is-active {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* ==========================================================================
   2.2 ELEMENTOR-STYLE COLLAPSIBLE SECTIONS (Accordions)
   ========================================================================== */
.saas-panel-section {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  overflow: hidden;
  margin-bottom: 0.55rem;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.saas-panel-section.is-open {
  border-color: rgba(212, 168, 67, 0.28);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.18);
}

.saas-section-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.8rem;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  text-align: left;
  cursor: pointer;
  user-select: none;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.saas-section-toggle:hover {
  background: rgba(255, 255, 255, 0.03);
  color: var(--brand-primary);
}

.saas-section-toggle-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.saas-section-toggle-left svg {
  width: 14px;
  height: 14px;
  stroke-width: 2.2;
  color: var(--text-muted);
  transition: color var(--transition-fast);
}

.saas-panel-section.is-open .saas-section-toggle-left svg {
  color: var(--brand-primary);
}

.saas-section-chevron {
  width: 13px;
  height: 13px;
  stroke-width: 2.5;
  color: var(--text-muted);
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), color var(--transition-fast);
  transform: rotate(0deg);
}

.saas-panel-section.is-open .saas-section-chevron {
  transform: rotate(90deg);
  color: var(--brand-primary);
}

.saas-section-body {
  display: none;
  padding: 0.4rem 0.8rem 0.75rem 0.8rem;
  flex-direction: column;
  gap: 0.65rem;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.saas-panel-section.is-open .saas-section-body {
  display: flex;
}

@keyframes saasTabFade {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.saas-tab-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 0.15rem;
}

.saas-tab-title {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.saas-tab-step {
  font-size: 0.7rem;
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--brand-primary);
  background: rgba(212, 168, 67, 0.12);
  padding: 0.15rem 0.5rem;
  border-radius: var(--radius-full);
  border: 1px solid rgba(212, 168, 67, 0.25);
}

.saas-tab-footer {
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--border-subtle);
}

/* Accordion Card (Fallback / Generic) */
.saas-accordion-card {
  background: var(--bg-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  overflow: hidden;
  transition: border-color var(--transition-fast);
}

.saas-accordion-card:hover {
  border-color: var(--border-strong);
}

.saas-accordion-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0.9rem;
  background: transparent;
  border: none;
  width: 100%;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-primary);
  text-align: left;
}

.saas-accordion-header .acc-title-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.saas-accordion-chevron {
  transition: transform var(--transition-fast);
  color: var(--text-muted);
}

.saas-accordion-card.is-open .saas-accordion-chevron {
  transform: rotate(180deg);
}

.saas-accordion-body {
  display: none;
  padding: 0.85rem 0.9rem 1rem;
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  flex-direction: column;
  gap: 0.75rem;
}

.saas-accordion-card.is-open .saas-accordion-body {
  display: flex;
}

.saas-field-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.saas-field-group label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.saas-field-group input,
.saas-field-group select,
.saas-field-group textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-size: 0.825rem;
  transition: border-color var(--transition-fast);
}

.saas-field-group input:focus,
.saas-field-group select:focus,
.saas-field-group textarea:focus {
  outline: none;
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 2px var(--brand-gold-glow);
}

/* ==========================================================================
   3. DESK CANVAS & AUTHENTIC A4 SIMULATOR
   ========================================================================== */
.saas-studio-canvas {
  background: #f1f5f9;
  background-image: radial-gradient(circle, #cbd5e1 1px, transparent 1px);
  background-size: 24px 24px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem 1rem 3.5rem;
  position: relative;
  min-height: 750px;
  scrollbar-width: thin;
  scrollbar-color: rgba(212, 168, 67, 0.3) transparent;
}

.saas-studio-canvas::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.saas-studio-canvas::-webkit-scrollbar-track {
  background: transparent;
}

.saas-studio-canvas::-webkit-scrollbar-thumb {
  background: rgba(212, 168, 67, 0.25);
  border-radius: 9999px;
}

.saas-studio-canvas::-webkit-scrollbar-thumb:hover {
  background: rgba(212, 168, 67, 0.6);
}

[data-theme="dark"] .saas-studio-canvas {
  background: #090d16;
  background-image: radial-gradient(circle, #1e293b 1px, transparent 1px);
}

/* A4 Sheet Desk Container */
.saas-a4-desk-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 820px;
}

/* Sheet Top Guide Banner */
.saas-a4-guide-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 800px;
  padding: 0.4rem 0.85rem;
  margin-bottom: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.saas-a4-guide-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  padding: 0.2rem 0.6rem;
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-sm);
  color: var(--text-secondary);
}

/* Physical A4 Sheet */
.saas-a4-paper {
  position: relative;
  width: 100%;
  max-width: 800px;
  min-height: 1060px; /* Standard A4 ratio (1 : 1.414) */
  background: #ffffff !important;
  color: #0f172a !important;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08), 0 8px 16px -4px rgba(0, 0, 0, 0.08), 0 24px 48px -12px rgba(0, 0, 0, 0.16);
  padding: 3.25rem 3rem;
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 0.875rem;
  line-height: 1.5;
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  transform-origin: top center;
}

[data-theme="dark"] .saas-a4-paper {
  background: #ffffff !important;
  color: #0f172a !important;
}

/* Corner Crop Registration Marks */
.a4-crop-mark {
  position: absolute;
  width: 12px;
  height: 12px;
  pointer-events: none;
  opacity: 0.45;
}

.a4-crop-tl { top: -8px; left: -8px; border-top: 2px solid #64748b; border-left: 2px solid #64748b; }
.a4-crop-tr { top: -8px; right: -8px; border-top: 2px solid #64748b; border-right: 2px solid #64748b; }
.a4-crop-bl { bottom: -8px; left: -8px; border-bottom: 2px solid #64748b; border-left: 2px solid #64748b; }
.a4-crop-br { bottom: -8px; right: -8px; border-bottom: 2px solid #64748b; border-right: 2px solid #64748b; }

/* INLINE DIRECT CLICK-TO-EDIT ENGINE */
.inline-editable {
  cursor: text;
  outline: 1px dashed transparent;
  border-radius: 3px;
  transition: all var(--transition-fast);
  display: inline-block;
  min-width: 24px;
}

.inline-editable:hover {
  outline: 1px dashed #D4A843 !important;
  background: rgba(212, 168, 67, 0.06) !important;
}

.inline-editable:focus {
  outline: 2px solid #D4A843 !important;
  background: rgba(212, 168, 67, 0.12) !important;
  box-shadow: 0 0 0 3px rgba(212, 168, 67, 0.18) !important;
}

/* ==========================================================================
   4. FULLSCREEN SAAS STUDIO VIEWPORT
   ========================================================================== */
.tool-workspace-container.is-fullscreen .saas-studio-app {
  border: none !important;
  border-radius: 0 !important;
  height: 100% !important;
}

.tool-workspace-container.is-fullscreen .workspace-canvas {
  padding: 0 !important;
  max-width: 100% !important;
  width: 100% !important;
}

.tool-workspace-container.is-fullscreen .saas-studio-body {
  height: calc(100vh - 120px) !important;
  height: calc(100dvh - 120px) !important;
}

/* ==========================================================================
   5. MOBILE NATIVE BOTTOM ACTION BAR & BOTTOM SHEET MODAL
   ========================================================================== */
.saas-mobile-bottom-bar {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  background: var(--bg-surface-elevated);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border-subtle);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  padding: 0.65rem 1rem max(0.65rem, env(safe-area-inset-bottom));
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.saas-mobile-action-btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.3rem 0.6rem;
  border-radius: var(--radius-sm);
}

.saas-mobile-action-btn:hover,
.saas-mobile-action-btn:active {
  color: var(--brand-primary);
}

/* Mobile Slide-Up Settings Sheet */
.saas-mobile-sheet-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: 10000;
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.saas-mobile-sheet-backdrop.is-open {
  display: block;
  opacity: 1;
}

.saas-mobile-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-surface);
  border-radius: 20px 20px 0 0;
  border-top: 1px solid var(--border-subtle);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.3);
  z-index: 10001;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform var(--transition-normal);
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.saas-mobile-sheet.is-open {
  transform: translateY(0);
}

.saas-mobile-sheet-drag-handle {
  width: 40px;
  height: 5px;
  border-radius: 3px;
  background: var(--border-strong);
  margin: 0.6rem auto 0.4rem;
}

.saas-mobile-sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border-subtle);
}

.saas-mobile-sheet-content {
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ==========================================================================
   6. RESPONSIVE RULES (≤ 900px)
   ========================================================================== */
@media (max-width: 900px) {
  .saas-studio-body {
    grid-template-columns: 1fr !important;
  }
  .saas-studio-sidebar {
    display: none !important; /* Managed via bottom sheet */
  }
  .saas-mobile-bottom-bar {
    display: flex !important;
  }
  .saas-studio-canvas {
    padding: 1rem 0.5rem 6rem !important; /* Space for fixed bottom bar */
  }
  .saas-a4-paper {
    padding: 1.5rem 1.15rem !important;
    min-height: auto !important;
    border-radius: 6px !important;
  }
  .a4-crop-mark {
    display: none !important;
  }
}

/* ==========================================================================
   7. FULLSCREEN APP MODE (Clean, Immersive, Simple UX)
   ========================================================================== */
body.has-fullscreen-open,
body:has(.is-fullscreen-app) {
  overflow: hidden !important;
}

body.has-fullscreen-open .site-header,
body:has(.is-fullscreen-app) .site-header,
body.has-fullscreen-open #siteHeader,
body:has(.is-fullscreen-app) #siteHeader,
body.has-fullscreen-open #wpadminbar,
body:has(.is-fullscreen-app) #wpadminbar {
  display: none !important;
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

.saas-studio-app.is-fullscreen-app {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  max-width: 100vw !important;
  max-height: 100vh !important;
  z-index: 99999999 !important;
  border-radius: 0 !important;
  margin: 0 !important;
  border: none !important;
  box-shadow: none !important;
  background: var(--bg-surface);
}

.saas-studio-app.is-fullscreen-app .saas-studio-toolbar {
  border-radius: 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

/* Brand Logo in Toolbar on Fullscreen */
.saas-studio-brand-wrap {
  display: none;
}

.saas-studio-app.is-fullscreen-app .saas-studio-brand-wrap,
body.has-fullscreen-open .saas-studio-brand-wrap {
  display: flex !important;
  align-items: center;
  padding-right: 0.75rem;
  margin-right: 0.5rem;
  border-right: 1px solid var(--border-subtle);
}

.saas-studio-app.is-fullscreen-app .saas-studio-body {
  height: calc(100vh - 54px) !important;
  min-height: auto !important;
}

.saas-studio-app.is-fullscreen-app .saas-studio-sidebar {
  max-height: calc(100vh - 54px) !important;
}

.saas-studio-app.is-fullscreen-app .saas-studio-canvas {
  height: calc(100vh - 54px) !important;
  min-height: auto !important;
  padding: 1rem 1rem 3rem !important;
}

#btnStudioFullscreen.is-active {
  background: var(--brand-primary);
  color: #000;
  border-color: var(--brand-primary);
  font-weight: 700;
}

/* Logo Preset Size Buttons */
.btn-logo-preset {
  flex: 1;
  padding: 0.3rem 0.2rem;
  background: var(--bg-surface-elevated, #1e293b);
  border: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.1));
  border-radius: var(--radius-sm, 4px);
  color: var(--text-secondary, #94a3b8);
  font-size: 0.68rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast, 0.15s ease);
  text-align: center;
  user-select: none;
}

.btn-logo-preset:hover {
  color: var(--text-primary, #ffffff);
  border-color: rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.06);
}

.btn-logo-preset.is-active {
  background: rgba(212, 168, 67, 0.18);
  border-color: rgba(212, 168, 67, 0.45);
  color: var(--brand-primary, #D4A843);
  font-weight: 700;
}
