* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #14141b;
  --panel: #20202b;
  --panel-2: #2a2a38;
  --line: #38384a;
  --ink: #e7e7ef;
  --sub: #9a9ab0;
  --accent: #ff8a5b;
  --accent-2: #5bb8ff;
  --active: #3a3a52;
  --danger: #ff5b6e;
  --radius: 8px;
}
html, body {
  height: 100%;
  overflow: hidden;
}
body {
  font-family: "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}
button, input, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }

/* ── Top bar ── */
.topbar {
  display: flex; align-items: center; gap: 12px;
  height: 46px; padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.topbar .back {
  text-decoration: none; color: var(--sub); font-size: 20px; line-height: 1;
  width: 30px; height: 30px; display: grid; place-items: center;
  border-radius: 6px;
}
.topbar .back:hover { background: var(--panel-2); color: var(--ink); }
.brand { font-weight: 700; font-size: 15px; letter-spacing: -0.2px; display: flex; align-items: center; gap: 6px; }
.brand .logo { font-size: 17px; }
.menu { display: flex; align-items: center; gap: 6px; margin-left: 10px; }
.mbtn {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
  padding: 6px 11px; border-radius: 6px; font-size: 12.5px; white-space: nowrap;
}
.mbtn:hover { background: var(--active); border-color: var(--accent); }
.mbtn.primary { background: var(--accent); color: #1a1208; border-color: var(--accent); font-weight: 700; }
.mbtn.file { display: inline-flex; align-items: center; }
.menu .sep { width: 1px; height: 22px; background: var(--line); margin: 0 2px; }
.ghlink { margin-left: auto; color: var(--sub); text-decoration: none; font-size: 12px; }
.ghlink:hover { color: var(--accent); }

.dropdown { position: relative; }
.dropdown-menu {
  position: absolute; top: 110%; left: 0; z-index: 50;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  display: none; flex-direction: column; min-width: 190px; padding: 5px;
  box-shadow: 0 10px 30px rgba(0,0,0,.45);
}
.dropdown:hover .dropdown-menu { display: flex; }
.dropdown-menu button {
  background: none; border: none; color: var(--ink); text-align: left;
  padding: 8px 10px; border-radius: 6px; font-size: 12.5px;
}
.dropdown-menu button:hover { background: var(--active); }

/* ── Workspace ── */
.workspace { flex: 1 1 auto; display: flex; min-height: 0; }

/* Left tools */
.tools {
  width: 64px; flex: 0 0 auto; background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 8px 6px; display: flex; flex-direction: column; gap: 12px;
  overflow-y: auto;
}
.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.tool {
  aspect-ratio: 1; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 7px; font-size: 16px; color: var(--ink);
  display: grid; place-items: center;
}
.tool:hover { border-color: var(--accent); }
.tool.active { background: var(--accent); border-color: var(--accent); color: #1a1208; }
.opt { border-top: 1px solid var(--line); padding-top: 9px; }
.opt-label { font-size: 10.5px; color: var(--sub); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 6px; }
.brush-sizes, .mirror-row { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.bs, .mir {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 6px 0; font-size: 13px;
}
.bs.active, .mir.active { background: var(--accent); border-color: var(--accent); color: #1a1208; font-weight: 700; }
.check { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--sub); cursor: pointer; }
.check input { accent-color: var(--accent); }

/* Canvas */
.canvas-area { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.canvas-bar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.cbtn {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
  border-radius: 6px; padding: 5px 9px; font-size: 13px; line-height: 1;
}
.cbtn:hover { border-color: var(--accent); }
.cbtn.toggle.active { background: var(--accent); border-color: var(--accent); color: #1a1208; }
.cbtn.play.playing { background: var(--accent); border-color: var(--accent); color: #1a1208; }
.canvas-bar .sep { width: 1px; height: 20px; background: var(--line); margin: 0 3px; }
.canvas-bar .spacer { flex: 1; }
.zoom { min-width: 56px; text-align: center; color: var(--sub); font-variant-numeric: tabular-nums; }
.coord { color: var(--sub); font-size: 12px; font-variant-numeric: tabular-nums; margin-left: 8px; }
.stage-wrap { flex: 1 1 auto; position: relative; overflow: hidden; }
#stage { position: absolute; inset: 0; display: block; touch-action: none; cursor: crosshair; }

/* Right sidebar */
.sidebar {
  width: 244px; flex: 0 0 auto; background: var(--panel);
  border-left: 1px solid var(--line); display: flex; flex-direction: column;
  overflow-y: auto;
}
.panel { padding: 11px; border-bottom: 1px solid var(--line); }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 9px; font-weight: 600; font-size: 12.5px; }
.panel-btns { display: flex; gap: 3px; }
.mini {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
  border-radius: 5px; width: 26px; height: 26px; font-size: 12px;
  display: inline-grid; place-items: center;
}
.mini:hover { border-color: var(--accent); }
.mini.danger:hover { border-color: var(--danger); color: var(--danger); }

/* Color panel */
.swatches { display: flex; gap: 10px; margin-bottom: 10px; }
.swatch-stack { position: relative; width: 52px; height: 46px; flex: 0 0 auto; }
.swatch {
  position: absolute; width: 34px; height: 34px; border-radius: 7px;
  border: 2px solid #0008; box-shadow: 0 0 0 1px var(--line);
  background-image: linear-gradient(45deg,#888 25%,transparent 25%,transparent 75%,#888 75%),
                    linear-gradient(45deg,#888 25%,#fff 25%,#fff 75%,#888 75%);
  background-size: 12px 12px; background-position: 0 0, 6px 6px;
}
.swatch.primary { top: 0; left: 0; z-index: 2; }
.swatch.secondary { bottom: 0; right: 0; z-index: 1; }
.color-controls { flex: 1; display: grid; grid-template-columns: 38px 1fr 30px; gap: 6px; align-items: center; }
#colorPicker { width: 38px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; }
#hexInput {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 6px; font-size: 12px; width: 100%; text-transform: uppercase;
}
.palette-head { display: flex; gap: 6px; margin-bottom: 8px; }
#paletteSelect {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px; font-size: 12px;
}
.palette-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px; }
.pal {
  aspect-ratio: 1; border-radius: 4px; border: 1px solid #0006;
  position: relative;
}
.pal.sel { box-shadow: 0 0 0 2px var(--accent); border-color: var(--accent); }

/* Layers */
.opacity-row { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--sub); margin-bottom: 9px; }
.opacity-row input[type=range] { flex: 1; accent-color: var(--accent); }
#opacityVal { width: 34px; text-align: right; font-variant-numeric: tabular-nums; }
.layer-list { display: flex; flex-direction: column; gap: 4px; max-height: 240px; overflow-y: auto; }
.layer-row {
  display: flex; align-items: center; gap: 7px; padding: 5px 6px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
}
.layer-row.active { border-color: var(--accent); background: var(--active); }
.layer-row .eye { background: none; border: none; font-size: 13px; width: 18px; opacity: .9; }
.layer-row.hidden-layer { opacity: .5; }
.layer-row .thumb { width: 30px; height: 30px; border-radius: 4px; background: #0006; image-rendering: pixelated; flex: 0 0 auto; }
.layer-row .lname { flex: 1; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-row .lname:focus { outline: 1px solid var(--accent); border-radius: 3px; }

/* Timeline */
.timeline { flex: 0 0 auto; background: var(--panel); border-top: 1px solid var(--line); }
.tl-controls { display: flex; align-items: center; gap: 6px; padding: 7px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tl-controls .sep { width: 1px; height: 18px; background: var(--line); }
.tl-controls .fps { display: flex; align-items: center; gap: 5px; color: var(--sub); font-size: 12px; }
.tl-controls .fps input {
  width: 48px; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 6px; color: var(--ink); padding: 4px 6px; font-size: 12px;
}
.tl-body { display: flex; align-items: stretch; gap: 12px; padding: 10px 12px; }
.frame-list { display: flex; gap: 8px; overflow-x: auto; flex: 1; padding-bottom: 4px; }
.frame-cell {
  flex: 0 0 auto; position: relative; cursor: pointer;
  border: 2px solid var(--line); border-radius: 7px; padding: 3px; background: var(--panel-2);
}
.frame-cell.active { border-color: var(--accent); }
.frame-cell canvas { display: block; width: 56px; height: 56px; border-radius: 4px; image-rendering: pixelated; background: #0006; }
.frame-cell .num { position: absolute; top: 2px; left: 5px; font-size: 9px; color: var(--sub); background: #0009; padding: 0 3px; border-radius: 3px; }
.preview-box { flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.preview-label { font-size: 10px; color: var(--sub); }
#previewCanvas {
  width: 72px; height: 72px; border: 1px solid var(--line); border-radius: 6px;
  image-rendering: pixelated; background: #0006;
}

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 100;
  display: grid; place-items: center;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 20px; width: 320px; box-shadow: 0 20px 50px rgba(0,0,0,.5);
}
.modal h3 { margin-bottom: 14px; font-size: 16px; }
.presets { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.presets button {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 6px; color: var(--ink); padding: 8px 0; font-size: 12px; min-width: 48px;
}
.presets button:hover { border-color: var(--accent); }
.modal-row { display: flex; gap: 12px; margin-bottom: 8px; }
.modal-row label { flex: 1; display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--sub); }
.modal-row input {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 8px; font-size: 14px;
}
.modal-note { font-size: 11px; color: var(--sub); min-height: 16px; margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Conversion modal */
.modal.conv { width: 600px; max-width: 94vw; }
.conv-body { display: flex; gap: 18px; margin-bottom: 16px; }
.conv-preview { flex: 0 0 280px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.conv-canvas-box {
  width: 280px; height: 280px; display: grid; place-items: center;
  background: repeating-conic-gradient(#2a2a38 0 25%, #20202b 0 50%) 50% / 24px 24px;
  border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
#convCanvas { image-rendering: pixelated; display: block; }
.conv-dim { font-size: 12px; color: var(--sub); font-variant-numeric: tabular-nums; }
.conv-controls { flex: 1; display: flex; flex-direction: column; gap: 10px; }
.cc-label { font-size: 11px; color: var(--sub); text-transform: uppercase; letter-spacing: .5px; }
.presets.sm button { padding: 6px 0; font-size: 12px; min-width: 36px; }
.cc-row { display: flex; gap: 10px; align-items: flex-end; }
.cc-row label { flex: 1; display: flex; flex-direction: column; gap: 4px; font-size: 11px; color: var(--sub); }
.cc-row input[type=number] {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 7px; font-size: 13px; width: 100%;
}
.cc-field { display: flex; flex-direction: column; gap: 4px; font-size: 11px; color: var(--sub); }
.cc-field select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 7px; font-size: 13px;
}
.chk { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink); cursor: pointer; }
.chk.wide { padding: 2px 0; }
.chk input { accent-color: var(--accent); }
.cc-field input[type=range] { width: 100%; accent-color: var(--accent); }
.opt input[type=range] { width: 100%; accent-color: var(--accent); margin-top: 4px; }
.opt-label span { color: var(--ink); font-weight: 600; }

/* Toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--panel-2); border: 1px solid var(--accent); color: var(--ink);
  padding: 10px 18px; border-radius: 10px; font-size: 13px; opacity: 0;
  pointer-events: none; transition: opacity .2s, transform .2s; z-index: 200;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 720px) {
  .sidebar { width: 200px; }
  .tools { width: 56px; }
  .menu .mbtn { padding: 6px 8px; font-size: 11.5px; }
}
