/* Studies directory desk mock — square folders, drag-tab, feature glitch */

html:has(.studiesDesk),
body:has(.studiesDesk) {
  height: 100%;
}

body:has(.studiesDesk) {
  overflow: hidden;
}

.studiesDesk {
  position: relative;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  user-select: none;

  /* ~4× previous desktop size, fluid down for smaller viewports */
  --folder-w: clamp(176px, 24vw, 360px);
  --tab-w: max(3.6rem, calc(var(--folder-w) * 0.28));
  --tab-h: max(1.15rem, calc(var(--folder-w) * 0.11));
  --tab-overlap: clamp(3px, 0.35vw, 6px);
  /* Face stays a full square; tab protrudes above and overlaps in front */
  --folder-h: calc(var(--folder-w) + var(--tab-h) - var(--tab-overlap));
  --icon-w: calc(var(--folder-w) + 1.25rem);
  --folder-pad: clamp(5px, 0.7vw, 12px);
  --folder-border: clamp(2px, 0.28vw, 4px);
  --label-size: clamp(13px, 1.5vw, 18px);
  --icon-gap: clamp(12px, 1.4vw, 18px);
  --handle-h: calc(var(--tab-h) + var(--tab-overlap) + 4px);
  --drop: clamp(4px, 0.45vw, 10px);
  --outline-pad: clamp(6px, 0.7vw, 12px);
}

.studiesFeature {
  position: absolute;
  inset: 0;
  z-index: 0;
  background: var(--bg);
  pointer-events: none;
  overflow: hidden;
}

.studiesFeatureLayer {
  position: absolute;
  inset: -8%;
  width: 116%;
  height: 116%;
  object-fit: cover;
  object-position: var(--crop-x, 50%) var(--crop-y, 50%);
  opacity: 0;
  transition: opacity 0.05s steps(1, end);
  will-change: transform, filter, object-position;
}

.studiesFeatureLayer.is-active {
  opacity: 1;
}

.studiesFeature.is-glitching .studiesFeatureLayer.is-active {
  animation: featureGlitch 0.28s steps(2, end);
}

@keyframes featureGlitch {
  0%,
  100% {
    transform: none;
    filter: none;
    clip-path: none;
  }
  18% {
    transform: translate(10px, -4px) skewX(-1.5deg);
    filter: contrast(1.85) saturate(1.4);
    clip-path: inset(8% 0 18% 0);
  }
  42% {
    transform: translate(-12px, 6px) skewX(2deg);
    filter: contrast(1.55) hue-rotate(18deg);
    clip-path: inset(0 12% 6% 4%);
  }
  68% {
    transform: translate(5px, 2px);
    filter: contrast(1.35);
    clip-path: inset(14% 2% 0 10%);
  }
}

.studiesFeatureScrim {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.28) 0%, rgba(0, 0, 0, 0.08) 50%, rgba(0, 0, 0, 0.22) 100%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.12) 0%, rgba(0, 0, 0, 0.04) 45%, rgba(0, 0, 0, 0.35) 100%);
}

.studiesFeatureGrain {
  position: absolute;
  inset: 0;
  opacity: 0.12;
  background-image: repeating-linear-gradient(
    0deg,
    rgba(255, 255, 255, 0.05) 0,
    rgba(255, 255, 255, 0.05) 1px,
    transparent 1px,
    transparent 3px
  );
  mix-blend-mode: overlay;
}

.studiesFlash {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--text);
  opacity: 0;
  pointer-events: none;
  mix-blend-mode: difference;
}

.studiesDeskGrid {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
  opacity: 0.72;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: clamp(28px, 4vw, 42px) clamp(28px, 4vw, 42px);
}

.studiesDesk.has-feature .studiesDeskGrid {
  opacity: 0.45;
}

.studiesFlash.is-active {
  animation: studiesFlash 0.42s steps(2, end);
}

