/* ============================================
   THE SWITCH — World-class Skeuomorphic Toggle

   Pure CSS. No images. No SVG filters.
   Techniques: 10-layer shadow stacks, 4-layer
   brushed metal, radial specular hotspot,
   flathead screws, indicator LED, warm/cool
   atmosphere via CSS custom properties,
   linear() spring easing, micro-interactions.
   ============================================ */

/* ============ Spring easing ============ */
:root {
  --spring-easing: linear(
    0, 0.006, 0.025 2.8%, 0.101 6.1%, 0.539 18.9%,
    0.721 25.3%, 0.849 31.5%, 0.937 38.1%,
    0.968 41.8%, 0.991 45.7%, 1.006 50.1%,
    1.015 55%, 1.017 63.9%, 1.001 85.6%, 1
  );
  --spring-duration: 450ms;
}

/* ============ Atmosphere tokens (dim = OFF) ============ */
.switch-container {
  /* Metal palette — dim room, cool steel in low light */
  --metal-base: linear-gradient(172deg,
    #7A7A80 0%, #6E6E76 15%, #60606A 35%,
    #565660 50%, #5E5E68 65%, #6A6A72 80%, #76767E 100%);

  /* Track interior */
  --track-bg: linear-gradient(to bottom, #0A0A0A 0%, #060606 40%, #040404 60%, #080808 100%);
  --track-glow: transparent;

  /* Lever surface — muted in low light */
  --lever-base: linear-gradient(168deg,
    #9A9A9A 0%, #8C8C8C 15%, #7E7E7E 30%,
    #727272 50%, #7A7A7A 65%, #888888 80%, #949494 100%);

  /* Specular intensity — dim when OFF */
  --specular-opacity: 0.4;

  /* Screw brightness */
  --screw-hi: #8A8A8A;
  --screw-mid: #6A6A6A;
  --screw-lo: #4A4A4A;
  --screw-dk: #3A3A3A;
  --screw-dkr: #303030;

  /* Indicator LED */
  --led-color: #2A2520;
  --led-shadow: 0 0 0 transparent;
  --led-opacity: 0.3;
}

/* ============ Warm atmosphere — light is ON ============ */
.switch-container.on {
  /* Metal brightens and warms — light just came on */
  --metal-base: linear-gradient(172deg,
    #D8D4CC 0%, #C6C0B6 15%, #B0AAA0 35%,
    #A09A90 50%, #AAA49A 65%, #BAB4AA 80%, #CCC8BE 100%);
  --track-bg: linear-gradient(to bottom, #12100E 0%, #0C0A08 40%, #080604 60%, #100E0C 100%);
  --track-glow: rgba(255, 150, 50, 0.07);
  /* Lever brightens with warm tint */
  --lever-base: linear-gradient(168deg,
    #F2EDE6 0%, #DED8CE 15%, #C8C2B8 30%,
    #B4AEA4 50%, #BEB8AE 65%, #D2CCC2 80%, #E6E0D6 100%);
  /* Full specular when lit */
  --specular-opacity: 1;
  /* Screws brighten */
  --screw-hi: #E8E8E8;
  --screw-mid: #C8C8C8;
  --screw-lo: #A0A0A0;
  --screw-dk: #787878;
  --screw-dkr: #606060;
  /* LED glows warm */
  --led-color: #FFB044;
  --led-shadow: 0 0 4px 1px rgba(255, 176, 68, 0.8), 0 0 14px 3px rgba(255, 140, 40, 0.35);
  --led-opacity: 1;
}

/* ============ Container ============ */

.switch-container {
  position: relative;
  perspective: 800px;
  cursor: pointer;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.switch-container:focus-visible .switch-plate {
  outline: 2px solid rgba(255, 179, 71, 0.5);
  outline-offset: 12px;
  border-radius: 14px;
}

/* ============ Faceplate ============ */

.switch-plate {
  position: relative;
  width: 170px;
  height: 280px;
  border-radius: 12px;

  /* 4-layer brushed metal texture */
  background:
    /* Ultra-fine brush lines — tight periodicity, high contrast */
    repeating-linear-gradient(90deg,
      transparent 0px, transparent 1px,
      rgba(255,255,255,0.13) 1px, rgba(255,255,255,0.13) 1.3px,
      transparent 1.3px, transparent 2.2px),
    /* Medium brush strokes — offset periodicity */
    repeating-linear-gradient(90deg,
      transparent 0px, transparent 3.5px,
      rgba(0,0,0,0.06) 3.5px, rgba(0,0,0,0.06) 4px,
      transparent 4px, transparent 7px),
    /* Wide strokes — slow modulation */
    repeating-linear-gradient(90deg,
      transparent 0px, transparent 11px,
      rgba(255,255,255,0.06) 11px, rgba(255,255,255,0.06) 14px),
    /* Diagonal micro-scratch */
    repeating-linear-gradient(92deg,
      transparent 0px, transparent 5px,
      rgba(0,0,0,0.025) 5px, rgba(0,0,0,0.025) 5.4px),
    /* Base metal gradient (warm/cool via variable) */
    var(--metal-base);

  /* 10-layer shadow stack — subdued bevels for dim room */
  box-shadow:
    0 0.5px 1px rgba(0, 0, 0, 0.4),
    0 1px 2px 0.5px rgba(0, 0, 0, 0.28),
    0 3px 6px rgba(0, 0, 0, 0.22),
    0 8px 16px rgba(0, 0, 0, 0.16),
    0 16px 32px rgba(0, 0, 0, 0.10),
    0 24px 48px rgba(0, 0, 0, 0.06),
    /* Bevels — dimmed (0.20 instead of 0.55) */
    inset 0 1.5px 0 rgba(255, 255, 255, 0.20),
    inset 0 -1px 0 rgba(0, 0, 0, 0.22),
    inset 1px 0 0 rgba(255, 255, 255, 0.06),
    inset -1px 0 0 rgba(0, 0, 0, 0.10);

  transform: rotateX(2deg);
  transform-style: preserve-3d;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.5s ease;
}

/* Specular highlight overlay — dims in dark mode */
.switch-plate::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    /* Focused specular hotspot — simulates ceiling light reflection */
    radial-gradient(
      ellipse 40% 20% at 45% 12%,
      rgba(255, 255, 255, 0.25) 0%,
      transparent 100%
    ),
    /* Broad directional wash */
    linear-gradient(
      170deg,
      rgba(255, 255, 255, 0.22) 0%,
      rgba(255, 255, 255, 0.06) 25%,
      transparent 50%,
      rgba(0, 0, 0, 0.03) 75%,
      rgba(0, 0, 0, 0.08) 100%
    );
  mix-blend-mode: overlay;
  opacity: var(--specular-opacity);
  transition: opacity 0.5s ease;
  pointer-events: none;
  z-index: 5;
}

/* Hover — subtle lift */
.switch-container:hover .switch-plate {
  box-shadow:
    0 0.5px 1px rgba(0, 0, 0, 0.4),
    0 1px 2px 0.5px rgba(0, 0, 0, 0.28),
    0 4px 8px rgba(0, 0, 0, 0.24),
    0 10px 20px rgba(0, 0, 0, 0.18),
    0 18px 36px rgba(0, 0, 0, 0.12),
    0 28px 56px rgba(0, 0, 0, 0.07),
    inset 0 1.5px 0 rgba(255, 255, 255, 0.60),
    inset 0 -1px 0 rgba(0, 0, 0, 0.22),
    inset 1px 0 0 rgba(255, 255, 255, 0.12),
    inset -1px 0 0 rgba(0, 0, 0, 0.10);
  transform: rotateX(2deg) translateY(-1px);
}

/* Press — realistic depression toward wall */
.switch-container:active .switch-plate {
  box-shadow:
    0 0.3px 0.5px rgba(0, 0, 0, 0.45),
    0 0.5px 1px 0.5px rgba(0, 0, 0, 0.32),
    0 2px 4px rgba(0, 0, 0, 0.24),
    0 4px 8px rgba(0, 0, 0, 0.16),
    0 8px 16px rgba(0, 0, 0, 0.08),
    0 12px 24px rgba(0, 0, 0, 0.04),
    inset 0 1.5px 0 rgba(255, 255, 255, 0.48),
    inset 0 -1px 0 rgba(0, 0, 0, 0.22),
    inset 1px 0 0 rgba(255, 255, 255, 0.12),
    inset -1px 0 0 rgba(0, 0, 0, 0.10);
  transform: rotateX(2deg) scale(0.99) translateY(0.5px);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

/* ============ Screws — Flathead slot ============ */

.switch-plate-screws {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
}

.screw {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  /* Directional light — brightness via variables */
  background:
    radial-gradient(circle at 35% 30%,
      var(--screw-hi) 0%, var(--screw-mid) 15%, var(--screw-lo) 40%,
      var(--screw-dk) 65%, var(--screw-dkr) 85%, var(--screw-dkr) 100%);
  box-shadow:
    /* Countersunk recess ring */
    0 0 0 1.5px rgba(0, 0, 0, 0.28),
    0 0 0 2.5px rgba(0, 0, 0, 0.08),
    /* Inner bevel */
    inset 0 1.5px 1px rgba(255, 255, 255, 0.50),
    inset 0 -1px 2px rgba(0, 0, 0, 0.35),
    /* Drop shadow */
    0 1px 3px rgba(0, 0, 0, 0.45);
  transition: background 0.5s ease;
}

/* Single flathead slot */
.screw::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 12px;
  height: 2px;
  border-radius: 0.5px;
  background: linear-gradient(to bottom,
    rgba(0, 0, 0, 0.4) 0%,
    rgba(0, 0, 0, 0.2) 50%,
    rgba(0, 0, 0, 0.45) 100%);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.22),
    inset 0 0.5px 0 rgba(0, 0, 0, 0.15);
  transform: translate(-50%, -50%) rotate(var(--screw-angle, 30deg));
}

.screw::after { display: none; }

/* Unique rotation per screw */
.screw-tl { top: 18px; left: 18px; --screw-angle: 25deg; }
.screw-tr { top: 18px; right: 18px; --screw-angle: -12deg; }
.screw-bl { bottom: 18px; left: 18px; --screw-angle: 55deg; }
.screw-br { bottom: 18px; right: 18px; --screw-angle: 8deg; }

/* ============ Indicator LED ============ */

.switch-indicator {
  position: absolute;
  top: 44px;
  left: 50%;
  transform: translateX(-50%);
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--led-color);
  opacity: var(--led-opacity);
  box-shadow:
    /* Recessed socket */
    inset 0 1px 2px rgba(0, 0, 0, 0.5),
    0 0.5px 0 rgba(255, 255, 255, 0.15),
    /* Glow (none OFF, warm ON) */
    var(--led-shadow);
  transition:
    background 0.4s ease,
    opacity 0.4s ease,
    box-shadow 0.4s ease;
  z-index: 4;
}

/* ============ Switch Body (recessed track) ============ */

.switch-body {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 66px;
  height: 136px;
  background: var(--track-bg);
  border-radius: 33px;
  /* Deep cavity shadows */
  box-shadow:
    /* Inner depth — the primary hole */
    inset 0 6px 14px rgba(0, 0, 0, 0.95),
    inset 0 -3px 8px rgba(0, 0, 0, 0.5),
    /* Side walls */
    inset 3px 0 6px rgba(0, 0, 0, 0.5),
    inset -3px 0 6px rgba(0, 0, 0, 0.5),
    /* Deep ambient occlusion */
    inset 0 0 24px 4px rgba(0, 0, 0, 0.6),
    /* Chamfered lip bevel */
    inset 0 2px 0 rgba(0, 0, 0, 0.7),
    /* Warm interior glow (ON state via variable) */
    inset 0 0 20px 2px var(--track-glow),
    /* Outer lip highlight */
    0 1.5px 0 rgba(255, 255, 255, 0.15),
    0 -0.5px 0 rgba(0, 0, 0, 0.10),
    /* Countersunk edge ring */
    0 0 0 1px rgba(0, 0, 0, 0.14);
  transition: box-shadow 0.4s ease;
  z-index: 1;
}

.switch-lever-track {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: inherit;
}

/* ============ The Lever ============ */

.switch-lever {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 58px;
  height: 66px;
  border-radius: 29px;
  /* OFF position: lever at bottom */
  bottom: 3px;

  /* Lever material with micro-texture */
  background:
    repeating-linear-gradient(90deg,
      transparent 0px, transparent 1.5px,
      rgba(255,255,255,0.05) 1.5px, rgba(255,255,255,0.05) 1.8px),
    var(--lever-base);

  /* 9-layer shadow stack — dimmed highlights for OFF */
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.55),
    0 1px 3px rgba(0, 0, 0, 0.35),
    0 8px 20px rgba(0, 0, 0, 0.25),
    /* Top bevel — subdued in dim light */
    inset 0 3px 2px rgba(255, 255, 255, 0.30),
    inset 0 -3px 4px rgba(0, 0, 0, 0.20),
    inset 3px 0 3px rgba(255, 255, 255, 0.10),
    inset -3px 0 3px rgba(0, 0, 0, 0.12),
    0 0 0 transparent,
    0 0 0 transparent;

  transition:
    bottom var(--spring-duration) var(--spring-easing),
    top var(--spring-duration) var(--spring-easing),
    box-shadow 0.4s ease,
    background 0.4s ease;
  cursor: pointer;
  z-index: 2;
}

/* Specular dome highlight — dims with the room */
.switch-lever::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    /* Dome specular spot */
    radial-gradient(
      ellipse 60% 40% at 42% 25%,
      rgba(255, 255, 255, 0.45) 0%,
      rgba(255, 255, 255, 0.12) 50%,
      transparent 100%
    ),
    /* Directional wash */
    linear-gradient(
      175deg,
      rgba(255, 255, 255, 0.38) 0%,
      rgba(255, 255, 255, 0.10) 30%,
      transparent 50%,
      rgba(0, 0, 0, 0.05) 70%,
      rgba(0, 0, 0, 0.14) 100%
    );
  mix-blend-mode: overlay;
  opacity: var(--specular-opacity);
  transition: opacity 0.5s ease;
  pointer-events: none;
}

/* Grip ridges — prominent tactile lines */
.lever-grip {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 26px;
  height: 22px;
  pointer-events: none;
}

.lever-grip::before {
  content: '';
  display: block;
  width: 100%;
  height: 2px;
  border-radius: 1px;
  background: rgba(0, 0, 0, 0.13);
  box-shadow:
    /* Highlight below each dark ridge */
    0 1px 0 rgba(255, 255, 255, 0.32),
    /* Ridge 2 */
    0 5px 0 rgba(0, 0, 0, 0.13),
    0 6px 0 rgba(255, 255, 255, 0.32),
    /* Ridge 3 */
    0 10px 0 rgba(0, 0, 0, 0.13),
    0 11px 0 rgba(255, 255, 255, 0.32),
    /* Ridge 4 */
    0 15px 0 rgba(0, 0, 0, 0.13),
    0 16px 0 rgba(255, 255, 255, 0.32),
    /* Ridge 5 */
    0 20px 0 rgba(0, 0, 0, 0.13),
    0 21px 0 rgba(255, 255, 255, 0.32);
}

.lever-grip::after { display: none; }

/* ============ ON State ============ */

.switch-container.on .switch-lever {
  bottom: auto;
  top: 3px;
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.55),
    0 1px 3px rgba(0, 0, 0, 0.35),
    0 8px 20px rgba(0, 0, 0, 0.25),
    /* Full-brightness bevels — light is on */
    inset 0 3px 2px rgba(255, 255, 255, 0.70),
    inset 0 -3px 4px rgba(0, 0, 0, 0.20),
    inset 3px 0 3px rgba(255, 255, 255, 0.22),
    inset -3px 0 3px rgba(0, 0, 0, 0.12),
    /* Warm glow halo */
    0 0 24px rgba(255, 160, 60, 0.16),
    0 0 48px rgba(255, 160, 60, 0.08);
}

/* ============ Glow Ring ============ */

.switch-glow-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 110px;
  height: 170px;
  border-radius: 55px;
  opacity: 0;
  background: radial-gradient(
    ellipse at center,
    rgba(255, 140, 66, 0.25) 0%,
    rgba(255, 179, 71, 0.10) 40%,
    transparent 70%
  );
  transition: opacity var(--glow-duration) ease;
  pointer-events: none;
  z-index: 0;
}

