:root {
  --sidebar-w: 260px;
  --bg: #f8f7f4;
  --sidebar-bg: #ffffff;
  --border: #e8e5e0;
  --text: #2c2c2c;
  --text-muted: #888;
  --accent: #5b7fff;
  --accent-hover: #4a6ae8;
  --shadow: 0 2px 12px rgba(0,0,0,0.06);
  --radius: 10px;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  user-select: none;
}

#app {
  display: flex;
  height: 100vh;
}

#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  box-shadow: var(--shadow);
  z-index: 1;
}

#sidebar h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
  padding-bottom: 8px;
  border-bottom: 2px solid var(--bg);
}

#sidebar h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.tool-group {
  display: flex;
  gap: 6px;
}

.tool-group button {
  flex: 1;
  padding: 8px 6px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--sidebar-bg);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

.tool-group button:hover {
  background: #f5f4f0;
  border-color: #ccc;
}

.tool-group button.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

#palette {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
  margin-bottom: 8px;
}

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

.palette-swatch:hover {
  transform: scale(1.12);
  z-index: 2;
}

.palette-swatch.active {
  border-color: var(--text);
  transform: scale(1.08);
  box-shadow: 0 0 0 2px white, 0 0 0 4px var(--text);
}

#custom-color-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

#custom-color-row input {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  background: none;
}

#actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

#actions button {
  padding: 8px 0;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--sidebar-bg);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

#actions button:hover {
  background: #f5f4f0;
}

#actions button:active {
  transform: scale(0.97);
}

#iron-btn {
  grid-column: 1 / -1;
  background: #fff0e0 !important;
  border-color: #f0c8a0 !important;
  color: #a06730;
  font-weight: 600;
}

#iron-btn.active {
  background: #e8c8a0 !important;
  border-color: #d0a870 !important;
  color: #704820;
}

#save-btn {
  color: #2c7a3e;
  font-weight: 600;
}

#clear-btn {
  color: #c0392b;
}

#reward-section {
  padding-top: 2px;
}

#reward-section p {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-muted);
}

#reward-btn {
  margin: 0 2px;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
}

#reward-btn:hover {
  color: var(--accent-hover);
}

#grid-settings {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

#grid-settings label {
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 4px;
}

#grid-settings input {
  width: 48px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
}

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

#resize-btn {
  padding: 5px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--sidebar-bg);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s;
}

#resize-btn:hover {
  background: #f5f4f0;
}

#canvas-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: hidden;
  background: #ecebe6;
}

#canvas {
  border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.10);
  cursor: crosshair;
  image-rendering: auto;
  touch-action: none;
}

#undo:disabled, #redo:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#reward-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

#reward-modal.open {
  display: flex;
}

#reward-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.42);
}

#reward-dialog {
  position: relative;
  z-index: 1;
  width: min(360px, 100%);
  max-height: calc(100dvh - 40px);
  padding: 18px;
  border-radius: var(--radius);
  background: var(--sidebar-bg);
  box-shadow: 0 12px 40px rgba(0,0,0,0.22);
  overflow-y: auto;
  text-align: center;
}

#reward-dialog h2 {
  margin-bottom: 6px;
  font-size: 18px;
}

#reward-dialog p {
  margin-bottom: 14px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-muted);
}

#reward-dialog img {
  display: block;
  width: 100%;
  max-height: 70dvh;
  object-fit: contain;
  border-radius: 8px;
}

#reward-close {
  position: absolute;
  z-index: 1;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--sidebar-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
}

@media (max-width: 640px) {
  :root {
    --mobile-controls-h: 42vh;
  }

  html, body {
    height: 100dvh;
  }

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

  #sidebar {
    order: 2;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    min-width: unset;
    height: var(--mobile-controls-h);
    max-height: var(--mobile-controls-h);
    padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
    border-right: none;
    border-top: 1px solid var(--border);
    overflow-y: auto;
  }

  #canvas-container {
    order: 1;
    flex: 0 0 calc(100dvh - var(--mobile-controls-h));
    width: 100%;
    height: calc(100dvh - var(--mobile-controls-h));
    min-height: calc(100dvh - var(--mobile-controls-h));
    padding: 10px;
  }
}
