:root {
  color-scheme: dark;
  --bg: #181b1f;
  --panel: #22272d;
  --panel-2: #2b3138;
  --text: #e7edf2;
  --muted: #9caab6;
  --line: #3a434d;
  --accent: #5edfff;
  --accent-2: #f0b35b;
  --danger: #ff6b6b;
}

* { box-sizing: border-box; }

html {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: #14171a;
}

h1, h2, p { margin: 0; }
h1 { font-size: 20px; font-weight: 700; }
h2 { font-size: 13px; text-transform: uppercase; color: var(--muted); margin-bottom: 10px; }
p { color: var(--muted); font-size: 12px; }

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

button, .file-button {
  min-height: 32px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}

button:hover, .file-button:hover { border-color: var(--accent); }
button.is-active, .tab.is-active { background: #12343c; border-color: var(--accent); }
button:disabled {
  opacity: 0.45;
  cursor: default;
}

.tabs { display: flex; gap: 8px; }
.tab { min-width: 88px; }

main {
  height: calc(100dvh - 64px);
  min-height: 0;
  overflow: hidden;
}
.panel {
  display: none;
  height: 100%;
  min-height: 0;
  grid-template-columns: 280px minmax(420px, 1fr) 300px;
}
.panel.is-active { display: grid; }

.sidebar {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 14px;
}

.rightbar {
  border-right: 0;
  border-left: 1px solid var(--line);
}

.tool-group {
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 10px;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #15191d;
  color: var(--text);
  border-radius: 6px;
  padding: 7px 8px;
}

input[type="checkbox"], input[type="color"] { width: auto; }
input[type="range"] { padding: 0; }
input[type="file"] { display: none; }
textarea {
  min-height: 180px;
  resize: vertical;
  font-family: Consolas, monospace;
  font-size: 12px;
  -webkit-user-select: text;
  user-select: text;
}

pre {
  white-space: pre-wrap;
  min-height: 120px;
  color: var(--muted);
  font-family: Consolas, monospace;
}

.workspace {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #191d22;
  overflow: hidden;
}

.toolbar {
  min-height: 48px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  flex-wrap: wrap;
}

.toolbar label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
}

.icon-button {
  width: 36px;
  min-width: 36px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  font-size: 20px;
  line-height: 1;
}

.frame-order-controls {
  display: grid;
  gap: 5px;
  justify-items: center;
  min-width: 92px;
  color: var(--muted);
  font-size: 12px;
}

.frame-tools {
  min-height: 74px;
  flex: 0 0 74px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: #1d2228;
}

.frame-move-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  width: 76px;
}

.frame-move-row button {
  min-height: 28px;
  padding: 2px 0;
  font-size: 16px;
  line-height: 1;
}

.draw-tool-grid {
  display: grid;
  grid-template-columns: 76px 1fr;
  grid-template-rows: 36px 36px;
  gap: 8px;
  margin-bottom: 10px;
}

.draw-tool-grid button {
  min-height: 36px;
}

.brush-preview-control {
  position: relative;
  width: 76px;
  height: 80px;
  grid-row: 1 / span 2;
  margin: 0;
  display: grid;
  place-items: center;
  gap: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  overflow: hidden;
}

.brush-preview-control:hover {
  border-color: var(--accent);
}

.brush-preview-control input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.brush-preview-text {
  position: absolute;
  inset: 8px 0 auto;
  display: grid;
  gap: 1px;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.05;
  pointer-events: none;
}

.brush-preview-dot {
  position: absolute;
  bottom: 9px;
  left: 50%;
  width: 10px;
  height: 10px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: #5edfff;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.42), 0 0 14px rgba(94,223,255,0.45);
  pointer-events: none;
}

.button-row, .segmented, .grid-two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.segmented { grid-template-columns: 1fr 1fr; }
.preset-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}

.preset-button {
  min-width: 0;
  padding: 5px 0;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
  margin-bottom: 10px;
}

.swatch {
  min-width: 0;
  height: 42px;
  padding: 4px;
  display: grid;
  grid-template-columns: 22px 1fr;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--muted);
}

.swatch-chip {
  width: 22px;
  height: 22px;
  border: 1px solid rgba(255,255,255,0.32);
  background-size: 8px 8px;
}

