:root {
  color-scheme: dark;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #f4efe3;
  background: #0d1722;
  --background: #0d1722;
  --surface: #142130;
  --surface-raised: #1a2a3a;
  --surface-hover: #223449;
  --border: #304257;
  --text: #f4efe3;
  --muted: #aab9c6;
  --accent: #f4ba71;
  --accent-hover: #ffd095;
  --accent-ink: #241911;
  --positive: #94d8c0;
  --warning: #ffc299;
  --radius: 18px;
  --shadow: 0 18px 48px #03081245;
}

*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 0;
  min-height: 100vh;
  background: radial-gradient(ellipse at 25% 0%, #1d3145 0, transparent 54%), var(--background);
}

button {
  border: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:disabled {
  cursor: default;
}

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 4px;
}

button, a {
  touch-action: manipulation;
}

h1, h2, h3, p {
  margin: 0;
}

canvas, img {
  display: block;
  max-width: 100%;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  width: min(1220px, calc(100% - 64px));
  margin-inline: auto;
  padding-block: 34px 15px;
}

.masthead {
  display: flex;
  align-items: center;
  gap: 15px;
  min-height: 52px;
  margin-bottom: 23px;
}

.masthead h1 {
  font-size: clamp(1.45rem, 2.55vw, 2.05rem);
  font-weight: 720;
  line-height: 1.23;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-mark {
  position: relative;
  display: grid;
  flex: 0 0 48px;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 13px;
  border: 1px solid #f4ba7145;
  background: #f4ba7118;
}

.brand-mark::before {
  content: "";
  width: 26px;
  height: 30px;
  background: var(--accent);
  clip-path: polygon(50% 5%, 75% 25%, 70% 25%, 70% 49%, 62% 49%, 77% 94%, 23% 94%, 38% 49%, 30% 49%, 30% 25%, 25% 25%);
}

.brand-mark::after {
  position: absolute;
  content: "";
  width: 7px;
  height: 5px;
  top: 16px;
  border-radius: 1px;
  background: #354050;
}

.save-state {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-inline-start: auto;
  color: var(--muted);
  text-align: end;
  font-size: .85rem;
}

.save-state::before {
  content: "";
  flex: 0 0 7px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--positive);
  box-shadow: 0 0 0 3px #94d8c01b;
}

.save-state[data-tone="fail"]::before {
  background: var(--warning);
  box-shadow: 0 0 0 3px #ffc2991b;
}

.mode-switch {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: min(100%, 478px);
  gap: 5px;
  padding: 5px;
  margin-bottom: 19px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #0b1420b8;
}

.mode-button {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  align-content: center;
  column-gap: 11px;
  padding: 8px 12px;
  min-height: 56px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  text-align: start;
  transition: background-color .16s, border-color .16s, color .16s;
}

.mode-button[aria-pressed="true"] {
  border-color: #f4ba7173;
  background: #f4ba711b;
  color: var(--accent-hover);
}

.mode-name, .mode-state {
  display: block;
}

.mode-name {
  color: var(--text);
  font-size: .98rem;
  font-weight: 700;
  line-height: 1.3;
}

.mode-button[aria-pressed="true"] .mode-name {
  color: var(--accent-hover);
}

.mode-state {
  color: var(--muted);
  font-size: .79rem;
  line-height: 1.35;
}

.mode-icon {
  position: relative;
  display: block;
  width: 25px;
  height: 25px;
  grid-row: span 2;
  flex-shrink: 0;
}

.mode-icon::before, .mode-icon::after {
  content: "";
  position: absolute;
}

.mode-button[data-mode="craft"] .mode-icon::before {
  inset: 4px 4px 4px 4px;
  border: 1.5px solid currentColor;
  border-radius: 5px;
  rotate: 45deg;
}

.mode-button[data-mode="craft"] .mode-icon::after {
  inset: 10px 10px 10px 10px;
  border-radius: 50%;
  background: currentColor;
}

.mode-button[data-mode="build"] .mode-icon::before {
  inset: 3px 5px 15px;
  border: 1.5px solid currentColor;
  border-radius: 2px;
}

.mode-button[data-mode="build"] .mode-icon::after {
  inset: 13px 2px 3px;
  border: 1.5px solid currentColor;
  border-radius: 2px;
}

.game-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.85fr) minmax(330px, 1fr);
  gap: 19px;
  align-items: start;
}

.scene-panel, .workshop {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.scene-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 5px 14px;
  min-height: 61px;
  padding: 13px 19px;
}

.scene-title {
  font-size: 1.12rem;
  font-weight: 700;
  line-height: 1.35;
}

