/* ==========================================================================
   SCENARIO STACK — Architectural Decomposition Interaction for Screen 01 Card 02
   «Сервисы, в которых удобно действовать»
   ========================================================================== */

/* --- Card 02 Local Socket (Idle State) --- */
#directions .service-visual {
  --service-visual-height: 347px;
  --service-key-size: 96px;
  --service-key-strip: 28px;
  --service-key-offset: 0px;
  --service-key-left-shift: -14px;
  --service-panel-inset: 28px;
  /* Reserve space below the existing panel without shrinking its contents. */
  height: calc(var(--service-visual-height) + var(--service-key-strip));
  padding-bottom: var(--service-key-strip);
  /* Clip the extended form without becoming a scrollIntoView scroll container. */
  overflow: clip;
}
.service-key-socket {
  position: absolute;
  left: calc(max(var(--service-panel-inset), (100% - 360px) / 2) + var(--service-key-offset) + var(--service-key-left-shift));
  top: calc(100% - var(--service-key-strip) + var(--service-key-offset));
  transform: translate(-50%, -50%);
  width: var(--service-key-size);
  height: var(--service-key-size);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 0;
  background: transparent;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: grab;
  color: #ffffff;
  pointer-events: auto;
}

.service-key-socket svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
  opacity: 0.25;
  transition: opacity .25s ease, transform .2s ease;
}

.service-key-socket path {
  fill: none;
  stroke: #ffffff;
  stroke-width: 22;
  stroke-linecap: butt;
  stroke-linejoin: miter;
  /* Paint ownership changes synchronously with the global key handoff. */
}

.service-key-socket:hover svg {
  transform: scale(1.05);
  opacity: 0.45;
}

.service-key-socket[data-key-painted='true'] svg {
  opacity: 1;
}

@media (max-width: 950px) {
  #directions .service-visual {
    --service-visual-height: 332px;
    --service-key-strip: 50px;
    --service-key-offset: 8px;
    --service-panel-inset: 15px;
    --service-key-left-shift: -10px;
  }
  #directions .service-composition { height: 320px; }
}
@media (max-width: 640px) {
  #directions .service-visual {
    --service-key-size: 75px;
    --service-key-strip: 40px;
  }
}

/* When active: Local Proxy Key takes the current key paint */
.service-key-socket[data-key-painted='true'] path {
  stroke: var(--service-key-paint, #f460c4);
}

.service-key-socket[data-key-paint-type='gradient'] path {
  stroke: url(#stack-data-gradient);
}

/* --- 2D Composition Container --- */
.service-composition {
  position: relative;
  z-index: 1;
  width: min(360px, 100%);
  height: 330px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
}

/* --- 3D Perspective Stage (isolated from 2D float overlay) --- */
.scenario-stack-stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  perspective: 1100px;
}

.scenario-stack-assembly {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 560ms cubic-bezier(.2,.8,.25,1);
  transform-origin: 50% 60%;
}

/* Active 3D State */
.service-composition[data-stack-state='active'] .scenario-stack-assembly {
  transform: scale(0.80) rotateX(13deg) rotateY(-10deg) rotateZ(0.5deg);
}

/* --- Common Layer Rules --- */
.scenario-stack-layer {
  position: absolute;
  inset: 0;
  border-radius: 19px 19px 0 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 450ms ease, transform 560ms cubic-bezier(.2,.8,.25,1);
  transform-style: preserve-3d;
}

.stack-layer-badge {
  position: absolute;
  top: 8px;
  right: 12px;
  font: 700 7px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .12em;
  text-transform: uppercase;
}

/* --- Layer 4: Infrastructure (Graphite) --- */
.scenario-stack-layer--infra {
  background: #1c1922;
  border: 1px solid #363042;
  box-shadow: 0 25px 60px rgba(0,0,0,0.35);
  color: #dfdbe6;
  padding: 18px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  transform: translate3d(0, 0, 0);
  z-index: 1;
}

.scenario-stack-layer--infra .stack-layer-badge {
  color: #a49cb2;
}

.stack-infra-grid {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 12px;
}

.stack-infra-node {
  display: flex;
  align-items: center;
  gap: 5px;
  background: #272230;
  border: 1px solid #433b52;
  border-radius: 7px;
  padding: 6px 9px;
  font: 600 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #eee9f4;
  white-space: nowrap;
}

.stack-infra-node .infra-dot {
  color: #8bbffc;
  font-size: 7px;
}

.stack-infra-line {
  flex: 1;
  height: 1px;
  background: #473e56;
}

.stack-infra-flow {
  font: 500 8.5px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #8f889d;
  letter-spacing: .02em;
}

.service-composition[data-stack-state='active'] .scenario-stack-layer--infra {
  opacity: 1;
  transform: translate3d(69px, 42px, 0px);
}

/* --- Layer 3: Logic --- */
.scenario-stack-layer--logic {
  background: rgba(246, 242, 250, 0.92);
  border: 1px solid #d9d0e5;
  box-shadow: 0 16px 40px rgba(45,25,65,0.12);
  color: #2b2533;
  padding: 18px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  transform: translate3d(0, 0, 0);
  z-index: 2;
}

.scenario-stack-layer--logic .stack-layer-badge {
  color: #847996;
}

.stack-logic-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 9px;
  font-weight: 600;
  color: #1f6b33;
  background: #e4f6e6;
  border: 1px solid #bce6c2;
  border-radius: 7px;
  padding: 6px 9px;
  margin-bottom: 12px;
}