@keyframes studiesFlash {
  0% {
    opacity: 0;
  }
  18% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.studiesDeskIcons {
  position: absolute;
  inset: 0;
  z-index: 20;
}

/* Desk intro — shared with study desks */
.studyIntroJump {
  display: none;
}

.studyDeskIntro {
  display: none;
}

@media (min-width: 641px) {
  .studyDeskIntro {
    display: block;
    position: fixed;
    left: 18px;
    top: 18px;
    z-index: 25;
    max-width: min(36ch, calc(100vw - 420px));
    margin: 0;
    padding: 0;
    color: var(--dim);
    font-size: 12px;
    line-height: 1.65;
    pointer-events: none;
  }

  .studyDeskIntroTitle {
    margin: 0 0 12px;
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--fg);
    line-height: 1.25;
  }

  .studyDeskIntro p {
    margin: 0 0 12px;
  }

  .studyDeskIntroLinks {
    margin: 0;
    font-size: 11px;
    letter-spacing: 0.02em;
  }

  .studyDeskIntro a {
    color: var(--green);
    pointer-events: auto;
    text-decoration: none;
  }

  .studyDeskIntro a:hover {
    text-decoration: underline;
  }
}

.studyIcon {
  position: absolute;
  left: var(--icon-x, 20%);
  top: var(--icon-y, 30%);
  width: var(--icon-w);
  z-index: 1;
  touch-action: none;
}

.studyIcon.is-dragging {
  z-index: 30;
  cursor: grabbing;
}

.studyIconLink {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--icon-gap);
  color: inherit;
  text-decoration: none;
}

.studyIconLink:hover {
  text-decoration: none;
}

.studyIconLink:hover .studyFolder,
.studyIconLink:focus-visible .studyFolder,
.studyIcon.is-preview .studyFolder {
  outline: none;
  transform: translateY(-10px) scale(1.03);
  filter:
    brightness(1.04)
    drop-shadow(0 14px 10px rgba(0, 0, 0, 0.55))
    drop-shadow(4px 4px 0 rgba(0, 0, 0, 0.35));
}

.studyIconLink:hover .studyIconLabel,
.studyIconLink:focus-visible .studyIconLabel,
.studyIcon.is-preview .studyIconLabel {
  text-decoration: none;
}

.studyIconLink:focus-visible {
  outline: none;
}

.studyIcon.is-dragging .studyFolder {
  outline: none;
  transform: translateY(-14px) scale(1.04);
  filter:
    brightness(1.06)
    drop-shadow(0 18px 14px rgba(0, 0, 0, 0.6))
    drop-shadow(5px 5px 0 rgba(0, 0, 0, 0.4));
}

.studyIconLink:active .studyFolder {
  transform: translateY(-4px) scale(1.01);
}

.studyFolder {
  position: relative;
  display: block;
  width: var(--folder-w);
  height: var(--folder-h);
  filter: drop-shadow(var(--drop) var(--drop) 0 rgba(0, 0, 0, 0.45));
  transition:
    transform 0.18s ease,
    filter 0.18s ease;
  will-change: transform, filter;
}

.studyFolderTab {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--tab-w);
  height: var(--tab-h);
  padding: 0 0.45em;
  background: var(--green);
  border: var(--folder-border) solid #000;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
  color: #000;
  font-size: clamp(9px, 1.05vw, 12px);
  font-weight: 600;
  letter-spacing: 0.08em;
  line-height: 1;
  text-transform: lowercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.studyFolderFace {
  position: absolute;
  top: calc(var(--tab-h) - var(--tab-overlap));
  left: 0;
  width: var(--folder-w);
  height: var(--folder-w);
  z-index: 1;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: var(--folder-pad);
  background: var(--green);
  border: var(--folder-border) solid #000;
  overflow: hidden;
}

.studyFolderCover {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: var(--crop-x, 50%) var(--crop-y, 40%);
  border: var(--folder-border) solid #000;
  background: #111;
  transform: scale(1.35);
  transform-origin: center;
}

.studyFolderLines {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  gap: calc(var(--folder-pad) + 2px);
  padding: calc(var(--folder-pad) * 1.5);
}

.studyFolderLine {
  display: block;
  height: clamp(3px, 0.4vw, 6px);
  background: #000;
}

