/* ═══════════════════════════════════════════════════════════════
   CNC Pro — Layout System
   layout.css
   ═══════════════════════════════════════════════════════════════ */

/* ─────────────────────────────────────────────
   APP SHELL
───────────────────────────────────────────── */

.app-body {
  min-height: 100vh;
  background-color: var(--bg-deep);
}

/* Sidebar: fixed left column, desktop only */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background-color: var(--bg-panel);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sidebar);
  overflow-y: auto;
  overflow-x: hidden;
  transition: transform var(--transition-slow);
}

/* Main wrapper: offset by sidebar on desktop */
.app-wrapper {
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ─────────────────────────────────────────────
   HEADER
───────────────────────────────────────────── */

.app-header {
  position: sticky;
  top: 0;
  height: var(--header-height);
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-4);
  z-index: var(--z-header);
  flex-shrink: 0;
}

.header__menu-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: background var(--transition-fast), color var(--transition-fast);
  flex-shrink: 0;
}
.header__menu-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.header__brand {
  display: none;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-lg);
  font-weight: var(--font-bold);
  letter-spacing: -0.3px;
}
.header__logo-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}
.header__title {
  color: var(--text-primary);
}
.header__title-accent {
  color: var(--accent-red);
}

.header__machine-selector {
  flex: 1;
  max-width: 220px;
  margin-left: var(--space-2);
}

.header__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.header__action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: background var(--transition-fast), color var(--transition-fast);
}
.header__action-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.header__user-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background-color: var(--accent-red);
  color: white;
  font-size: var(--text-sm);
  font-weight: var(--font-bold);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  text-transform: uppercase;
}

/* ─────────────────────────────────────────────
   MAIN CONTENT
───────────────────────────────────────────── */

.app-main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background-color: var(--bg-base);
  min-height: 0;
}

/* Content pages: default padding */
.page {
  padding: var(--space-6);
  max-width: 1400px;
  margin: 0 auto;
}

.page--full {
  padding: 0;
  max-width: none;
}

/* Route loading bar */
.route-loading-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 0;
  height: 3px;
  background: var(--accent-blue, #3b82f6);
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.2s;
}
.route-loading-bar.active {
  opacity: 1;
  animation: route-loading 1.2s ease-in-out infinite;
}
@keyframes route-loading {
  0%   { width: 0; left: 0; }
  50%  { width: 60%; left: 0; }
  100% { width: 100%; left: 0; }
}

/* Loading state */
.app-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  gap: var(--space-4);
}
.app-loading__text {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* ─────────────────────────────────────────────
   SIDEBAR INTERNALS
───────────────────────────────────────────── */

.sidebar__logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sidebar__logo-icon {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}
.sidebar__logo-text {
  font-size: var(--text-xl);
  font-weight: var(--font-bold);
  letter-spacing: -0.5px;
  color: var(--text-primary);
}
.sidebar__logo-accent {
  color: var(--accent-red);
}

.sidebar__machine-selector {
  padding: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sidebar__machine-label {
  display: block;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  font-weight: var(--font-semibold);
  margin-bottom: var(--space-2);
}

.sidebar__nav {
  flex: 1;
  padding: var(--space-3) 0;
  overflow-y: auto;
}
.sidebar__nav-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 var(--space-2);
}

.sidebar__nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  transition: background var(--transition-fast), color var(--transition-fast);
  text-decoration: none;
}
.sidebar__nav-link:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  text-decoration: none;
}
.sidebar__nav-link.active {
  background: var(--accent-red-10);
  color: var(--accent-red);
}
.sidebar__nav-link .nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.sidebar__footer {
  border-top: 1px solid var(--border-subtle);
  padding: var(--space-3) var(--space-4);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

.sidebar__user {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 1;
  min-width: 0;
}
.sidebar__user-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background-color: var(--accent-red);
  color: white;
  font-size: var(--text-sm);
  font-weight: var(--font-bold);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  text-transform: uppercase;
}
.sidebar__user-details {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.sidebar__user-name {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__user-role {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.sidebar__logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: background var(--transition-fast), color var(--transition-fast);
  flex-shrink: 0;
}
.sidebar__logout-btn:hover {
  background: var(--accent-red-10);
  color: var(--accent-red);
}

/* ─────────────────────────────────────────────
   MACHINE SELECT WIDGET
───────────────────────────────────────────── */

.select-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.select-wrapper .select-arrow {
  position: absolute;
  right: var(--space-2);
  width: 14px;
  height: 14px;
  color: var(--text-muted);
  pointer-events: none;
}

.machine-select {
  width: 100%;
  padding: var(--space-2) var(--space-6) var(--space-2) var(--space-3);
  background-color: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  appearance: none;
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}
.machine-select:hover {
  border-color: var(--border-strong);
}
.machine-select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.15);
}

