:root {
  color-scheme: light;
  --ink: #2c201a;
  --ink-soft: #675247;
  --paper: #ead7a9;
  --paper-light: #fff3d2;
  --paper-dark: #c79e63;
  --red: #a64032;
  --gold: #e4b749;
  --wood: #75422a;
  --wood-dark: #3d2117;
  --line: rgba(58, 38, 27, 0.27);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { min-width: 320px; }

body {
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 17% 4%, rgba(197, 116, 61, 0.15), transparent 28rem),
    repeating-linear-gradient(0deg, transparent 0 4px, rgba(255, 255, 255, 0.018) 5px),
    #211714;
  color: var(--paper-light);
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", system-ui, sans-serif;
}

button,
a { font: inherit; }

button:focus-visible,
a:focus-visible {
  outline: 3px solid #ffe38a;
  outline-offset: 3px;
}

.page-shell {
  width: min(100% - 32px, 980px);
  margin: 0 auto;
}

.topbar {
  min-height: 58px;
  border-bottom: 1px solid rgba(245, 231, 200, 0.2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.back-link {
  color: rgba(255, 243, 210, 0.74);
  font: 800 0.68rem/1 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.12em;
  text-decoration: none;
}

.back-link:hover { color: var(--paper-light); }

.top-actions { display: flex; gap: 8px; }

.utility-button {
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(245, 231, 200, 0.3);
  background: transparent;
  color: rgba(255, 243, 210, 0.82);
  cursor: pointer;
  font-size: 0.73rem;
  font-weight: 800;
}

.utility-button:hover { background: rgba(255, 255, 255, 0.07); }

.title-block {
  padding: 20px 0 22px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 32px;
}

.eyebrow,
.section-label,
.overlay-kicker {
  margin: 0;
  color: var(--gold);
  font: 850 0.62rem/1.2 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.15em;
}

.title-block h1 {
  margin: 4px 0 0;
  font: 900 clamp(2.8rem, 7vw, 4.7rem)/0.9 Georgia, "Yu Mincho", serif;
  letter-spacing: -0.08em;
}

.title-block h1 span {
  margin-right: 0.16em;
  color: #e9bd56;
}

.lead {
  max-width: 390px;
  margin: 0;
  color: rgba(255, 243, 210, 0.72);
  font-size: 0.86rem;
  font-weight: 650;
  line-height: 1.8;
}

.is-playing .title-block { display: none; }

.game-layout { width: 100%; }

.loom-panel {
  padding: clamp(14px, 2.5vw, 24px);
  border: 2px solid #281711;
  background:
    repeating-linear-gradient(4deg, transparent 0 8px, rgba(91, 57, 34, 0.035) 9px),
    var(--paper);
  color: var(--ink);
  box-shadow: 12px 14px 0 rgba(7, 4, 3, 0.27);
}

.panel-heading {
  min-height: 64px;
  margin-bottom: 14px;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto auto;
  align-items: center;
  gap: 18px;
}

.panel-title h2 {
  margin: 5px 0 0;
  font: 850 clamp(1.25rem, 3vw, 1.8rem)/1.1 Georgia, "Yu Mincho", serif;
}

.target-preview-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-soft);
  font: 800 0.57rem/1.2 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.07em;
}

.target-preview {
  --pattern-accent: #f3c969;
  --pattern-base: #2d5260;
  width: 44px;
  aspect-ratio: 4 / 5;
  border: 1px solid rgba(38, 27, 23, 0.35);
  background: #f4e7c5;
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  grid-template-rows: repeat(20, 1fr);
  box-shadow: 3px 3px 0 rgba(65, 39, 24, 0.12);
}

.target-cell { background: var(--pattern-base); }
.target-cell.is-accent { background: var(--pattern-accent); }

.run-stats {
  margin: 0;
  display: flex;
  border: 1px solid var(--line);
  background: rgba(255, 246, 218, 0.45);
}