.goal-state {
  min-width: 0;
  padding: 5px 10px;
  border: 1px solid #829db44f;
  border-radius: 20px;
  color: #d3e1ed;
  background: #829db414;
  font-size: .8rem;
  line-height: 1.4;
}

.goal-state[data-tone="pass"] {
  border-color: #94d8c05c;
  color: var(--positive);
  background: #94d8c012;
}

.goal-state[data-tone="fail"] {
  border-color: #ffc29963;
  color: var(--warning);
  background: #ffc29912;
}

.scene-frame {
  position: relative;
  isolation: isolate;
  width: 100%;
  aspect-ratio: 1.72;
  overflow: hidden;
  background: #263857;
}

.scene-frame #coast {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.scene-frame.is-drop-target::after {
  position: absolute;
  z-index: 1;
  content: "";
  inset: 0;
  border: 2px dashed var(--accent-hover);
  background: #f4ba7114;
  pointer-events: none;
}

.scene-drop-hint {
  position: absolute;
  z-index: 2;
  inset: auto 13px 13px auto;
  padding: 5px 10px;
  border: 1px solid #f4ba7180;
  border-radius: 8px;
  color: var(--accent-hover);
  background: #0d1722e8;
  font-size: .86rem;
  opacity: 0;
  pointer-events: none;
}

.scene-frame.is-drop-target .scene-drop-hint { opacity: 1; }

.scene-details {
  padding: 15px 18px 16px;
}

.challenge-bar {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 6px 8px;
}

.challenge-button {
  min-height: 40px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--muted);
  background: #13202e;
  font-size: .875rem;
  line-height: 1.4;
  transition: background-color .16s, border-color .16s, color .16s;
}

.challenge-button[aria-pressed="true"] {
  border-color: #f4ba7173;
  color: var(--accent-hover);
  background: #f4ba7114;
}

.challenge-button[data-completed="true"]::after {
  content: "✓";
  margin-inline-start: 6px;
  color: var(--positive);
  font-weight: 700;
}

.challenge-button:disabled {
  color: #a8b3bd;
  border-color: #273747;
  background: transparent;
}

#scene-note {
  margin-top: 8px;
  color: var(--muted);
  min-height: 1.5em;
  font-size: .875rem;
}

.workshop {
  padding: 19px;
}

.workshop-header h2 {
  font-size: 1.28rem;
  font-weight: 720;
  line-height: 1.35;
}

.workshop-header p {
  margin-top: 5px;
  color: var(--muted);
  font-size: .91rem;
  line-height: 1.55;
}

.feedback {
  margin-block: 15px 18px;
  padding: 10px 12px;
  border: 1px solid #6d9dc334;
  border-inline-start: 3px solid #80b6d9;
  border-radius: 9px;
  background: #6d9dc310;
}

.feedback[data-tone="pass"] {
  border-color: #94d8c037;
  border-inline-start-color: var(--positive);
  background: #94d8c00d;
}

.feedback[data-tone="fail"] {
  border-color: #ffc29940;
  border-inline-start-color: var(--warning);
  background: #ffc2990d;
}

#feedback-title {
  display: block;
  font-size: .94rem;
  font-weight: 690;
  line-height: 1.35;
}

#feedback-text {
  margin-top: 3px;
  color: var(--muted);
  font-size: .86rem;
  line-height: 1.5;
}

#tray-label, .progress-panel h3 {
  margin-bottom: 9px;
  color: var(--text);
  font-size: .9rem;
  font-weight: 655;
  line-height: 1.4;
}

.tray {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.piece {
  --piece-color: #c8d9e8;
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  min-height: 58px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-raised);
  text-align: start;
  transition: background-color .16s, border-color .16s;
}

