* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #1e1e1e;
  --stage: #2a2a2a;
  --panel: #2b2b2b;
  --panel-2: #363636;
  --line: #474747;
  --ink: #e8e8e8;
  --sub: #9a9a9a;
  --accent: #2b8cff;
  --accent-ink: #ffffff;
  --active: #34507a;
  --danger: #ff5b6e;
  --radius: 7px;
}
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, output { font-family: inherit; color: inherit; }
button { cursor: pointer; }

/* ── Top bar ── */
.topbar {
  display: flex; align-items: center; gap: 10px;
  height: 44px; 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: 2px; margin-left: 6px; }
.mbtn {
  background: none; border: 1px solid transparent; color: var(--ink);
  padding: 6px 10px; border-radius: 6px; font-size: 12.5px; white-space: nowrap;
}
.mbtn:hover { background: var(--panel-2); }
.mbtn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.mbtn.primary:hover { filter: brightness(1.08); }
.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: 108%; left: 0; z-index: 60;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  display: none; flex-direction: column; min-width: 224px; padding: 5px;
  box-shadow: 0 12px 34px rgba(0,0,0,.5);
}
.dropdown:hover .dropdown-menu { display: flex; }
.dropdown-menu button, .dd-file {
  background: none; border: none; color: var(--ink); text-align: left;
  padding: 8px 10px; border-radius: 6px; font-size: 12.5px;
  display: flex; align-items: center; justify-content: space-between; gap: 14px; cursor: pointer;
}
.dropdown-menu button:hover, .dd-file:hover { background: var(--active); }
.dd-sep { height: 1px; background: var(--line); margin: 4px 4px; }
.kbd { color: var(--sub); font-size: 11px; }

/* ── Option bar ── */
.optionbar {
  display: flex; align-items: center; gap: 14px;
  height: 40px; padding: 0 12px;
  background: #252525; border-bottom: 1px solid var(--line);
  flex: 0 0 auto; overflow-x: auto; white-space: nowrap;
}
.opt-tool { font-weight: 700; font-size: 12.5px; min-width: 56px; }
.opt-sep { width: 1px; height: 20px; background: var(--line); }
.optgroup { display: none; align-items: center; gap: 12px; }
.optgroup.show { display: flex; }
.opt-field { display: flex; align-items: center; gap: 7px; font-size: 11.5px; color: var(--sub); }
.opt-field input[type=range] { width: 110px; accent-color: var(--accent); }
.opt-field input[type=number] {
  width: 60px; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 6px; color: var(--ink); padding: 4px 6px; font-size: 12px;
}
.opt-field select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 4px 6px; font-size: 12px;
}
.opt-field output { color: var(--ink); font-variant-numeric: tabular-nums; min-width: 26px; }
.opt-check { display: flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--sub); cursor: pointer; }
.opt-check input { accent-color: var(--accent); }
.opt-hint { color: var(--sub); font-size: 11.5px; }
.opt-btn {
  background: var(--accent); border: 1px solid var(--accent); color: var(--accent-ink);
  border-radius: 6px; padding: 5px 11px; font-size: 12px; font-weight: 600;
}
.opt-btn.ghost { background: var(--panel-2); border-color: var(--line); color: var(--ink); font-weight: 400; }
.opt-btn:hover { filter: brightness(1.08); }

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

/* Left tools */
.tools {
  width: 60px; 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: 4px; }
