*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:      #1a1a1a;
  --bg2:     #222222;
  --bg3:     #2c2c2c;
  --border:  #3a3a3a;
  --text:    #e0e0e0;
  --text-dim:#888888;
  --accent:  #4a7fff;
  --btn:     #333333;
  --btn-h:   #444444;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

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

/* ── Toolbar ─────────────────────────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 6px 10px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#toolbar::-webkit-scrollbar { display: none; }

.toolbar-section {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 0 10px 0 0;
  margin-right: 10px;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}

.toolbar-section:last-child {
  border-right: none;
  margin-right: 0;
}

label {
  color: var(--text-dim);
  font-size: 11px;
  white-space: nowrap;
}

button, .btn {
  background: var(--btn);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 9px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1.4;
  transition: background 0.1s;
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button:hover, .btn:hover {
  background: var(--btn-h);
}

select {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 12px;
  cursor: pointer;
}

input[type="number"] {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 12px;
  width: 52px;
}

/* ── Tool buttons ────────────────────────── */
.material-symbols-outlined {
  font-family: 'Material Symbols Outlined';
  font-weight: normal;
  font-style: normal;
  font-size: 22px;
  line-height: 1;
  letter-spacing: 0;
  text-transform: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  direction: ltr;
  font-feature-settings: 'liga';
  -webkit-font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
}
.tool-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}


.tool-btn .material-symbols-outlined {
  font-size: 22px;
  font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 24;
}

.tool-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-gap-on  { background: #3a3a3a; }
.btn-gap-off { background: #555; color: #fff; }

/* ── Main layout ─────────────────────────── */
#main-area {
  display: flex;
  flex: 1;
  min-height: 0;
}

#canvas-scroll {
  flex: 1;
  overflow: auto;
  background: #111;
  padding: 20px;
}

#editor-canvas {
  display: block;
  image-rendering: pixelated;
  cursor: crosshair;
}

/* ── Sidebar ─────────────────────────────── */
#sidebar {
  width: 216px;
  flex-shrink: 0;
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.panel {
  border-bottom: 1px solid var(--border);
  padding: 10px;
}

.panel-header {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.btn-inline {
  font-size: 10px;
  padding: 2px 7px;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}

/* ── Preview ─────────────────────────────── */
#preview-canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  background: #111;
}

/* ── Palette ─────────────────────────────── */
#palette-swatches {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  margin-bottom: 10px;
}

.palette-swatch {
  aspect-ratio: 1;
  border-radius: 3px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.1s;
}

.palette-swatch:hover {
  transform: scale(1.08);
}

.palette-swatch.selected {
  border-color: #fff;
}

#color-editor {
  display: flex;
  gap: 7px;
  align-items: center;
}

#color-picker {
  width: 34px;
  height: 26px;
  padding: 1px 2px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}

#hex-input {
  flex: 1;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-family: 'SF Mono', 'Menlo', monospace;
  font-size: 12px;
}

#hex-input:focus {
  outline: none;
  border-color: var(--accent);
}

/* ── File input ──────────────────────────── */

.icon-text-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.icon-text-btn .material-symbols-outlined {
  font-size: 18px;
  font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 20;
}

.popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: start center;
  padding: 58px 14px 14px;
  background: rgba(0, 0, 0, 0.48);
}

.popup-overlay[hidden] {
  display: none;
}

.popup-panel {
  width: min(420px, 100%);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.42);
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.popup-header h2 {
  font-size: 13px;
  font-weight: 700;
}

.popup-section {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.popup-section:last-child {
  border-bottom: none;
}

.popup-section-title {
  margin-bottom: 9px;
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 700;
}

.field-grid {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  gap: 7px;
  align-items: center;
  margin-bottom: 10px;
}

.field-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 7px;
  align-items: center;
  margin-bottom: 10px;
}

.field-label {
  font-size: 12px;
}

.popup-panel input[type="number"] {
  width: 100%;
}

.popup-primary {
  width: 100%;
}

.export-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px;
}

.export-actions button,
.export-actions .btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
}
#file-import-json {
  display: none;
}

/* ── モバイル ─────────────────────────────── */
@media (max-width: 700px) {
  body { font-size: 14px; }

  #main-area {
    flex-direction: column;
  }

  #canvas-scroll {
    flex: 1;
    min-height: 0;
    padding: 10px;
  }

  #sidebar {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0;
    max-height: 160px;
  }

  .panel {
    min-width: 180px;
    border-bottom: none;
    border-right: 1px solid var(--border);
    flex-shrink: 0;
  }

  #preview-panel { min-width: 140px; }

  #preview-canvas {
    width: auto;
    height: 100px;
  }

  #palette-swatches {
    grid-template-columns: repeat(5, 32px);
  }

  .palette-swatch {
    width: 32px;
    height: 32px;
  }

  button, .btn {
    padding: 6px 11px;
    font-size: 13px;
  }

  select, input[type="number"] {
    font-size: 14px;
    padding: 5px 6px;
  }
}