.run-stats div {
  min-width: 74px;
  padding: 8px 10px;
  border-left: 1px solid var(--line);
  text-align: center;
}

.run-stats div:first-child { border-left: 0; }

.run-stats dt {
  color: var(--ink-soft);
  font-size: 0.58rem;
  font-weight: 800;
}

.run-stats dd {
  margin: 3px 0 0;
  font: 850 0.82rem/1 ui-monospace, SFMono-Regular, Consolas, monospace;
}

.loom-machine {
  --drag-progress: 0;
  padding: 10px 12px 12px;
  border: 2px solid #26140e;
  background:
    repeating-linear-gradient(90deg, transparent 0 33px, rgba(255, 215, 150, 0.035) 34px),
    linear-gradient(105deg, #512c1e, #8d5837 48%, #4a281c);
  box-shadow:
    inset 0 0 0 3px rgba(255, 218, 163, 0.08),
    0 12px 22px rgba(39, 20, 12, 0.22);
}

.loom-topbar,
.loom-footer {
  position: relative;
  z-index: 8;
  min-height: 24px;
  border: 2px solid #2d1912;
  background: linear-gradient(#a66b43, #6f4029);
  box-shadow: inset 0 4px 0 rgba(255, 224, 172, 0.15);
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.loom-topbar span,
.loom-footer span {
  width: 11px;
  height: 11px;
  border: 2px solid #2a1913;
  border-radius: 50%;
  background: #c58b4b;
  box-shadow: inset 1px 1px 0 #f8d68c;
}

.loom-footer { min-height: 29px; }
.loom-footer span { width: 15px; height: 15px; }

.loom-stage {
  position: relative;
  height: clamp(350px, 48vh, 470px);
  min-height: 0;
  padding: 10px 44px;
  background:
    linear-gradient(90deg, rgba(34, 17, 12, 0.38), transparent 12% 88%, rgba(34, 17, 12, 0.38)),
    #bc8754;
}

.warp-bed {
  position: relative;
  height: 100%;
  aspect-ratio: 4 / 5;
  max-width: 100%;
  margin: 0 auto;
  overflow: hidden;
  border: 2px solid #3e251a;
  background:
    repeating-linear-gradient(90deg, rgba(111, 77, 51, 0.2) 0 1px, transparent 1px 6.25%),
    linear-gradient(#f8edce, #e9d29c);
  box-shadow: inset 0 0 22px rgba(70, 43, 26, 0.2);
}

.cloth-grid {
  --pattern-accent: #f3c969;
  --pattern-base: #2d5260;
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  grid-template-rows: repeat(20, 1fr);
}

.cloth-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid rgba(96, 66, 40, 0.08);
  transition: background 170ms ease, opacity 170ms ease, transform 170ms ease;
}

.cloth-cell.is-woven {
  border-top: 1px solid rgba(255, 244, 208, 0.26);
  border-bottom: 1px solid rgba(24, 17, 14, 0.19);
  background: var(--pattern-base);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.06);
}

.cloth-cell.is-accent { background: var(--pattern-accent); }

.cloth-cell.is-new {
  z-index: 1;
  animation: yarn-settle 260ms ease-out both;
}

.active-row {
  position: absolute;
  z-index: 3;
  left: 0;
  right: 0;
  top: 95%;
  height: 5%;
  border-top: 2px dashed rgba(151, 86, 45, 0.72);
  border-bottom: 2px dashed rgba(151, 86, 45, 0.72);
  background: rgba(255, 225, 143, 0.16);
  pointer-events: none;
  transition: top 180ms ease;
}

.shuttle {
  position: absolute;
  z-index: 7;
  left: 8px;
  top: calc(97.5% - 14px);
  width: 80px;
  height: 28px;
  padding: 0;
  border: 2px solid #25140e;
  border-radius: 50% 10px 10px 50%;
  background: linear-gradient(180deg, #d8974f, #8a4a28 58%, #5b2d1c);
  box-shadow: 0 6px 10px rgba(34, 15, 9, 0.27), inset 0 3px 0 rgba(255, 221, 161, 0.28);
  color: #fff2cd;
  cursor: grab;
  touch-action: none;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 900;
  transition-property: left, box-shadow;
  transition-timing-function: cubic-bezier(0.24, 0.72, 0.34, 1);
}

.shuttle:disabled {
  cursor: default;
  opacity: 0.78;
}

.shuttle:not(:disabled):hover { box-shadow: 0 8px 13px rgba(34, 15, 9, 0.34), inset 0 3px 0 rgba(255, 221, 161, 0.32); }
.shuttle.is-dragging { cursor: grabbing; box-shadow: 0 10px 16px rgba(34, 15, 9, 0.4), inset 0 3px 0 rgba(255, 221, 161, 0.32); }

.shuttle::after {
  position: absolute;
  right: -8px;
  top: 8px;
  width: 12px;
  height: 8px;
  background: #2c1810;
  clip-path: polygon(0 0, 100% 50%, 0 100%);
  content: "";
}

.shuttle.faces-left {
  border-radius: 10px 50% 50% 10px;
}

.shuttle.faces-left::after {
  right: auto;
  left: -8px;
  transform: scaleX(-1);
}

.shuttle i {
  position: absolute;
  left: 10px;
  width: 17px;
  height: 9px;
  border: 2px solid #372017;
  border-radius: 50%;
  background: var(--gold);
}

.shuttle.faces-left i { right: 10px; left: auto; }

.beater {
  position: absolute;
  z-index: 6;
  left: 2%;
  right: 2%;
  top: calc(97.5% - 6px);
  height: 12px;
  border: 2px solid #28160f;
  background: linear-gradient(#b77d4e, #6e3d29);
  box-shadow: 0 4px 8px rgba(40, 22, 15, 0.25);
  pointer-events: none;
  opacity: 0.65;
}

.beater i {
  position: absolute;
  inset: 2px 12px;
  background: repeating-linear-gradient(90deg, #2c1911 0 2px, transparent 2px 11px);
}

.beater.is-beating { animation: beat-row 180ms ease-out both; }

.loom-rail {
  position: absolute;
  z-index: 1;
  top: 0;
  bottom: 0;
  width: 27px;
  border: 2px solid #2c1912;
  background: linear-gradient(90deg, #5a321f, #9a613d 50%, #522c1d);
  box-shadow: inset 3px 0 0 rgba(255, 216, 161, 0.11);
}

.loom-rail-left { left: 7px; }
.loom-rail-right { right: 7px; }

.game-overlay {
  position: absolute;
  z-index: 20;
  inset: 0;
  padding: 18px;
  background: rgba(25, 15, 11, 0.58);
  backdrop-filter: blur(3px);
  display: grid;
  place-items: center;
}

.overlay-card {
  width: min(500px, 100%);
  padding: clamp(22px, 4vw, 34px);
  border: 2px solid #372118;
  background:
    linear-gradient(rgba(255, 245, 214, 0.98), rgba(236, 215, 168, 0.98)),
    repeating-linear-gradient(5deg, transparent 0 7px, rgba(96, 65, 37, 0.08) 8px);
  box-shadow: 10px 10px 0 rgba(35, 18, 11, 0.32);
  color: var(--ink);
  text-align: center;
}

.overlay-card h2 {
  margin: 10px 0 12px;
  font-size: clamp(1.55rem, 4vw, 2.3rem);
  line-height: 1.08;
}

.heading-break { display: block; }

.overlay-card > p:not(.overlay-kicker) {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.86rem;
  font-weight: 650;
  line-height: 1.75;
}

.overlay-card small {
  display: block;
  margin-top: 12px;
  color: var(--ink-soft);
  font-size: 0.7rem;
  font-weight: 750;
}

.primary-button {
  min-height: 48px;
  margin-top: 18px;
  padding: 0 22px;
  border: 2px solid #281812;
  background: var(--ink);
  color: var(--paper-light);
  box-shadow: 5px 5px 0 var(--gold);
  cursor: pointer;
  font-weight: 850;
  transition: transform 150ms ease, box-shadow 150ms ease;
}

.primary-button:hover {
  transform: translate(2px, 2px);
  box-shadow: 3px 3px 0 var(--gold);
}

.loom-control-deck {
  position: relative;
  z-index: 9;
  min-height: 104px;
  padding: 12px 14px;
  border: 2px solid #2d1912;
  border-top: 0;
  background:
    repeating-linear-gradient(4deg, transparent 0 7px, rgba(75, 47, 28, 0.035) 8px),
    #ead6a9;
  color: var(--ink);
}

.play-controls {
  display: block;
}

.drag-console {
  min-width: 0;
  padding: 8px 12px 7px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.drag-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.drag-heading p {
  margin: 0;
  font-size: clamp(0.9rem, 2vw, 1.05rem);
  font-weight: 900;
}

.drag-heading span {
  color: var(--ink-soft);
  font-size: 0.67rem;
  font-weight: 800;
}

.drag-route {
  position: relative;
  height: 14px;
  margin: 9px 0 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drag-route::before,
.drag-route::after {
  position: absolute;
  left: 7px;
  right: 7px;
  top: 6px;
  height: 2px;
  content: "";
}

.drag-route::before { background: rgba(66, 44, 31, 0.2); }

.drag-route::after {
  right: auto;
  width: calc((100% - 14px) * var(--drag-progress));
  background: var(--red);
  transition: width 60ms linear;
}

.drag-route span,
.drag-route i {
  position: relative;
  z-index: 1;
  width: 10px;
  height: 10px;
  border: 2px solid #7a5a44;
  border-radius: 50%;
  background: #f4e4bb;
}

.drag-route span { width: 14px; height: 14px; border-color: var(--red); }

.status-line {
  min-height: 1.45em;
  margin: 0;
  overflow: hidden;
  color: #594238;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.45;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-panel {
  min-height: 102px;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto auto auto;
  align-items: center;
  gap: 18px;
}

.result-panel h2 {
  margin: 4px 0 3px;
  font: 900 1.35rem/1.1 Georgia, "Yu Mincho", serif;
}

.result-message {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1.5;
}

.result-score { display: flex; align-items: baseline; }

.result-score strong {
  color: var(--red);
  font: 900 3.3rem/0.85 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: -0.08em;
}

.result-score span { font-size: 0.72rem; font-weight: 850; }

.result-details {
  margin: 0;
  padding-left: 16px;
  border-left: 1px solid var(--line);
}

.result-details div { display: grid; grid-template-columns: auto auto; gap: 8px; }
.result-details dt { color: var(--ink-soft); font-size: 0.62rem; font-weight: 750; }
.result-details dd { margin: 0; font: 850 0.67rem/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }

.result-action { text-align: right; }
.result-action .primary-button { min-height: 42px; margin-top: 7px; }

.best-line {
  margin: 0;
  color: var(--ink-soft);
  font: 750 0.62rem/1 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.08em;
}

footer {
  min-height: 64px;
  margin-top: 18px;
  border-top: 1px solid rgba(245, 231, 200, 0.2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  color: rgba(245, 231, 200, 0.5);
  font: 700 0.55rem/1.5 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.1em;
}

.help-dialog {
  width: min(620px, calc(100% - 28px));
  max-height: calc(100vh - 36px);
  padding: clamp(24px, 5vw, 40px);
  border: 2px solid #3c2419;
  background: #f3e4bd;
  color: var(--ink);
  box-shadow: 14px 14px 0 rgba(18, 9, 6, 0.35);
}

.help-dialog::backdrop {
  background: rgba(21, 12, 9, 0.76);
  backdrop-filter: blur(4px);
}

.dialog-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-heading button {
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-size: 1.5rem;
}

.help-dialog h2 {
  margin: 12px 0 18px;
  font-size: clamp(1.8rem, 6vw, 2.8rem);
}

.help-dialog ol { margin: 0; padding: 0; list-style: none; }

.help-dialog li {
  padding: 12px 0;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 40px 1fr;
  gap: 12px;
}

.help-dialog li > span {
  color: var(--red);
  font: 850 0.7rem/1.6 ui-monospace, SFMono-Regular, Consolas, monospace;
}

.help-dialog li p { margin: 0; line-height: 1.7; }

.keyboard-note {
  margin: 16px 0 0;
  padding: 12px;
  background: rgba(122, 81, 43, 0.1);
  font-size: 0.8rem;
  line-height: 1.65;
}

kbd {
  padding: 3px 6px;
  border: 1px solid rgba(40, 27, 22, 0.45);
  border-bottom-width: 3px;
  border-radius: 4px;
  background: rgba(255, 248, 226, 0.75);
  color: var(--ink);
  font: 800 0.7rem/1 ui-monospace, SFMono-Regular, Consolas, monospace;
}

@keyframes beat-row {
  0% { transform: translateY(-10px); opacity: 0.4; }
  55% { transform: translateY(3px); opacity: 1; }
  100% { transform: translateY(0); opacity: 0.7; }
}

@keyframes yarn-settle {
  0% { transform: scaleY(0.35); opacity: 0.25; }
  100% { transform: scaleY(1); opacity: 1; }
}

@media (max-width: 760px) {
  .title-block { grid-template-columns: 1fr; gap: 9px; }
  .lead br { display: none; }
  .panel-heading { grid-template-columns: 1fr auto; }
  .run-stats { grid-column: 1 / -1; }
  .run-stats div { flex: 1; }
  .drag-console { padding: 5px 2px 2px; }
  .result-panel { grid-template-columns: 1fr auto; }
  .result-details { padding-left: 0; border-left: 0; }
  .result-action { align-self: end; }
}

@media (max-width: 520px) {
  .page-shell { width: min(100% - 20px, 980px); }
  .topbar { min-height: 54px; }
  .title-block { padding: 14px 0 16px; }
  .title-block h1 { font-size: 3rem; }
  .lead { font-size: 0.77rem; line-height: 1.55; }
  .loom-panel { padding: 10px; box-shadow: 7px 9px 0 rgba(7, 4, 3, 0.25); }
  .panel-heading { min-height: 54px; margin-bottom: 9px; gap: 9px; }
  .target-preview-wrap > span { display: none; }
  .target-preview { width: 36px; }
  .run-stats div { min-width: 0; padding: 6px; }
  .loom-machine { padding: 7px 7px 9px; }
  .loom-stage { height: clamp(330px, 54vh, 420px); padding: 7px 24px; }
  .loom-rail { width: 18px; }
  .loom-rail-left { left: 3px; }
  .loom-rail-right { right: 3px; }
  .shuttle { width: 68px; height: 27px; }
  .loom-control-deck { min-height: 0; padding: 10px; }
  .drag-heading { align-items: flex-start; flex-direction: column; gap: 3px; }
  .drag-route { margin: 7px 0 6px; }
  .status-line { white-space: normal; }
  .game-overlay { padding: 10px; }
  .overlay-card { padding: 20px 15px; }
  .overlay-card br { display: none; }
  .result-panel { gap: 10px; }
  .result-panel > div:first-child { grid-column: 1 / -1; }
  .result-score strong { font-size: 2.7rem; }
  .result-action .primary-button { padding: 0 12px; }
  footer { padding: 16px 0; align-items: flex-start; flex-direction: column; gap: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