.select-wrapper--compact .machine-select {
  font-size: var(--text-xs);
  padding-top: var(--space-1);
  padding-bottom: var(--space-1);
}

/* ─────────────────────────────────────────────
   BOTTOM NAV (mobile)
───────────────────────────────────────────── */

.bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottom-nav-height);
  background-color: var(--bg-panel);
  border-top: 1px solid var(--border-subtle);
  z-index: var(--z-header);
  padding: 0 var(--space-1);
  align-items: center;
  justify-content: space-around;
}

.bottom-nav__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  flex: 1;
  height: 100%;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: var(--font-medium);
  text-decoration: none;
  transition: color var(--transition-fast);
  position: relative;
}
.bottom-nav__item:hover,
.bottom-nav__item.active {
  color: var(--accent-red);
  text-decoration: none;
}
.bottom-nav__item .nav-icon {
  width: 22px;
  height: 22px;
}

.bottom-nav__item--fab {
  color: transparent;
}
.bottom-nav__fab {
  width: 52px;
  height: 52px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--accent-red), #c73452);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-glow-red);
  transition: transform var(--transition-spring), box-shadow var(--transition-base);
  margin-bottom: 8px;
}
.bottom-nav__item--fab:hover .bottom-nav__fab,
.bottom-nav__item--fab.active .bottom-nav__fab {
  transform: scale(1.1);
  box-shadow: 0 0 28px rgba(233, 69, 96, 0.5);
}

.bottom-nav__avatar {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-full);
  background-color: var(--accent-red);
  color: white;
  font-size: 11px;
  font-weight: var(--font-bold);
  display: flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
}

/* ─────────────────────────────────────────────
   SIDEBAR OVERLAY (mobile)
───────────────────────────────────────────── */

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: calc(var(--z-sidebar) - 1);
  opacity: 0;
  transition: opacity var(--transition-slow);
}
.sidebar-overlay.visible {
  opacity: 1;
}

/* ─────────────────────────────────────────────
   EDITOR PAGE — 3-COLUMN LAYOUT
───────────────────────────────────────────── */

.editor-layout {
  display: grid;
  grid-template-columns: var(--editor-left-width) 1fr var(--editor-right-width);
  grid-template-rows: 1fr;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

.editor-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border-subtle);
}
.editor-panel:last-child {
  border-right: none;
}

.editor-panel__header {
  padding: var(--space-3) var(--space-4);
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
  min-height: 48px;
}
.editor-panel__title {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.editor-panel__body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-3);
}

.editor-panel__body--no-pad {
  padding: 0;
  overflow: hidden;
}