.stack-logic-status .logic-status-dot {
  color: #238636;
  font-size: 8px;
}

.stack-logic-chain {
  display: flex;
  align-items: center;
  gap: 8px;
  font: 600 9px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #554c62;
}

.stack-logic-chain .logic-node {
  background: #ded5ea;
  padding: 3px 6px;
  border-radius: 5px;
}

.stack-logic-chain .logic-arrow {
  color: #a497b7;
}

.service-composition[data-stack-state='active'] .scenario-stack-layer--logic {
  opacity: 1;
  transform: translate3d(26px, 15px, 48px);
}

/* --- Layer 2: Web / Interaction --- */
.scenario-stack-layer--web {
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 12px 36px rgba(40,20,55,0.08);
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  transform: translate3d(0, 0, 0);
  z-index: 3;
}

.scenario-stack-layer--web .stack-layer-badge {
  color: #928ca0;
}

.stack-web-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px;
}

.stack-web-node {
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: #f7f4fa;
  border: 1px solid #e7e2ee;
  border-radius: 8px;
  padding: 6px 8px;
  position: relative;
  z-index: 2;
}

.stack-web-node span {
  font-size: 8.5px;
  font-weight: 700;
  color: #3b3544;
}

.stack-web-node code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 7.5px;
  color: #8b8398;
}

.service-composition[data-stack-state='active'] .scenario-stack-layer--web {
  opacity: 1;
  transform: translate3d(-17px, -10px, 95px);
}

/* --- Layer 1: Top UI Fragments --- */
.service-panel {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  border-radius: 19px 19px 0 0;
  padding: 21px 21px 0;
  box-shadow: 0 10px 60px rgba(33,20,47,.12);
  transform-style: preserve-3d;
  transition: background .4s ease, box-shadow .4s ease, transform 560ms cubic-bezier(.2,.8,.25,1);
  z-index: 4;
}

/* Extend only the blank form floor. The pastel visual owns the bottom crop;
   content, assembly geometry and the note anchor keep their existing positions. */
#directions .service-panel {
  height: calc(100% + var(--service-key-strip) + 20px);
  padding-bottom: 20px;
}

/* Active Top UI: Shell vanishes, fragments float individually */
.service-composition[data-stack-state='active'] .service-panel,
#directions .service-composition[data-stack-state='active'] .service-panel {
  transform: translate3d(-60px, -36px, 142px);
  background: transparent !important;
  box-shadow: none !important;
  border: none !important;
}

.service-ui-fragment {
  transition: transform 560ms cubic-bezier(.2,.8,.25,1), background .4s ease, box-shadow .4s ease, border-color .4s ease;
  transform-style: preserve-3d;
}

.service-composition[data-stack-state='active'] .service-ui-fragment--head {
  transform: translate3d(-10px, -8px, 15px);
}

.service-composition[data-stack-state='active'] .service-ui-fragment--title {
  transform: translate3d(-6px, -4px, 8px);
}

.service-composition[data-stack-state='active'] .service-ui-fragment--choice-1 {
  transform: translate3d(-4px, 0px, 8px);
  background: #f8eff8 !important;
  border-color: #ebceec !important;
  box-shadow: 0 10px 24px rgba(35,15,45,0.12);
}

.service-composition[data-stack-state='active'] .service-ui-fragment--choice-2 {
  transform: translate3d(0px, 4px, 0px);
  background: #ffffff !important;
  border-color: #e7e4e9 !important;
  box-shadow: 0 10px 24px rgba(35,15,45,0.08);
}

.service-composition[data-stack-state='active'] .service-ui-fragment--bottom {
  transform: translate3d(4px, 8px, 0px);
  border-top-color: transparent !important;
}

/* --- Node Depth Ordering: Controls/Text stay ABOVE data routes --- */
.stack-web-node,
.stack-logic-status,
.stack-logic-chain .logic-node,
.stack-infra-node,
.choice,
.service-next,
.service-ui-fragment--title,
.service-ui-fragment--head {
  position: relative;
  z-index: 2;
}

