@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;600&display=swap');

:root {
  --ink: #0b0b0f;
  --blue: #8fbef8;
  --blue-strong: #2b6bf3;
  --ui-bg: rgba(255, 255, 255, 0.92);
  --ui-border: #cdd7ec;
  --ui-shadow: rgba(30, 45, 80, 0.14);
  --menu-bar-height: 46px;
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: "Space Grotesk", sans-serif;
  background: #ffffff;
  color: var(--ink);
  touch-action: none;
}

#app {
  position: relative;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: #ffffff;
  touch-action: none;
  cursor: crosshair;
}

#ui {
  position: absolute;
  inset: 0;
  z-index: 100;
  pointer-events: none;
}

.ui-shell {
  position: relative;
  width: 100%;
  height: 100%;
  isolation: isolate;
}

.menu-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: var(--menu-bar-height);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(248, 250, 255, 0.92);
  border-bottom: 1px solid var(--ui-border);
  box-shadow: 0 6px 18px var(--ui-shadow);
  backdrop-filter: blur(8px);
  pointer-events: auto;
  z-index: 300;
}

.menu-group {
  position: relative;
}

.menu-bar,
.menu-bar *,
.menu-panel,
.menu-panel * {
  -webkit-user-select: none;
  user-select: none;
}

.menu-trigger {
  border: none;
  border-radius: 4px;
  background: transparent;
  display: inline-flex;
  align-items: center;
  color: #213259;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: background 120ms ease, color 120ms ease;
  user-select: none;
}

.menu-trigger,
.menu-action,
.menu-toggle,
.menu-item.has-submenu,
button {
  touch-action: none;
}

.menu-trigger:hover,
.menu-group.open .menu-trigger {
  background: rgba(43, 107, 243, 0.12);
  color: #122950;
}

.menu-panel {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 220px;
  display: none;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  background: var(--ui-bg);
  border: 1px solid var(--ui-border);
  box-shadow: 0 12px 26px var(--ui-shadow);
  backdrop-filter: blur(8px);
  pointer-events: auto;
  z-index: 400;
}

.menu-panel-grid {
  min-width: 240px;
}

.menu-panel-tools {
  min-width: 228px;
}

.menu-panel-settings {
  min-width: 308px;
  max-height: calc(100vh - var(--menu-bar-height) - 20px);
  overflow-y: auto;
}

.menu-group.open .menu-panel {
  display: flex;
}

.menu-action {
  width: 100%;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: #213259;
  font-size: 12px;
  font-weight: 500;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
  text-align: left;
}

.menu-action-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.menu-action-label > span:last-child {
  white-space: nowrap;
}

.menu-action:hover,
.menu-action:focus {
  background: #eef3ff;
}

.menu-action.touch-highlight,
.menu-toggle.touch-highlight,
.menu-item.has-submenu.touch-highlight {
  background: #dfe8ff;
}

.menu-panel button.touch-highlight {
  box-shadow: 0 0 0 2px rgba(43, 107, 243, 0.9) inset;
}

.menu-panel .swatch.touch-highlight,
.menu-panel .stamp-btn.touch-highlight {
  box-shadow: 0 0 0 2px rgba(43, 107, 243, 0.9) inset;
}

.menu-action.selected {
  background: #dfe8ff;
  font-weight: 600;
}

.menu-action.danger {
  color: #9c1c1c;
}

.menu-shortcut {
  font-size: 11px;
  font-weight: 600;
  opacity: 0.6;
}

.menu-divider {
  height: 1px;
  background: #e1e7f4;
  margin: 2px 0;
}

.menu-help-block {
  margin: 0;
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #2f426a;
  font-size: 11px;
  line-height: 1.35;
  padding: 7px 8px;
  min-height: 31px;
}

.menu-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #213259;
  cursor: pointer;
  user-select: none;
}

.menu-toggle input {
  accent-color: #2b6bf3;
}

.menu-section-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #4d5a7a;
}

