/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --border: #30363d;
  --text: #c9d1d9;
  --text2: #8b949e;
  --accent: #58a6ff;
  --accent2: #1f6feb;
  --danger: #f85149;
  --success: #3fb950;
  --toolbar-size: 52px;
  --topbar-h: 42px;
  --statusbar-h: 26px;
  --props-w: 240px;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', 'Fira Code', monospace;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg2: #f6f8fa;
  --bg3: #e1e4e8;
  --border: #d0d7de;
  --text: #24292f;
  --text2: #57606a;
  color-scheme: light;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  contain: layout style;
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 3px; }

/* ===== TOP BAR ===== */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-h);
  padding: 0 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
}
.brand-name {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -0.5px;
}
.brand-ver {
  font-size: 10px;
  color: var(--text2);
  background: var(--bg3);
  padding: 1px 5px;
  border-radius: 10px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.separator {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

/* ===== BUTTONS ===== */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text2);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.icon-btn:hover, .icon-btn.active { background: var(--bg3); color: var(--text); }
.icon-btn:disabled { opacity: .3; pointer-events: none; }

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px; height: 42px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text2);
  cursor: pointer;
  transition: background .15s, color .15s;
  position: relative;
}
.tool-btn:hover { background: var(--bg3); color: var(--text); }
.tool-btn.active {
  background: var(--accent2);
  color: #fff;
}
.tool-btn.active::after {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 20px;
  background: var(--accent);
  border-radius: 0 3px 3px 0;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg3);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); background: var(--accent2); color: #fff; }

.btn-sm {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg3);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  transition: background .15s;
}
.btn-sm:hover { background: var(--accent2); color: #fff; border-color: var(--accent); }

/* ===== MAIN LAYOUT ===== */
#main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ===== TOOLBAR ===== */
#toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 5px;
  width: var(--toolbar-size);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: hidden;
}

/* ===== CANVAS AREA ===== */
#canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background:
    linear-gradient(45deg, var(--bg3) 25%, transparent 25%),
    linear-gradient(-45deg, var(--bg3) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--bg3) 75%),
    linear-gradient(-45deg, transparent 75%, var(--bg3) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  contain: layout style paint;
}

#canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  cursor: crosshair;
  will-change: transform;
}

/* ===== PROPS PANEL ===== */
#props {
  width: var(--props-w);
  background: var(--bg2);
  border-left: 1px solid var(--border);
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  transition: width .2s ease, transform .2s ease;
}
#props.collapsed { width: 0; overflow: hidden; }
#props.collapsed .props-inner { display: none; }

.props-toggle {
  position: absolute;
  left: -24px; top: 50%;
  transform: translateY(-50%);
  width: 24px; height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-right: none;
  border-radius: var(--radius) 0 0 var(--radius);
  color: var(--text2);
  cursor: pointer;
  z-index: 5;
  transition: transform .2s;
}
#props.collapsed .props-toggle svg { transform: rotate(180deg); }

.props-inner {
  padding: 8px;
}

/* Panels */
.panel {
  margin-bottom: 8px;
}
.panel-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text2);
  padding: 8px 0 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}
.panel-title::before {
  content: '▾';
  font-size: 10px;
  transition: transform .15s;
}
.panel-title.collapsed::before {
  transform: rotate(-90deg);
}
.panel-body {
  overflow: hidden;
  transition: max-height .2s ease;
}
.panel-title.collapsed + .panel-body {
  max-height: 0 !important;
  padding: 0;
}

/* Size */
.size-pills, .scale-pills {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.pill {
  height: 28px;
  min-width: 40px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text2);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
}
.pill:hover { border-color: var(--accent); color: var(--text); }
.pill.active { background: var(--accent2); border-color: var(--accent); color: #fff; }

.size-custom {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
}
.size-custom label {
  font-size: 12px;
  color: var(--text2);
  white-space: nowrap;
}
.size-custom input {
  width: 60px;
  height: 28px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  font-family: var(--font-mono);
}

/* Colors */
.palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, 24px);
  gap: 3px;
  margin-bottom: 8px;
}
.swatch {
  width: 24px; height: 24px;
  border-radius: 4px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: border-color .1s, transform .1s;
}
.swatch:hover { transform: scale(1.15); }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