.swatch.is-active {
  border-color: var(--accent-2);
  color: var(--text);
}

.canvas-wrap {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 18px;
}

canvas {
  image-rendering: pixelated;
  background: #0f1215;
  border: 1px solid var(--line);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#tilesetCanvas {
  width: 100%;
  height: auto;
}

.sprite-stage {
  display: grid;
  grid-template-columns: minmax(420px, 1fr) 190px;
  gap: 18px;
  align-items: start;
  justify-items: start;
  padding: 18px;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

.draw-surface {
  width: 100%;
  min-height: 0;
  display: grid;
  place-items: start center;
  overflow: hidden;
  touch-action: none;
}

#drawCanvas {
  justify-self: center;
  align-self: start;
  max-width: 100%;
  max-height: calc(100dvh - 64px - 60px - 74px - 116px - 36px);
}
#previewCanvas { width: 160px; height: 160px; }

.timeline {
  min-height: 116px;
  flex: 0 0 116px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 8px;
  padding: 12px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  background: var(--panel);
}

.thumb {
  width: 76px;
  height: 88px;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #15191d;
  padding: 5px;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}

.thumb.is-active { border-color: var(--accent-2); }
.thumb canvas { width: 64px; height: 64px; display: block; margin-bottom: 4px; }
.note { line-height: 1.45; }

.savezone-panel.is-active {
  grid-template-columns: 320px minmax(420px, 1fr);
}

.savezone-list {
  display: grid;
  gap: 10px;
}

.savezone-item {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 72px;
  padding: 7px;
  text-align: left;
}

.savezone-item canvas {
  width: 56px;
  height: 56px;
  border-radius: 4px;
}

.savezone-item strong,
.savezone-item span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.savezone-item strong {
  color: var(--text);
  font-size: 13px;
}

.savezone-item span {
  color: var(--muted);
  font-size: 11px;
}

.savezone-preview {
  min-height: 0;
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(260px, 420px) minmax(260px, 1fr);
  gap: 18px;
  padding: 18px;
  overflow: hidden;
}

#savezonePreviewCanvas {
  width: min(100%, 420px);
  height: auto;
  align-self: start;
}

#savezoneMeta {
  min-height: 0;
  overflow: auto;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #15191d;
}

@media (max-width: 1100px) {
  .panel.is-active { grid-template-columns: 240px minmax(360px, 1fr); }
  .rightbar { display: none; }
}

@media (hover: none) and (pointer: coarse) {
  button, .file-button {
    min-height: 38px;
    padding: 7px 10px;
  }

  .app-header {
    height: 58px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  h1 { font-size: 18px; }
  .app-header p { display: none; }
  main { height: calc(100dvh - 58px); }

  .panel.is-active {
    grid-template-columns: 230px minmax(0, 1fr);
  }

  .sidebar {
    padding: 10px;
  }

  .tool-group {
    padding-bottom: 12px;
    margin-bottom: 12px;
  }

  .toolbar {
    min-height: 50px;
    gap: 7px;
    padding: 6px 8px;
  }

  .toolbar button {
    min-height: 34px;
    padding: 5px 8px;
    font-size: 12px;
  }

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

  .sprite-stage {
    grid-template-columns: minmax(0, 1fr) 136px;
    gap: 10px;
    padding: 10px;
  }

  #previewCanvas {
    width: 128px;
    height: 128px;
  }

  #drawCanvas {
    max-height: calc(100dvh - 58px - 56px - 66px - 104px - 20px);
  }

  .frame-tools {
    min-height: 66px;
    flex-basis: 66px;
    padding: 8px;
  }

  .frame-move-row button {
    min-height: 30px;
  }

  .savezone-panel.is-active {
    grid-template-columns: 260px minmax(0, 1fr);
  }

  .savezone-preview {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 10px;
  }

  .timeline {
    min-height: 104px;
    flex-basis: 104px;
    padding: 8px;
  }

  .thumb {
    width: 68px;
    height: 82px;
  }

  .thumb canvas {
    width: 56px;
    height: 56px;
  }
}

@media (max-width: 820px) {
  .panel.is-active {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .sprite-stage {
    grid-template-columns: 1fr;
  }

  #previewCanvas {
    display: none;
  }
}
