html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, sans-serif;
  background: #fafafa;
}

#grid, #ink {
  position: absolute;
  inset: 0;
  display: block;
  touch-action: none;
}

#grid { z-index: 0; }
#ink  { z-index: 1; cursor: default; }

#error_banner {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  background: #c62828;
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 13px;
  max-width: 80vw;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  cursor: pointer;
}

#canvas_actions {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 6;
  display: flex;
  gap: 4px;
}

.canvas_action_btn {
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255,255,255,0.85);
  color: #444;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.canvas_action_btn:hover { background: #f2f2f2; color: #222; }

#zoom_label {
  height: 32px;
  padding: 0 8px;
  background: rgba(255,255,255,0.85);
  color: #444;
  border-radius: 6px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  pointer-events: none;
  white-space: nowrap;
  opacity: 1;
  transition: opacity 0.3s ease;
}

#zoom_label[hidden] {
  display: none;
}

#zoom_label.fading {
  opacity: 0;
}

#ink.pen_mode    { cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><path d='M2 18 L4 14 L14 4 L16 6 L6 16 Z' fill='%23222' stroke='%23fff' stroke-width='0.8'/><path d='M13 5 L15 7' stroke='%23fff' stroke-width='0.8'/></svg>") 2 18, crosshair; }
#ink.wand_mode   { cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 28 28'><path d='M2 18 L4 14 L14 4 L16 6 L6 16 Z' fill='%23222' stroke='%23fff' stroke-width='0.8'/><path d='M13 5 L15 7' stroke='%23fff' stroke-width='0.8'/><path d='M18 0 L18.85 2.2 L21.2 2.4 L19.4 3.9 L20 6.2 L18 4.9 L16 6.2 L16.6 3.9 L14.8 2.4 L17.15 2.2 Z' fill='%23000' stroke='%23fff' stroke-width='0.5' stroke-linejoin='round'/></svg>") 2 18, crosshair; }
#ink.laser_mode  { cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><circle cx='10' cy='10' r='3' fill='%23ff00ff' stroke='%23fff' stroke-width='1'/></svg>") 10 10, crosshair; }
#ink.dot_mode    { cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'><circle cx='8' cy='8' r='3' fill='%23222' stroke='%23fff' stroke-width='1'/></svg>") 8 8, crosshair; }
#ink.text_mode   { cursor: text; }
#ink.eraser_mode { cursor: none; }
#ink.pick_mode   { cursor: default; }
#ink.pick_mode.over_image { cursor: move !important; }
#ink.place_mode  { cursor: crosshair; }

#selection_overlay {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  border: 1.5px dashed #1e5fbf;
  box-sizing: border-box;
}

#selection_overlay[hidden] { display: none; }

.resize_handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 1.5px solid #1e5fbf;
  border-radius: 2px;
  pointer-events: auto;
  box-sizing: border-box;
}

.resize_handle.tl { left: -6px; top: -6px; cursor: nwse-resize; }
.resize_handle.tr { right: -6px; top: -6px; cursor: nesw-resize; }
.resize_handle.bl { left: -6px; bottom: -6px; cursor: nesw-resize; }
.resize_handle.br { right: -6px; bottom: -6px; cursor: nwse-resize; }

#eraser_cursor {
  position: fixed;
  z-index: 5;
  pointer-events: none;
  border: 1.5px solid #666;
  background: rgba(255,255,255,0.35);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  left: 0;
  top: 0;
}

#toolbar {
  position: fixed;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 10;
  height: 44px;
  padding: 0 12px;
  background: #fff;
  border: 1px solid #d0d0d0;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  gap: 10px;
}

#secondary_toolbar {
  position: fixed;
  right: 12px;
  top: 12px;
  bottom: 68px;
  z-index: 10;
  width: 64px;
  padding: 8px 6px;
  background: #fff;
  border: 1px solid #d0d0d0;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#secondary_toolbar[hidden] { display: none; }

.library_add_btn {
  width: 44px;
  height: 36px;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 0;
  flex-shrink: 0;
}

