:root {
  color-scheme: dark;
  --ink: #f4f8ff;
  --muted: #a7b5c7;
  --quiet: #74869d;
  --panel: rgba(10, 15, 22, 0.96);
  --panel-solid: #0a0f16;
  --line: rgba(180, 200, 225, 0.18);
  --line-strong: rgba(210, 224, 242, 0.3);
  --mint: #33d2c4;
  --amber: #e8b84a;
  --coral: #e65b45;
  --max-width: 1240px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #6e9861;
  color: var(--ink);
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
a,
input,
select,
textarea {
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
  text-decoration: none;
}

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--mint);
  outline-offset: 2px;
}

#stage {
  position: fixed;
  inset: 0;
  cursor: default;
  touch-action: none;
}

#stage.is-dragging {
  cursor: grabbing;
}

#stage.can-move {
  cursor: move;
}

#stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.topbar {
  position: fixed;
  z-index: 30;
  top: 12px;
  left: 50%;
  width: min(var(--max-width), calc(100% - 24px));
  min-height: 54px;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto minmax(180px, 1fr);
  align-items: center;
  gap: 16px;
  transform: translateX(-50%);
  border: 1px solid rgba(255, 255, 255, 0.17);
  border-radius: 6px;
  background: rgba(10, 15, 22, 0.88);
  padding: 7px 8px 7px 11px;
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(18px);
}

.brand {
  min-width: 0;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 820;
}

.brand-mark {
  width: 27px;
  height: 27px;
  flex: 0 0 27px;
  display: grid;
  grid-template-columns: repeat(3, 5px);
  grid-template-rows: repeat(3, 5px);
  gap: 2px;
  align-content: center;
  justify-content: center;
  border-radius: 4px;
  background: #17232e;
}

.brand-mark i {
  width: 5px;
  height: 5px;
  background: var(--mint);
}

.brand-mark i:nth-child(1) { grid-area: 1 / 1; }
.brand-mark i:nth-child(2) { grid-area: 1 / 2; }
.brand-mark i:nth-child(3) { grid-area: 2 / 1; }
.brand-mark i:nth-child(4) { grid-area: 3 / 1; }
.brand-mark i:nth-child(5) { grid-area: 3 / 2; background: var(--coral); }

.world-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.2);
}

.world-switch a {
  min-width: 92px;
  border-radius: 4px;
  padding: 7px 12px;
  color: #b8c4cf;
  font-size: 12px;
  font-weight: 750;
  text-align: center;
  white-space: nowrap;
}

.world-switch a:hover {
  color: #fff;
}

.world-switch a[aria-current="page"] {
  background: #edf3f7;
  color: #15202a;
  box-shadow: 0 2px 7px rgba(0, 0, 0, 0.22);
}

.utility-nav {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.utility-nav a {
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 8px 10px;
  color: #cdd9e8;
  font-size: 12px;
  font-weight: 680;
}

.utility-nav a:hover {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: #fff;
}

.studio {
  position: fixed;
  z-index: 24;
  top: 78px;
  right: 16px;
  bottom: 16px;
  width: min(368px, calc(100% - 32px));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--panel);
  box-shadow: 0 22px 56px rgba(0, 0, 0, 0.38);
  backdrop-filter: blur(18px);
}

.studio-head {
  min-height: 65px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  padding: 10px 12px 10px 15px;
}

.studio-head h1 {
  margin: 2px 0 0;
  font-size: 18px;
  line-height: 1.15;
  letter-spacing: 0;
}

.connection {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}

.connection i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--quiet);
}

.connection i.live {
  background: var(--mint);
  box-shadow: 0 0 0 3px rgba(75, 213, 178, 0.12);
}

.connection i.error {
  background: var(--coral);
}

.icon-button {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}

.icon-button:hover {
  border-color: var(--line-strong);
  background: rgba(255, 255, 255, 0.09);
}