/* ─── Code Editor pane ─── */
.editor-code-pane {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.editor-code-wrapper {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

.editor-line-numbers {
  width: 44px;
  padding: var(--space-3) var(--space-2);
  background-color: var(--bg-deep);
  color: var(--text-disabled);
  text-align: right;
  font-size: 12px;
  line-height: 1.6;
  user-select: none;
  border-right: 1px solid var(--border-subtle);
  overflow: hidden;
  flex-shrink: 0;
}

.editor-textarea {
  flex: 1;
  padding: var(--space-3) var(--space-3);
  background-color: var(--bg-deep);
  color: var(--text-primary);
  border: none;
  resize: none;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  caret-color: var(--accent-red);
  outline: none;
  tab-size: 4;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}
.editor-textarea:focus {
  outline: none;
}

/* ─── Canvas pane ─── */
.canvas-pane {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: var(--bg-deep);
}

.canvas-toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  flex-wrap: wrap;
  flex-shrink: 0;
}

.canvas-tabs {
  display: flex;
  gap: 2px;
  background-color: var(--bg-deep);
  border-radius: var(--radius-md);
  padding: 2px;
}
.canvas-tab {
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  color: var(--text-muted);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
}
.canvas-tab.active {
  background-color: var(--bg-raised);
  color: var(--text-primary);
}

.canvas-area {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: crosshair;
}
.canvas-area canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.canvas-status {
  padding: var(--space-2) var(--space-3);
  background-color: var(--bg-panel);
  border-top: 1px solid var(--border-subtle);
  font-size: var(--text-xs);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-shrink: 0;
}
.canvas-status span {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

/* ─── Info/Stats pane ─── */
.editor-info-pane {
  background-color: var(--bg-base);
}

/* ─────────────────────────────────────────────
   EDITOR MOBILE TABS
───────────────────────────────────────────── */

.editor-tabs-bar {
  display: none;
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  overflow-x: auto;
  scrollbar-width: none;
}
.editor-tabs-bar::-webkit-scrollbar { display: none; }

.editor-tabs-bar__inner {
  display: flex;
  white-space: nowrap;
}
.editor-tab-btn {
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  transition: color var(--transition-fast), border-color var(--transition-fast);
  white-space: nowrap;
  flex-shrink: 0;
}
.editor-tab-btn.active {
  color: var(--accent-red);
  border-bottom-color: var(--accent-red);
}

/* ─────────────────────────────────────────────
   GRID HELPERS
───────────────────────────────────────────── */

.grid-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--space-4);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
}

/* ─────────────────────────────────────────────
   PAGE HEADER PATTERN
───────────────────────────────────────────── */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-bottom: var(--space-6);
}

.page-header__left {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.page-title {
  font-size: var(--text-2xl);
  font-weight: var(--font-bold);
  color: var(--text-primary);
  line-height: var(--leading-tight);
}

.page-subtitle {
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.page-header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* ─────────────────────────────────────────────
   SECTION PATTERN
───────────────────────────────────────────── */

.section {
  margin-bottom: var(--space-8);
}

.section__title {
  font-size: var(--text-base);
  font-weight: var(--font-semibold);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: var(--space-4);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.section__title::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-subtle);
}

/* ─────────────────────────────────────────────
   RESPONSIVE — TABLET & MOBILE
───────────────────────────────────────────── */

@media (max-width: 1100px) {
  /* Collapse editor right panel */
  .editor-layout {
    grid-template-columns: var(--editor-left-width) 1fr;
  }
  .editor-info-pane {
    display: none;
  }
  .editor-info-pane.visible {
    display: flex;
    grid-column: 1 / -1;
    grid-row: 2;
    height: 300px;
    border-top: 1px solid var(--border-subtle);
  }
}

@media (max-width: 768px) {
  /* Hide sidebar, show mobile nav */
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-xl);
  }

  .sidebar-overlay {
    display: block;
  }

  .app-wrapper {
    margin-left: 0;
  }

  .header__menu-toggle {
    display: flex;
  }
  .header__brand {
    display: flex;
  }
  .header__machine-selector {
    display: none;
  }

  .bottom-nav {
    display: flex;
  }

  /* Push main content above bottom nav */
  .app-main {
    padding-bottom: var(--bottom-nav-height);
  }

  /* Editor: switch to mobile tabs */
  .editor-layout {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--header-height) - var(--bottom-nav-height));
    overflow: hidden;
  }
  .editor-tabs-bar {
    display: block;
    flex-shrink: 0;
  }
  .editor-panel {
    display: none;
    flex: 1;
    border-right: none;
    border-bottom: 1px solid var(--border-subtle);
  }
  .editor-panel.active {
    display: flex;
  }

  /* Pages */
  .page {
    padding: var(--space-4);
  }
  .page-title {
    font-size: var(--text-xl);
  }
  .grid-2,
  .grid-3 {
    grid-template-columns: 1fr;
  }
  .grid-cards {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .page {
    padding: var(--space-3);
  }
  .page-header {
    margin-bottom: var(--space-4);
  }
}