.library_add_btn:hover { background: #f2f2f2; color: #222; }

.library_list {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1;
  width: 100%;
  scrollbar-width: none;
  touch-action: none;
  overscroll-behavior: contain;
}

.library_list::-webkit-scrollbar { display: none; }

.library_thumb {
  position: relative;
  width: 48px;
  height: 48px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-sizing: border-box;
}

.library_thumb_x {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #c62828;
  color: #fff;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  cursor: pointer;
}

.library_thumb.active .library_thumb_x { display: flex; }
.library_thumb_x:hover { background: #a11f1f; }

.library_thumb img {
  max-width: 100%;
  max-height: 100%;
  display: block;
  image-rendering: -webkit-optimize-contrast;
}

.library_thumb:hover { border-color: #d0d0d0; }
.library_thumb.active { border-color: #1e5fbf; background: #eaf1fb; }

.divider {
  width: 1px;
  height: 24px;
  background: #888;
}

#palette {
  display: flex;
  gap: 6px;
}

.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.swatch .tick { display: none; }
.swatch.active .tick { display: block; }

.nib_group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.nib_btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 0;
}

.nib_btn:hover { background: #f2f2f2; color: #222; }
.nib_btn.active { background: #6b7280; color: #fff; }

.nib_group[hidden] { display: none; }
.nib_group.invisible { visibility: hidden; }

.mode_btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 0;
}

.mode_btn:hover { background: #f2f2f2; color: #222; }
.mode_btn.active { background: #6b7280; color: #fff; }

.type_input {
  position: fixed;
  z-index: 20;
  background: rgba(255,255,255,0.9);
  border: 1px dashed #888;
  border-radius: 3px;
  padding: 0 2px;
  outline: none;
  min-width: 40px;
}

.icon_btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 0;
}

.icon_btn:hover { background: #f2f2f2; color: #222; }
.icon_btn.active { background: #6b7280; color: #fff; }

.canvas_btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 0;
}

.canvas_btn:hover { background: #f2f2f2; color: #222; }
.canvas_btn.active { background: #6b7280; color: #fff; }

#page_menu { position: relative; display: flex; align-items: center; gap: 0; }

.page_caret_btn {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 22px;
  color: #111;
  border-radius: 4px;
  background: transparent;
}
.page_caret_btn:hover { background: #e8e8e8; color: #000; }

.page_label_btn {
  width: 100px;
  height: 28px;
  border: none;
  background: #f2f2f2;
  font-size: 13px;
  font-weight: 500;
  color: #333;
  cursor: pointer;
  padding: 0 24px 0 6px;
  border-radius: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-sizing: border-box;
  text-align: left;
}

.page_label_btn:hover { background: #e8e8e8; }

.page_label_input {
  width: 100px;
  height: 28px;
  border: none;
  background: #e2e2e2;
  color: #333;
  font-size: 13px;
  font-weight: 500;
  padding: 0 24px 0 6px;
  border-radius: 6px;
  outline: none;
  box-sizing: border-box;
}

#page_dropdown {
  position: absolute;
  bottom: calc(100% + 10px);
  right: 0;
  min-width: 160px;
  background: #fff;
  border: 1px solid #d0d0d0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  overflow: hidden;
}

#page_dropdown[hidden] { display: none; }

.page_item {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 0 6px 0 14px;
  min-height: 34px;
  border: none;
  background: transparent;
  font-size: 13px;
  color: #333;
  box-sizing: border-box;
}

.page_item:hover { background: #f2f2f2; }
.page_item.active { font-weight: 600; background: #f0f0f0; }

.page_label {
  flex: 1;
  cursor: pointer;
  user-select: none;
  padding: 6px 0;
}

.page_rename_input {
  flex: 1;
  border: 1px solid #aaa;
  border-radius: 4px;
  font-size: 13px;
  padding: 2px 6px;
  outline: none;
}

.page_delete_btn {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: #aaa;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  padding: 0;
  opacity: 0;
  transition: opacity 0.1s;
}

.page_item:hover .page_delete_btn { opacity: 1; }
.page_delete_btn:hover { background: #eee; color: #111; }

.page_add {
  border-bottom: 1px solid #eee;
  color: #1e5fbf;
  cursor: pointer;
  padding: 8px 14px;
  justify-content: flex-start;
}
