:root {
  --bg: #2a2f3a;
  --bg-2: #313744;
  --panel: #373e4d;
  --panel-2: #424a5c;
  --border: #545d72;
  --text: #f3f5fa;
  --muted: #b7c0d4;
  --accent: #7fe1d8;
  --accent-2: #f6b878;
  --ok: #a3e08e;
  --err: #ec9a88;
  --mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: radial-gradient(130% 130% at 85% -15%, #3a4150 0%, var(--bg) 60%);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  padding: 0.85rem;
  gap: 0.85rem;
}

/* ---- top bar ---- */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 0.7rem; }
.logo { font-size: 2rem; filter: drop-shadow(0 2px 5px rgba(0,0,0,0.5)); }
.brand-text h1 {
  margin: 0;
  font-size: 1.45rem;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand-text p { margin: 0.1rem 0 0; font-size: 0.8rem; color: var(--muted); }

.tools { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.field { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; color: var(--muted); }
.field select, .field input[type=range] { cursor: pointer; }
.field select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 0.35rem 0.5rem;
  font-family: var(--sans);
}
.spd input { width: 110px; accent-color: var(--accent); }
.field input[type=number] {
  width: 4.6rem;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 0.35rem 0.4rem;
  font-family: var(--mono);
  font-size: 0.82rem;
}
.gridsize .times { color: var(--muted); }

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: #4d5670; border-color: #647095; }
.btn-run { background: linear-gradient(180deg, #34857a, #277168); border-color: #41998d; color: #eafffb; }
.btn-run:hover { background: linear-gradient(180deg, #3d9a8e, #2f8579); }
.btn-run.running { background: linear-gradient(180deg, #a86e36, #875427); border-color: #bd8044; }

/* ---- main layout ---- */
.main {
  display: grid;
  grid-template-columns: 1fr clamp(290px, 28%, 400px);
  gap: 0.85rem;
  min-height: 0;
}

.stage {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
#grid {
  flex: 1;
  width: 100%;
  min-height: 0;
  border-radius: 8px;
  cursor: crosshair;
  touch-action: none;
}
.stage-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 0.5rem;
  font-size: 0.8rem;
}
.status { color: var(--accent); font-family: var(--mono); }

/* ---- side ---- */
.side {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  min-height: 0;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem 0.7rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.phead {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}
.dim { color: var(--muted); font-weight: 400; font-size: 0.74rem; }

.palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.swatch {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 7px;
  border: 2px solid transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1;
  user-select: none;
  transition: transform 0.08s;
}
.swatch:hover { transform: translateY(-2px); }
.swatch.sel { border-color: #fff; box-shadow: 0 0 0 2px rgba(255,255,255,0.35); }
.swatch.erase {
  background: var(--panel-2);
  border: 2px dashed var(--border);
  color: var(--muted);
  font-size: 1rem;
}

.io-panel { flex: 0 0 auto; }
.output {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.95rem;
  color: var(--ok);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.6rem;
  min-height: 2.4rem;
  max-height: 5.5rem;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.log-panel { flex: 1 1 0; }
.log {
  flex: 1;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.76rem;
  line-height: 1.5;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.55rem;
  min-height: 0;
}
.log-row { white-space: nowrap; }
.log-row .lt { color: var(--muted); }
.log-row .lc { font-weight: 700; }
.log-row .lop { color: var(--text); }
.log-row .ln { color: var(--accent-2); }

/* ---- help overlay ---- */
.help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8,10,14,0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 50;
}
.help-overlay[hidden] { display: none; }
.help-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  max-width: 720px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}
.help-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--border);
}
.help-head h2 { margin: 0; font-size: 1.1rem; color: var(--accent); }
.help-body { padding: 1rem 1.1rem; overflow-y: auto; }
.help-body p { margin: 0 0 0.8rem; line-height: 1.55; font-size: 0.9rem; }
.help-ops {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem 1.1rem;
  margin: 0.4rem 0 1rem;
}
.op-row { display: flex; align-items: center; gap: 0.55rem; font-size: 0.84rem; }
.op-chip {
  width: 1.7rem; height: 1.7rem;
  flex: 0 0 auto;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.8rem;
}
.op-name { font-weight: 600; }
.op-desc { color: var(--muted); }

@media (max-width: 820px) {
  .main { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .help-ops { grid-template-columns: 1fr; }
}
