#workspace {
  flex: 1;
  position: relative;
  min-width: 0;
  background:
    radial-gradient(circle at 50% 50%, rgba(216,168,56,0.06), transparent 60%),
    repeating-radial-gradient(circle at 30% 40%, rgba(255,255,255,0.012) 0 2px, transparent 2px 6px),
    linear-gradient(135deg, #0d0719, #1a0e2c);
  overflow: hidden;
  border-right: 1px solid var(--border);
}

/* Flipped: library on the left, workspace on the right. */
body.layout-flipped #stage { flex-direction: row-reverse; }
body.layout-flipped #workspace { border-right: none; border-left: 1px solid var(--border); }

#workspace::before {
  content: '';
  position: absolute;
  inset: 24px;
  border: 1px solid rgba(216,168,56,0.12);
  border-radius: 14px;
  pointer-events: none;
  background:
    radial-gradient(circle at 20% 30%, rgba(232,84,26,0.04), transparent 35%),
    radial-gradient(circle at 80% 75%, rgba(170,138,255,0.04), transparent 35%);
}

#workspace::after {
  content: '✦';
  position: absolute;
  bottom: 18px;
  right: 26px;
  font-size: 60px;
  color: rgba(216,168,56,0.06);
  pointer-events: none;
  font-family: 'Cinzel', serif;
}

#workspace-surface {
  position: absolute;
  inset: 0;
  z-index: 1;
}

#discovery-banner {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, var(--bg-mid), var(--bg-soft));
  border: 1px solid var(--gold);
  border-radius: var(--radius);
  padding: 12px 22px;
  font-family: 'Cinzel', serif;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 14px;
  color: var(--gold-bright);
  box-shadow: var(--glow), 0 8px 24px rgba(0,0,0,0.5);
  z-index: 20;
  animation: banner-in 2.4s ease forwards;
  pointer-events: none;
  text-align: center;
}
#discovery-banner .label { display: block; font-size: 10px; color: var(--ink-dim); letter-spacing: 0.3em; margin-bottom: 4px; }
#discovery-banner .name { font-size: 18px; color: var(--parchment); }

@keyframes banner-in {
  0%   { opacity: 0; transform: translate(-50%, -10px) scale(0.95); }
  10%  { opacity: 1; transform: translate(-50%, 0) scale(1); }
  85%  { opacity: 1; transform: translate(-50%, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -6px) scale(1); }
}

/* Tiles on the workspace */
.tile {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 78px;
  height: 78px;
  background: linear-gradient(160deg, rgba(42,28,68,0.92), rgba(28,18,48,0.92));
  border: 1px solid var(--border-light);
  border-radius: 14px;
  padding: 6px;
  cursor: grab;
  user-select: none;
  box-shadow: 0 6px 16px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.05);
  transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
  touch-action: none;
  z-index: 2;
}
.tile.dragging { cursor: grabbing; box-shadow: 0 12px 28px rgba(0,0,0,0.7), var(--glow); border-color: var(--gold); z-index: 10; }
.tile.overlapping { border-color: var(--gold-bright); box-shadow: var(--glow), 0 6px 16px rgba(0,0,0,0.5); }
.tile .name {
  font-family: 'Cinzel', serif;
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--parchment);
  text-align: center;
  text-transform: uppercase;
  pointer-events: none;
  line-height: 1.1;
}
.tile .icon { pointer-events: none; }
.tile.miss { animation: shake 0.4s ease; }
.tile.poof { animation: poof 0.35s ease forwards; }

.tile-close {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  min-height: 22px;
  padding: 0;
  border-radius: 50%;
  background: var(--bg-deep);
  color: var(--parchment-dim);
  border: 1px solid var(--border-light);
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 14px;
  line-height: 1;
  letter-spacing: 0;
  text-transform: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.12s ease, color 0.12s ease, transform 0.05s ease;
  z-index: 3;
}
.tile:hover .tile-close { opacity: 1; }
.tile-close:hover { background: var(--danger); color: white; border-color: var(--danger); box-shadow: none; }
.tile-close:active { transform: scale(0.9); }

@media (hover: none) {
  .tile-close { opacity: 0.85; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-3px); }
}
@keyframes poof {
  0%   { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.4); filter: blur(3px); }
}