.world-editor {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.world-settings {
  border-bottom: 1px solid var(--line);
  padding: 14px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.section-heading h2 {
  margin: 0;
  font-size: 13px;
  line-height: 1.25;
  letter-spacing: 0;
}

.section-kicker {
  margin: 0 0 3px;
  color: var(--quiet);
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
}

.save-state {
  min-width: 52px;
  color: var(--quiet);
  font-size: 10px;
  text-align: right;
}

.save-state.saving {
  color: var(--amber);
}

.save-state.error {
  color: var(--coral);
}

.field {
  min-width: 0;
  display: grid;
  gap: 5px;
  margin-top: 11px;
}

.field > span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 730;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.04);
  padding: 9px;
  color: var(--ink);
  font-size: 12px;
}

.field input,
.field select {
  height: 36px;
  padding-block: 0;
}

.field textarea {
  resize: vertical;
  line-height: 1.5;
}

.field select {
  cursor: pointer;
}

.field input:hover,
.field select:hover,
.field textarea:hover {
  border-color: var(--line-strong);
}

.field-counter {
  color: var(--quiet);
  font-size: 9px;
  text-align: right;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.inventory {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.inventory-heading {
  min-height: 61px;
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
}

.count-badge {
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 3px 6px;
  color: var(--muted);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
}

.inventory-heading-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

.create-structure-button {
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid #edf6f1;
  border-radius: 4px;
  background: #edf6f1;
  padding: 5px 9px;
  color: #141815;
  cursor: pointer;
  font-size: 10px;
  font-weight: 820;
}

.create-structure-button > span:first-child {
  font-size: 15px;
  line-height: 1;
}

.create-structure-button:hover {
  background: #ffffff;
}

.personal-build-queue {
  flex: 0 0 auto;
  max-height: 178px;
  overflow-y: auto;
  border-bottom: 1px solid var(--line);
  background: rgba(51, 210, 196, 0.035);
}

.queue-heading {
  position: sticky;
  z-index: 1;
  top: 0;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid rgba(180, 200, 225, 0.1);
  background: #0b1119;
  padding: 7px 12px;
  color: var(--quiet);
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
}

.personal-build-item {
  width: 100%;
  min-height: 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 0;
  border-bottom: 1px solid rgba(180, 200, 225, 0.08);
  background: transparent;
  padding: 8px 12px;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.personal-build-item:hover {
  background: rgba(255, 255, 255, 0.045);
}

.personal-build-item strong,
.personal-build-item small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.personal-build-item strong {
  font-size: 11px;
}

.personal-build-item small {
  margin-top: 2px;
  color: var(--quiet);
  font-size: 9px;
}

.build-status {
  color: var(--amber);
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
  white-space: nowrap;
}

.build-status.is-ready {
  color: var(--mint);
}

.build-status.is-error {
  color: #ff9d8e;
}

.inventory-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-color: #435269 transparent;
}

.inventory-item {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid rgba(180, 200, 225, 0.1);
  padding: 10px 12px;
}

.inventory-item:hover {
  background: rgba(255, 255, 255, 0.035);
}

.build-swatch {
  width: 36px;
  height: 36px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 4px;
  overflow: hidden;
  background: #17232e;
  padding: 4px;
}

.build-swatch i {
  display: block;
  background: var(--swatch, var(--mint));
}

.build-swatch i:nth-child(2),
.build-swatch i:nth-child(6) {
  opacity: 0.64;
}

.build-swatch i:nth-child(3),
.build-swatch i:nth-child(7) {
  opacity: 0.34;
}

.inventory-copy {
  min-width: 0;
}

.inventory-copy strong {
  display: block;
  overflow: hidden;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inventory-copy span {
  display: block;
  margin-top: 3px;
  overflow: hidden;
  color: var(--quiet);
  font-size: 10px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.place-button {
  min-width: 70px;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 1px solid #edf6f1;
  border-radius: 4px;
  background: #edf6f1;
  color: #141815;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 10px;
  font-weight: 800;
}

.place-button:hover:not(:disabled) {
  border-color: #ffffff;
  background: #ffffff;
}

.place-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.empty-inventory,
.signed-out {
  padding: 26px 20px;
}

.empty-inventory h3,
.signed-out h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.25;
  letter-spacing: 0;
}

.empty-inventory p,
.signed-out p:not(.section-kicker) {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.empty-inventory button,
.primary-action {
  border: 0;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 17px;
  border-radius: 4px;
  background: #edf6f1;
  padding: 8px 13px;
  color: #141815;
  font-size: 11px;
  font-weight: 820;
  cursor: pointer;
}

.empty-inventory button:hover,
.primary-action:hover {
  background: #ffffff;
}

.custom-build-dialog {
  width: min(620px, calc(100% - 32px));
  max-height: min(760px, calc(100dvh - 32px));
  margin: auto;
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--panel-solid);
  padding: 0;
  color: var(--ink);
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.58);
}

.custom-build-dialog::backdrop {
  background: rgba(2, 7, 11, 0.72);
  backdrop-filter: blur(5px);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--line);
  padding: 15px 16px;
}

.dialog-head h2 {
  margin: 2px 0 0;
  font-size: 19px;
  letter-spacing: 0;
}

#custom-build-form,
.custom-build-handoff {
  padding: 4px 16px 16px;
}

.size-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  margin: 16px 0 0;
  border: 0;
  padding: 0;
}

