* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  min-height: 100%;
  font-family: Arial, Helvetica, sans-serif;
  background: linear-gradient(135deg, #111827, #1f2937);
  color: #f9fafb;
}

body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow-x: hidden;
}

.app-shell {
  width: min(1400px, 100%);
  max-width: 100%;
  min-height: 90vh;
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  background: rgba(17, 24, 39, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 24px;
  padding: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}

.canvas-panel {
  order: 1;
}

.sidebar {
  order: 2;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 3vw, 2.8rem);
  line-height: 1.2;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}


.layer-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: grab;
  background: rgba(148, 163, 184, 0.08);
  border: 1px solid rgba(148, 163, 184, 0.16);
  border-radius: 10px;
  padding: 7px 6px;
  min-height: 44px;
}

.layer-item:active {
  cursor: grabbing;
}

.layer-item.active {
  border-color: rgba(129, 140, 248, 0.9);
  background: rgba(79, 70, 229, 0.12);
}

.layer-item.dragging {
  opacity: 0.55;
}

.layer-select,
.layer-label,
.layer-visibility {
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: rgba(15, 23, 42, 0.75);
  color: #f8fafc;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.layer-select {
  width: 24px;
  height: 24px;
  font-size: 0.72rem;
  line-height: 1;
}

.layer-label {
  min-width: 34px;
  height: 26px;
  background: rgba(79, 70, 229, 0.18);
  color: #dfe7ff;
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  padding: 0 8px;
  text-transform: uppercase;
}

.mask-upload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 62px;
  height: 26px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 8px;
  background: rgba(79, 70, 229, 0.22);
  color: #e2e8f0;
  font-size: 0.75rem;
  font-weight: 700;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.layer-visibility {
  width: 26px;
  height: 26px;
  font-size: 0.82rem;
  padding: 0;
}

.collage-tools {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 4px;
}

.primary-btn,
.danger-btn {
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.primary-btn:hover,
.danger-btn:hover {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

.primary-btn {
  background: linear-gradient(135deg, #7c3aed, #4f46e5);
  color: white;
}

.danger-btn {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: white;
}

.upload-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.upload-btn input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.canvas-panel {
  position: relative;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 18px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.mask-canvas-button {
  position: absolute;
  transform: translate(-50%, -50%);
  z-index: 4;
  border: 1px solid rgba(124, 58, 237, 0.4);
  background: rgba(79, 70, 229, 0.9);
  color: #fff;
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 10px 22px rgba(79, 70, 229, 0.28);
}

#fabric-canvas {
  position: relative;
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  max-height: 70vh;
  background: #f8fafc;
  border-radius: 12px;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.2);
  touch-action: none;
}

@media (max-width: 860px) {
  body {
    padding: 10px;
  }

  .app-shell {
    width: 100%;
    min-height: auto;
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 14px;
    border-radius: 18px;
  }

  .sidebar {
    gap: 12px;
  }

  h1 {
    font-size: clamp(1.6rem, 7vw, 2.2rem);
  }

  .toolbar {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 10px;
  }

  .primary-btn,
  .danger-btn {
    flex: 1 1 calc(50% - 10px);
    min-width: 0;
    padding: 11px 12px;
    font-size: 0.82rem;
  }

  .canvas-panel {
    padding: 10px;
    min-height: 48vh;
  }

  #fabric-canvas {
    width: min(100%, 520px);
    max-width: 100%;
  }
}

@media (max-width: 420px) {
  .toolbar {
    flex-direction: column;
  }

  .primary-btn,
  .danger-btn {
    width: 100%;
    flex-basis: 100%;
  }

  .layer-item {
    padding: 7px 8px;
  }

  .layer-label {
    font-size: 0.8rem;
  }
}