.drag-ghost {
  position: fixed !important;
  z-index: 1000;
  pointer-events: none;
  opacity: 0.95;
  cursor: grabbing;
  box-shadow: 0 14px 30px rgba(0,0,0,0.7), var(--glow);
  border-color: var(--gold) !important;
  transform: scale(1.06);
  transition: transform 0.12s ease, opacity 0.18s ease;
}
.drag-ghost.over-workspace {
  transform: scale(1);
}
.drag-ghost-cancel {
  opacity: 0;
  transform: scale(0.7);
}

.spark {
  position: absolute;
  width: 6px;
  height: 6px;
  background: var(--gold-bright);
  border-radius: 50%;
  pointer-events: none;
  box-shadow: 0 0 8px var(--gold-bright);
  animation: spark 0.6s ease forwards;
  z-index: 9;
}
@keyframes spark {
  0%   { opacity: 1; transform: translate(0,0) scale(1); }
  100% { opacity: 0; transform: translate(var(--dx), var(--dy)) scale(0.2); }
}

/* Library sidebar */
#library {
  width: var(--library-w, 320px);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--bg), var(--bg-deep));
  min-height: 0;
  transition: width 0.25s ease;
}

.library-header {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#search {
  flex: 1;
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 15px;
  background: var(--bg-deep);
  color: var(--parchment);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 8px 12px;
  outline: none;
  min-height: 36px;
}
#search:focus { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }
#search::placeholder { color: var(--ink-mute); font-style: italic; }

/* Sort bar — segmented selector above the category filters. Three
   modes: alphabetical (A–Z), most recent discovery (Recent), and
   group-by-category (By Type). Active mode is highlighted gold. */
#sort-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px 6px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  flex-shrink: 0;
}
#sort-bar .sort-label {
  font-family: 'Cinzel', serif;
  font-size: 9px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--parchment-dim, var(--ink-dim));
  margin-right: 6px;
  flex-shrink: 0;
}
#sort-bar .sort-opt {
  font-family: 'Cinzel', serif;
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 4px 9px;
  min-height: 24px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--parchment-dim, var(--ink-dim));
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}
#sort-bar .sort-opt:hover {
  color: var(--parchment);
  border-color: var(--border-light);
  background: rgba(58, 40, 90, 0.4);
  box-shadow: none;
}
#sort-bar .sort-opt.active {
  background: var(--bg-soft);
  color: var(--gold-bright);
  border-color: var(--gold);
}

#filter-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 14px;
  background: transparent;
  border: none;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  color: var(--ink-dim);
  font-family: 'Cinzel', serif;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  text-align: left;
  cursor: pointer;
  min-height: 34px;
  transition: color 0.12s ease, background 0.12s ease;
}
#filter-toggle:hover { color: var(--parchment); background: rgba(58,40,90,0.3); box-shadow: none; }
#filter-toggle .ft-label { flex-shrink: 0; }
#filter-toggle .ft-active {
  flex: 1;
  text-align: right;
  padding-right: 10px;
  color: var(--gold-bright);
  font-size: 10px;
  letter-spacing: 0.14em;
}
#filter-toggle .ft-active:empty { display: none; }
#filter-toggle .ft-chevron {
  flex-shrink: 0;
  color: var(--parchment-dim);
  font-size: 14px;
  transition: transform 0.2s ease;
}
#library.filters-collapsed #filter-toggle .ft-chevron { transform: rotate(-90deg); }

#category-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding: 10px 12px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  max-height: 200px;
  overflow: hidden;
  transition: max-height 0.25s ease, padding 0.25s ease, opacity 0.2s ease;
}
#library.filters-collapsed #category-tabs {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  opacity: 0;
  border-bottom: none;
}

.cat-tab {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 5px 5px 11px;
  font-family: 'Cinzel', serif;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: transparent;
  color: var(--ink-dim);
  border: 1px solid var(--border);
  border-radius: 16px;
  cursor: pointer;
  white-space: nowrap;
  min-height: 26px;
  line-height: 1;
  transition: color 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}
.cat-tab:hover { color: var(--parchment); border-color: var(--border-light); background: rgba(58,40,90,0.4); box-shadow: none; }
.cat-tab.active { background: var(--bg-soft); color: var(--gold-bright); border-color: var(--gold); }
.cat-count {
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  color: var(--parchment-dim);
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1px 8px;
  min-width: 34px;
  text-align: center;
}
.cat-tab:hover .cat-count { color: var(--parchment); }
.cat-tab.active .cat-count { color: var(--gold-bright); background: rgba(0,0,0,0.5); border-color: var(--gold); }