.size-picker legend {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 730;
}

.size-picker label {
  position: relative;
  cursor: pointer;
}

.size-picker input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.size-picker label > span {
  min-height: 54px;
  display: grid;
  align-content: center;
  gap: 3px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.035);
  padding: 8px 10px;
}

.size-picker input:checked + span {
  border-color: var(--mint);
  background: rgba(51, 210, 196, 0.1);
}

.size-picker input:focus-visible + span {
  outline: 2px solid var(--mint);
  outline-offset: 2px;
}

.size-picker strong {
  font-size: 11px;
}

.size-picker small {
  color: var(--quiet);
  font-size: 9px;
}

.form-error {
  margin: 12px 0 0;
  color: #ff9d8e;
  font-size: 11px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.dialog-actions .primary-action {
  margin-top: 0;
}

.primary-action.compact,
.secondary-action {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  padding: 8px 13px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 820;
}

.primary-action.compact:disabled {
  cursor: wait;
  opacity: 0.58;
}

.secondary-action {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--ink);
}

.secondary-action:hover {
  background: rgba(255, 255, 255, 0.09);
}

.custom-build-handoff {
  padding-top: 16px;
}

.handoff-status-line,
.handoff-token,
.handoff-instruction > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.handoff-status-line {
  color: var(--quiet);
  font-size: 9px;
}

.handoff-token {
  margin-top: 12px;
  border-block: 1px solid var(--line);
  padding: 12px 0;
}

.handoff-token span,
.handoff-instruction span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 730;
}

.handoff-token strong {
  color: var(--ink);
  font: 750 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  overflow-wrap: anywhere;
}

.handoff-instruction {
  margin-top: 14px;
}

.handoff-instruction button {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  padding: 5px 8px;
  color: var(--ink);
  cursor: pointer;
  font-size: 9px;
  font-weight: 780;
}