/* --- SVG Data Connections & Depth Guides --- */
.scenario-stack-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
  opacity: 0;
  transition: opacity 450ms ease;
  z-index: 1; /* under text/badges */
  transform: translate3d(0, 0, 0);
}

.service-composition[data-stack-state='active'] .scenario-stack-svg {
  opacity: 1;
}

/* 4 Corner Depth Guides */
.stack-guide-line {
  stroke: #c490ff;
  stroke-width: 1;
  stroke-dasharray: 2 3;
  opacity: .28;
  stroke-linecap: round;
}

/* 3 Primary Data Paths */
.stack-route-path {
  fill: none;
  stroke: url(#stack-data-gradient);
  stroke-width: 1.3;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 4 4;
  opacity: .72;
}

.service-composition[data-stack-state='active'] .stack-route-path {
  animation: stack-route-flow 3.2s linear infinite;
}

@keyframes stack-route-flow {
  to { stroke-dashoffset: -32; }
}

/* --- Floating Note: 2D Overlay outside 3D perspective --- */
/* Default Desktop (>= 1360px): Shifted right by ~50-60% of width, ~20% overlap */
#directions .service-float,
.service-float {
  position: absolute;
  z-index: 10;
  top: 68px;
  left: min(calc(100% - 150px), calc(50% + min(180px, 45%) - 28px));
  right: auto;
  width: 140px;
  max-width: none;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 40px #816c8f33;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 800;
  font-size: 9px;
  transform: rotate(6deg);
  transform-origin: 0% 50%;
  transition: left .45s cubic-bezier(.2,.8,.25,1), width .45s cubic-bezier(.2,.8,.25,1), transform .45s ease;
  pointer-events: none;
  box-sizing: border-box;
}

#directions .service-float-icon,
.service-float-icon {
  flex-shrink: 0;
}

.service-float-text {
  line-height: 1.35;
  transition: opacity .2s ease;
}

/* Active State Desktop: expand horizontally to ~2 lines, keep 2D anchor */
#directions .service-float[data-stack-active='true'],
#directions .service-visual [data-service-float][data-stack-active='true'],
.service-float[data-stack-active='true'] {
  width: 295px !important;
  max-width: calc(100% - 24px);
  font-size: 10.5px;
  line-height: 1.35;
  transform: rotate(4deg);
}

/* Medium Desktop / Tablet (1050px - 1359px): Keep 2D position stable */
@media (min-width: 1050px) and (max-width: 1359px) {
  #directions .service-float,
  .service-float {
    left: calc(100% - 62px);
    right: auto;
  }
  #directions .service-composition[data-stack-state='active'] .service-float,
  .service-composition[data-stack-state='active'] .service-float,
  .service-float[data-stack-active='true'] {
    width: 240px;
  }
}

/* Small Tablet (951px - 1049px) */
@media (min-width: 951px) and (max-width: 1049px) {
  #directions .service-float,
  .service-float {
    left: calc(100% - 116px);
    right: auto;
  }
  #directions .service-composition[data-stack-state='active'] .service-float,
  .service-composition[data-stack-state='active'] .service-float,
  .service-float[data-stack-active='true'] {
    width: 230px;
  }
}

/* Mobile (<= 950px): Position on right edge with safe padding */
@media (max-width: 950px) {
  #directions .service-float,
  .service-float {
    left: auto;
    right: 8px;
    top: 38px;
    transform-origin: 100% 50%;
  }
  #directions .service-composition[data-stack-state='active'] .service-float,
  .service-composition[data-stack-state='active'] .service-float,
  .service-float[data-stack-active='true'] {
    left: auto;
    right: 8px;
    width: 220px;
  }
}

/* --- Reduced Motion Fallback --- */
@media (prefers-reduced-motion: reduce) {
  .scenario-stack-assembly,
  .scenario-stack-layer,
  .service-ui-fragment,
  .service-float {
    transition: none !important;
  }
  .service-composition[data-stack-state='active'] .scenario-stack-assembly {
    transform: scale(0.92);
  }
  .service-composition[data-stack-state='active'] .scenario-stack-layer--infra {
    transform: translate(16px, 16px);
  }
  .service-composition[data-stack-state='active'] .scenario-stack-layer--logic {
    transform: translate(8px, 8px);
  }
  .service-composition[data-stack-state='active'] .scenario-stack-layer--web {
    transform: translate(0, 0);
  }
  .service-composition[data-stack-state='active'] .service-ui-fragment {
    transform: none;
  }
  .stack-route-path {
    animation: none !important;
    stroke-dasharray: none;
  }
}

/* One local owner for seating, sink and rise: background < key < form < note.
   The form itself occludes the key; no portal floats above the card UI. */
#directions .service-visual { isolation: isolate; }
#directions .service-glow { z-index: 0; }
#directions .service-composition { z-index: 2; }
.service-slot-layer { position: absolute; inset: 0; z-index: 1; overflow: hidden; pointer-events: none; }
.service-slot-key { position: absolute; display: block; pointer-events: none; }
.service-slot-key svg { display: block; width: 100%; height: 100%; overflow: visible; }
.service-slot-key path { fill: none; stroke: var(--service-slot-paint); stroke-width: 22; stroke-linecap: butt; }
.service-key-socket[data-key-occupied='true'] svg { opacity: 0; transition: none; }

/* The shell morphs; both content layers keep their own final text geometry.
   Restore the approved 6deg idle / 4deg active tilt from 5b852594. */
#directions .service-visual [data-service-float] {
  width: 176px;
  height: 54px;
  display: block;
  font-size: 11px;
  rotate: 6deg;
  transform: none;
  transform-origin: 0 0;
  overflow: hidden;
  transition: width 380ms cubic-bezier(.22,.7,.25,1), height 380ms cubic-bezier(.22,.7,.25,1), rotate 380ms cubic-bezier(.22,.7,.25,1), box-shadow 380ms ease;
}
#directions .service-visual [data-service-float][data-stack-active='true'] {
  width: var(--service-note-active-width, 300px) !important;
  height: 58px;
  max-width: none;
  rotate: 4deg;
  transform: none;
  font-size: 12.5px;
  font-weight: 500;
  line-height: 1.35;
}
.service-note-content { position: absolute; left: 12px; top: 12px; display: flex; align-items: center; gap: 9px; opacity: 0; transition: opacity 70ms ease; }
.service-note-idle { width: 152px; font-size: 11px; font-weight: 800; }
.service-note-active { width: calc(var(--service-note-active-width, 300px) - 24px); font-size: 12.5px; font-weight: 500; }
[data-note-view='idle'] .service-note-idle,
[data-note-view='active'] .service-note-active { opacity: 1; }
.service-note-content .service-float-text { min-width: 0; line-height: 1.35; }
.service-float-icon svg { display: block; width: 18px; height: 18px; }