.switch-container.on .switch-glow-ring {
  opacity: 1;
}

/* Plate brightens when light is on */
.switch-container.on .switch-plate {
  box-shadow:
    0 0.5px 1px rgba(0, 0, 0, 0.4),
    0 1px 2px 0.5px rgba(0, 0, 0, 0.28),
    0 3px 6px rgba(0, 0, 0, 0.22),
    0 8px 16px rgba(0, 0, 0, 0.16),
    0 16px 32px rgba(0, 0, 0, 0.10),
    0 24px 48px rgba(0, 0, 0, 0.06),
    /* Full-brightness bevels */
    inset 0 1.5px 0 rgba(255, 255, 255, 0.55),
    inset 0 -1px 0 rgba(0, 0, 0, 0.22),
    inset 1px 0 0 rgba(255, 255, 255, 0.12),
    inset -1px 0 0 rgba(0, 0, 0, 0.10);
}

/* ============ Toggle Animation ============ */

/* Multi-phase snap: resistance → overshoot → bounce → settle */
@keyframes switch-snap {
  0%   { transform: translateX(-50%) translateY(0); }
  25%  { transform: translateX(-50%) translateY(-2px); }
  50%  { transform: translateX(-50%) translateY(3px); }
  70%  { transform: translateX(-50%) translateY(-1px); }
  85%  { transform: translateX(-50%) translateY(0.5px); }
  100% { transform: translateX(-50%) translateY(0); }
}