#library-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(calc(var(--lib-tile-size, 78px) + 4px), 1fr));
  gap: 8px;
  align-content: start;
  scrollbar-width: thin;
  /* Allow native pan-y scrolling on the empty gutters between tiles —
     the tiles themselves use touch-action:none and we manually scroll
     this container based on vertical pointer movement. */
  touch-action: pan-y;
}
#library-list::-webkit-scrollbar { width: 6px; }
#library-list::-webkit-scrollbar-track { background: transparent; }
#library-list::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }

.lib-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 4px;
  background: rgba(42,28,68,0.45);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: grab;
  /* none — we handle scroll vs drag manually from pointer events so we
     can make touch intent unambiguous (vertical swipe scrolls; long-press
     or sideways drag enters drag mode). */
  touch-action: none;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.08s ease, box-shadow 0.12s ease;
  min-height: var(--lib-tile-size, 78px);
}
.lib-tile.lib-pressed {
  background: rgba(58,40,90,0.85);
  border-color: var(--gold);
  transform: scale(1.06);
  box-shadow: 0 0 14px rgba(216,168,56,0.4);
}
.lib-tile:hover { background: rgba(58,40,90,0.7); border-color: var(--border-light); }
.lib-tile:active { cursor: grabbing; transform: scale(0.96); }
.lib-tile .name {
  font-family: 'Cinzel', serif;
  font-size: var(--lib-name-size, 9px);
  letter-spacing: 0.06em;
  color: var(--parchment);
  text-align: center;
  text-transform: uppercase;
  line-height: 1.1;
  pointer-events: none;
}

.empty-state {
  grid-column: 1 / -1;
  color: var(--ink-mute);
  font-style: italic;
  text-align: center;
  padding: 30px 10px;
  font-size: 14px;
}

