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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #1a1714;
  font-family: system-ui, -apple-system, sans-serif;
}

#canvas {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  cursor: crosshair;
  touch-action: none;
}

/* ── Read-only overlay ───────────────────────────────────────────────── */
#readonly-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  opacity: 0;
  transition: opacity 0.3s;
}

#readonly-overlay.active {
  opacity: 1;
}

#readonly-overlay::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(55, 35, 10, 0.22);
}

#readonly-banner {
  position: absolute;
  top: 3.0rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(160, 100, 30, 0.92);
  color: rgba(255, 240, 210, 0.95);
  padding: 0.28rem 1.1rem;
  border-radius: 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  white-space: nowrap;
  box-shadow: 0 2px 10px rgba(0,0,0,0.5);
  z-index: 1;
}

/* ── HUD top ─────────────────────────────────────────────────────────── */
#hud-top {
  position: fixed;
  top: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.9rem;
  background: rgba(10, 9, 14, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2rem;
  padding: 0.3rem 1rem;
  z-index: 100;
  pointer-events: none;
  backdrop-filter: blur(10px);
  user-select: none;
}

#title-label {
  color: rgba(255, 255, 255, 0.88);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.22em;
}

#user-count {
  color: rgba(255, 255, 255, 0.42);
  font-size: 0.68rem;
  letter-spacing: 0.06em;
}

/* ── Layer panel ─────────────────────────────────────────────────────── */
#layer-panel {
  position: fixed;
  right: 0.8rem;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(10, 9, 14, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 1rem;
  padding: 0.7rem 0.5rem;
  z-index: 100;
  backdrop-filter: blur(14px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  width: 4.4rem;
  box-shadow: 0 4px 24px rgba(0,0,0,0.55);
  user-select: none;
}

#layer-panel-title {
  color: rgba(255, 255, 255, 0.3);
  font-size: 0.52rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  margin-bottom: 0.15rem;
}

.layer-edge-label {
  color: rgba(255, 255, 255, 0.28);
  font-size: 0.5rem;
  letter-spacing: 0.06em;
}

#layer-slider-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 9rem;
  padding: 0.1rem 0;
}

#layer-slider {
  -webkit-appearance: none;
  appearance: none;
  writing-mode: vertical-lr;
  width: 22px;
  height: 100%;
  cursor: pointer;
  background: transparent;
  outline: none;
  padding: 0;
  border: none;
}

#layer-slider::-webkit-slider-runnable-track {
  background: rgba(255, 255, 255, 0.14);
  border-radius: 3px;
  width: 5px;
}

#layer-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(220, 160, 60, 0.9);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  margin-left: -5.5px;
  cursor: pointer;
  transition: background 0.15s;
}

#layer-slider::-webkit-slider-thumb:hover {
  background: rgba(240, 185, 70, 1);
}

#layer-slider::-moz-range-track {
  background: rgba(255, 255, 255, 0.14);
  border-radius: 3px;
  width: 5px;
}

#layer-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(220, 160, 60, 0.9);
  border: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  cursor: pointer;
}

.layer-arrow-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: rgba(255, 255, 255, 0.55);
  border-radius: 0.3rem;
  width: 1.8rem;
  height: 1.3rem;
  cursor: pointer;
  font-size: 0.62rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.13s, color 0.13s;
  user-select: none;
  padding: 0;
  flex-shrink: 0;
}

.layer-arrow-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.95);
}

.layer-arrow-btn:disabled {
  opacity: 0.22;
  cursor: default;
}

#layer-today-badge {
  background: rgba(220, 155, 40, 0.88);
  color: #1a0e00;
  border-radius: 0.3rem;
  padding: 0.12rem 0.4rem;
  font-size: 0.5rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  margin-top: 0.15rem;
}

#layer-date-display {
  color: rgba(255, 255, 255, 0.82);
  font-size: 0.62rem;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
}

#layer-count-display {
  color: rgba(255, 255, 255, 0.32);
  font-size: 0.52rem;
  text-align: center;
  white-space: nowrap;
}

/* ── Bottom panel (spray settings + toolbar stacked) ─────────────────── */
#bottom-panel {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  z-index: 100;
}

/* ── Spray settings panel ────────────────────────────────────────────── */
#spray-settings {
  display: none;
  align-items: center;
  gap: 0.9rem;
  background: rgba(10, 9, 16, 0.93);
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 1.5rem;
  padding: 0.5rem 1.1rem;
  backdrop-filter: blur(14px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.55);
  white-space: nowrap;
}

#spray-settings.visible {
  display: flex;
}

.spray-setting {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.spray-label {
  color: rgba(255, 255, 255, 0.45);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  user-select: none;
  min-width: 3.2rem;
}

.spray-setting input[type="range"] {
  width: 4.5rem;
  cursor: pointer;
  accent-color: rgba(150, 180, 255, 0.85);
}

#spray-pattern-btns {
  display: flex;
  gap: 0.28rem;
}

.spray-pattern-btn {
  width: 1.55rem;
  height: 1.55rem;
  border-radius: 0.3rem;
  border: 1px solid rgba(150, 180, 255, 0.28);
  background: rgba(150, 180, 255, 0.06);
  color: rgba(190, 210, 255, 0.5);
  cursor: pointer;
  font-size: 0.78rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  user-select: none;
  padding: 0;
  flex-shrink: 0;
}

