/**
 * ============================================================================
 * RIGHT TOOL RAIL - KNOMAD Tool Launcher
 * ============================================================================
 *
 * A compact, right-side tool launcher with hover-reveal labels.
 * Bootstrap-compatible CSS variables for theming consistency.
 *
 * CLASSES:
 * - .bento-rail              - Base rail container
 * - .bento-rail--right       - Right-side positioning
 * - .bento-rail__stack       - Button stack container
 * - .bento-rail__btn         - Individual tool button
 * - .bento-rail__btn--idle   - Idle state
 * - .bento-rail__btn--hover  - Hover state
 * - .bento-rail__label       - Hover-reveal label
 * - .bento-rail__editor      - Inline edit popover
 * - .bento-rail__contextmenu - Right-click context menu
 */

/* ============================================================================
   CSS VARIABLES
   ============================================================================ */

:root {
  /* Rail dimensions */
  --bs-bento-rail-width: 52px;
  --bs-bento-rail-gap: 8px;
  --bs-bento-rail-radius: 12px;
  --bs-bento-rail-padding: 6px;

  /* Rail colors - matched to KNOMAD dark theme */
  --bs-bento-rail-bg: rgba(20, 20, 22, 0.85);
  --bs-bento-rail-border: rgba(0, 200, 255, 0.25);
  --bs-bento-rail-border-hover: rgba(0, 200, 255, 0.5);

  /* Button dimensions */
  --bs-bento-rail-btn-size: 40px;
  --bs-bento-rail-btn-radius: 8px;

  /* Opacity states */
  --bs-bento-rail-idle-opacity: 0.55;
  --bs-bento-rail-hover-opacity: 1;

  /* Chromatic boost for idle state */
  --bs-bento-rail-chroma-boost: 1.15;

  /* Label styling */
  --bs-bento-rail-label-bg: rgba(20, 20, 22, 0.95);
  --bs-bento-rail-label-border: rgba(0, 200, 255, 0.4);
  --bs-bento-rail-label-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);

  /* Transitions */
  --bs-bento-rail-transition-fast: 100ms;
  --bs-bento-rail-transition-normal: 150ms;
}

/* ============================================================================
   RAIL CONTAINER
   ============================================================================ */

.bento-rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: auto;
  height: 100%;
  padding: var(--bs-bento-rail-padding);
  background: var(--bs-bento-rail-bg);
  border: 1px solid var(--bs-bento-rail-border);
  border-radius: var(--bs-bento-rail-radius);
  box-shadow: inset 0 0 16px rgba(0, 200, 255, 0.03);
  transition: border-color var(--bs-bento-rail-transition-normal) ease;
  position: relative;
  z-index: 50;
  flex-shrink: 0;
}

.bento-rail--right {
  margin-left: 0;
}

.bento-rail:hover {
  border-color: var(--bs-bento-rail-border-hover);
}

/* ============================================================================
   BUTTON STACK
   ============================================================================ */

.bento-rail__stack {
  display: flex;
  flex-direction: column;
  gap: var(--bs-bento-rail-gap);
  width: 100%;
  height: 100%;
  align-items: center;
}

.bento-rail__btn-wrapper {
  flex: 1 1 0;
  display: flex;
  align-items: stretch;
  position: relative;
}

/* ============================================================================
   TOOL BUTTON
   ============================================================================ */

.bento-rail__btn {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 32px; /* Prevent buttons from becoming unusably small in short viewports */
  aspect-ratio: 1;
  background: rgba(40, 40, 44, 0.7);
  border: 1px solid rgba(0, 200, 255, 0.2);
  border-radius: var(--bs-bento-rail-btn-radius);
  color: rgba(180, 190, 200, 0.9);
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  position: relative;
  transition:
    opacity var(--bs-bento-rail-transition-fast) ease,
    background var(--bs-bento-rail-transition-fast) ease,
    border-color var(--bs-bento-rail-transition-fast) ease,
    box-shadow var(--bs-bento-rail-transition-fast) ease,
    transform var(--bs-bento-rail-transition-fast) ease;
  -webkit-user-select: none;
  user-select: none;
}

/* Idle state */
.bento-rail__btn--idle {
  opacity: var(--bs-bento-rail-idle-opacity);
  filter: saturate(var(--bs-bento-rail-chroma-boost));
}

