:root {
  --bg: #ffffff;
  --panel: #f6f7f9;
  --panel-2: #eceef2;
  --border: #d9dce3;
  --text: #1a1d24;
  --muted: #6b7180;
  --accent: #ff3b6b;
  --accent-2: #3b6bff;
  --select: #16a34a;
  --avail: #e6e8ee;
  --owned: #9aa1b4;
  --mine: #ffb020;
  --radius: 12px;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: #fff;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo-mark { width: 46px; height: 46px; border-radius: 12px; flex: 0 0 auto; box-shadow: 0 4px 14px rgba(59,107,255,0.18); }
.brand-text h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.03em;
}
.brand-accent {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tagline { margin: 3px 0 0; font-size: 13px; color: var(--muted); letter-spacing: 0.01em; }

.stats { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; min-width: 230px; }
.stat-nums { display: flex; gap: 26px; }
.stat { text-align: right; }
.stat-num { display: block; font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.07em; }
.progress {
  width: 230px; height: 6px; border-radius: 999px;
  background: var(--panel-2); overflow: hidden; border: 1px solid var(--border);
}
.progress-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 999px; transition: width 0.4s ease;
}

/* Hero */
.hero {
  padding: 16px 28px 4px;
  max-width: 820px;
}
.hero-line { margin: 0; font-size: 15px; line-height: 1.5; color: var(--text); font-weight: 500; }
.hero-history { margin: 6px 0 0; font-size: 13px; line-height: 1.5; color: var(--muted); }
.hero-history em { font-style: italic; color: var(--text); opacity: 0.85; }

@media (max-width: 620px) {
  .stats { align-items: flex-start; }
  .stat { text-align: left; }
  .progress, .stats { width: 100%; }
}

/* Main layout */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 20px;
  padding: 16px 28px 24px;
  min-height: 0;
}

/* Canvas */
.canvas-wrap { display: flex; flex-direction: column; min-width: 0; }
.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.toolbar-group { display: flex; align-items: center; gap: 8px; }
.tool-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.tool-btn.text { width: auto; padding: 0 12px; font-size: 13px; }
.tool-btn:hover { border-color: var(--accent-2); }
.zoom-label { font-size: 13px; color: var(--muted); min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; }

/* Zoom slider */
#zoomSlider {
  -webkit-appearance: none;
  appearance: none;
  width: 160px;
  height: 6px;
  border-radius: 4px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  cursor: pointer;
}
#zoomSlider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  cursor: pointer;
}
#zoomSlider::-moz-range-thumb {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  cursor: pointer;
}
.toolbar-hint { font-size: 13px; color: var(--muted); }

.stage {
  position: relative;
  flex: 1;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 420px;
  max-height: 78vh;
  cursor: crosshair;
}
#grid {
  display: block;
  position: absolute;
  top: 0; left: 0;
  image-rendering: pixelated;
}

/* Grid lines drawn over the white canvas (graph-paper look) */
.grid-overlay {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(40, 50, 70, 0.28);
}
.selection-box {
  position: absolute;
  border: 1.5px solid var(--select);
  background: rgba(34, 197, 94, 0.18);
  pointer-events: none;
}
.selection-box.designing {
  border: 1.5px dashed var(--select);
  background: transparent;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.6);
}
.coord-readout {
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  height: 16px;
}

/* Sidebar */
.sidebar { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(20, 30, 60, 0.04);
}
.panel h2 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); font-weight: 700; }
.sel-empty { font-size: 13px; color: var(--muted); line-height: 1.5; }

.sel-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.sel-row span { color: var(--muted); }
.sel-row.total { border-bottom: none; font-size: 16px; padding-top: 12px; }
.sel-row.total b { color: var(--accent); font-size: 20px; }
.sel-warn { color: #b26b00; font-size: 12.5px; margin: 8px 0 0; }
.sel-actions { display: flex; gap: 8px; margin-top: 14px; }

/* Buttons */
.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
  flex: 1;
}
.btn.small { padding: 7px 12px; font-size: 13px; flex: 0 0 auto; }
.btn.primary { background: linear-gradient(135deg, var(--accent), #ff5c84); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { border-color: var(--accent-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* Legend */
.legend ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 10px 18px; }
.legend li { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); }
.swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--border); display: inline-block; }
.swatch.avail { background: var(--avail); }
.swatch.owned { background: var(--owned); }
.swatch.mine { background: var(--mine); }
.swatch.selecting { background: rgba(34, 197, 94, 0.5); border-color: var(--select); }

