:root{
  --bg: #14161b;
  --panel: #1b1e26;
  --panel-alt: #20242c;
  --line: #2b2f3b;
  --text: #e9e8e2;
  --muted: #868b9c;
  --lime: #4caf50;
  --lime-dim: #3d8b40;
  --lime-text: #eafcea;
  --coral: #ff6f5e;
  --radius: 3px;
  --display: 'Space Grotesk', sans-serif;
  --mono: 'JetBrains Mono', monospace;
  --body: 'Inter', sans-serif;
}

*{ box-sizing: border-box; }

html, body{
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--body);
  min-height: 100vh;
}

body{
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 28px 28px;
  background-position: -1px -1px;
  background-attachment: fixed;
  background-color: var(--bg);
}

.grain{
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background: radial-gradient(ellipse at top left, rgba(76,175,80,0.07), transparent 55%),
              radial-gradient(ellipse at bottom right, rgba(255,111,94,0.04), transparent 55%);
}

button{ font-family: inherit; }

.topbar{
  position: relative; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(28px + 70px) clamp(20px, 5vw, 56px) 18px;
  border-bottom: 1px solid var(--line);
}
.brand{ display: flex; align-items: center; gap: 14px; }
.brand-mark{
  width: 34px; height: 34px; flex: none;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px; overflow: hidden;
  image-rendering: pixelated;
  box-shadow: 0 0 0 1px var(--line);
}
.brand-mark img{ width: 100%; height: 100%; display: block; image-rendering: pixelated; }
.brand-text h1{
  font-family: var(--display); font-weight: 700; font-size: 20px;
  margin: 0; letter-spacing: -0.01em;
}
.brand-text p{
  margin: 2px 0 0; font-size: 13px; color: var(--muted); font-family: var(--mono);
}
.tabs{
  position: relative; z-index: 1;
  display: flex; gap: 2px;
  padding: 0 clamp(20px, 5vw, 56px);
  background: var(--panel-alt);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.tab{
  flex: 1; min-width: 200px;
  background: transparent; border: none; border-bottom: 2px solid transparent;
  color: var(--muted); cursor: pointer; text-align: left;
  padding: 16px 18px; display: flex; flex-direction: column; gap: 4px;
  transition: background .15s, color .15s, border-color .15s;
}
.tab:hover{ background: rgba(255,255,255,0.02); color: var(--text); }
.tab.is-active{ color: var(--text); border-bottom-color: var(--lime); background: var(--bg); }
.tab-index{ font-family: var(--mono); font-size: 11px; color: var(--lime-dim); }
.tab.is-active .tab-index{ color: var(--lime); }
.tab-label{ font-family: var(--display); font-weight: 500; font-size: 15px; }
.tab-sub{ font-size: 12px; color: var(--muted); font-family: var(--mono); }

/* ---------- layout ---------- */
main{ position: relative; z-index: 1; max-width: 1220px; margin: 0 auto; padding: 0 clamp(20px, 5vw, 56px); }
.tool{ display: none; grid-template-columns: 1fr 1fr; gap: 48px; padding: 40px 0 64px; }
.tool.is-active{ display: grid; }
@media (max-width: 860px){ .tool{ grid-template-columns: 1fr; } }

@media (max-width: 480px){
  .topbar{ padding-top: calc(28px + 76px); }
}

.col h2{
  font-family: var(--display); font-size: 22px; font-weight: 700; margin: 0 0 20px;
  display: flex; flex-direction: column; gap: 2px;
}
.eyebrow{
  font-family: var(--mono); font-size: 11px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--lime-dim); font-weight: 400;
}