/* Hover state */
.bento-rail__btn--hover,
.bento-rail__btn:hover {
  opacity: var(--bs-bento-rail-hover-opacity);
  background: rgba(40, 40, 44, 0.95);
  border-color: rgba(0, 200, 255, 0.5);
  box-shadow:
    0 0 8px rgba(0, 200, 255, 0.15),
    inset 0 0 8px rgba(0, 200, 255, 0.05);
  color: rgba(230, 240, 250, 1);
  transform: scale(1.02);
}

/* Active/pressed */
.bento-rail__btn:active {
  transform: scale(0.98);
  background: rgba(40, 40, 44, 1);
}

/* Focus */
.bento-rail__btn:focus-visible {
  outline: 2px solid rgba(0, 200, 255, 0.7);
  outline-offset: 2px;
}

/* Assigned indicator */
.bento-rail__btn--assigned::after {
  content: '';
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 6px;
  height: 6px;
  background: rgba(0, 200, 255, 0.8);
  border-radius: 50%;
}

/* ============================================================================
   HOVER-REVEAL LABEL
   ============================================================================ */

.bento-rail__label {
  position: absolute;
  right: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  padding: 6px 12px;
  background: var(--bs-bento-rail-label-bg);
  border: 1px solid var(--bs-bento-rail-label-border);
  border-radius: 6px;
  box-shadow: var(--bs-bento-rail-label-shadow);
  color: rgba(230, 240, 250, 1);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  z-index: 100;

  /* Animation */
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--bs-bento-rail-transition-fast) ease,
    visibility var(--bs-bento-rail-transition-fast) ease,
    transform var(--bs-bento-rail-transition-fast) ease;
  transform: translateY(-50%) translateX(4px);
}

.bento-rail__label--visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(-50%) translateX(0);
}

/* Arrow pointing right */
.bento-rail__label::after {
  content: '';
  position: absolute;
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent;
  border-left-color: var(--bs-bento-rail-label-border);
}

.bento-rail__label::before {
  content: '';
  position: absolute;
  right: -5px;
  top: 50%;
  transform: translateY(-50%);
  border: 4px solid transparent;
  border-left-color: var(--bs-bento-rail-label-bg);
  z-index: 1;
}

/* ============================================================================
   INLINE EDITOR
   ============================================================================ */

.bento-rail__editor {
  position: fixed;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--bs-bento-rail-label-bg);
  border: 1px solid var(--bs-bento-rail-label-border);
  border-radius: 8px;
  box-shadow: var(--bs-bento-rail-label-shadow);
  z-index: 200;
  min-width: 180px;
}

.bento-rail__editor-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: rgba(180, 190, 200, 0.8);
  margin: 0 0 4px 0;
}

.bento-rail__editor-input {
  width: 100%;
  padding: 8px 10px;
  background: rgba(20, 20, 22, 0.8);
  border: 1px solid rgba(0, 200, 255, 0.3);
  border-radius: 4px;
  color: rgba(230, 240, 250, 1);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}

.bento-rail__editor-input:focus {
  border-color: rgba(0, 200, 255, 0.6);
}

.bento-rail__editor-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.bento-rail__editor-btn {
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 4px;
  cursor: pointer;
  transition:
    background 0.15s ease,
    border-color 0.15s ease;
}

.bento-rail__editor-btn--save {
  background: rgba(0, 200, 255, 0.2);
  border: 1px solid rgba(0, 200, 255, 0.4);
  color: rgba(0, 200, 255, 1);
}

.bento-rail__editor-btn--save:hover {
  background: rgba(0, 200, 255, 0.3);
  border-color: rgba(0, 200, 255, 0.6);
}

.bento-rail__editor-btn--cancel {
  background: transparent;
  border: 1px solid rgba(180, 190, 200, 0.3);
  color: rgba(180, 190, 200, 0.8);
}

.bento-rail__editor-btn--cancel:hover {
  background: rgba(180, 190, 200, 0.1);
  border-color: rgba(180, 190, 200, 0.5);
}

/* ============================================================================
   CONTEXT MENU
   ============================================================================ */

.bento-rail__contextmenu {
  position: fixed;
  display: flex;
  flex-direction: column;
  padding: 4px 0;
  background: var(--bs-bento-rail-label-bg);
  border: 1px solid var(--bs-bento-rail-label-border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  z-index: 300;
  min-width: 160px;
}

.bento-rail__contextmenu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 13px;
  color: rgba(230, 240, 250, 0.9);
  background: transparent;
  border: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: background 0.1s ease;
}

