* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  background: #e9e9ea;
  touch-action: none;
}

#canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
  background: #ffffff;
}

.toolbar {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  display: flex;
  gap: 8px;
  z-index: 20;
  /* Fallback; JS sets a precise value per toolbar so tl/tr never overlap */
  max-width: calc(100vw - 20px);
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding: 5px;
  /* Fade the edges so it's visually obvious the row can scroll */
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
  mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
}
.toolbar::-webkit-scrollbar { display: none; }
#toolbar-tl { left: max(10px, env(safe-area-inset-left)); }
#toolbar-tr { right: max(10px, env(safe-area-inset-right)); }
#toolbar-tl.no-fade-l { -webkit-mask-image: linear-gradient(to right, #000 0, #000 calc(100% - 14px), transparent 100%); mask-image: linear-gradient(to right, #000 0, #000 calc(100% - 14px), transparent 100%); }
#toolbar-tl.no-fade-r { -webkit-mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 100%); mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 100%); }
#toolbar-tl.no-fade-l.no-fade-r { -webkit-mask-image: none; mask-image: none; }

.tbtn {
  width: 44px; height: 44px;
  flex: 0 0 44px;
  border-radius: 12px;
  border: none;
  background: rgba(30,30,30,0.86);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.18);
  padding: 0;
}
.tbtn svg { width: 22px; height: 22px; stroke: #fff; fill: #fff; }
.tbtn:active { background: rgba(60,60,60,0.9); }
.tbtn.active { background: #2b6cf6; }
.tbtn:disabled { opacity: 0.35; }
.tbtn:disabled:active { background: rgba(30,30,30,0.86); }

#zoom-indicator {
  position: fixed;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30,30,30,0.78);
  color: #fff;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 999px;
  z-index: 20;
  pointer-events: none;
  transition: opacity 0.3s;
}

.hidden { display: none !important; }

.popup {
  position: fixed;
  z-index: 40;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 16px;
  padding: 18px;
  width: min(320px, 84vw);
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.popup-title {
  font-size: 15px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 12px;
}
.popup-btn {
  display: block;
  width: 100%;
  padding: 12px;
  margin-bottom: 8px;
  border-radius: 10px;
  border: 1px solid #ddd;
  background: #f5f5f5;
  color: #1a1a1a;
  font-size: 14px;
  text-align: left;
}
.popup-btn:active { background: #e8e8e8; }
.update-desc { font-size: 13px; color: #444; margin: 0 0 14px; line-height: 1.5; }
.popup-btn.primary { background: #2b6cf6; border-color: #2b6cf6; color: #fff; text-align: center; font-weight: 600; }
.popup-btn.danger { color: #c62828; }
.popup-close {
  width: 100%;
  padding: 10px;
  border: none;
  background: none;
  color: #777;
  font-size: 13px;
  margin-top: 4px;
}

.context-menu {
  left: 0;
  top: 0;
  transform: none;
  width: min(220px, 70vw);
  padding: 8px;
}
.context-menu .popup-btn {
  margin-bottom: 6px;
  padding: 10px 12px;
  font-size: 13.5px;
}
#save-menu.context-menu { width: min(260px, 78vw); }

.mode-menu {
  width: min(230px, 74vw);
  padding: 8px;
}
.mode-menu-title {
  font-size: 11.5px;
  font-weight: 600;
  color: #888;
  padding: 4px 6px 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.mode-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 6px;
  margin-bottom: 4px;
  border-radius: 10px;
  border: none;
  background: none;
  color: #1a1a1a;
  font-size: 13.5px;
  text-align: left;
}
.mode-menu-item:last-child { margin-bottom: 0; }
.mode-menu-item:active { background: #f0f0f0; }
.mode-menu-icon {
  width: 34px; height: 34px;
  flex: 0 0 34px;
  border-radius: 10px;
  background: rgba(30,30,30,0.86);
  display: flex; align-items: center; justify-content: center;
}
.mode-menu-icon svg { width: 18px; height: 18px; stroke: #fff; fill: #fff; }

.field { display: block; font-size: 13px; color: #444; margin-bottom: 14px; }
.field-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.field-row input[type=range] { flex: 1; }
#grid-size-value { font-size: 13px; color: #1a1a1a; min-width: 42px; text-align: right; }

#text-edit-overlay {
  position: fixed; inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.35);
  display: flex; align-items: center; justify-content: center;
}
.text-edit-box {
  background: #fff;
  border-radius: 16px;
  padding: 18px;
  width: min(340px, 88vw);
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
#text-edit-input {
  width: 100%;
  min-height: 90px;
  border: 1px solid #ddd;
  border-radius: 10px;
  padding: 10px;
  font-size: 15px;
  resize: none;
  font-family: inherit;
}
.text-edit-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}
.text-edit-buttons .popup-btn { width: auto; flex: 0 0 auto; margin-bottom: 0; padding: 10px 14px; }
.text-edit-buttons .spacer { flex: 1; }

.hint {
  position: fixed;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30,30,30,0.86);
  color: #fff;
  font-size: 12.5px;
  padding: 8px 14px;
  border-radius: 10px;
  z-index: 25;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s;
  max-width: 86vw;
  text-align: center;
}
.hint.show { opacity: 1; }

#rotation-lock {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: transparent;
  touch-action: none;
}