.tool {
  aspect-ratio: 1; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 7px; font-size: 15px; color: var(--ink);
  display: grid; place-items: center;
}
.tool:hover { border-color: var(--accent); }
.tool.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.colorblock { display: flex; flex-direction: column; align-items: center; gap: 8px; border-top: 1px solid var(--line); padding-top: 12px; }
.swatch-stack { position: relative; width: 52px; height: 52px; }
.swatch {
  position: absolute; width: 34px; height: 34px; border-radius: 7px;
  border: 2px solid #000a; box-shadow: 0 0 0 1px var(--line);
}
.swatch.primary { top: 0; left: 0; z-index: 2; background: #1e90ff; }
.swatch.secondary { bottom: 0; right: 0; z-index: 1; background: #ffffff; }
.sw-swap, .sw-reset {
  position: absolute; background: none; border: none; color: var(--sub); font-size: 12px; padding: 0;
}
.sw-swap { top: -3px; right: -3px; z-index: 3; }
.sw-reset { bottom: -3px; left: -3px; z-index: 3; }
.sw-swap:hover, .sw-reset:hover { color: var(--accent); }
#colorInput { width: 46px; height: 28px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; }

/* Canvas */
.canvas-area { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; background: var(--stage); }
.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; min-width: 30px;
}
.cbtn:hover { border-color: var(--accent); }
.canvas-bar .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.canvas-bar .spacer { flex: 1; }
.zoom { min-width: 52px; text-align: center; color: var(--sub); font-variant-numeric: tabular-nums; }
.coord { color: var(--sub); font-size: 12px; font-variant-numeric: tabular-nums; }
.selLabel { color: var(--accent); }

.stage { flex: 1 1 auto; position: relative; overflow: hidden; }
.doc-wrap { position: absolute; top: 0; left: 0; transform-origin: 0 0; }
.checker {
  position: absolute; inset: 0;
  background-image: linear-gradient(45deg,#666 25%,transparent 25%,transparent 75%,#666 75%),
                    linear-gradient(45deg,#666 25%,#999 25%,#999 75%,#666 75%);
  background-size: 18px 18px; background-position: 0 0, 9px 9px;
  box-shadow: 0 0 0 1px #000, 0 8px 40px rgba(0,0,0,.5);
}
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }

.text-edit {
  position: absolute; background: rgba(43,140,255,.06); border: 1px dashed var(--accent);
  color: #000; outline: none; resize: none; overflow: hidden; line-height: 1.2;
  padding: 0; margin: 0; white-space: pre; min-width: 20px;
}

/* Right sidebar */
.sidebar {
  width: 250px; flex: 0 0 auto; background: var(--panel);
  border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0;
}
.panel { padding: 11px; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.panel-grow { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.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; min-width: 24px; height: 24px; font-size: 11px; padding: 0 4px;
  display: inline-grid; place-items: center;
}
.mini:hover { border-color: var(--accent); }
.mini.danger:hover { border-color: var(--danger); color: var(--danger); }

.hex-row { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.hex-hash { color: var(--sub); }
#hexInput {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 6px 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px;
}
.palette-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 3px; margin-bottom: 8px; }
.pal { aspect-ratio: 1; border-radius: 4px; border: 1px solid #0007; cursor: pointer; }
.pal:hover { transform: scale(1.12); }
.addsw { width: 100%; background: var(--panel-2); border: 1px solid var(--line); color: var(--sub); border-radius: 6px; padding: 6px; font-size: 12px; }
.addsw:hover { border-color: var(--accent); color: var(--ink); }

.blend-row { margin-bottom: 8px; }
#blendSelect { width: 100%; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; color: var(--ink); padding: 6px; font-size: 12px; }
.opacity-row { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--sub); margin-bottom: 10px; }
.opacity-row input[type=range] { flex: 1; accent-color: var(--accent); }
#layerOpacityVal { width: 30px; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.layer-list { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; flex: 1 1 auto; min-height: 60px; }
.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; cursor: pointer; }
.layer-row.hidden-layer .thumb, .layer-row.hidden-layer .lname { opacity: .4; }
.layer-row .thumb {
  width: 32px; height: 32px; border-radius: 4px; flex: 0 0 auto; border: 1px solid #0008;
  background-image: linear-gradient(45deg,#666 25%,transparent 25%,transparent 75%,#666 75%),
                    linear-gradient(45deg,#666 25%,#999 25%,#999 75%,#666 75%);
  background-size: 10px 10px; background-position: 0 0, 5px 5px;
}
.layer-row .thumb canvas { width: 100%; height: 100%; display: block; }
.layer-row .lname { flex: 1; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: default; }
.layer-row .lname:focus { outline: 1px solid var(--accent); border-radius: 3px; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 200; 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: 340px; box-shadow: 0 24px 60px rgba(0,0,0,.55);
}
.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: 56px; }
.presets button:hover { border-color: var(--accent); }
.modal-row { display: flex; gap: 12px; margin-bottom: 10px; }
.modal-row label { flex: 1; display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--sub); }
.modal-row input, .modal-row select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink); padding: 8px; font-size: 14px;
}
.modal-check { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--sub); margin-bottom: 12px; cursor: pointer; }
.modal-check input { accent-color: var(--accent); }
.modal-note { font-size: 11px; color: var(--sub); min-height: 16px; margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.adj-field { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; font-size: 12px; color: var(--sub); }
.adj-field label { width: 64px; flex: 0 0 auto; }
.adj-field input[type=range] { flex: 1; accent-color: var(--accent); }
.adj-field output { width: 42px; text-align: right; color: var(--ink); font-variant-numeric: tabular-nums; }

/* 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: 300;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 820px) {
  .sidebar { width: 210px; }
  .tools { width: 54px; }
  .opt-field input[type=range] { width: 80px; }
}