.bento-rail__contextmenu-item:hover {
  background: rgba(0, 200, 255, 0.1);
}

.bento-rail__contextmenu-divider {
  height: 1px;
  background: rgba(0, 200, 255, 0.15);
  margin: 4px 0;
}

.bento-rail__contextmenu-item--danger {
  color: rgba(255, 100, 100, 0.9);
}

.bento-rail__contextmenu-item--danger:hover {
  background: rgba(255, 100, 100, 0.1);
}

/* ============================================================================
   OVERLAY BACKDROP
   ============================================================================ */

.bento-rail__overlay {
  position: fixed;
  inset: 0;
  z-index: 199;
  background: transparent;
}

/* ============================================================================
   RESPONSIVE
   ============================================================================ */

@media (max-width: 1280px) {
  :root {
    --bs-bento-rail-padding: 4px;
    --bs-bento-rail-gap: 6px;
  }
}

@media (max-width: 768px) {
  :root {
    --bs-bento-rail-btn-size: 36px;
    --bs-bento-rail-width: 48px;
  }
}

/* ============================================================================
   REDUCED MOTION
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
  .bento-rail__btn,
  .bento-rail__label,
  .bento-rail {
    transition: none !important;
  }
}

/* ============================================================================
   LAYOUT INTEGRATION - Separate column on right edge, outside canvas
   ============================================================================ */

/* Override app-layout grid: LEFT BENTO | CANVAS | RAIL */
.app-layout.has-right-rail {
  grid-template-columns: minmax(320px, 18%) 1fr auto !important;
}

/* Force canvas to stay in column 2 only (not span to 3) */
.app-layout.has-right-rail .canvas-panel {
  grid-column: 2 !important;
}

.right-rail-section {
  grid-column: 3;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: stretch;
  height: 100%;
  min-height: 0;
  padding: 0;
  overflow: visible;
  /* Match app-shell background to visually separate from canvas */
  background: var(--surface-bg, #0a0a0a);
  border-left: 1px solid var(--border-default, rgba(0, 200, 255, 0.15));
}

/* Hide on mobile */
@media (max-width: 768px) {
  .right-rail-section {
    display: none;
  }
}

/* ============================================================================
   HUD PREVIEW BUTTON - 2x2 grid of mini HUD thumbnails
   ============================================================================ */

.bento-rail__btn--hud-grid {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 3px;
  background: rgba(20, 25, 30, 0.9);
}

.bento-rail__hud-mini {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  flex: 1;
  background: rgba(0, 20, 30, 0.7);
  border: 1px solid rgba(0, 200, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
  padding: 0 3px;
  font-size: 6px;
  line-height: 1;
  color: rgba(0, 200, 255, 0.8);
}

.bento-rail__hud-mini-label {
  font-size: 5px;
  text-transform: uppercase;
  letter-spacing: 0.2px;
  color: rgba(0, 255, 100, 0.8);
}

.bento-rail__hud-mini-value {
  font-size: 5px;
  font-weight: 600;
  color: rgba(0, 200, 255, 1);
  font-family: 'Roboto Mono', monospace;
}

/* Active state when HUDs are visible */
.bento-rail__btn--hud-grid.hud-active {
  border-color: rgba(0, 255, 100, 0.6);
  box-shadow: 0 0 8px rgba(0, 255, 100, 0.3);
}

.bento-rail__btn--hud-grid.hud-active .bento-rail__hud-mini {
  border-color: rgba(0, 255, 100, 0.4);
}

/* ============================================================================
   BLOCK BUTTON IDENTIFIER - Reusable stylized letter for block-style buttons
   Usage: Add .bento-rail__btn-id with a single letter inside the button
   The letter fills the button with ~20% padding, subdued appearance
   ============================================================================ */

.bento-rail__btn-id {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: 'Roboto Mono', 'SF Mono', monospace;
  font-weight: 900;
  font-size: 52px;
  line-height: 1;
  color: rgba(0, 200, 255, 0.18);
  text-transform: uppercase;
  pointer-events: none;
  -webkit-user-select: none;
  user-select: none;
  z-index: 0;
}

/* Active state - slightly brighter */
.bento-rail__btn.hud-active .bento-rail__btn-id,
.bento-rail__btn--assigned .bento-rail__btn-id {
  color: rgba(0, 200, 255, 0.25);
}