/* Compact landscape phone layout */
@media (orientation: landscape) and (max-height: 500px) {
  /* library width left to --library-w (the Settings layout preset) so the
     Focus on board / Balanced / Focus on library choice works on phones too */
  .tile { width: 70px; height: 70px; }
  #library-list { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
  :root { --topbar-h: 40px; }
  #progress-bar { height: 16px; }

  /* Topbar trims down so all 6 actions fit at ~800px wide. */
  #topbar { padding: 0 10px; gap: 6px; }
  .title h1 { display: none; }            /* keep just the alembic glyph */
  .title .glyph { font-size: 22px; }
  .stats { font-size: 10px; letter-spacing: 0.06em; gap: 5px; }
  .stats-sep { display: none; }
  #topbar button {
    font-size: 9px;
    padding: 5px 8px;
    letter-spacing: 0.08em;
    min-height: 28px;
  }
  #topbar .actions { gap: 4px; }
  /* Progress bar label squeezes too */
  #progress-label { font-size: 8px; letter-spacing: 0.14em; gap: 6px; }

  /* Slot, crucible, and drag-ghost mobile sizing lives in circle.css so it
     beats the base rules there in cascade order. */
  .tile.drag-ghost { width: 60px; height: 60px; padding: 4px; }
  .tile.drag-ghost .icon { width: 30px; height: 30px; }
  .tile.drag-ghost .name { font-size: 8px; }

  /* Library row sizing — tile content size shrinks */
  .lib-tile { min-height: 70px; }
  .lib-tile .name { font-size: 8px; }
  .cat-tab { font-size: 9px; padding: 3px 7px; min-height: 22px; }
  .cat-count { font-size: 10px; padding: 0 6px; min-width: 28px; }

  /* Filter toggle compact */
  #filter-toggle { padding: 6px 10px; font-size: 9px; min-height: 28px; }

  /* Sort bar compact on landscape phones */
  #sort-bar { padding: 6px 10px 4px; gap: 3px; }
  #sort-bar .sort-label { font-size: 8px; margin-right: 4px; }
  #sort-bar .sort-opt   { font-size: 9px; padding: 3px 7px; min-height: 22px; }

  /* Modal cards usable on tiny landscape screens.
     dvh (dynamic viewport height) is the modern unit that excludes the
     iOS Safari URL bar; vh is the older fallback for browsers without
     dvh support. Both lines are declared so old browsers ignore dvh
     and modern browsers take it as the winning later declaration. */
  .modal-card {
    padding: 14px 18px 12px;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
  }
  .modal-title { font-size: 16px; margin-bottom: 8px; }
  .modal-body { font-size: 12.5px; line-height: 1.4; }
  .modal-body h3 { font-size: 10px; margin: 10px 0 4px; }
  .modal-body p, .modal-body ul { margin-bottom: 6px; }
  .modal-flavor { font-size: 13px; margin-bottom: 8px !important; }
  /* On short screens the board-offset can push the card a hair off-screen —
     swap to viewport centering for every modal so nothing clips at the edges. */
  #about-modal .modal-card,
  #settings-modal .modal-card,
  #saves-modal .modal-card,
  #reset-modal .modal-card,
  #legacy-saves-modal .modal-card,
  #lineage-modal .modal-card,
  #graph-modal .modal-card,
  #hint-modal .modal-card {
    top: 50%;
    left: 50%;
  }

  /* The compact-mobile rules below use parent-id prefixes so they
     win the cascade against the per-modal CSS files (hints.css,
     lineage.css, themes.css) that load AFTER workspace.css. */

  /* Hint modal: shrink the recipe-preview chips and pull padding tight. */
  #hint-modal .hint-card { max-width: calc(100vw - 28px); }
  #hint-modal .hint-recipe { padding: 14px 8px; gap: 8px; }
  #hint-modal .hint-chip { font-size: 11px; padding: 6px 10px; }
  #hint-modal .hint-mystery { font-size: 16px; padding: 4px 10px; }
  #hint-modal .hint-watch,
  #hint-modal .hint-retry,
  #hint-modal .hint-begin {
    padding: 11px 14px;
    font-size: 11px;
    letter-spacing: 0.1em;
  }

  /* Lineage modal: tighter head + smaller icon so the recipe lists below
     get more vertical room. */
  #lineage-modal .lineage-head { gap: 10px; padding-bottom: 10px; margin-bottom: 12px; }
  #lineage-modal .lineage-icon-wrap { width: 44px; height: 44px; }
  #lineage-modal .lineage-icon-wrap .icon { width: 36px; height: 36px; }
  #lineage-modal .lineage-name { font-size: 16px; }
  #lineage-modal .lineage-section h3 { font-size: 10px; }
  #lineage-modal .lineage-chip { font-size: 10px; padding: 4px 8px; }

  /* Graph modal: cap the canvas so the close button + title stay onscreen,
     and let rows scroll inside. dvh fallback pair for iOS Safari. */
  #graph-modal .graph-card {
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
  }
  #graph-modal .graph-canvas {
    max-height: calc(100vh - 90px);
    max-height: calc(100dvh - 90px);
  }
  #graph-modal .graph-tier-label { width: 56px; font-size: 9px; padding-top: 2px; }
  #graph-modal .graph-node { padding: 3px 7px 3px 4px; }
  #graph-modal .graph-node-name { font-size: 9px; }

  /* Rank-up banner: trimmed padding + smaller type, plus board-offset
     centring replaced with viewport centring so the banner doesn't push
     off the edge of narrow landscape phones. body-class adds enough
     specificity to win over themes.css. */
  body .rank-up-banner {
    top: 50%;
    left: 50%;
    padding: 16px 26px;
    min-width: 220px;
    max-width: calc(100vw - 32px);
  }
  body .rank-up-title  { font-size: 22px; margin-bottom: 4px; }
  body .rank-up-flavor { font-size: 12.5px; }
  body .rank-up-label  { font-size: 9px; }

  /* Tutorial card: keep the base 360px max-width (or shrink it on
     very narrow screens), just trim padding + font sizes for the
     shorter landscape-phone layout. The 360px cap was correct —
     don't let the card take half the screen. */
  body .tutorial-card {
    max-width: min(360px, calc(100vw - 24px));
    padding: 12px 14px 10px;
  }
  body .tutorial-title { font-size: 14px; }
  body .tutorial-body  { font-size: 12px; }

  /* Slot-unlock cutscene: trim margins so the preview slots + dismiss
     hint don't get pushed off a short screen. circle.css loads after
     workspace.css so body-prefix wins the cascade. */
  body .suc-subtitle { margin-bottom: 16px; font-size: 13px; }
  body .suc-preview { gap: 10px; margin-bottom: 14px; }
  body .suc-slot { width: 44px; height: 44px; }
}
