:root {
  --gold: #f5c35b;
  --gold-bright: #ffe6a3;
  --teal: #5ee0c8;
  --ink: #071018;
  --panel: rgba(7, 17, 26, 0.78);
  --border: rgba(255, 255, 255, 0.13);
  --text: #f3eee4;
  --muted: rgba(243, 238, 228, 0.58);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #061019;
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

body { position: fixed; inset: 0; touch-action: none; }

#editorApp { position: fixed; inset: 0; overflow: hidden; }

#editorCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
}

/* --------------------------------------------------------------------------
   Header
   -------------------------------------------------------------------------- */
.ed-header {
  position: absolute;
  top: calc(14px + var(--safe-top));
  left: 16px;
  right: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.28);
}

.ed-brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  padding: 6px 8px;
  color: var(--gold-bright);
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-decoration: none;
  white-space: nowrap;
}

.brand-gem {
  color: var(--gold);
  font-size: 15px;
  text-shadow: 0 0 12px rgba(245, 195, 91, 0.7);
}

.level-name-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 130px;
  flex: 1 1 210px;
  padding: 0 10px;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.035);
}

.level-label {
  color: rgba(243, 238, 228, 0.4);
  font-size: 10px;
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.level-name {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: 600 13px/1 inherit;
  letter-spacing: 0.03em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 7px;
  flex: 0 0 auto;
}

.ed-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.045);
  color: rgba(243, 238, 228, 0.76);
  font: 600 12px/1 inherit;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: color 0.18s ease, border-color 0.18s ease, background 0.18s ease, transform 0.18s ease;
  white-space: nowrap;
}

.ed-btn:hover {
  color: var(--gold-bright);
  border-color: rgba(245, 195, 91, 0.35);
  background: rgba(245, 195, 91, 0.08);
}

.ed-btn:active { transform: translateY(1px); }