.menu-slider {
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-label {
  font-size: 12px;
  color: #4d5a7a;
  min-width: 36px;
}

.menu-slider input[type="range"] {
  flex: 1;
  accent-color: #2b6bf3;
}

.menu-slider-value {
  min-width: 46px;
  text-align: right;
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.menu-choice-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.menu-choice-label {
  font-size: 12px;
  color: #4d5a7a;
}

.menu-choice-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  width: 100%;
}

.menu-choice-btn {
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 11px;
  font-weight: 600;
  padding: 6px 4px;
  cursor: pointer;
}

.menu-choice-btn:hover {
  background: #e2e9ff;
}

.menu-choice-btn.active {
  border-color: #2b6bf3;
  background: #2b6bf3;
  color: #ffffff;
}

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
  color: #213259;
  padding: 4px 6px;
  border-radius: 4px;
}

.menu-item.has-submenu {
  position: relative;
  cursor: pointer;
}

.menu-item.has-submenu:hover,
.menu-item.has-submenu:focus-within {
  background: #eef3ff;
}

.submenu-caret {
  font-size: 14px;
  opacity: 0.6;
}

.menu-submenu {
  position: absolute;
  top: -8px;
  left: calc(100% - 1px);
  min-width: 180px;
  display: none;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  background: var(--ui-bg);
  border: 1px solid var(--ui-border);
  box-shadow: 0 12px 26px var(--ui-shadow);
  backdrop-filter: blur(8px);
  z-index: 410;
}

.menu-item.has-submenu:hover .menu-submenu,
.menu-item.has-submenu:focus-within .menu-submenu {
  display: flex;
}

.menu-item.has-submenu.touch-submenu-open .menu-submenu {
  display: flex;
}

.palette-window {
  position: absolute;
  width: 296px;
  display: flex;
  flex-direction: column;
  background: var(--ui-bg);
  border: 1px solid var(--ui-border);
  border-radius: 14px;
  box-shadow: 0 10px 28px var(--ui-shadow);
  backdrop-filter: blur(8px);
  pointer-events: auto;
  z-index: 200;
  overflow: hidden;
}

.palette-window.tool-palette {
  width: 296px;
}

.palette-window.fill-palette {
  width: 336px;
}

.palette-window.stamp-palette {
  width: 296px;
}

.palette-window-header {
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 8px 0 10px;
  border-bottom: 1px solid #dce4f4;
  background: rgba(237, 242, 255, 0.9);
  cursor: move;
  user-select: none;
  touch-action: none;
}

.palette-window-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #506088;
}