.spray-pattern-btn:hover {
  background: rgba(150, 180, 255, 0.18);
  color: rgba(210, 225, 255, 0.9);
  border-color: rgba(150, 180, 255, 0.55);
}

.spray-pattern-btn.active {
  background: rgba(150, 180, 255, 0.24);
  border-color: rgba(150, 180, 255, 0.85);
  color: rgba(220, 235, 255, 1);
}

/* ── Main toolbar ────────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(10, 9, 16, 0.93);
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 2rem;
  padding: 0.55rem 1.1rem;
  backdrop-filter: blur(14px);
  box-shadow: 0 6px 32px rgba(0, 0, 0, 0.65);
  max-width: calc(100vw - 5.5rem);
}

#toolbar-main-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

#history-row {
  display: none;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  justify-content: center;
  padding-top: 0.38rem;
  margin-top: 0.3rem;
  border-top: 1px solid rgba(255, 255, 255, 0.09);
  width: 100%;
}

.history-label {
  color: rgba(255, 255, 255, 0.22);
  font-size: 0.48rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  user-select: none;
  flex-shrink: 0;
}

#history-swatches {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}

.history-swatch {
  width: 1.25rem;
  height: 1.25rem;
}

.tb-sep {
  width: 1px;
  height: 1.6rem;
  background: rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
  align-self: center;
}

.tb-group {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.tool-btn {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 0.45rem;
  border: 1.5px solid rgba(255, 255, 255, 0.12);
  background: transparent;
  color: rgba(255, 255, 255, 0.65);
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.13s, border-color 0.13s, color 0.13s;
  flex-shrink: 0;
  user-select: none;
}

.tool-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.tool-btn.active {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.48);
  color: #fff;
}

.color-swatch {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  outline: none;
  transition: transform 0.13s, border-color 0.13s;
  flex-shrink: 0;
}

.color-swatch:hover {
  transform: scale(1.22);
}

.color-swatch.active {
  border-color: rgba(255, 255, 255, 0.9);
  transform: scale(1.12);
}

.color-custom-btn {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  border: 1.5px dashed rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.05);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 0.8rem;
  flex-shrink: 0;
  transition: border-color 0.13s, background 0.13s;
  position: relative;
  user-select: none;
}

.color-custom-btn:hover {
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.1);
}

#color-picker-input {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

#size-group {
  gap: 0.5rem;
}

#size-preview {
  width: 2rem;
  height: 2rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#size-slider {
  width: 5rem;
  cursor: pointer;
  accent-color: rgba(200, 200, 220, 0.8);
}

#clear-btn {
  background: rgba(255, 50, 50, 0.1);
  border: 1px solid rgba(255, 50, 50, 0.28);
  color: rgba(255, 120, 120, 0.88);
  padding: 0.35rem 0.8rem;
  border-radius: 0.45rem;
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.13s, border-color 0.13s;
  user-select: none;
}

#clear-btn:hover {
  background: rgba(255, 50, 50, 0.25);
  border-color: rgba(255, 50, 50, 0.55);
}

#download-btn {
  background: rgba(50, 140, 255, 0.1);
  border: 1px solid rgba(50, 140, 255, 0.28);
  color: rgba(100, 180, 255, 0.88);
  padding: 0.35rem 0.8rem;
  border-radius: 0.45rem;
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.13s, border-color 0.13s;
  user-select: none;
}

#download-btn:hover {
  background: rgba(50, 140, 255, 0.25);
  border-color: rgba(50, 140, 255, 0.55);
}

/* ── Toast ───────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  top: 3.2rem;
  left: 50%;
  transform: translateX(-50%) translateY(-0.4rem);
  background: rgba(10, 9, 16, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 0.5rem;
  padding: 0.45rem 1.1rem;
  color: rgba(255, 255, 255, 0.82);
  font-size: 0.78rem;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.28s, transform 0.28s;
  white-space: nowrap;
  user-select: none;
}

#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Cursor ring ─────────────────────────────────────────────────────── */
#cursor-ring {
  position: fixed;
  pointer-events: none;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.55);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
  transform: translate(-50%, -50%);
  display: none;
  z-index: 1000;
}

/* ── Small screens ───────────────────────────────────────────────────── */
@media (max-width: 600px) {
  #toolbar {
    padding: 0.45rem 0.7rem;
    max-width: calc(100vw - 4.5rem);
  }

  #toolbar-main-row {
    gap: 0.35rem;
  }

  .tool-btn {
    width: 1.8rem;
    height: 1.8rem;
    font-size: 0.88rem;
  }

  .color-swatch,
  .color-custom-btn {
    width: 1.25rem;
    height: 1.25rem;
  }

  .history-swatch {
    width: 1.1rem;
    height: 1.1rem;
  }

  #size-slider {
    width: 3.5rem;
  }

  #size-preview {
    width: 1.6rem;
    height: 1.6rem;
  }

  #layer-panel {
    right: 0.4rem;
    padding: 0.5rem 0.35rem;
    width: 3.6rem;
  }

  #layer-slider-wrap {
    height: 6rem;
  }
}