.piece[data-tone="ember"] { --piece-color: #ff9e80; }
.piece[data-tone="gold"] { --piece-color: #ffd18a; }
.piece[data-tone="blue"] { --piece-color: #a3d9f3; }
.piece[data-tone="steel"] { --piece-color: #d6e4eb; }
.piece[data-tone="stone"] { --piece-color: #abbfda; }
.piece[data-tone="warm"] { --piece-color: #efad92; }

.piece[aria-pressed="true"], .piece[data-drag-over] {
  border-color: var(--accent);
  background: #f4ba7119;
}

.piece-icon {
  position: relative;
  display: grid;
  flex: 0 0 34px;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid #7189a43f;
  border-radius: 8px;
  color: var(--piece-color);
  background: #0a152367;
  font-size: 1.23rem;
  line-height: 1;
}

.piece[data-placed="true"] .piece-icon::after {
  position: absolute;
  content: "✓";
  inset: auto -4px -3px auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  color: #0d201a;
  background: var(--positive);
  text-align: center;
  font-size: .69rem;
  font-weight: 750;
  line-height: 14px;
}

.piece-icon > svg {
  width: 22px;
  height: 22px;
}

.piece-copy {
  display: block;
  min-width: 0;
}

.piece-copy strong, .piece-copy small {
  display: block;
  overflow-wrap: anywhere;
}

.piece-copy strong {
  font-size: .9rem;
  font-weight: 680;
  line-height: 1.35;
}

.piece-copy small {
  margin-top: 1px;
  color: var(--muted);
  font-size: .75rem;
  line-height: 1.35;
}

.work-actions {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  margin-top: 11px;
}

.primary-action {
  padding: 9px 14px;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 9px;
  color: var(--accent-ink);
  background: var(--accent);
  font-size: .9rem;
  font-weight: 740;
  line-height: 1.35;
  transition: background-color .16s;
}

.primary-action.is-drop-target {
  outline: 2px dashed var(--accent-hover);
  outline-offset: 3px;
}

.text-action {
  padding: 6px 9px;
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--muted);
  background: transparent;
  font-size: .875rem;
  line-height: 1.35;
  text-align: start;
}

.progress-panel {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.steps {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: start;
  gap: 8px;
  padding: 0;
  margin: 0;
  list-style: none;
}

.steps li {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  color: var(--muted);
  font-size: .79rem;
  line-height: 1.25;
}

.steps li[data-state="done"] { color: var(--positive); }
.steps li[data-state="active"] { color: var(--accent-hover); }

.step-mark {
  display: grid;
  place-items: center;
  flex: 0 0 21px;
  width: 21px;
  height: 21px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: #0d1826;
  font-size: .71rem;
  line-height: 1;
}

[data-state="done"] .step-mark {
  border-color: #94d8c066;
  background: #94d8c017;
}

[data-state="active"] .step-mark {
  border-color: #f4ba7180;
  background: #f4ba711a;
}

.app-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 7px 12px;
  min-height: 48px;
  margin-top: 8px;
  color: var(--muted);
  font-size: .81rem;
}

#reset-dialog {
  width: min(420px, calc(100% - 32px));
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow);
}

#reset-dialog::backdrop { background: #030811b9; }

#reset-dialog h2 {
  font-size: 1.25rem;
  line-height: 1.4;
}

#reset-dialog p {
  margin-top: 8px;
  color: var(--muted);
  font-size: .91rem;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 19px;
}

@media (hover: hover) {
  .mode-button:hover, .challenge-button:not(:disabled):hover, .piece:hover {
    background: var(--surface-hover);
  }

  .mode-button[aria-pressed="true"]:hover, .challenge-button[aria-pressed="true"]:hover, .piece[aria-pressed="true"]:hover {
    background: #f4ba7125;
  }

  .primary-action:hover { background: var(--accent-hover); }
  .text-action:hover { color: var(--text); background: var(--surface-hover); }
}

@media (min-width: 851px) and (max-height: 820px) {
  .app-shell { padding-top: 20px; }
  .masthead { margin-bottom: 15px; }
  .mode-switch { margin-bottom: 14px; }
  .scene-frame { max-height: max(290px, calc(100vh - 355px)); }
}

@media (max-width: 850px) {
  .app-shell { width: min(650px, calc(100% - 38px)); padding-top: 22px; }
  .game-grid { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .mode-switch { width: 100%; margin-bottom: 14px; }
}

@media (max-width: 460px) {
  .app-shell { width: calc(100% - 24px); padding-top: 14px; }
  .masthead { flex-wrap: wrap; gap: 7px 11px; margin-bottom: 14px; }
  .masthead h1 { font-size: 1.35rem; }
  .brand { gap: 9px; }
  .brand-mark { width: 42px; height: 42px; flex-basis: 42px; }
  .brand-mark::after { top: 13px; }
  .save-state { gap: 5px; font-size: .73rem; }
  .save-state::before { width: 5px; height: 5px; flex-basis: 5px; }
  .scene-header { min-height: 49px; padding: 9px 12px; }
  .scene-title { font-size: 1rem; }
  .goal-state { font-size: .73rem; }
  .scene-details, .workshop { padding: 12px; }
  .challenge-button { padding-inline: 8px; font-size: .81rem; }
  .app-footer { flex-wrap: wrap; }
  .piece { gap: 7px; padding-inline: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