.color-input {
  display: flex;
  align-items: center;
  gap: 6px;
}
.color-input input[type="color"] {
  width: 28px; height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  cursor: pointer;
  background: none;
}
.color-input input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
.color-input input[type="color"]::-webkit-color-swatch { border: none; border-radius: 3px; }
.color-input input[type="text"] {
  flex: 1;
  height: 28px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  font-family: var(--font-mono);
}

/* Export */
.export-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.export-row label {
  font-size: 12px;
  color: var(--text2);
  white-space: nowrap;
}
.drop-zone {
  margin-top: 8px;
  padding: 16px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  text-align: center;
  color: var(--text2);
  font-size: 12px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(88,166,255,.05);
}

/* ===== STATUS BAR ===== */
#statusbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--statusbar-h);
  padding: 0 12px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text2);
  flex-shrink: 0;
}
.status-color {
  width: 12px; height: 12px;
  border-radius: 3px;
  border: 1px solid var(--border);
}

/* ===== TOASTS ===== */
#toasts {
  position: fixed;
  bottom: 40px;
  right: 12px;
  display: flex;
  flex-direction: column-reverse;
  gap: 6px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  padding: 8px 14px;
  border-radius: var(--radius);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  color: var(--text);
  font-size: 12px;
  animation: toastIn .25s ease;
  pointer-events: auto;
}
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--success); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== MOBILE ===== */
@media (max-width: 768px) {
  :root {
    --toolbar-size: 48px;
    --topbar-h: 40px;
  }

  #main {
    flex-direction: column;
    position: relative;
  }

  #toolbar {
    order: 2;
    flex-direction: row;
    width: 100%;
    height: var(--toolbar-size);
    border-right: none;
    border-top: 1px solid var(--border);
    padding: 4px 8px;
    overflow-x: auto;
    overflow-y: hidden;
    justify-content: center;
  }

  .tool-btn {
    width: 40px; height: 40px;
    flex-shrink: 0;
  }
  .tool-btn.active::after {
    left: 50%; top: auto;
    bottom: 0;
    transform: translateX(-50%);
    width: 20px; height: 3px;
    border-radius: 3px 3px 0 0;
  }

  #canvas-wrap {
    order: 1;
    flex: 1;
  }

  #props {
    position: fixed;
    top: var(--topbar-h);
    right: 0;
    bottom: calc(var(--toolbar-size) + var(--statusbar-h));
    width: 260px;
    z-index: 20;
    transform: translateX(100%);
    transition: transform .25s ease;
    box-shadow: -4px 0 20px rgba(0,0,0,.3);
  }
  #props.open { transform: translateX(0); }
  #props.collapsed { transform: translateX(100%); }

  .props-toggle {
    left: -36px;
    width: 36px; height: 36px;
    border-radius: var(--radius) 0 0 var(--radius);
  }

  #statusbar {
    order: 3;
  }

  .brand-name { font-size: 12px; }
  .brand-ver { display: none; }

  #toasts {
    bottom: calc(var(--toolbar-size) + var(--statusbar-h) + 8px);
    right: 8px;
    left: 8px;
  }
  .toast { width: 100%; }
}

@media (max-width: 380px) {
  .tool-btn { width: 36px; height: 36px; }
  .tool-btn svg { width: 18px; height: 18px; }
  :root { --toolbar-size: 44px; }
}

/* ===== HOVER-ONLY ===== */
@media (hover: none) {
  .tool-btn:hover { background: transparent; color: var(--text2); }
  .tool-btn.active:hover { background: var(--accent2); color: #fff; }
  .icon-btn:hover { background: transparent; }
  .icon-btn.active:hover { background: var(--bg3); color: var(--text); }
}
