/* =========================================================
   LAYOUT: App Shell, Workspace, Sidebar Docking System
   ========================================================= */

/* App Shell */
.app-header {
  height: var(--header-height);
  min-height: var(--header-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  padding: 0 var(--space-md);
  z-index: 100;
}

.workspace {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

.main-content {
  flex: 1;
  overflow: auto;
  background: var(--bg-primary);
  position: relative;
}

.ticker-bar {
  height: var(--ticker-height);
  min-height: var(--ticker-height);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  overflow: hidden;
  z-index: 100;
}

/* =========================================================
   SIDEBAR: Dockable / Floating / Resizable
   ========================================================= */

.sidebar {
  position: relative;
  width: var(--sidebar-width);
  min-width: var(--sidebar-min-width);
  max-width: var(--sidebar-max-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-base),
              box-shadow var(--transition-base),
              width var(--transition-fast);
  z-index: 50;
}

/* Floating State */
.sidebar.is-floating {
  position: absolute;
  top: var(--space-sm);
  left: var(--space-sm);
  bottom: var(--space-sm);
  height: auto;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-float);
  z-index: 200;
}

.sidebar.is-floating .sidebar-handle {
  cursor: grab;
  background: var(--bg-hover);
}
.sidebar.is-floating .sidebar-handle:active {
  cursor: grabbing;
}

/* Hidden State */
.sidebar.is-hidden {
  transform: translateX(-100%);
  width: 0 !important;
  min-width: 0;
  border: none;
  overflow: hidden;
}

/* Handle (drag grip) */
.sidebar-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  min-height: 24px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  transition: background var(--transition-fast);
  position: relative;
}
.sidebar-handle:hover {
  background: var(--bg-hover);
}
.sidebar-handle::before {
  content: '';
  width: 24px;
  height: 3px;
  background: var(--text-muted);
  border-radius: 2px;
  opacity: 0.5;
}
.sidebar-handle:hover::before {
  opacity: 0.8;
}

/* Sidebar Content */
.sidebar-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-sm);
}

/* Resize Handle */
.sidebar-resize {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
  transition: background var(--transition-fast);
}
.sidebar-resize:hover,
.sidebar-resize.is-resizing {
  background: var(--accent-blue);
  opacity: 0.3;
}

/* When sidebar is floating, resize from right edge */
.sidebar.is-floating .sidebar-resize {
  right: -3px;
}

/* Sidebar Toggle Button (in header) */
.sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast);
}
.sidebar-toggle:hover {
  background: var(--bg-hover);
}

/* Widget Grid in Main Content */
.widget-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: var(--space-sm);
  padding: var(--space-sm);
  height: 100%;
  min-height: 0;
}

.widget-grid .panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.widget-grid .panel-body {
  flex: 1;
  overflow: auto;
}

/* Trade view panel placement — kept in CSS (not inline styles) so the
   responsive rules below can actually override it. */
.trade-chart-panel { grid-column: 1 / 2; grid-row: 1 / 3; }
.trade-book-panel  { grid-column: 2 / 3; grid-row: 1 / 2; }
.trade-order-panel { grid-column: 2 / 3; grid-row: 2 / 3; }

/* Responsive: stack widgets into a single scrollable column on tablet/phone */
@media (max-width: 1200px) {
  .widget-grid {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    height: auto;
  }
  .trade-chart-panel,
  .trade-book-panel,
  .trade-order-panel {
    grid-column: 1 / 2;
    grid-row: auto;
  }
  .trade-chart-panel { min-height: 360px; }
  .trade-book-panel,
  .trade-order-panel { min-height: 300px; }
}

@media (max-width: 640px) {
  .widget-grid { padding: var(--space-xs); gap: var(--space-xs); }
  .trade-chart-panel { min-height: 300px; }
  .trade-book-panel,
  .trade-order-panel { min-height: 260px; }
}

/* Drag Ghost */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  opacity: 0.8;
  z-index: 9999;
  box-shadow: var(--shadow-float);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

/* Overlay when sidebar is floating */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: transparent;
  z-index: 150;
  display: none;
}
.sidebar-overlay.is-active {
  display: block;
}

/* =========================================================
   RESPONSIVE: Sidebar becomes a slide-in drawer on phone widths.
   Reuses the existing dock/float/hide state — no new interaction
   model, just different positioning while the screen is narrow.
   ========================================================= */
@media (max-width: 640px) {
  .sidebar,
  .sidebar.is-floating {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: var(--ticker-height);
    height: auto;
    width: min(85vw, 320px) !important;
    max-width: none;
    border-radius: 0;
    z-index: 250;
    box-shadow: var(--shadow-float);
  }

  .sidebar.is-hidden {
    transform: translateX(-100%);
  }

  .sidebar-resize { display: none; }

  .sidebar-overlay.is-active {
    background: rgba(0, 0, 0, 0.5);
  }
}