.palette-window-close {
  width: 20px;
  height: 20px;
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 14px;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.palette-window-close:hover {
  background: #e2e9ff;
}

.palette-window-body {
  padding: 12px 14px;
  max-height: calc(100vh - var(--menu-bar-height) - 24px);
  overflow-y: auto;
}

.fill-palette .controls {
  margin-top: 0;
}

.stamp-palette .stamp-controls {
  margin-top: 0;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.tool-palette-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tool-help-text {
  margin: 0;
  border: 1px solid #d7e0f4;
  border-radius: 10px;
  background: #eef3ff;
  color: #2f426a;
  font-size: 12px;
  line-height: 1.4;
  padding: 8px 10px;
  min-height: 34px;
}

.tool-btn {
  display: grid;
  place-items: center;
  aspect-ratio: 1 / 1;
  border: 1px solid #d7e0f4;
  border-radius: 10px;
  padding: 0;
  background: #f4f7ff;
  color: #213259;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.tool-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 14px rgba(44, 76, 140, 0.18);
}

.tool-btn.active {
  background: #2b6bf3;
  border-color: #2b6bf3;
  color: #ffffff;
  box-shadow: 0 10px 22px rgba(43, 107, 243, 0.35);
}

.tool-icon {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.tool-btn .tool-icon {
  width: 20px;
  height: 20px;
}

.menu-action-label .tool-icon {
  width: 15px;
  height: 15px;
}

.controls {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  margin-top: 10px;
}

.palette {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  width: 100%;
}

.menu-panel-settings .palette {
  grid-template-columns: repeat(6, 1fr);
}

.swatch {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  border: 1px solid #d7e0f4;
  padding: 0;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.swatch.active {
  outline: 2px solid #2b6bf3;
  outline-offset: 1px;
}

.swatch:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 12px rgba(44, 76, 140, 0.18);
}

.swatch.add {
  background: #f4f7ff;
  color: #213259;
  font-size: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.swatch-add-label {
  pointer-events: none;
}

.swatch-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  background: #1f2a44;
  color: #ffffff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.swatch:hover .swatch-remove {
  opacity: 1;
  pointer-events: auto;
}

.palette-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.palette-input.edit {
  position: fixed;
  left: -9999px;
  top: -9999px;
  width: 1px;
  height: 1px;
  pointer-events: none;
  opacity: 0;
  cursor: pointer;
  z-index: 20;
}

.controls label {
  font-size: 12px;
  color: #4d5a7a;
}

.guide-controls {
  margin-top: 8px;
  width: 100%;
}

.guide-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #213259;
  cursor: pointer;
  user-select: none;
}

.guide-toggle input {
  accent-color: #2b6bf3;
}

.stamp-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.stamp-label {
  font-size: 12px;
  color: #4d5a7a;
}

.stamp-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  width: 100%;
}

.stamp-btn {
  background: #f4f7ff;
  color: #213259;
  display: grid;
  place-items: center;
}

.stamp-icon {
  width: 62%;
  height: 62%;
}

.stamp-icon * {
  fill: none;
  stroke: currentColor;
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.stamp-size {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stamp-size-label {
  font-size: 11px;
  color: #4d5a7a;
  min-width: 32px;
}

.stamp-size input[type="range"] {
  flex: 1;
  accent-color: #2b6bf3;
}

.stamp-size-value {
  min-width: 50px;
  text-align: right;
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.controls input[type="color"] {
  border: none;
  width: 32px;
  height: 24px;
  padding: 0;
  background: transparent;
  cursor: pointer;
}

.info-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 64px 12px 12px;
  background: rgba(11, 20, 39, 0.42);
  backdrop-filter: blur(3px);
  pointer-events: auto;
  z-index: 520;
}

.info-popup {
  width: min(500px, calc(100vw - 24px));
  border: 1px solid var(--ui-border);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 14px 34px rgba(16, 31, 62, 0.24);
  color: #213259;
  padding: 14px;
}

.info-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.info-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #122950;
}

.info-close {
  width: 26px;
  height: 26px;
  border: 1px solid #d7e0f4;
  border-radius: 9px;
  background: #eef3ff;
  color: #213259;
  font-size: 17px;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.info-close:hover {
  background: #e2e9ff;
}

.info-text {
  margin: 10px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: #2f426a;
}

.info-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 14px;
}

.info-btn {
  border: 1px solid #2b6bf3;
  border-radius: 8px;
  background: #2b6bf3;
  color: #ffffff;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  cursor: pointer;
}

.info-btn:hover {
  background: #245cd1;
  border-color: #245cd1;
}

.save-overlay {
  z-index: 540;
}

.save-popup {
  width: min(420px, calc(100vw - 24px));
}

.save-text {
  margin-bottom: 10px;
}

.save-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.save-label {
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.save-input {
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 13px;
  padding: 8px 10px;
  outline: none;
}

.save-input:focus {
  border-color: #2b6bf3;
  box-shadow: 0 0 0 2px rgba(43, 107, 243, 0.15);
}

.save-actions {
  gap: 8px;
}

.save-btn {
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  cursor: pointer;
}

.save-btn-secondary {
  border: 1px solid #d7e0f4;
  background: #eef3ff;
  color: #213259;
}

.save-btn-secondary:hover {
  background: #e2e9ff;
}

.mobile-footer-toolbar {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  border: 1px solid var(--ui-border);
  border-radius: 14px;
  background: rgba(248, 250, 255, 0.95);
  box-shadow: 0 12px 30px rgba(31, 46, 82, 0.2);
  backdrop-filter: blur(10px);
  pointer-events: auto;
  z-index: 320;
}

.mobile-footer-btn {
  flex: 1 1 0;
  min-width: 0;
  width: auto;
  height: 34px;
  border: 1px solid #d7e0f4;
  border-radius: 10px;
  background: #eef3ff;
  color: #213259;
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}

.mobile-footer-btn:hover {
  background: #e2e9ff;
}

.mobile-footer-btn.active {
  border-color: #2b6bf3;
  background: #2b6bf3;
  color: #ffffff;
  box-shadow: 0 8px 18px rgba(43, 107, 243, 0.28);
}

.hud-info {
  position: absolute;
  left: 12px;
  bottom: 12px;
  max-width: min(460px, calc(100% - 24px));
  pointer-events: none;
  z-index: 120;
}

.status {
  margin-top: 8px;
  min-height: 16px;
  font-size: 12px;
  color: #425279;
}

.hud-info .status {
  margin-top: 0;
}

.menu-zoom-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.menu-zoom-actions {
  display: flex;
  gap: 6px;
}

.zoom-btn {
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 12px;
  padding: 4px 10px;
  cursor: pointer;
}

.zoom-btn:hover {
  background: #e2e9ff;
}

.thickness-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
}

.thickness-label {
  font-size: 12px;
  color: #4d5a7a;
}

.thickness-controls input[type="range"] {
  flex: 1;
  accent-color: #2b6bf3;
}

.thickness-value {
  min-width: 50px;
  text-align: right;
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.alpha-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
}

.alpha-label {
  font-size: 12px;
  color: #4d5a7a;
}

.alpha-controls input[type="range"] {
  flex: 1;
  accent-color: #2b6bf3;
}

.alpha-value {
  min-width: 50px;
  text-align: right;
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.menu-panel-settings .thickness-controls,
.menu-panel-settings .alpha-controls {
  margin-top: 0;
}

.grid-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.grid-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.grid-label,
.grid-sub {
  font-size: 12px;
  color: #4d5a7a;
  min-width: 54px;
}

.grid-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: #213259;
  cursor: pointer;
  user-select: none;
}

.grid-toggle input {
  accent-color: #2b6bf3;
}

.grid-select {
  flex: 1;
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 12px;
  padding: 4px 6px;
  cursor: pointer;
}

.grid-row input[type="range"] {
  flex: 1;
  accent-color: #2b6bf3;
}

.grid-value {
  min-width: 46px;
  text-align: right;
  font-size: 12px;
  font-weight: 600;
  color: #213259;
}

.action-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
}

.export-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
}

