:root {
  color-scheme: dark;
  --bg: #07090c;
  --panel: #111720;
  --panel-2: #161d27;
  --line: #2c3745;
  --text: #e7edf2;
  --muted: #9aa8b8;
  --accent: #f0c95a;
  --cyan: #68d8d6;
  --red: #f26f63;
  --green: #8ddf9a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 50% 0%, #18202a 0, #07090c 42rem);
  color: var(--text);
  font-family: "Segoe UI", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
}

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

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #1b2430;
  color: var(--text);
  padding: 0.45rem 0.65rem;
  cursor: pointer;
}

button:hover, button.active { border-color: var(--cyan); color: #ffffff; }
button.primary { background: #243743; border-color: var(--cyan); }

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.2rem 0.8rem;
  border-bottom: 1px solid var(--line);
  background: rgba(7, 9, 12, 0.82);
  position: sticky;
  top: 0;
  z-index: 20;
}

h1, h2, p { margin: 0; }
h1 { font-size: clamp(1.3rem, 2.2vw, 2.15rem); letter-spacing: 0; }
h2 { font-size: 0.78rem; text-transform: uppercase; color: var(--cyan); margin-bottom: 0.7rem; }
.topbar p { color: var(--muted); margin-top: 0.2rem; }

.readout {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.readout span, .metric {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.42rem 0.6rem;
  background: #111720;
  white-space: nowrap;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(250px, 320px) minmax(420px, 1fr) minmax(270px, 350px);
  gap: 0.8rem;
  padding: 0.8rem;
  min-height: 0;
}

.panel, .stage-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(17, 23, 32, 0.92);
  min-width: 0;
}

.panel {
  overflow: auto;
  max-height: calc(100vh - 6rem);
}

.group {
  padding: 0.9rem;
  border-bottom: 1px solid var(--line);
}

.group:last-child { border-bottom: 0; }

label {
  display: grid;
  gap: 0.32rem;
  color: var(--muted);
  font-size: 0.82rem;
  margin: 0.58rem 0;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #080c12;
  color: var(--text);
  padding: 0.45rem 0.55rem;
}

input[type="range"] { padding: 0; accent-color: var(--accent); }
textarea {
  min-height: 9rem;
  resize: vertical;
  font-family: Consolas, "Cascadia Mono", monospace;
  font-size: 0.84rem;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  align-items: center;
}

.button-row.compact { justify-content: flex-end; }

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.45rem;
}

.metric {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  color: var(--muted);
  margin: 0.6rem 0;
}

.metric strong { color: var(--accent); font-weight: 600; }
.check { display: flex; grid-template-columns: auto 1fr; align-items: center; gap: 0.5rem; }
.check input { width: auto; }

.stage-panel {
  display: flex;
  flex-direction: column;
  min-height: 34rem;
  overflow: hidden;
}

.stage-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.65rem;
  border-bottom: 1px solid var(--line);
}

.tabs {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 24rem;
  background: #05070a;
  overflow: hidden;
}

canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#sandCanvas { pointer-events: auto; }

#exciterHandle {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 9999px transparent, 0 0 16px rgba(240, 201, 90, 0.7);
  pointer-events: none;
}

.statusline {
  display: flex;
  justify-content: space-between;
  gap: 0.8rem;
  padding: 0.55rem 0.7rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
  font-size: 0.78rem;
}

.mode-list {
  display: grid;
  gap: 0.38rem;
  max-height: 16rem;
  overflow: auto;
}

.atlas-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
  margin-bottom: 0.6rem;
}

.mode-atlas {
  display: grid;
  grid-template-columns: repeat(var(--atlas-cols, 6), minmax(2.25rem, 1fr));
  gap: 0.3rem;
}

.atlas-cell {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #05070a;
  min-width: 0;
  aspect-ratio: 1;
  padding: 0;
  overflow: hidden;
}

.atlas-cell.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(240, 201, 90, 0.45);
}

.atlas-cell canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.atlas-cell span {
  position: absolute;
  left: 0.25rem;
  bottom: 0.15rem;
  font-size: 0.62rem;
  color: #f6e7b0;
  text-shadow: 0 1px 2px #000;
  pointer-events: none;
}

.mode-row {
  display: grid;
  grid-template-columns: 4.6rem 5.2rem 1fr 2.4rem;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.4rem;
  background: #0b1017;
  cursor: pointer;
  font-size: 0.78rem;
}

.mode-row.selected { border-color: var(--accent); }
.bar { height: 0.42rem; background: #273240; border-radius: 999px; overflow: hidden; }
.bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--cyan), var(--accent)); }

.keyboard {
  display: grid;
  grid-template-columns: repeat(14, minmax(1.2rem, 1fr));
  gap: 0.25rem;
  margin-top: 0.7rem;
}

.key {
  min-height: 1.8rem;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #0b1017;
  color: var(--muted);
  display: grid;
  place-items: center;
  font-size: 0.72rem;
}

.key.active {
  background: #40351a;
  color: #fff5cf;
  border-color: var(--accent);
}

.cli-log {
  min-height: 9rem;
  max-height: 12rem;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #05070a;
  padding: 0.55rem;
  font-family: Consolas, "Cascadia Mono", monospace;
  color: #c9d6e2;
  font-size: 0.78rem;
  white-space: pre-wrap;
}

.note {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.45;
  margin-top: 0.65rem;
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 1fr;
  }
  .panel {
    max-height: none;
  }
  .stage-panel {
    min-height: 70vh;
    order: -1;
  }
}

@media (max-width: 680px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }
  .workspace { padding: 0.45rem; }
  .stage-toolbar, .statusline {
    flex-direction: column;
  }
  .grid-3 { grid-template-columns: 1fr; }
  .keyboard { grid-template-columns: repeat(7, minmax(1.5rem, 1fr)); }
}