/* Owned list */
.owned-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.owned-list li {
  display: flex; align-items: center; gap: 10px;
  font-size: 13px;
  background: var(--panel-2);
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.owned-list li:hover { outline: 1px solid var(--accent-2); }
.owned-chip { width: 16px; height: 16px; border-radius: 4px; border: 1px solid var(--border); flex: 0 0 auto; }
.owned-meta { color: var(--muted); margin-left: auto; font-variant-numeric: tabular-nums; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(6, 8, 13, 0.7);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal-step[hidden] { display: none; }
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 460px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.5);
}
.modal h2 { margin: 0 0 6px; font-size: 20px; }
.modal-sub { margin: 0 0 18px; color: var(--muted); font-size: 13.5px; line-height: 1.5; }
.confirm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.confirm-grid > div { background: var(--panel-2); padding: 12px 14px; }
.confirm-grid span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.confirm-grid b { font-size: 16px; }
.confirm-total { grid-column: 1 / -1; }
.confirm-total b { color: var(--accent); font-size: 22px; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }

/* Design tools */
.design-hint { font-size: 12.5px; color: var(--muted); line-height: 1.5; margin: 0 0 14px; }
.paint-tools { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
input[type="color"] {
  width: 44px; height: 44px; padding: 0; border: 1px solid var(--border);
  border-radius: 10px; background: none; cursor: pointer;
}
.preset-colors { display: flex; gap: 6px; flex-wrap: wrap; }
.preset {
  width: 24px; height: 24px; border-radius: 6px; border: 1px solid var(--border); cursor: pointer;
}
.preset[title="#ffffff"] { border-color: var(--muted); }
.preset:hover { transform: scale(1.1); }

.tool-toggle {
  display: flex; gap: 6px; margin-bottom: 12px;
  background: var(--panel-2); padding: 4px; border-radius: 10px; border: 1px solid var(--border);
}
.toggle-btn {
  flex: 1; padding: 7px 10px; border: none; background: transparent; color: var(--muted);
  font-size: 13px; font-weight: 600; border-radius: 7px; cursor: pointer;
}
.toggle-btn.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.08); }

.image-tools { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.image-tools #uploadBtn { flex: 0 0 auto; }
.fit-toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.fit-toggle input { accent-color: var(--accent); }

/* Confirm preview */
.confirm-body { display: flex; gap: 16px; align-items: center; margin-bottom: 4px; flex-wrap: wrap; }
.preview-wrap {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 8px;
  min-width: 88px; min-height: 88px;
}
#confirmPreview { image-rendering: pixelated; display: block; max-width: 220px; }
.confirm-body .confirm-grid { flex: 1; min-width: 180px; grid-template-columns: 1fr 1fr; }

/* Buy form (username / email / message) */
.buy-form { margin-top: 18px; display: flex; flex-direction: column; gap: 12px; }
.buy-form .field { display: flex; flex-direction: column; gap: 5px; }
.buy-form label { font-size: 12.5px; color: var(--muted); }
.buy-form .req { color: var(--accent); }
.buy-form .opt { opacity: 0.7; }
.buy-form input, .buy-form textarea {
  width: 100%;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 14px;
  color: var(--text);
  font-family: inherit;
  resize: vertical;
}
.buy-form input:focus, .buy-form textarea:focus { outline: none; border-color: var(--accent-2); }
.buying-as { margin: 0; font-size: 13.5px; color: var(--text); }
.buying-as b { color: var(--accent-2); }
.form-error { margin: 0; font-size: 13px; color: #d12b52; }

/* Hover tooltip over a plot */
.plot-tooltip {
  position: absolute;
  z-index: 20;
  max-width: 220px;
  background: #1a1d24;
  color: #fff;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  line-height: 1.4;
  pointer-events: none;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  transform: translate(-50%, calc(-100% - 12px));
}
.plot-tooltip::after {
  content: "";
  position: absolute;
  left: 50%; bottom: -5px;
  width: 10px; height: 10px;
  background: #1a1d24;
  transform: translateX(-50%) rotate(45deg);
}
.plot-tooltip .pt-user { font-weight: 700; color: #ffb020; }
.plot-tooltip .pt-msg { margin-top: 3px; color: #e7e9f0; word-wrap: break-word; }
.plot-tooltip[hidden] { display: none; }

/* Error / status banner */
.banner {
  position: fixed;
  left: 50%; bottom: 22px;
  transform: translateX(-50%);
  background: #1a1d24;
  color: #fff;
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  z-index: 100;
  max-width: 90vw;
}
.banner[hidden] { display: none; }

/* Footer */
.footer { padding: 14px 24px; border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted); }
.footer .muted { opacity: 0.75; }

@media (max-width: 860px) {
  main { grid-template-columns: 1fr; }
  .sidebar { order: 2; }
}