/* 630ms layers + 90ms stagger = 900ms; collapse remains 590ms. */
#directions .scenario-stack-assembly { transition: transform 500ms cubic-bezier(.22,.7,.25,1); }
#directions [data-stack-state='active'] .scenario-stack-assembly { transition-duration: 630ms; }
#directions .scenario-stack-layer,
#directions .service-panel,
#directions .service-ui-fragment { transition-duration: 380ms; transition-timing-function: cubic-bezier(.22,.7,.25,1); }
#directions .scenario-stack-layer--infra { transition-delay: 0ms; }
#directions .scenario-stack-layer--logic { transition-delay: 70ms; }
#directions .scenario-stack-layer--web { transition-delay: 140ms; }
#directions .service-panel, #directions .service-ui-fragment { transition-delay: 210ms; }
#directions [data-stack-state='active'] .service-panel,
#directions [data-stack-state='active'] .service-ui-fragment { transition-delay: 0ms; transition-duration: 630ms; }
#directions [data-stack-state='active'] .scenario-stack-layer--web { transition-delay: 90ms; transition-duration: 630ms; }
#directions [data-stack-state='active'] .scenario-stack-layer--logic { transition-delay: 180ms; transition-duration: 630ms; }
#directions [data-stack-state='active'] .scenario-stack-layer--infra { transition-delay: 270ms; transition-duration: 630ms; }
@media (prefers-reduced-motion: reduce) {
  #directions .scenario-stack-assembly, #directions .scenario-stack-layer,
  #directions .service-panel, #directions .service-ui-fragment { transition-duration: 0ms; transition-delay: 0ms; }
}

@media (max-width: 950px) {
  #directions .service-composition[data-stack-state='active'] .service-panel { transform: translate3d(-54px,-36px,142px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--web { transform: translate3d(-18px,-10px,95px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--logic { transform: translate3d(22px,15px,48px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--infra { transform: translate3d(62px,42px,0); }
}
@media (prefers-reduced-motion: reduce) {
  #directions .service-visual [data-service-float], .service-note-content { transition: none; }
  #directions .service-composition[data-stack-state='active'] .service-panel { transform: translate3d(-54px,-36px,142px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--infra { transform: translate(16px,16px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--logic { transform: translate(8px,8px); }
  #directions .service-composition[data-stack-state='active'] .scenario-stack-layer--web { transform: none; }
}
