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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  background: #f4f5fb;
  color: #1f2348;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

body { touch-action: manipulation; }

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ─── トップバー ─── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #ffffff;
  border-bottom: 1px solid #eceaf5;
}

.topbar-button {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 6px 10px;
  border: 1px solid #ece8f5;
  background: #ffffff;
  color: #1f2348;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-width: 0;
}

.topbar-button.primary { flex-direction: row; align-items: center; margin-left: auto; }

.topbar-button:active { transform: translateY(1px); }

.topbar-button.primary {
  background: linear-gradient(180deg, #a78bfa 0%, #8b5cf6 100%);
  color: #ffffff;
  border-color: transparent;
  box-shadow: 0 2px 6px rgba(124, 58, 237, 0.25);
}

.topbar-label {
  font-size: 11px;
  font-weight: 500;
  color: #6b7280;
}

.topbar-value {
  font-variant-numeric: tabular-nums;
}

/* ─── キャンバス ─── */
.canvas-wrap {
  flex: 1 1 auto;
  position: relative;
  padding: 16px;
  background: #f4f5fb;
  overflow: hidden;
}

.canvas-viewport {
  position: absolute;
  inset: 16px;
  overflow: hidden;
  touch-action: none;
}

.canvas-stage {
  position: absolute;
  top: 0;
  left: 0;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 18px rgba(31, 35, 72, 0.08);
  padding: 8px;
  touch-action: none;
  transform-origin: 0 0;
  will-change: transform;
}

.zoom-indicator {
  position: absolute;
  right: 12px;
  bottom: 12px;
  padding: 4px 10px;
  background: rgba(31, 35, 72, 0.7);
  color: #ffffff;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.zoom-indicator.is-visible { opacity: 1; }

/* ─── 選択バー ─── */
.selection-bar {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
  padding: 6px;
  background: rgba(31, 35, 72, 0.95);
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(31, 35, 72, 0.3);
  z-index: 10;
}

.sel-btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 8px;
  background: transparent;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  min-width: 44px;
}

.sel-btn svg { width: 18px; height: 18px; }
.sel-btn.danger { color: #fca5a5; }
.sel-btn.primary { color: #a5f3fc; }
.sel-btn:active { background: rgba(255, 255, 255, 0.12); }

.selection-bar[hidden], .sel-btn[hidden] { display: none; }

.draw-canvas, .grid-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  background:
    linear-gradient(45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(-45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eef0f8 75%),
    linear-gradient(-45deg, transparent 75%, #eef0f8 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #ffffff;
}

.grid-canvas {
  position: absolute;
  top: 8px;
  left: 8px;
  pointer-events: none;
  background: transparent;
}

/* ─── ツールバー ─── */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 4px;
  padding: 8px 8px 12px;
  background: #ffffff;
  border-top: 1px solid #eceaf5;
}

.tool {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid transparent;
  background: transparent;
  color: #1f2348;
  border-radius: 10px;
  cursor: pointer;
  padding: 0;
  position: relative;
}

.tool svg { width: 22px; height: 22px; }

.color-swatch { width: 26px; height: 26px; }

.tool-btn:active { background: #f4f5fb; }

.tool-btn.is-active {
  background: #ede9fe;
  color: #6d28d9;
  border-color: #ddd6fe;
}

.tool-btn.danger { color: #dc2626; }

.color-tool {
  position: relative;
  overflow: hidden;
  cursor: pointer;
}

.color-tool input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.color-swatch {
  display: block;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1px #d4d0e6, 0 1px 3px rgba(31, 35, 72, 0.15);
}

/* ─── ダイアログ ─── */
.dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  background: #ffffff;
  color: #1f2348;
  max-width: 340px;
  width: calc(100% - 32px);
  box-shadow: 0 20px 40px rgba(31, 35, 72, 0.25);
}

.dialog::backdrop { background: rgba(31, 35, 72, 0.4); }

.dialog-body {
  padding: 20px;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.dialog-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.dialog-note {
  margin: 0;
  font-size: 12px;
  color: #6b7280;
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.preset {
  padding: 12px 4px;
  border: 1px solid #ece8f5;
  background: #ffffff;
  color: #1f2348;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.preset:active { background: #f4f5fb; }

.preset.is-current {
  background: #ede9fe;
  border-color: #ddd6fe;
  color: #6d28d9;
}

.custom-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 4px;
  border-top: 1px dashed #eceaf5;
  padding-top: 12px;
}

.custom-row label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #6b7280;
}

.custom-row input {
  width: 60px;
  padding: 6px 8px;
  border: 1px solid #ddd6fe;
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
  color: #1f2348;
}

.custom-row .times { color: #9ca3af; font-weight: 700; }

.apply-btn {
  margin-left: auto;
  padding: 8px 14px;
  background: #8b5cf6;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 6px;
}

.ghost {
  padding: 8px 14px;
  background: transparent;
  color: #6b7280;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn {
  padding: 8px 16px;
  background: #8b5cf6;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.save-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.save-choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 8px;
  border: 1px solid #ece8f5;
  background: #ffffff;
  color: #1f2348;
  border-radius: 12px;
  cursor: pointer;
}

.save-choice-name { font-size: 16px; font-weight: 700; }
.save-choice-note { font-size: 11px; color: #6b7280; }

.save-choice:active { background: #f4f5fb; }

.save-mode {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.save-mode-btn {
  padding: 10px;
  background: #ffffff;
  color: #1f2348;
  border: 1px solid #ece8f5;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.save-mode-btn.is-active {
  background: #ede9fe;
  color: #6d28d9;
  border-color: #ddd6fe;
}

.save-sheet-config {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: #f8f7fd;
  border-radius: 10px;
}

.save-sheet-config label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: #4b5063;
  font-weight: 600;
}

.save-sheet-config input {
  width: 80px;
  padding: 6px 8px;
  border: 1px solid #ddd6fe;
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
  color: #1f2348;
}

.save-divider {
  height: 1px;
  background: #eceaf5;
  margin: 4px 0;
}

.save-project {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.save-project-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.save-project-actions .save-mode-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 6px;
  font-size: 12px;
}

.save-project-actions .save-mode-btn svg { width: 16px; height: 16px; }

.save-scale {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: #f8f7fd;
  border-radius: 10px;
}

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

.scale-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.scale-btn {
  padding: 8px 0;
  background: #ffffff;
  color: #1f2348;
  border: 1px solid #ece8f5;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.scale-btn.is-active {
  background: #ede9fe;
  color: #6d28d9;
  border-color: #ddd6fe;
}

.save-scale-hint {
  font-size: 11px;
  color: #6b7280;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ─── レイヤーモーダル ─── */
.layers-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 260px;
  overflow-y: auto;
}

.layer-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: #ffffff;
  border: 1px solid #ece8f5;
  border-radius: 10px;
  cursor: pointer;
  min-height: 44px;
}

.layer-row.is-active {
  background: #ede9fe;
  border-color: #ddd6fe;
}

.layer-vis {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  background: transparent;
  border: none;
  color: #6b7280;
  cursor: pointer;
  padding: 0;
  border-radius: 6px;
}

.layer-vis svg { width: 20px; height: 20px; }
.layer-vis.is-hidden { color: #d1d5db; }
.layer-vis:active { background: #f4f5fb; }

.layer-thumb {
  width: 34px;
  height: 34px;
  background:
    linear-gradient(45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(-45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eef0f8 75%),
    linear-gradient(-45deg, transparent 75%, #eef0f8 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0;
  background-color: #ffffff;
  border: 1px solid #eceaf5;
  border-radius: 6px;
  image-rendering: pixelated;
  flex-shrink: 0;
}

.layer-name {
  flex: 1 1 auto;
  font-size: 13px;
  font-weight: 600;
  color: #1f2348;
  text-align: left;
  background: transparent;
  border: none;
  padding: 4px 6px;
  border-radius: 6px;
  min-width: 0;
}

.layer-name.is-hidden { color: #9ca3af; text-decoration: line-through; }
.layer-name:focus { outline: 2px solid #ddd6fe; background: #ffffff; }

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

.layer-action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  background: #ffffff;
  color: #1f2348;
  border: 1px solid #ece8f5;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
}

.layer-action-btn svg { width: 18px; height: 18px; }
.layer-action-btn.danger { color: #dc2626; }
.layer-action-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.layer-action-btn:active:not(:disabled) { background: #f4f5fb; }

/* ─── カラーパレットストリップ ─── */
.palette-strip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 4px;
  background: #ffffff;
  border-top: 1px solid #eceaf5;
}

.palette-star-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #ede9fe;
  color: #f59e0b;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  flex-shrink: 0;
}

.palette-star-btn svg { width: 16px; height: 16px; }
.palette-star-btn:active { background: #ddd6fe; }

.palette-list {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  flex: 1 1 auto;
  padding: 2px 0;
  -webkit-overflow-scrolling: touch;
}

.palette-list::-webkit-scrollbar { height: 3px; }
.palette-list::-webkit-scrollbar-thumb { background: #ddd6fe; border-radius: 2px; }

.palette-divider {
  width: 1px;
  height: 20px;
  background: #eceaf5;
  flex-shrink: 0;
  align-self: center;
}

.palette-swatch {
  position: relative;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1px #d4d0e6;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}

.palette-swatch.is-current { box-shadow: 0 0 0 2px #8b5cf6; }

.palette-swatch.is-favorite::before {
  content: '';
  position: absolute;
  top: -3px;
  right: -3px;
  width: 8px;
  height: 8px;
  background: #f59e0b;
  border-radius: 50%;
  border: 1px solid #ffffff;
}

/* ─── フレームストリップ ─── */
.frame-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: #ffffff;
  border-top: 1px solid #eceaf5;
  overflow: hidden;
}

.frame-list {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x proximity;
  flex: 1 1 auto;
  padding: 2px 0;
  -webkit-overflow-scrolling: touch;
}

.frame-list::-webkit-scrollbar { height: 4px; }
.frame-list::-webkit-scrollbar-thumb { background: #ddd6fe; border-radius: 2px; }

.frame-item {
  position: relative;
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  background:
    linear-gradient(45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(-45deg, #eef0f8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eef0f8 75%),
    linear-gradient(-45deg, transparent 75%, #eef0f8 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0;
  background-color: #ffffff;
  border: 2px solid #ece8f5;
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  scroll-snap-align: start;
  overflow: hidden;
}

.frame-item canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.frame-item.is-active {
  border-color: #8b5cf6;
  box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.frame-badge {
  position: absolute;
  top: 1px;
  left: 3px;
  font-size: 9px;
  font-weight: 700;
  color: #ffffff;
  background: rgba(31, 35, 72, 0.65);
  border-radius: 999px;
  padding: 0 4px;
  line-height: 12px;
  pointer-events: none;
}

.frame-add {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  background: #f4f5fb;
  border: 2px dashed #ddd6fe;
  border-radius: 6px;
  color: #8b5cf6;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.frame-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.frame-ctrl-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #ede9fe;
  color: #6d28d9;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.frame-ctrl-btn svg { width: 16px; height: 16px; }
.frame-ctrl-btn:active { background: #ddd6fe; }
.frame-ctrl-btn.is-playing { background: #f43f5e; color: #ffffff; }

.frame-ctrl-btn.ghost-btn {
  background: transparent;
  color: #9ca3af;
  border: 1px solid transparent;
}

.frame-ctrl-btn.ghost-btn.is-active {
  color: #8b5cf6;
  background: #ede9fe;
  border-color: #ddd6fe;
}

.fps-input {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: #6b7280;
  font-weight: 600;
}

.fps-input input {
  width: 38px;
  padding: 3px 4px;
  border: 1px solid #ddd6fe;
  border-radius: 6px;
  font-size: 12px;
  text-align: center;
  color: #1f2348;
  -moz-appearance: textfield;
}

.fps-input input::-webkit-outer-spin-button,
.fps-input input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* フレーム編集モーダル */
.frame-edit-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