.switch-container.thunk .switch-lever {
  animation: switch-snap 250ms ease-out;
}

/* Screen shake */
@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(0.5px, 0.3px); }
  40% { transform: translate(-0.3px, -0.5px); }
  60% { transform: translate(0.3px, -0.2px); }
  80% { transform: translate(-0.2px, 0.3px); }
}

.app.shake {
  animation: shake 120ms ease-out;
}

/* ============ Cooldown ============ */

.switch-container.on-cooldown {
  cursor: not-allowed;
}

.switch-container.on-cooldown .switch-lever {
  cursor: not-allowed;
  filter: brightness(0.95);
}

.cooldown-indicator {
  position: relative;
  width: 48px;
  height: 48px;
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  margin-top: -16px;
  margin-bottom: -16px;
}

.cooldown-indicator.active {
  opacity: 1;
  transform: scale(1);
}

.cooldown-ring {
  width: 48px;
  height: 48px;
  transform: rotate(-90deg);
}

.cooldown-ring-bg {
  fill: none;
  stroke: var(--card-border, rgba(255, 255, 255, 0.06));
  stroke-width: 2.5;
  transition: stroke var(--glow-duration) ease;
}

.cooldown-ring-fill {
  fill: none;
  stroke: var(--text-secondary, #555);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 125.66;
  stroke-dashoffset: 125.66;
  transition: stroke var(--glow-duration) ease;
}

.cooldown-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-counter, 'iA Writer Mono', monospace);
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-secondary, #555);
  transition: color var(--glow-duration) ease;
}

/* ============ Reduced Motion ============ */

@media (prefers-reduced-motion: reduce) {
  .switch-lever {
    transition-duration: 0ms !important;
  }
  .switch-glow-ring,
  .ambient-glow {
    transition-duration: 0ms;
  }
  .app.shake {
    animation: none;
  }
  .switch-container.thunk .switch-lever {
    animation: none;
  }
}