.dropzone{
  border: 1px dashed var(--line); border-radius: var(--radius);
  background: var(--panel);
  padding: 32px 20px; text-align: center; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.is-drag{ border-color: var(--lime-dim); background: var(--panel-alt); }
.dropzone-inner p{ margin: 6px 0 0; font-size: 14px; }
.dropzone-inner svg{ color: var(--muted); }
.dropzone-inner .hint{ font-size: 12px; color: var(--muted); font-family: var(--mono); }

.filerow{
  display: flex; justify-content: space-between; align-items: baseline; gap: 12px;
  margin-top: 12px; padding: 10px 14px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); font-family: var(--mono); font-size: 12px;
}
.filerow-name{ color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filerow-dims{ color: var(--muted); flex: none; }

.preview-frame{
  margin-top: 16px; border: 1px solid var(--line); border-radius: var(--radius);
  background:
    linear-gradient(45deg, #23262f 25%, transparent 25%) -8px 0/16px 16px,
    linear-gradient(-45deg, #23262f 25%, transparent 25%) -8px 0/16px 16px,
    linear-gradient(45deg, transparent 75%, #23262f 75%) -8px 0/16px 16px,
    linear-gradient(-45deg, transparent 75%, #23262f 75%) -8px 0/16px 16px,
    var(--panel);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  min-height: 160px; max-height: 320px;
}
.preview-frame canvas{ max-width: 100%; max-height: 320px; image-rendering: pixelated; display: block; }

.field-row{ display: flex; gap: 14px; margin-top: 18px; }
.field{ flex: 1; display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); font-family: var(--mono); }
.field select{
  font-family: var(--mono); font-size: 14px; color: var(--text);
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 10px; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23868b9c'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.field select:focus{ outline: 2px solid var(--lime-dim); outline-offset: 1px; }

.btn{
  border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer;
  font-size: 14px; padding: 12px 18px; transition: transform .1s, background .15s, border-color .15s, color .15s;
}
.btn:active{ transform: translateY(1px); }
.btn:disabled{ opacity: .4; cursor: not-allowed; }
.btn-primary{
  margin-top: 20px; width: 100%; background: var(--lime); color: var(--lime-text); border-color: var(--lime); font-weight: 600;
}
.btn-primary:not(:disabled):hover{ background: #5cbf60; }
.btn-secondary{
  background: var(--panel); color: var(--text); font-family: var(--mono); font-size: 13px;
}
.btn-secondary:hover{ border-color: var(--lime-dim); color: var(--lime); }
.btn-row{ display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.btn-row .btn{ flex: 1; min-width: 160px; }

.error{
  margin-top: 12px; color: var(--coral); font-family: var(--mono); font-size: 13px;
  border: 1px solid rgba(255,111,94,0.35); background: rgba(255,111,94,0.08);
  padding: 10px 12px; border-radius: var(--radius);
}
.status{
  margin-top: 12px; color: var(--lime); font-family: var(--mono); font-size: 12px;
}

.stat-strip{ display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
.stat{
  flex: 1; min-width: 110px; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px;
  background: var(--panel); text-align: left;
}
.stat-value{ display: block; font-family: var(--display); font-size: 26px; font-weight: 700; }
.stat-label{ font-family: var(--mono); font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.stat-highlight{ border-color: var(--lime-dim); background: rgba(76,175,80,0.1); }
.stat-highlight .stat-value{ color: var(--lime); }

.reorder-hint{
  margin: 0 0 8px; font-family: var(--mono); font-size: 11px; color: var(--muted);
}
.reorder-hint:empty, .reorder-hint[hidden]{ display: none; }

.tile-grid{
  display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px; max-height: 420px; overflow-y: auto; padding: 4px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel);
}
.tile-grid:empty{ display: none; }

.tile-cell {
  position: relative;
  display: inline-block;
  width: 100%;
  aspect-ratio: 1;
}

.tile-cell canvas{
  width: 100%; aspect-ratio: 1; image-rendering: pixelated; border: 1px solid var(--line);
  background:
    linear-gradient(45deg, #262a34 25%, transparent 25%) -6px 0/12px 12px,
    linear-gradient(-45deg, #262a34 25%, transparent 25%) -6px 0/12px 12px,
    linear-gradient(45deg, transparent 75%, #262a34 75%) -6px 0/12px 12px,
    linear-gradient(-45deg, transparent 75%, #262a34 75%) -6px 0/12px 12px,
    var(--panel-alt);
  animation: tile-in .25s ease both;
  cursor: grab;
}

.tile-cell.is-dragging canvas { opacity: .35; cursor: grabbing; }
.tile-cell.is-drop-target { outline: 2px solid var(--lime); outline-offset: -2px; }

@keyframes tile-in{ from{ opacity: 0; transform: scale(.9); } to{ opacity: 1; transform: scale(1); } }
@media (prefers-reduced-motion: reduce){
  .tile-cell canvas{ animation: none; }
}

.tile-delete {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  background: var(--coral);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 12px;
  font-weight: bold;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 10;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.tile-cell:hover .tile-delete {
  opacity: 1;
  transform: scale(1);
}

.tile-delete:hover {
  background: #ff8577;
}

footer{
  position: relative; z-index: 1; text-align: center; padding: 28px 20px 40px;
  font-family: var(--mono); font-size: 12px;
}
footer p{
  display: inline-block; margin: 0; padding: 10px 16px;
  color: var(--coral); border: 1px solid rgba(255,111,94,0.35); background: rgba(255,111,94,0.08);
  border-radius: var(--radius);
}