.handoff-instruction pre {
  max-height: 220px;
  margin: 8px 0 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #070c11;
  padding: 13px;
  color: #a8f1e5;
  font: 11px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.handoff-message {
  min-height: 18px;
  margin: 11px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.selection-bar {
  position: fixed;
  z-index: 23;
  left: 18px;
  bottom: 18px;
  max-width: calc(100% - 420px);
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: 18px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: rgba(10, 15, 22, 0.94);
  padding: 7px 8px 7px 13px;
  box-shadow: 0 14px 38px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(14px);
}

.selection-copy {
  min-width: 100px;
  max-width: 260px;
}

.selection-copy span {
  display: block;
  color: var(--quiet);
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
}

.selection-copy strong {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selection-actions {
  display: flex;
  overflow: hidden;
  gap: 0;
  border: 1px solid var(--line);
  border-radius: 4px;
}

.selection-actions button {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 0;
  border-radius: 0;
  background: rgba(255, 255, 255, 0.035);
  padding: 6px 10px;
  color: var(--ink);
  cursor: pointer;
  font-size: 10px;
  font-weight: 760;
}

.selection-actions button + button {
  border-left: 1px solid var(--line);
}

.selection-actions button span {
  font-size: 14px;
  line-height: 1;
}

.selection-actions button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.selection-actions .primary-tool {
  background: #edf6f1;
  color: #141815;
}

.selection-actions .primary-tool:hover {
  background: #ffffff;
}

.selection-actions .danger {
  color: #ffb6ab;
}

.world-meta {
  position: fixed;
  z-index: 18;
  left: 18px;
  top: 82px;
  display: flex;
  align-items: baseline;
  gap: 5px;
  border: 1px solid rgba(255, 255, 255, 0.23);
  border-radius: 4px;
  background: rgba(10, 15, 22, 0.78);
  padding: 7px 9px;
  color: #dbe5f1;
  font-size: 10px;
  backdrop-filter: blur(10px);
}

.world-meta strong {
  color: #fff;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.world-meta i {
  width: 1px;
  height: 12px;
  margin: 0 4px;
  background: var(--line-strong);
}

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: 20px;
  max-width: min(420px, calc(100% - 32px));
  transform: translateX(-50%);
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: #101720;
  padding: 10px 13px;
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.38);
  color: #eef5ef;
  font-size: 12px;
}

.toast.error {
  border-color: rgba(239, 118, 100, 0.65);
}

.loading {
  position: fixed;
  z-index: 50;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a0f16;
  color: var(--muted);
  font-size: 13px;
}

.loading[hidden] {
  display: none;
}

.spinner {
  width: 19px;
  height: 19px;
  margin-right: 11px;
  border: 2px solid var(--line);
  border-top-color: var(--mint);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (min-width: 901px) {
  .studio:not(.is-open) {
    top: 78px;
    bottom: auto;
    width: 48px;
    grid-template-rows: auto;
  }

  .studio:not(.is-open) .studio-head {
    min-height: 46px;
    border-bottom: 0;
    padding: 6px;
  }

  .studio:not(.is-open) .studio-head > div,
  .studio:not(.is-open) .world-editor,
  .studio:not(.is-open) .signed-out {
    display: none;
  }
}

@media (max-width: 900px) {
  .topbar {
    top: 8px;
    width: calc(100% - 16px);
    min-height: 50px;
    grid-template-columns: auto 1fr auto;
    gap: 8px;
  }

  .brand > span:last-child {
    display: none;
  }

  .world-switch {
    justify-self: center;
  }

  .world-switch a {
    min-width: 78px;
    padding: 6px 9px;
    font-size: 11px;
  }

  .utility-nav a:first-child {
    display: none;
  }

  .studio {
    top: auto;
    right: 8px;
    bottom: 8px;
    left: 8px;
    width: auto;
    height: min(64dvh, 590px);
    max-height: calc(100dvh - 72px);
    transition: transform 0.2s ease;
  }

  .studio:not(.is-open) {
    transform: translateY(calc(100% - 58px));
  }

  .studio-head {
    min-height: 57px;
  }

  .selection-bar {
    right: 10px;
    bottom: 74px;
    left: 10px;
    max-width: none;
    justify-content: space-between;
  }

  .studio.is-open ~ .selection-bar {
    display: none;
  }

  .world-meta {
    top: 68px;
    left: 10px;
  }

  .toast {
    bottom: 76px;
  }
}

@media (max-width: 520px) {
  .topbar {
    padding-left: 7px;
  }

  .utility-nav a {
    padding: 7px;
    font-size: 11px;
  }

  .studio {
    height: min(72dvh, 620px);
  }

  .world-settings {
    padding: 12px;
  }

  .selection-copy {
    max-width: 120px;
  }

  .selection-actions button {
    padding: 5px 7px;
    font-size: 9px;
  }
}

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

  .spinner {
    animation-duration: 1.5s;
  }
}