.ed-btn.primary {
  border-color: rgba(255, 230, 163, 0.55);
  background: linear-gradient(135deg, #ffe6a3, #f5c35b 55%, #c78b2d);
  color: #1d1304;
  font-weight: 800;
  box-shadow: 0 6px 18px rgba(245, 195, 91, 0.18);
}

.ed-btn.primary:hover { color: #1d1304; filter: brightness(1.04); }

.ed-btn.ghost { color: rgba(243, 238, 228, 0.45); }

.file-btn { position: relative; overflow: hidden; }
.file-btn input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

/* --------------------------------------------------------------------------
   Left palette
   -------------------------------------------------------------------------- */
.palette {
  position: absolute;
  z-index: 9;
  top: calc(82px + var(--safe-top));
  left: 16px;
  width: 156px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.24);
}

.panel-eyebrow {
  color: rgba(243, 238, 228, 0.36);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  margin-bottom: 2px;
}

.tool {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  height: 36px;
  padding: 0 9px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: rgba(243, 238, 228, 0.64);
  font: 600 12px/1 inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.tool:hover { background: rgba(255, 255, 255, 0.055); color: var(--text); }

.tool.active {
  border-color: rgba(245, 195, 91, 0.3);
  background: rgba(245, 195, 91, 0.12);
  color: var(--gold-bright);
}

.tool.danger.active { border-color: rgba(255, 125, 114, 0.35); background: rgba(255, 125, 114, 0.12); color: #ffb2aa; }
.tool.danger:hover { color: #ffb2aa; }
.tool.subtle { color: rgba(243, 238, 228, 0.36); font-size: 11px; }
.tool.subtle:hover { color: rgba(243, 238, 228, 0.72); }

.tool-dot {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  border-radius: 50%;
  box-shadow: 0 0 8px currentColor;
}
.tool-dot.chest { background: radial-gradient(circle at 35% 30%, #fff0bd, #f5c35b 45%, #a86a1c); color: var(--gold); }
.tool-dot.tree { background: #3f7a45; border-radius: 3px 3px 1px 1px; color: #3f7a45; }
.tool-dot.rock { background: #66727c; border-radius: 4px; color: #66727c; }

.tool-icon {
  width: 14px;
  text-align: center;
  font-size: 14px;
  line-height: 1;
}

.panel-divider { height: 1px; margin: 3px 2px; background: rgba(255, 255, 255, 0.075); }

.palette-stats {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  margin-top: 4px;
  padding-top: 9px;
  border-top: 1px solid rgba(255, 255, 255, 0.075);
  color: rgba(243, 238, 228, 0.35);
  font-size: 9px;
  letter-spacing: 0.04em;
}

.palette-stats b { color: rgba(245, 195, 91, 0.9); font-weight: 700; }

/* --------------------------------------------------------------------------
   Inspector
   -------------------------------------------------------------------------- */
.inspector {
  position: absolute;
  z-index: 9;
  right: 16px;
  top: calc(82px + var(--safe-top));
  width: 236px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.24);
}

.inspector.hidden { display: none; }

.inspector-type {
  margin: 3px 0 12px;
  color: var(--gold-bright);
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }

.field-row label,
.range-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  color: rgba(243, 238, 228, 0.42);
  font-size: 9px;
  letter-spacing: 0.08em;
}

.field-row input[type="number"] {
  width: 100%;
  height: 32px;
  padding: 0 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  outline: 0;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font: 600 12px/1 inherit;
  font-variant-numeric: tabular-nums;
}

.range-field > span {
  float: right;
  color: rgba(243, 238, 228, 0.62);
  font-variant-numeric: tabular-nums;
}

.range-field { margin-bottom: 11px; }

input[type="range"] {
  width: 100%;
  height: 20px;
  margin: 0;
  accent-color: var(--gold);
}

.danger-btn {
  width: 100%;
  margin-top: 4px;
  border-color: rgba(255, 125, 114, 0.28);
  color: #ffaaa1;
}

.danger-btn:hover { border-color: rgba(255, 125, 114, 0.55); background: rgba(255, 125, 114, 0.1); color: #ffd0ca; }

/* --------------------------------------------------------------------------
   Hint / toast / loading
   -------------------------------------------------------------------------- */
.hint {
  position: absolute;
  z-index: 8;
  left: 50%;
  bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%);
  padding: 9px 16px;
  border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 999px;
  background: rgba(5, 13, 20, 0.52);
  color: rgba(243, 238, 228, 0.38);
  font-size: 10px;
  letter-spacing: 0.05em;
  white-space: nowrap;
  pointer-events: none;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.toast {
  position: absolute;
  z-index: 20;
  top: calc(82px + var(--safe-top));
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 9px 16px;
  border: 1px solid rgba(245, 195, 91, 0.3);
  border-radius: 999px;
  background: rgba(7, 17, 26, 0.88);
  color: var(--gold-bright);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.03em;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  white-space: nowrap;
  max-width: calc(100vw - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: rgba(255, 125, 114, 0.35); color: #ffb5ad; }

.loading {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: #061019;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 0.08em;
}

/* --------------------------------------------------------------------------
   Share dialog
   -------------------------------------------------------------------------- */
.share-dialog {
  position: absolute;
  inset: 0;
  z-index: 25;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(2, 7, 11, 0.68);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.share-dialog.hidden { display: none; }

.share-card {
  width: min(480px, 100%);
  padding: 24px;
  border: 1px solid rgba(245, 195, 91, 0.3);
  border-radius: 20px;
  background: rgba(7, 17, 26, 0.94);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
}

.share-card h3 {
  margin: 8px 0 6px;
  font-family: "Georgia", "Songti SC", "Microsoft YaHei", serif;
  font-size: 26px;
  color: var(--gold-bright);
}

.share-card p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.share-card input {
  width: 100%;
  height: 38px;
  padding: 0 10px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 9px;
  outline: 0;
  background: rgba(255, 255, 255, 0.04);
  color: rgba(243, 238, 228, 0.72);
  font: 11px/1 "Consolas", monospace;
}

.share-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.share-actions .ed-btn { flex: 1; height: 36px; }

.loader-gem {
  color: var(--gold);
  font-size: 28px;
  text-shadow: 0 0 26px rgba(245, 195, 91, 0.5);
  animation: gemPulse 1.6s ease-in-out infinite;
}

@keyframes gemPulse {
  0%, 100% { transform: translateY(0) rotate(0deg); opacity: 0.7; }
  50% { transform: translateY(-5px) rotate(180deg); opacity: 1; }
}

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */
@media (max-width: 1080px) {
  .ed-header {
    flex-wrap: wrap;
    gap: 8px;
    padding: 9px;
  }

  .ed-brand { font-size: 13px; }
  .level-name-wrap { order: 3; flex: 1 1 100%; height: 32px; }
  .header-actions { margin-left: auto; overflow-x: auto; max-width: 100%; }
  .ed-btn { height: 31px; padding: 0 9px; font-size: 11px; }
  .palette { top: calc(122px + var(--safe-top)); width: 140px; padding: 9px; }
  .inspector { top: calc(122px + var(--safe-top)); width: 210px; }
  .hint { display: none; }
}

@media (max-width: 700px) {
  .ed-header { left: 8px; right: 8px; top: calc(8px + var(--safe-top)); border-radius: 14px; }
  .ed-brand { padding: 3px 4px; }
  .ed-brand span:last-child { display: none; }
  .header-actions { gap: 4px; }
  .ed-btn { height: 29px; padding: 0 8px; font-size: 10px; }
  .ed-btn.ghost { display: none; }
  .level-name-wrap { height: 30px; }

  .palette {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: calc(8px + var(--safe-bottom));
    width: auto;
    flex-direction: row;
    align-items: center;
    gap: 4px;
    padding: 7px;
    overflow-x: auto;
    border-radius: 13px;
  }

  .palette .panel-eyebrow,
  .palette .panel-divider,
  .palette .palette-stats,
  .palette .subtle { display: none; }

  .tool { width: auto; flex: 0 0 auto; height: 32px; padding: 0 8px; font-size: 11px; gap: 6px; }
  .tool-icon { font-size: 12px; }
  .tool-dot { width: 9px; height: 9px; }

  .inspector {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: calc(56px + var(--safe-bottom));
    width: auto;
    padding: 10px;
  }

  .inspector-type { margin: 2px 0 8px; font-size: 13px; }
  .field-row { grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 6px; }
  .field-row input[type="number"] { height: 28px; }
  .range-field { margin-bottom: 6px; }
  .danger-btn { height: 30px; margin-top: 2px; }
}

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