.action-btn {
  flex: 1;
  border: 1px solid #d7e0f4;
  border-radius: 8px;
  background: #eef3ff;
  color: #213259;
  font-size: 12px;
  padding: 6px 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.action-btn:hover {
  background: #e2e9ff;
}

.action-btn.danger {
  background: #ffe9e9;
  border-color: #ffb7b7;
  color: #9c1c1c;
}

.action-key {
  font-size: 11px;
  font-weight: 600;
  opacity: 0.7;
}

@media (max-width: 768px), (pointer: coarse) {
  .menu-bar {
    gap: 8px;
    padding: 8px;
  }

  .menu-panel {
    max-width: min(92vw, 320px);
  }

  .menu-group-end .menu-panel {
    left: auto;
    right: 0;
  }

  .mobile-footer-toolbar {
    display: flex;
  }

  .hud-info {
    bottom: calc(64px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 640px) {

  .info-overlay {
    padding-top: 56px;
  }

  .info-popup {
    padding: 12px;
  }

  .info-title {
    font-size: 16px;
  }

  .palette-window.tool-palette {
    width: 228px;
  }

  .palette-window.fill-palette {
    width: 252px;
  }

  .palette-window.stamp-palette {
    width: 252px;
  }

  .palette-window-body {
    padding: 10px 11px;
  }

  .menu-panel-settings {
    min-width: 250px;
  }

  .menu-panel-settings .palette {
    grid-template-columns: repeat(4, 1fr);
  }

  .tool-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .palette {
    grid-template-columns: repeat(4, 1fr);
  }

  .hud-info {
    max-width: min(320px, calc(100% - 24px));
  }
}