.studyIconCaption {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: var(--icon-w);
}

.studyIconLabel {
  color: #fff;
  font-size: var(--label-size);
  font-weight: 600;
  letter-spacing: 0.04em;
  line-height: 1.25;
  text-align: center;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.9);
}

/* Drag hit area — tab only, so the cover stays clickable underneath */
.studyIconHandle {
  appearance: none;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  width: calc(var(--tab-w) + 10px);
  height: var(--handle-h);
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: grab;
}

.studyIcon.is-dragging .studyIconHandle {
  cursor: grabbing;
}

.studyIconHandleLabel {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

html.mode-invert .studyIconLabel {
  color: #000;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55);
}

html.mode-invert .studiesFeatureScrim {
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.28) 0%, rgba(255, 255, 255, 0.08) 50%, rgba(255, 255, 255, 0.2) 100%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.12) 0%, rgba(255, 255, 255, 0.04) 45%, rgba(255, 255, 255, 0.3) 100%);
}

@media (max-width: 640px) {
  html:has(.studiesDesk),
  body:has(.studiesDesk) {
    width: 100%;
    height: 100%;
    overflow: hidden !important;
    overscroll-behavior: none;
  }

  body:has(.studiesDesk) {
    position: fixed;
    inset: 0;
  }

  .studiesDesk {
    --folder-w: 80vw;
    --icon-w: 80vw;
    --label-size: clamp(13px, 3.6vw, 16px);
    --icon-gap: 12px;
    min-height: 100dvh;
    height: 100dvh;
    overflow: hidden;
  }

  .studyIcon {
    position: relative !important;
    left: auto !important;
    top: auto !important;
    flex: 0 0 var(--icon-w);
    width: var(--icon-w);
    scroll-snap-align: center;
    scroll-snap-stop: always;
    /* Desktop drag uses touch-action:none; swipe strip needs pan-x on the icons */
    touch-action: pan-x !important;
  }

  .studyFolder {
    width: var(--folder-w);
    height: calc(var(--folder-w) + var(--tab-h) - var(--tab-overlap));
  }

  .studyFolderFace {
    width: var(--folder-w);
    height: var(--folder-w);
  }

  /* Horizontal study strip — same swipe model as in-study mobile desk */
  .studiesDeskIcons {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    align-content: center;
    justify-content: flex-start;
    gap: 18px !important;
    width: 100vw !important;
    height: 100dvh !important;
    margin: 0;
    padding: 112px 10vw 48px !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    touch-action: pan-x !important;
    scroll-snap-type: x mandatory;
    scroll-padding-inline: 10vw;
    -webkit-overflow-scrolling: touch;
  }

  .studiesDeskIcons::before,
  .studiesDeskIcons::after {
    content: "";
    flex: 0 0 1px;
  }

  .studiesDeskIcons::-webkit-scrollbar {
    height: 0;
  }

  .studyIconHandle {
    display: none;
  }

  .studyIconCaption {
    max-width: 80vw;
  }

  .studyIconLink {
    width: 100%;
    touch-action: pan-x !important;
  }

  /* Feature wallpaper stays behind the strip — never capture swipe */
  .studiesFeature,
  .studiesFeatureLayer,
  .studiesFlash {
    pointer-events: none !important;
  }

  /* Flash/glitch layer under folders so only the field jitters */
  .studiesFlash {
    z-index: 15;
  }

  /* Keep glitch on wallpaper only; icons must not transform/filter during swipe */
  .studyIcon.is-preview .studyFolder,
  .studyIconLink:hover .studyFolder,
  .studyIconLink:focus-visible .studyFolder,
  .studyIcon.is-dragging .studyFolder,
  .studyIconLink:active .studyFolder {
    transform: none;
    filter: drop-shadow(var(--drop) var(--drop) 0 rgba(0, 0, 0, 0.45));
  }

  /* Match shared 07-win95-chrome mobile app menu — only page-specific overrides below */

  /* grid/scatter are desktop desk controls — hidden on mobile swipe viewer */
  #directoryAlign,
  #directoryScatter {
    display: none !important;
  }

  body.studies-open .studiesDeskIcons,
  body.studies-open .studiesFeature,
  body.studies-open .studiesDeskGrid,
  body.studies-open .studiesFlash,
  body.studies-open .studyIntroJump,
  body.studies-open .studyDeskIntro {
    opacity: 0;
    pointer-events: none;
  }

  .studyIntroJump {
    display: block;
    position: fixed;
    top: 44px;
    left: 12px;
    z-index: 190;
    max-width: calc(100vw - 88px);
    font-size: 10px;
    line-height: 1.45;
    color: var(--green);
    text-decoration: underline;
    text-underline-offset: 2px;
    letter-spacing: 0.02em;
    text-transform: lowercase;
    pointer-events: auto;
    padding: 2px 0;
  }

  .studyIntroJump:active {
    opacity: 0.72;
  }

  .studyDeskIntro {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 0 0 var(--icon-w);
    width: var(--icon-w);
    position: relative !important;
    left: auto !important;
    top: auto !important;
    margin: 0 !important;
    padding: 20px 18px 22px;
    scroll-snap-align: start;
    scroll-snap-stop: always;
    color: var(--dim);
    font-size: 12px;
    line-height: 1.65;
    pointer-events: auto;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(0, 0, 0, 0.76);
    box-shadow: 0 18px 52px rgba(0, 0, 0, 0.62);
    flex-shrink: 0;
  }

  .studyDeskIntroTitle {
    margin: 0 0 14px;
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--fg);
    line-height: 1.25;
  }

  .studyDeskIntro p {
    margin: 0 0 12px;
  }

  body.studies-open .toggle {
    display: inline-flex;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
    left: 14px;
    right: auto;
  }

  /* Same cue language as in-study mobile desk — sits under the folder + label */
  html.mode-invert .mobileSwipeCue {
    color: #000;
  }

  html.mode-invert .mobileSwipeRail {
    border-color: rgba(0, 0, 0, 0.34);
  }

  html.mode-invert .mobileSwipeRail i {
    background: #000;
  }

  .mobileSwipeCue {
    position: fixed;
    left: 50%;
    top: auto;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 112px);
    transform: translateX(-50%);
    z-index: 72;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--green);
    pointer-events: none;
    opacity: 0.82;
  }

  body.studies-open .mobileSwipeCue {
    opacity: 0;
  }

  .mobileSwipeRail {
    position: relative;
    width: 86px;
    height: 22px;
    border: 1px solid rgba(0, 255, 0, 0.48);
    overflow: hidden;
  }

  .mobileSwipeRail::before,
  .mobileSwipeRail::after {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    line-height: 1;
    color: currentColor;
    opacity: 0.82;
  }

  .mobileSwipeRail::before {
    content: "<";
    left: 7px;
  }

  .mobileSwipeRail::after {
    content: ">";
    right: 7px;
  }

  .mobileSwipeRail i {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 6px;
    height: 6px;
    margin: -3px 0 0 -3px;
    border-radius: 999px;
    background: var(--green);
    animation: directorySwipeDot 1.8s ease-in-out infinite;
  }

  @keyframes directorySwipeDot {
    0%,
    100% {
      transform: translateX(-24px);
      opacity: 0.38;
    }
    50% {
      transform: translateX(24px);
      opacity: 1;
    }
  }
}

@media (min-width: 641px) {
  .mobileSwipeCue {
    display: none !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .studiesFeatureLayer {
    transition: none;
  }

  .studyFolder {
    transition: none;
  }

  .studyIconLink:hover .studyFolder,
  .studyIconLink:focus-visible .studyFolder,
  .studyIcon.is-preview .studyFolder,
  .studyIcon.is-dragging .studyFolder {
    transform: none;
  }

  .studiesFeature.is-glitching .studiesFeatureLayer.is-active,
  .studiesFlash.is-active {
    animation: none;
  }

  .mobileSwipeRail i {
    animation: none;
    opacity: 0.72;
    transform: none;
  }
}
